首页 > 解决方案 > 是否可以每 x 秒将用户限制为 1 个命令?

问题描述

我知道单独的命令有一个命令冷却时间,但我想知道你是否可以一次限制每个命令,所以无论该人使用什么命令,它都会在之后有一个冷却时间,然后才能使用下一个命令。所以如果我跑了,!test我就不!test1能再使用 5 秒。

标签: discorddiscord.py

解决方案


Simply create a global cooldown instance

global_cooldown = commands.CooldownMapping.from_cooldown(1, 5.0, commands.BucketType.user) # Feel free to change it

And then add a check, if you want a global cooldown for all commands:

@bot.check
async def cooldown_check(ctx):
    bucket = global_cooldown.get_bucket(ctx.message)
    retry_after = bucket.update_rate_limit()
    if retry_after:
        raise commands.CommandOnCooldown(bucket, retry_after)
    return True

All commands will have a shared cooldown.

If you want a decorator for a few commands with a shared cooldown

def shared_cooldown():
    def predicate(ctx):
        bucket = global_cooldown.get_bucket(ctx.message)
        retry_after = bucket.update_rate_limit()
        if retry_after:
            raise commands.CommandOnCooldown(bucket, retry_after)
        return True
    return commands.check(predicate)

@shared_cooldown()
async def foo(ctx):
    ...

@shared_cooldown()
async def baz(ctx):
    ...

foo and baz will have a shared cooldown.


推荐阅读