-
-
Save AileenLumina/510438b241c16a2960e9b0b014d9ed06 to your computer and use it in GitHub Desktop.
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) |
How do i detect the error of the @commands.has_role()
check?
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.
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.
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
ffs use codeblocks @barryii
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!
How did you get a stack trace when ignoring an exception and printing it to STDERR?