在某个时间Bot写一些东西在Discord聊天与后台任务



我想让Bot在某个时间(比如10:00)在聊天中打招呼。问题是"当"不是异步进程(如"asyncio.sleep")。谁能告诉我怎么做?

我试过这个(写在"on_ready"之后):

while cyclebb == 1: #"cyclebb" is an always active variable
blt = f"{datetime.now().hour} : {datetime.now().minute} : {datetime.now().second}"
if blt == '10 : 42 : 15':
chn = client.get_channel(964837651108732979)
await chn.send('Hi guys!!')
await asyncio.sleep(2) #I put it to prevent spamming

可以使用任务循环。https://discordpy.readthedocs.io/en/stable/ext/tasks/index.html?highlight=tasks%20loop discord.ext.tasks.loop

它看起来像这样。

from discord.ext import tasks
async def on_ready():
greet_task.start()

@tasks.loop(hours=24)
async def greet_task():
...stuff here

最新更新