首页 > 解决方案 > 命令冷却数小时和数分钟

问题描述

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

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

标签: pythonpython-3.xdiscorddiscord.py

解决方案


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 分钟的冷却时间。


推荐阅读