我正试图为我的不和谐.py机器人添加一个冷却时间,这样用户就不能垃圾邮件发送命令,在我的情况下,它很好。我使用下面的decorator和bot.event
,我可以让机器人发送一条消息,说冷却已经到位。
@bot.event
async def on_command_error(ctx, error):
if isinstance(error, commands.CommandOnCooldown):
await ctx.send(f"wait for {round(error.retry_after, 2)} seconds before trying again")
我通过在我的命令中使用这个装饰来做到这一点。
@bot.command(name="dice", help="bet a certain amount of money in dice")
@commands.cooldown(1, 1200, commands.BucketType.user)
然而,问题是,我有几个不同的冷却命令,我想为这些不同的命令中的每一个发送一条自定义消息,比如对于dice
命令,机器人可能会说";赌博太多是不好的,你不认为吗"并且对于CCD_ 3命令,机器人可以说";硬币不是直接从天上掉下来的"等等。
我想知道是否有某种方法可以从本质上检测机器人接收到的命令,并在此基础上提供不同的冷却消息。
TL;DR如何为每个命令制作个性化的冷却消息?
所以问题是检查调用了什么命令,我们可以使用提供的上下文来完成:
if isinstance(error, commands.CommandOnCooldown):
if ctx.command.name == 'mine':
await ctx.send("Coins dont rain from the sky")
elif ctx.command.name == 'beg':
await ctx.send("there is no one in the street to beg to")
else:
await ctx.send('command is on cooldown')
参考文献:
ctx.command
command.name
注意:默认情况下,command.name
是函数的名称,您可以通过在bot.command
装饰器中传递name=bar
来更改它。