1
votes

I am using discord.py to write a discord bot for my server and was wondering how to message to the server when someone doesnt have the correct permissions to execute the command

Im using the decorators:

@client.command()
@commands.has_any_role("Administrator", "BOT SQUAD")

and obviously when I run the command with my test account, it gives an error that says that it doesnt have permissions which is normal

What Im looking to do is make it so that when this error occurs, instead of printing out in the logs that the user doesnt have permissions, it tells the user in the channel he messaged in or even private messages them saying that they dont have the permissions to execute the command.

Im new to discord.py and so i have no idea how to implement this. I have looked all through the documentation and this has not helped me nor has any other question ive looked at.

If anyone could help me to do this then that would be perfect. Bonus Points if you keep the decorators ive already got as thats the only way I know how to implement seperate roles for functions at the moment!

1

1 Answers

0
votes

In d.py there's an event called on_command_error() which, as the name suggests, executes when there's an error while running a command. Here's an example:

@client.event
async def on_command_error(ctx, error):
    if isinstance(error, discord.ext.commands.errors.CheckFailure): # checking which type of error it is
        missing_roles = error.missing_roles # returns tuple
        linebreak = "\n"
        await ctx.send(f":x: Check failure!\nYou are missing the following roles:\n{linebreak.join(missing_roles)}")
    else:
        await ctx.send(error) # I can recommend popping this in here also if you wish to catch other errors

Here's a list of all the different exceptions.

In the example above, that would be the check you would want in your code. CheckFailure is referring to any checking decorators on your functions.