我希望我的一个命令的冷却仅在满足函数中的条件时启动,如下所示:
@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 decorator,以下是源代码:
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 decorator之后)
Idk如果这是您正在寻找的,但是有一种方法可以使冷却仅在代码正确解析后激活:
您可以找到commands.cooldownhere的文档
以及解析here后冷却的文档
有很多方法可以让你自己进行冷却,这里有一个简单的方法可以做到这一点。其背后的想法是让机器人“记住”上次有人使用这个特定命令的时间,并在允许玩家移动之前检查这一次
注意:您可能需要也可能不需要删除
@bot.command
装饰符后面的括号相关问题 更多 >
编程相关推荐