首页 文章

discord.py机器人获得冷却时间的命令

提问于
浏览
1

我正在研究一个基于python的discord bot,它具有以下命令

@client.command(name="Mine",
            description="Mine daily.",
            brief="Mine daily.",
            aliases=['mine', 'm'],
            pass_context=True)
@commands.cooldown(1, 30, commands.BucketType.user)
async def mine(ctx, arg):
   <content>

但是当用户达到命令的30秒速率限制时,它会将错误输出到python shell

Ignoring exception in command Mine
Traceback (most recent call last):
   File "C:\Users\raner\AppData\Local\Programs\Python\Python36\lib\site- 
      packages\discord\ext\commands\bot.py", line 846, in process_commands
      yield from command.invoke(ctx)
   File "C:\Users\raner\AppData\Local\Programs\Python\Python36\lib\site- 
      packages\discord\ext\commands\core.py", line 367, in invoke
      yield from self.prepare(ctx)
   File "C:\Users\raner\AppData\Local\Programs\Python\Python36\lib\site- 
      packages\discord\ext\commands\core.py", line 351, in prepare
      raise CommandOnCooldown(bucket, retry_after)
discord.ext.commands.errors.CommandOnCooldown: You are on cooldown. Try 
again in 28.58s

我想要做的是得到一些可以获得剩余冷却时间的东西并将其放入可以在不和谐的情况下回复给用户的东西,例如'此命令是限速的,请在28.58s再试一次'

我无法在网上找到太多帮助,而且大部分都已过时或似乎无法正常工作 .

谢谢!

1 回答

  • 1

    您需要为处理CommandOnCooldown错误并发送消息的命令编写error handler .

    @mine.error
    async def mine_error(error, ctx):
        if isinstance(error, commands.CommandOnCooldown):
            msg = 'This command is ratelimited, please try again in {:.2f}s'.format(error.retry_after)
            await client.send_message(ctx.message.channel, msg)
    

相关问题