2

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!

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Sorry to downvote, but this isn't working for me. guild.members.me is undefined
That is not a problem with my post, guild.members.me is clearly documented on discord.js. Please check how you try to access the variable, or if you somehow have such an outdated discord.js version that the option isn't there. In addition, please read when should I vote down on the SO help center. The answer obviously worked for other users (hence the marked as answer), so the downvoting feature should not be used just because it didn't work for you.
Seems NPM didn't update discord.js despite me asking nicely. Checked the version and I'm still on v13.20.0—whoops.
Glad that you got it working. Just a suggestion for you to remove your downvote, so that users with this problem don't reach this page and immediately leave, thinking that the answer was wrong because its score in the negatives. I edited your solution into the post as well.
Done! Happy coding everyone!
0

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}); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.