How to check for Discord.js Bot for permissions in a channel?

Viewed 1773

In the program, a channel is selected (by the user via settings) for the discord bot to monitor. I need to make sure the bot has permissions to monitor that channel prior to the channel being set.

The following code doesn't work:

// Make sure bot has read permissions by checking if it's missing any
if (message.guild.me.permissionsIn(channel).missing(['SEND_MESSAGES', 'READ_MESSAGE_HISTORY'])) {
    return message.channel.send("I don't have permission in that channel.");
}
// If it isn't missing any then do stuff
console.log("do stuff");

This also doesn't work:

if (!message.guild.me.permissionsIn(channel).has(['SEND_MESSAGES', 'READ_MESSAGE_HISTORY'])) {
    return message.channel.send("I don't have permission in that channel.");
}

console.log("do stuff");

The output is ALWAYS "do stuff" (the if statement returns a channelID), rather than returning. How can I do this?

2 Answers

I think in discord.js instead of using "permissionsIn" you can just use "permissions".

if(!message.member.permissions.has("SEND_MESSAGES", "READ_MESSAGE_HISTORY")) {
    return message.channel.send("I have permissions");
} else {
    message.channel.send("I do not have permissions");
}

If you are using v12.5 Discord.js this should work:

if (!message.channel.permissionsFor(client.user.id).has(['SEND_MESSAGES', 'READ_MESSAGE_HISTORY', 'EMBED_LINKS', 'VIEW_CHANNEL'])) {
  message.author.send("Heck, It looks like I may lack perms. Please make sure that I have all of the following permissions: `SEND_MESSAGES`, `READ_MESSAGE_HISTORY`, `EMBED_LINKS` and `VIEW_CHANNEL`.")
  return
};

Related