如何使discord.py重写循环?

问题描述 投票:0回答:1

机器人必须做一些事情每60秒。我试图用create_task,但它不工作(机器人开始了,但什么都没有发生)。这又如何实现?

python-3.x discord.py discord.py-rewrite
1个回答
0
投票

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')
© www.soinside.com 2019 - 2024. All rights reserved.