how can i ignore all bot's permission errors in Discord.JS? I have already the checking bot's permissions functions, but this functions doesn't work on the channels permissions. You can answer how can i ignore all bot's permission errors (if it can) or how to check permissions even in the channels permissions. There is my error (its same as not checking permissions):
throw new DiscordAPIError(data, res.status, request);
^
DiscordAPIError: Missing Permissions
CodePudding user response:
You could solve this in multiple ways
- Wrap it in a try/catch statement
This would look something like:
try{
// Do this
} catch (error) {
// Do that
}
- You caould use a .catch() function
role.doSomething().catch((error) => {
// Do that
})
If you wanted to see if the error code was due to a missing permission you could do:
const { Constants } = require("discord.js")
try{
// Do this
} catch (error) {
// Do that
if(error.code === Constants.APIErrors.MISSING_PERMISSIONS){
// Do this and that
}
}
CodePudding user response:
You can check GuildMember#permissions to see if the client has a certain permission in a guild.
// Example
const botMember = message.guild.me;
if (!botMember.permissions.has('KICK_MEMBERS')) {
// Code
} else {
// Error Handling
}
Additionally, you can use GuildMember#permissionsIn() to check the client's permissions in a certain channel
const botMember = message.guild.me;
if (!botMember.permissionsIn(CHANNEL).has('permission')) {
// Code
} else {
// Error Handling
}
As shown in other answers, you can also handle the promise rejection using .catch()
or catch () {}
Ensure you're await
ing any promise function when using async/await try/catch
// Example
try {
await message.channel.send(...);
} catch (err) {
// Handle err
}
message.channel.send(...)
.then(msg => {})
.catch(err => {
// Handle err
});
CodePudding user response:
You could try exception handling, if you're just wanting the bot to proceed without crashing it.
I would recommend at least logging the error in console. For this example I'll use a try/catch block.
try{
await doSomethingThatRequiresPermissions();
}catch(error){
console.log("Error: " error " at doSomethingThatRequiresPermissions");
}
NodeJS error handling documentation
Discord.js specific permission handling
Edit: I don't have access to my server at the moment (as I'm at work) but I will test this later to find a more comprehensive answer.