如何在 discord.py rewrite 中创建一个循环? [英] How to make a loop in discord.py rewrite?
本文介绍了如何在 discord.py rewrite 中创建一个循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
机器人必须每 60 秒执行一次操作.我尝试使用 create_task,但它不起作用(机器人启动但什么也没发生).如何实现?
The bot must do something every 60 seconds. I tried to use create_task, but it does not work(the bot started but nothing happened). How can this be implemented?
推荐答案
client.loop.create_task
应该仍然可以在 rewrite
版本中正常工作.rewrite
版本中的后台任务示例可以在 这里.
client.loop.create_task
should still work fine with the rewrite
version. Example of a background task in the rewrite
version can be found 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')
这篇关于如何在 discord.py rewrite 中创建一个循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文