Skip to content

Instantly share code, notes, and snippets.

@MarkKoz
Last active December 30, 2020 00:01
Show Gist options
  • Save MarkKoz/85bbabd16515002a3fe37c9bf128101f to your computer and use it in GitHub Desktop.
Save MarkKoz/85bbabd16515002a3fe37c9bf128101f to your computer and use it in GitHub Desktop.
Discord.py Rewrite Global Command Error Handler
async def on_command_error(
self,
ctx: commands.Context,
error: commands.CommandError
):
# Skips errors that were already handled locally.
if getattr(ctx, 'handled', False):
return
if isinstance(error, commands.NoPrivateMessage):
await ctx.send('This command cannot be used in direct messages.')
elif isinstance(error, commands.TooManyArguments):
await ctx.send('Too many arguments.')
elif isinstance(error, commands.MissingRequiredArgument):
await ctx.send(f'Missing required argument `{error.param.name}`.')
elif (
isinstance(error, commands.NotOwner)
or isinstance(error, commands.MissingPermissions)
):
await ctx.send(
'You do not have the required permissions to invoke this '
'command.'
)
elif (
isinstance(error, commands.CommandOnCooldown)
or isinstance(error, commands.CheckFailure)
):
await ctx.send(error)
elif isinstance(error, commands.DisabledCommand):
await ctx.send(
'This command is currently disabled and cannot be used.'
)
elif isinstance(error, commands.BadArgument):
await ctx.send(f'Bad argument: {error}')
elif isinstance(error, commands.BotMissingPermissions):
await ctx.send(
'Oops! The bot does not have the required permissions to '
'execute this command.'
)
log.error(
f'{ctx.command.qualified_name} cannot be executed because the '
f'bot is missing the following permissions: '
f'{", ".join(error.list)}'
)
elif isinstance(error, commands.CommandInvokeError):
await ctx.send('Something went wrong internally!')
log.error(
f'{ctx.command.qualified_name} failed to execute. '
f'{error.original.__class__.__name__}: {error.original}\n'
f'{"".join(traceback.format_tb(error.original.__traceback__))}'
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment