python - CommandOnCooldown 错误处理程序未捕获错误!已经测试了一切

标签 python error-handling discord discord.py discord.py-rewrite

我查看了其他类似的问题,修复不起作用这是我的代码。

 @bot.event
async def on_command_error(ctx, error):

    if isinstance(error, commands.CommandOnCooldown):
        await ctx.send("Command On Cooldown. Sorry fo rugly text. I will fix it - Kwuartz")
        m, s = divmod(error.retry_after, 60)
        h, m = divmod(m, 60)
        d, h = divmod(h, 60)

        if int(h) == 0 and int(m) == 0:
            embed = discord.Embed(colour=0x1ABC9C)
            embed.title = "**Command Cooldown:**"
            embed.description = f"**Why this happened:**\nThis command is on a cooldown of **{int(s)} seconds** on a per user basis."
            await ctx.send(embed = embed)

        elif int(d) == 0 and int(h) == 0 and int(m) != 0:
            embed = discord.Embed(colour=0x1ABC9C)
            embed.title = "**Command Cooldown:**"
            embed.description = f"**Why this happened:**\nThis command is on a cooldown of **{int(m)} minutes** and **{int(s)} seconds** on a per user basis."
            await ctx.send(embed = embed)

        elif int(d) == 0 and int(h) != 0:
            embed = discord.Embed(colour=0x1ABC9C)
            embed.title = "**Command Cooldown:**"
            embed.description = f"**Why this happened:**\nThis command is on a cooldown of **{int(h)} hours**, **{int(m)} minutes** and **{int(s)} seconds** on a per user basis."
            await ctx.send(embed = embed)

        else:
            embed = discord.Embed(colour=0x1ABC9C)
            embed.title = "**Command Cooldown:**"
            embed.description = f"**Why this happened:**\nThis command is on a cooldown of **{int(d)} days**, **{int(h)} hours**, **{int(m)} minutes** and **{int(s)} seconds** on a per user basis."
            await ctx.send(embed = embed)

    elif isinstance(error, commands.CommandNotFound):
        embed = discord.Embed(colour=0x1ABC9C)
        embed.title = "**Command Error:**"
        embed.description = f":x: **Why this happened:**\nThe command you specified does not exist."
        await ctx.send(embed = embed)

    else:
        raise error
PS:我是新手,所以可能有愚蠢的错误。对不起!如有必要,我将附上更多代码
控制台输出:
忽略 on_message 中的异常
回溯(最近一次通话最后):
_run_event 中的文件“C:\Users\OnlyMe\AppData\Roaming\Python\Python38\site-packages\discord\client.py”,第 333 行
等待 coro(*args, **kwargs)
文件“C:\Users\OnlyMe\PythonProjects\Atom\ImpulseBot\main_setup.py”,第 158 行,on_message
等待 bot.process_commands(消息)
文件“C:\Users\OnlyMe\AppData\Roaming\Python\Python38\site-packages\discord\ext\commands\bot.py”,第 940 行,在 process_commands
等待 self.invoke(ctx)
文件“C:\Users\OnlyMe\AppData\Roaming\Python\Python38\site-packages\discord\ext\commands\bot.py”,第 907 行,在调用中
等待 ctx.command.dispatch_error(ctx, exc)
文件“C:\Users\OnlyMe\AppData\Roaming\Python\Python38\site-packages\discord\ext\commands\core.py”,第 422 行,在 dispatch_error
等待注入(inject)(cog,ctx,错误)
文件“C:\Users\OnlyMe\AppData\Roaming\Python\Python38\site-packages\discord\ext\commands\core.py”,第 71 行,已包装
ret = 等待 coro(*args, **kwargs)
文件“C:\Users\OnlyMe\PythonProjects\Atom\ImpulseBot\cogs\moderation_commands.py”,第 225 行,位于 clear_error
引发错误
文件“C:\Users\OnlyMe\AppData\Roaming\Python\Python38\site-packages\discord\ext\commands\bot.py”,第 903 行,在调用中
等待 ctx.command.invoke(ctx)
文件“C:\Users\OnlyMe\AppData\Roaming\Python\Python38\site-packages\discord\ext\commands\core.py”,第 851 行,在调用中
等待 self.prepare(ctx)
文件“C:\Users\OnlyMe\AppData\Roaming\Python\Python38\site-packages\discord\ext\commands\core.py”,第 785 行,在准备中
self._prepare_cooldowns(ctx)
_prepare_cooldowns 中的文件“C:\Users\OnlyMe\AppData\Roaming\Python\Python38\site-packages\discord\ext\commands\core.py”,第 773 行
提高 CommandOnCooldown(桶,retry_after)
discord.ext.commands.errors.CommandOnCooldown:你正在冷却。请在 9.53 秒后重试

最佳答案

如果您想以 hours, minutes, seconds, 格式获取时间那么您可以使用模运算符来发挥您的优势:

d = math.floor(error.retry_after / 60 / 60 / 24) 
h = math.floor(error.retry_after / 60 / 60) 
m = math.floor(error.retry_after / 60) 
s = error.retry_after % 60
如果 on_command_error没有捕获所有内容,这是因为它仅适用于命令错误。
PS。请说明您的问题,以便更容易解决。
这就是我的代码在工作时的样子:
if isinstance(error, commands.CommandOnCooldown):
    retryAfter = [math.floor(error.retry_after / 360), math.floor(error.retry_after / 60), error.retry_after % 60]
    await ctx.send('You cannot use command `%s%s` for %s hours, %s minutes, and %.2f seconds.' % (
    str(bot.command_prefix), str(ctx.command), retryAfter[0], retryAfter[1], retryAfter[2]))
    print('Command "%s" is on a %.3f second cooldown' % (ctx.command, error.retry_after))

关于python - CommandOnCooldown 错误处理程序未捕获错误!已经测试了一切,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64402245/

相关文章:

java - 括号和数组声明的问题

python - Python input()函数引发EOF错误

python - 启动 [Selenium] [Python] 后 Chrome 崩溃

python - 添加到列表与更改列表元素

python - Python 中的 base64 到 json 属性

php - php yii将错误发送到actionCreate上的表单

javascript - Discord - 添加到用户昵称然后删除添加的单词

javascript - Discord 帮助命令 - args.join 不是函数

javascript - 我正在为我的 Discord 机器人创建一个用户信息命令,想知道是否有办法让 .createdAt 看起来更好

python - 在 Raspberry Pi 启动时启动 shell 脚本