【发布时间】:2021-05-04 06:17:17
【问题描述】:
我希望仅当满足函数中的条件时才开始我的命令之一的冷却,如下所示:
@bot.command
async def move(ctx, destination):
destinations=["d1", "d2", "d3"] # List of valid arguments for the command
if destination in destinations:
movement(destination) # Function to actually move, not important for the question
# Start cooldown only here
else:
await ctx.send("This is not a valid destination")
这样,如果用户输入错误的目的地,他们将不会受到冷却时间的惩罚。我怎样才能做到这一点?
EDIT1:通常会使用 discord.py 的内置 @commands.cooldown 装饰器,这里是源代码:
def cooldown(rate, per, type=BucketType.default):
def decorator(func):
if isinstance(func, Command):
func._buckets = CooldownMapping(Cooldown(rate, per, type))
else:
func.__commands_cooldown__ = Cooldown(rate, per, type)
return func
return decorator
但这适用于整个命令。(它通常放在@bot.command 装饰器之后)
【问题讨论】:
-
你如何处理冷却时间?你使用函数吗?
-
@ImranD 编辑了消息以包含冷却装饰器的来源,有关其元素的更多信息在 discord.py 文档中
标签: python discord.py