Frexom
Frexom

Reputation: 83

Check if bot has enough permissions (or handle it)

I'm looking for a way of handling a bot missing permissions. Like I remove it "send_messages" permission and I try to make it send a message.

Thing is i don't know how to do, I've been looking for this for hours, and i don't want to add a

if bot.has_permissions("send_messages"):

each time i want to do anything. Also the purpose of this bot if to be on servers I am not on so editing guild's permissions is not an option.

Thanks

Upvotes: 0

Views: 823

Answers (1)

moinierer3000
moinierer3000

Reputation: 1979

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.")

Upvotes: 2

Related Questions