So, I have gone through and added different functions to my discord bot. I am working on making it to where a function if used incorrectly can have two different error outputs depending on the users permissions.
For example: When I use the command "!clear" as a server admin, I have it to where I will get the error, "Please specify an amount of messages to delete." because the command should be used like "!clear (amount)".
But when someone that is not an admin runs this command, I would like for a different error to populate. For example: A normal user tries to use "!clear", I would want the error output to say "Error: You do not have permissions for this command."
But I am not able to figure out how to apply two different error outputs to one function / command.
Whenever I go onto the normal account and do command "!clear", it populates "Please specify an amount of messages to delete.", when I would only want that error output for admins.
I only want for the output "Error: You do not have permissions for this command." to show when a normal user tries to use an administrative bot command.
@client.command()
@commands.has_permissions(administrator=True) # Only allows administrators to use this command
async def clear(ctx, amount : int):
await ctx.channel.purge(limit=amount)
await ctx.send(f'**Messages have been cleared! :thumbsup:**')
@clear.error
async def clear_error(ctx, error):
if isinstance(error, commands.MissingPermissions): # Error populates when someone with no permissions tries to run the command.
await ctx.send(f'**Error: You do not have permissions for this command.**')
@clear.error
async def clear_error(ctx, error): # Error populates when the user does not specify the amount of messages to delete
await ctx.send(f'**Please specify an amount of messages to delete. :memo:**')```
You could just check the error that is arised in the same clear_error
function, for example:
@client.command()
@commands.has_permissions(administrator=True) # Only allows administrators to use this command
async def clear(ctx, amount : int):
await ctx.channel.purge(limit=amount)
await ctx.send(f'**Messages have been cleared! :thumbsup:**')
@clear.error
async def clear_error(ctx, error):
if isinstance(error, commands.MissingPermissions): # Error populates when someone with no permissions tries to run the command.
await ctx.send(f'**Error: You do not have permissions for this command.**')
elif isinstance(error, commands.MissingRequiredArgument): # Error populates when the user does not specify the amount of messages to delete
await ctx.send(f'**Please specify an amount of messages to delete. :memo:**')
You can even add more types of errors and a better approach is to have more general error messages.
And to avoid putting the @clear.error
decorator before every function, you could just override the default error handler from discord.py:
@commands.Cog.listener()
async def on_command_error(self, ctx, error):
if isinstance(error, commands.MissingPermissions): # Error populates when someone with no permissions tries to run the command.
await ctx.send(f'**Error: You do not have permissions for this command.**')
elif isinstance(error, commands.MissingRequiredArgument): # Error populates when the user does not specify the amount of messages to delete
await ctx.send(f'**Please specify an amount of messages to delete. :memo:**')
Docs: