如何在discord.py重写中进行循环? [英] How to make a loop in discord.py rewrite?

查看:84
本文介绍了如何在discord.py重写中进行循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

机器人必须每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 版本的后台任务示例。 background_task.py rel = nofollow noreferrer>此处

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重写中进行循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆