Last active
May 2, 2022 17:40
-
-
Save AileenLumina/510438b241c16a2960e9b0b014d9ed06 to your computer and use it in GitHub Desktop.
discord.py command error handler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
it doesn't work
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")
Thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ffs use codeblocks @barryii