Discord.py任务未运行

时间:2020-07-31 03:07:58

标签: python discord.py

我正在尝试向Discord.py中的齿轮添加后台任务,但是该任务未运行。

齿轮:

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

其余的齿轮都可以工作(事件侦听器,命令等),但是由于某种原因该任务永远不会运行。我如何才能运行它?

1 个答案:

答案 0 :(得分:1)

您需要开始任务。您可以在构造函数 (__init__) 中执行此操作。

它应该是这样的:

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

self.comic_update.start() 启动该任务,现在每五秒循环一次。