【问题标题】:Discord.py How to avoid the first loop to trigger using discord.ext.tasksDiscord.py 如何避免使用 discord.ext.tasks 触发第一个循环
【发布时间】:2021-04-24 19:52:36
【问题描述】:

如何防止我的不和谐机器人中的第一个循环在开始触发后立即触发?循环应该每 30 分钟发生一次,我不希望它在机器人启动时触发。

from discord.ext import tasks

@tasks.loop(minutes=30) 
async def hunger():
    #Doesn't really matter what is here..


@hunger.before_loop
async def before():
    await client.wait_until_ready()

hunger.start()
client.run('xxx')

【问题讨论】:

    标签: python python-3.x loops discord.py task


    【解决方案1】:

    您可以通过current_loop 检查循环的当前迭代(注意这是从 0 开始的)

    @tasks.loop(minutes=30) 
    async def hunger():
        if hunger.current_loop != 0:
            #Doesn't really matter what is here..
    

    参考:

    【讨论】:

    • 表示未分配局部变量饥饿:/
    • 你是如何命名你的函数的?上面的代码对我来说完美无缺。
    • 如果你在@Bertos 类中有它,你需要self.func.current_loop
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-23
    • 2011-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-22
    • 1970-01-01
    相关资源
    最近更新 更多