任务仅运行一次且不循环(在齿轮内)

时间:2020-08-25 17:02:39

标签: python discord.py

我正在尝试使用discord.ext中的Tasks为我的机器人运行后台条纹检查。但是,该任务只能运行一次,并且不会进一步循环。

代码如下:

class StreakCheck(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
        self.streak_check.start()

    @tasks.loop(seconds=5.0)
    async def streak_check(self):
        rid = await self.canishel.fetchall()
        idlist = [id[0] for id in rid]
        for id in idlist:
            await self.canishel.execute("SELECT mailbox FROM Anishel WHERE id = ?", (id,))
            mailcd = await self.canishel.fetchone()
            mailcd = mailcd[0]
            limit = mailcd+86400
            if int(time.time()) > limit:
                await self.canishel.execute("SELECT streak FROM Anishel WHERE id = ?", (id,))
                streak = await self.canishel.fetchone()
                streak = streak[0]
                if streak == 1:
                    print("Nothing to reset")
                else:
                    await self.canishel.execute("UPDATE Anishel SET streak=1 WHERE id = ?", (id,))
                    print("Streak reset for {}".format(id))
            else:
                print("All clear")
        await self.dbanishel.commit()

    @streak_check.before_loop
    async def before_streak_check(self):
        await self.bot.wait_until_ready()
        # Code to connect to sqlite database and define cursor
        
def setup(bot):
    bot.add_cog(StreakCheck(bot))

0 个答案:

没有答案