机器人必须做一些事情每60秒。我试图用create_task,但它不工作(机器人开始了,但什么都没有发生)。这又如何实现?
client.loop.create_task
应该仍然正常工作与rewrite
版本。在rewrite
版本后台任务的例子可以发现here。
from discord.ext import commands
import asyncio
client = commands.Bot(command_prefix='!')
async def background_task():
await client.wait_until_ready()
counter = 0
channel = client.get_channel(123456) # Insert channel ID here
while not client.is_closed():
counter += 1
await channel.send(counter)
await asyncio.sleep(10)
client.loop.create_task(background_task())
client.run('token')