为了账号安全,请及时绑定邮箱和手机立即绑定

discord.py bot 获取命令的剩余冷却时间

discord.py bot 获取命令的剩余冷却时间

慕少森 2021-06-10 18:01:25
我正在开发一个基于 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 shellIgnoring exception in command MineTraceback (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.58 秒后重试”我在网上找不到太多帮助,而且大部分已经过时或似乎不起作用。
查看完整描述

1 回答

?
ITMISS

TA贡献1871条经验 获得超8个赞

您需要为您的命令编写一个错误处理程序来处理CommandOnCooldown错误并发送消息。


@mine.error

async def mine_error(ctx, error):

    if isinstance(error, commands.CommandOnCooldown):

        msg = 'This command is ratelimited, please try again in {:.2f}s'.format(error.retry_after)

        await ctx.send(msg)

    else:

        raise error


查看完整回答
反对 回复 2021-06-16
  • 1 回答
  • 0 关注
  • 189 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号