【问题标题】:Command cooldown for hours and minutes命令冷却数小时和数分钟
【发布时间】:2018-06-12 02:30:40
【问题描述】:

我添加了一个命令冷却时间,但如何让它持续数小时和数分钟。

@bot.command(pass_context=True)
@commands.cooldown(1, 30, commands.BucketType.user)
async def ping(ctx):
    msg = "Pong {0.author.mention}".format(ctx.message)
    await bot.say(msg)

【问题讨论】:

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


    【解决方案1】:

    commands.cooldown 的第二个参数,per 以秒为单位,您可以轻松地将所需的小时和分钟转换为秒,方法是乘以它们的等效秒数(1 分钟 = 60 秒,1 小时 = 3600 秒)。您还可以创建一个为您进行转换的包装函数:

    def cooldown(rate, per_sec=0, per_min=0, per_hour=0, type=commands.BucketType.default):
        return commands.cooldown(rate, per_sec + 60 * per_min + 3600 * per_hour, type)
    
    @bot.command(pass_context=True)
    @cooldown(1, per_min=5, per_hour=1, type=commands.BucketType.user)
    async def ping(ctx):
        msg = "Pong {0.author.mention}".format(ctx.message)
        await bot.say(msg)
    

    这将启动 1 小时 5 分钟的冷却时间。

    【讨论】:

    • @Demotry 如果要将秒转换为小时/分钟,请查看this question
    • @abccd 好的,我会问另一个问题。
    猜你喜欢
    • 1970-01-01
    • 2021-06-17
    • 2021-05-20
    • 1970-01-01
    • 2017-11-21
    • 2021-06-26
    • 2021-05-13
    • 2021-02-16
    • 2021-03-11
    相关资源
    最近更新 更多