0
votes

I have looked at the other questions similar the fixes are not working this is my code.

 @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: I am new so may have stupid mistakes. Sorry! I will attach more code if necessary

Console output:

Ignoring exception in on_message Traceback (most recent call last): File "C:\Users\OnlyMe\AppData\Roaming\Python\Python38\site-packages\discord\client.py", line 333, in _run_event await coro(*args, **kwargs) File "C:\Users\OnlyMe\PythonProjects\Atom\ImpulseBot\main_setup.py", line 158, in on_message await bot.process_commands(message) File "C:\Users\OnlyMe\AppData\Roaming\Python\Python38\site-packages\discord\ext\commands\bot.py", line 940, in process_commands await self.invoke(ctx) File "C:\Users\OnlyMe\AppData\Roaming\Python\Python38\site-packages\discord\ext\commands\bot.py", line 907, in invoke await ctx.command.dispatch_error(ctx, exc) File "C:\Users\OnlyMe\AppData\Roaming\Python\Python38\site-packages\discord\ext\commands\core.py", line 422, in dispatch_error await injected(cog, ctx, error) File "C:\Users\OnlyMe\AppData\Roaming\Python\Python38\site-packages\discord\ext\commands\core.py", line 71, in wrapped ret = await coro(*args, **kwargs) File "C:\Users\OnlyMe\PythonProjects\Atom\ImpulseBot\cogs\moderation_commands.py", line 225, in clear_error raise error File "C:\Users\OnlyMe\AppData\Roaming\Python\Python38\site-packages\discord\ext\commands\bot.py", line 903, in invoke await ctx.command.invoke(ctx) File "C:\Users\OnlyMe\AppData\Roaming\Python\Python38\site-packages\discord\ext\commands\core.py", line 851, in invoke await self.prepare(ctx) File "C:\Users\OnlyMe\AppData\Roaming\Python\Python38\site-packages\discord\ext\commands\core.py", line 785, in prepare self._prepare_cooldowns(ctx) File "C:\Users\OnlyMe\AppData\Roaming\Python\Python38\site-packages\discord\ext\commands\core.py", line 773, in _prepare_cooldowns raise CommandOnCooldown(bucket, retry_after) discord.ext.commands.errors.CommandOnCooldown: You are on cooldown. Try again in 9.53s

1

1 Answers

0
votes

If you want to get the time in the format hours, minutes, seconds, then you can use the modulo operator to your advantage:

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

If the on_command_error is not catching everything, it's because it only applies to command errors.

PS. Please specify your problem so it is easier to fix.

This is what my code was when it worked:

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))