Sebastián Camargo
Sebastián Camargo

Reputation: 302

Get permissions from bot user in Discord.js v14?

I want to check my bot's permissions before it executes a command. I had it working perfectly before:

// Discord.js v13
if (interaction.guild.me.permissions.has(Permissions.FLAGS.MANAGE_MESSAGES)) {
    interaction.reply("I can manage messages!");
}

However Guild.me is no longer available in Discord.js v14 and the Official Guide suggests instead to use GuildMemberManager.me

I tried to use the new object:

const { GuildMemberManager, PermissionsBitField } = require('discord.js');

// Attempt #1

if (GuildMemberManager.me.permissions.has(PermissionsBitField.Flags.ManageMessages)) {
    interaction.reply("I can manage messages!");
}

// Attempt #2

if (interaction.guild.GuildMemberManager.me.permissions.has(PermissionsBitField.Flags.ManageMessages)) {
    interaction.reply("I can manage messages!");
}

// Attempt #3

if (GuildMemberManager.me.permissionsIn(channel).has(PermissionsBitField.Flags.ManageMessages)) {
    interaction.reply("I can manage messages!");
}

However, all these attempts return the same error:

TypeError: Cannot read properties of undefined (reading 'me');

// Attempt #3
TypeError: Cannot read properties of undefined (reading 'permissionsIn');

I do not understand how the new GuildMemberManager.me object works. Any further explanation or solution to my problem will be greatly appreciated!

Upvotes: 2

Views: 6617

Answers (2)

isaac.g
isaac.g

Reputation: 726

In the discord.js docs, the class of the object is GuildMemberManager, but the object is actually just referenced with the keyword members. Here's a link to the object in the docs. This worked for me in v14:

interaction.guild.members.me.permissions.has(PermissionsBitField.Flags.ManageMessages)

Additionally, make sure you are using the latest version of discord.js. This property is not defined in earlier versions such as v13.

Upvotes: 6

TheCoder
TheCoder

Reputation: 1

use this for discord.js v14:

if (!interaction.member.permissions.has(PermissionsBitField.Flags.Administrator)) return await interaction.reply({ content: "⚠️ You must me a moderator to use this command!", ephemeral: true});

Upvotes: 0

Related Questions