【问题标题】:Discord.py task is not runningDiscord.py 任务未运行
【发布时间】:2021-03-28 05:34:45
【问题描述】:

我正在尝试在 Discord.py 中将后台任务添加到我的 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')

其余的 cog 工作(事件侦听器、命令等),但该任务由于某种原因从未运行。我怎样才能让它运行?

【问题讨论】:

  • self.bot 定义后是否添加了self.comic_update.start()?

标签: python discord.py


【解决方案1】:

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

它应该是这样的:

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

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

【讨论】:

    猜你喜欢
    • 2021-07-30
    • 2021-03-15
    • 2021-09-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-10
    • 2022-06-24
    • 2018-09-12
    • 1970-01-01
    相关资源
    最近更新 更多