discord - 是否可以每 x 秒将用户限制为 1 个命令?
问题描述
我知道单独的命令有一个命令冷却时间,但我想知道你是否可以一次限制每个命令,所以无论该人使用什么命令,它都会在之后有一个冷却时间,然后才能使用下一个命令。所以如果我跑了,!test
我就不!test1
能再使用 5 秒。
解决方案
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.
推荐阅读
- maven - 如何使用 JDK 11 构建 JDK7 pom.xml?
- javascript - jQuery .Click 事件触发器超出了它分配给的输入元素
- react-native - 如何以编程方式调用 Pressable 的 onPress() 方法?
- postgresql - PostgreSQL:to_tsquery 以搜索开始并以搜索结束
- python - 由 Python 解释的 C++ 字节数组(结构)
- javascript - Plyr - 持续时间结束时监听事件
- python - 提取文本直到达到第一个数字 - Python
- php - PHP联系页面设置
- javascript - 是否可以从网站运行不和谐的机器人命令
- javascript - 如何使用 Node js 在单个 API 调用中传递两个请求 - Cloudant