如何在discord.py重写中进行循环?

Buz*_*ard 0 python-3.x discord.py

机器人必须每 60 秒执行一次操作。我尝试使用 create_task,但它不起作用(机器人启动但没有任何反应)。如何实施?

Ben*_*jin 5

client.loop.create_task应该仍然可以很好地使用该rewrite版本。可以在此处rewrite找到该版本中后台任务的示例。

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')
Run Code Online (Sandbox Code Playgroud)