python-3.x discord.py如何在等待输入时保持机器人运行()

o8x7eapl  于 2023-10-21  发布在  Python
关注(0)|答案(1)|浏览(462)

我想我的问题是我不能保持机器人清醒,而等待输入我已经尝试了一些事情,但似乎没有帮助

import os
import re
import discord
MyDiscordID = os.environ["DiscordID"] # my discord account id
people = os.environ["people"] # input everyone i want to dm
message = os.environ["message"] # input message
client = discord.Client(intents=discord.Intents.all(), token=os.environ["Token"]) # discord intents & bot token
names = [people, "yourself"] # yourself for testing
names = str(names).replace("'", "")
@client.event
async def on_ready():
  while True:
     user = input("send the dms to who? ")
                                           # stay alive while waiting
     if user == "yourself":
       user = await client.fetch_user(MyDiscordID)
       await user.send("# ```(This message was generated by a bot)```")
       await user.send(message)
       print("you sent the dm to yourself")
     if user not in names:
       print("no user found with that id")
... 

@client.event

我正试图使机器人的dms某些人某些短语在未来我想让它自动从当我得到一个dm(我已经安装)但我似乎不能让它等待输入我使用replit主机代码
在离开10秒后,我得到以下错误消息:

send the dms to who? 2023-10-18 19:10:01 WARNING  discord.gateway Shard ID None heartbeat blocked for more than 10 seconds.
Loop thread traceback (most recent call last):
  File "/home/runner/guild-members/main.py", line 38, in <module>
    client.run(os.environ["Token"])
  File "/home/runner/guild-members/.pythonlibs/lib/python3.10/site-packages/discord/client.py", line 860, in run
    asyncio.run(runner())
  File "/nix/store/xf54733x4chbawkh1qvy9i1i4mlscy1c-python3-3.10.11/lib/python3.10/asyncio/runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "/nix/store/xf54733x4chbawkh1qvy9i1i4mlscy1c-python3-3.10.11/lib/python3.10/asyncio/base_events.py", line 636, in run_until_complete
    self.run_forever()
  File "/nix/store/xf54733x4chbawkh1qvy9i1i4mlscy1c-python3-3.10.11/lib/python3.10/asyncio/base_events.py", line 603, in run_forever
    self._run_once()
  File "/nix/store/xf54733x4chbawkh1qvy9i1i4mlscy1c-python3-3.10.11/lib/python3.10/asyncio/base_events.py", line 1909, in _run_once
    handle._run()
  File "/nix/store/xf54733x4chbawkh1qvy9i1i4mlscy1c-python3-3.10.11/lib/python3.10/asyncio/events.py", line 80, in _run
    self._context.run(self._callback, *self._args)
  File "/home/runner/guild-members/.pythonlibs/lib/python3.10/site-packages/discord/client.py", line 441, in _run_event
    await coro(*args, **kwargs)
  File "/home/runner/guild-members/main.py", line 12, in on_ready
    user = input("send the dms to who? ")

(编辑一次)

vptzau2j

vptzau2j1#

您可以启动一个单独的线程,并通过该线程获取输入。我相信有一个更好的方法来做到这一点(也许与齿轮,但我不熟悉他们),但仍然是一个技术上的工程解决方案。这个例子只是为了展示你可以做什么来快速而肮脏的解决方案。

from threading import Thread

from discord import Client, Intents, Message

client = Client(intents=Intents.default())
messages = []

@client.event
async def on_message(message: Message):
    if client.user.id == message.author.id:
        return
    if messages:
        new_message = messages.pop()
        await message.channel.send(content=new_message)
    else:
        await message.channel.send(content="No new messages.")

@client.event
async def on_ready():
    Thread(target=lambda: messages.append(input()), daemon=True).start()

client.run("YOUR_TOKEN")

相关问题