我想讓機器人在某個時間(比如 10:00)在聊天中打招呼。問題是“while”不是異步程序(如“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
uj5u.com熱心網友回復:
您可以使用任務回圈。https://discordpy.readthedocs.io/en/stable/ext/tasks/index.html?highlight=tasks loop#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
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/496320.html
上一篇:按一列分組并獲得最小值回傳NA
下一篇:需要解決python邏輯任務
