【问题标题】:How to make a loop in discord.py rewrite?如何在 discord.py rewrite 中创建一个循环?
【发布时间】:2023-03-25 10:25:01
【问题描述】:

机器人必须每 60 秒执行一次操作。 我尝试使用 create_task,但它不起作用(机器人启动但什么也没发生)。如何实现?

【问题讨论】:

  • 使用for 循环还是while 循环?你到底想做什么?

标签: python-3.x discord.py discord.py-rewrite


【解决方案1】:

client.loop.create_taskrewrite 版本上仍然可以正常工作。 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')

【讨论】:

    猜你喜欢
    • 2021-09-28
    • 2017-04-05
    • 1970-01-01
    • 2016-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-10
    • 1970-01-01
    相关资源
    最近更新 更多