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!
CodePudding user response:
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)