Permission Check Discord.Py Bot

How do I check if my Python Discord bot has the necessary permissions?

User objects don't have server permissions, Member objects do. (Because a User can be a Member of multiple servers, with different permissions in each) To get the Member object representing your bot on a particular server, use Server.me. So for all of your examples, change

client.user.server_permissions

to

message.channel.server.me.server_permissions

Check if bot has enough permissions (or handle it)

You could add a general error handler for the Forbidden error, which discord.py raises on a 403 Forbidden: Missing Permissions error. You need to unpack it first from the general CommandInvokeError before.

@bot.event
async def on_command_error(ctx, error):
if isinstance(error, commands.CommandInvokeError):
error = error.original

if isinstance(error, discord.errors.Forbidden):
await ctx.send("Whatever you want to say here.")

How to allow command with one discord.py check?

I found an answer! After reviewing the docs, I found the @commands.check_any() decorator. You can input checks for it to check, and if any of the inputted checks will be passed, it adds that check.

Read more about this decorator in the documentation.

Discord.py using multiple permission check decorators on command methods

You can use the commands.check_any decorator

@client.command()
@commands.check_any(commands.is_owner(), commands.has_permissions(manage_channels=True))
async def ...

Reference:

  • commands.check_any


Related Topics



Leave a reply



Submit