How to make a loop in discord.py rewrite?

微笑、不失礼 提交于 2019-12-25 00:14:43

问题


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?


回答1:


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')


来源:https://stackoverflow.com/questions/54495679/how-to-make-a-loop-in-discord-py-rewrite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!