Skip to content

Instantly share code, notes, and snippets.

@AileenLumina
Last active May 2, 2022
Embed
What would you like to do?
discord.py command error handler
async def on_command_error(self, ctx, error):
# if command has local error handler, return
if hasattr(ctx.command, 'on_error'):
return
# get the original exception
error = getattr(error, 'original', error)
if isinstance(error, commands.CommandNotFound):
return
if isinstance(error, commands.BotMissingPermissions):
missing = [perm.replace('_', ' ').replace('guild', 'server').title() for perm in error.missing_perms]
if len(missing) > 2:
fmt = '{}, and {}'.format("**, **".join(missing[:-1]), missing[-1])
else:
fmt = ' and '.join(missing)
_message = 'I need the **{}** permission(s) to run this command.'.format(fmt)
await ctx.send(_message)
return
if isinstance(error, commands.DisabledCommand):
await ctx.send('This command has been disabled.')
return
if isinstance(error, commands.CommandOnCooldown):
await ctx.send("This command is on cooldown, please retry in {}s.".format(math.ceil(error.retry_after)))
return
if isinstance(error, commands.MissingPermissions):
missing = [perm.replace('_', ' ').replace('guild', 'server').title() for perm in error.missing_perms]
if len(missing) > 2:
fmt = '{}, and {}'.format("**, **".join(missing[:-1]), missing[-1])
else:
fmt = ' and '.join(missing)
_message = 'You need the **{}** permission(s) to use this command.'.format(fmt)
await ctx.send(_message)
return
if isinstance(error, commands.UserInputError):
await ctx.send("Invalid input.")
await self.send_command_help(ctx)
return
if isinstance(error, commands.NoPrivateMessage):
try:
await ctx.author.send('This command cannot be used in direct messages.')
except discord.Forbidden:
pass
return
if isinstance(error, commands.CheckFailure):
await ctx.send("You do not have permission to use this command.")
return
# ignore all other exception types, but print them to stderr
print('Ignoring exception in command {}:'.format(ctx.command), file=sys.stderr)
traceback.print_exception(type(error), error, error.__traceback__, file=sys.stderr)
@barryii
Copy link

barryii commented Dec 23, 2020

Nice code, help me so much. Thanks!
EDIT: But can i add an CheckFailure error to a one command? If i do this that command stops working.

Maybe you can add a decorator called @commands.is_owner() if you don't have a cog_check in this cog.
The error of @commands.is_owner() is called NotOwner. If you have cog_check in this cog, then it will just be CheckFailure.
And you have to put NotOwner before CheckFailure.
Or you can make an independent error for only one command in the same cog.
@yourcommandname.error
async def yourcommandname_error(self,ctx,error):
if isinstance(error, commands.errors.CheckFailure):
await ctx.send('blahblahblah')
return

@RGBCube
Copy link

RGBCube commented Dec 11, 2021

ffs use codeblocks @barryii

@cwz6h
Copy link

cwz6h commented Mar 30, 2022

it doesn't work

@Hunter87ff
Copy link

Hunter87ff commented Apr 27, 2022

How do i detect the error of the @commands.has_role() check?

@bot.event
async def on_command_error(ctx, error):
    elif isinstance(error, (commands.MissingRole, commands.MissingAnyRole)):
      await ctx.send("You dont have the exact role to use this command")

More Detail

@iSimpp
Copy link

iSimpp commented May 2, 2022

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment