Bashar Zain
Bashar Zain

Reputation: 20

How to ignore “Missing Permissions” error and reply with a message? (Discord)

If you’ve experienced with Discord.js a good bit, you would know:

DiscordAPIError: Missing Permissions

I’ve dealt with this error before, and it’s easy to fix, but I don’t want this error to crash my bot when my bot becomes public and users try to do something that requires a permission the bot just can’t do.

How am I able to send a message saying “I don’t have Permissions to do this!” and so on? I also don’t want to add code in all of my commands. I want to make this universal. Something in my main file.

So Instead of the DiscordAPIError in console, the user would get a message or Direct Message (In certain occasions) saying this:

Sorry, I don’t have permissions to do this, please contact an Administrator for more information.

Upvotes: 0

Views: 985

Answers (2)

Lofn
Lofn

Reputation: 940

Like Wolf suggested on the second example, if you want to send them message, just .catch() it.

message.channel.send("Your super cool message here").catch(e => {
    message.author.send("Missing permissions to do this!");
    console.log(e);
}

The reason why you should rather send a message to the command sender/message author than send the message again saying no permission is because you already got an API error trying to send a message to that channel, so the second message won't do any difference.

Upvotes: 0

Wolf Yuan
Wolf Yuan

Reputation: 128

You can catch error and ignore it when you call message.channel.send

Example: (1)

try {
    message.channel.send("Foo bar");
} catch () {} /*Ignore error*/

(2)

message.channel.send("Foo bar").catch(() => {/*Ignore error*/})

Upvotes: 2

Related Questions