How to add a function to discord.py event loop?

前端 未结 3 1687
我在风中等你
我在风中等你 2021-02-16 00:01

I am using Python with discord.py. Documentation here

I\'ve got a bot that is running on a Discord server that links the server with a subr

3条回答
  •  终归单人心
    2021-02-16 00:17

    The other answers here don't take into account discord.py's helpful tasks.loop decorator.

    To make an event occur every 5 seconds, you would use

    from discord.ext import tasks, commands
    
    class MyCog(commands.Cog):
        def __init__(self):
            self.foo.start()
    
        def cog_unload(self):
            self.printer.cancel()
    
        @tasks.loop(seconds=5.0)
        async def foo(self):
            print('bar')
    

    More can be found here: https://discordpy.readthedocs.io/en/latest/ext/tasks/

提交回复
热议问题