discord.py 中的命令冷却时间

Jef*_*ang 0 python bots discord discord.py

我希望我的不和谐机器人的命令有冷却时间。我尝试过其他方法,但似乎没有一个适合我所用的方法。

@client.event
async def on_message(message):
  if message.content == 'shear sheep':
    await message.channel.send('you sheared your sheep, gaining 1 wool.')
    #cooldown?
Run Code Online (Sandbox Code Playgroud)

小智 6

如果你用的话会更好@bot.command,而不是event。然后,你必须在@commands.cooldown(1, {seconds off cooldown}, commands.BucketType.user)下面输入@bot.command

例子:

@bot.command
@commands.cooldown(1, 15, commands.BucketType.user)
async def shearsheep(ctx):
    await ctx.send('you sheared your sheep, gaining 1 wool.')
Run Code Online (Sandbox Code Playgroud)

然后,您可以创建一个错误处理程序,当您尝试使用命令时,它将发送一条消息,但它将处于冷却状态,例如:

@bot.event
async def on_command_error(ctx, error):
    if isinstance(error, commands.CommandOnCooldown):
        await ctx.send('This command is on cooldown, you can use it in {round(error.retry_after, 2)}')
Run Code Online (Sandbox Code Playgroud)

我认为这是最简单的方法。