Skip to content

Instantly share code, notes, and snippets.

@AileenLumina
Last active Jun 3, 2021
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)
@GNUGradyn

This comment has been minimized.

Copy link

@GNUGradyn GNUGradyn commented Nov 9, 2018

How did you get a stack trace when ignoring an exception and printing it to STDERR?

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented May 20, 2019

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

@jordanchiquet

This comment has been minimized.

Copy link

@jordanchiquet jordanchiquet commented Aug 1, 2019

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

@terminate.error
async def terminate_error(ctx, error):
    if isinstance(error, commands.CheckFailure):

This is what I use for my 'terminate' command which requires admin role.

@MaciejkaG

This comment has been minimized.

Copy link

@MaciejkaG MaciejkaG commented Oct 18, 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.

@barryii

This comment has been minimized.

Copy link

@barryii 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

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