简体   繁体   中英

Discord.py task is not running

I'm trying to add a background task to my cog in Discord.py, but the task isn't running.

The cog:

class ComicPoster(Cog):
    def __init__(self, bot):
        self.bot: Bot = bot
        migrate()

    @tasks.loop(seconds=5)
    async def comic_update(self):
        print('asdf')
        log.info('Updating all comics')

The rest of the cog works (event listeners, commands, etc), but the task never runs for some reason. How can I get it to run?

You need to start the task. You can do this in the constructor ( __init__ ).

Here is what it should look like:

def __init__(self, bot):
    self.bot: Bot = bot
    migrate()
    # the new line of code
    self.comic_update.start()

The self.comic_update.start() starts that task and it will loop every five seconds now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM