最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I check if the message author has an admin role using Discord.js? - Stack Overflow

programmeradmin2浏览0评论

I'm building a Discord bot and I want to have an if statement that will only proceed if the message author has an administrator role in the guild.

I've tried having role-specific permissions, but this means that there will have to be the exact same name role on all servers that the bot is on.

How can I check if the message author has an admin role? (The role has the administrator permission.)

I'm building a Discord bot and I want to have an if statement that will only proceed if the message author has an administrator role in the guild.

I've tried having role-specific permissions, but this means that there will have to be the exact same name role on all servers that the bot is on.

How can I check if the message author has an admin role? (The role has the administrator permission.)

Share Improve this question edited Jul 8, 2019 at 4:01 slothiful 5,6233 gold badges13 silver badges36 bronze badges asked Jul 8, 2019 at 0:01 CharlieRG20CharlieRG20 351 gold badge3 silver badges7 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 12

Some of the following code must be modified to use in the newest major Discord.js version (v12 at the time of this edit) due to the implementation of Managers.

There's really three different questions needed to be addressed here. They're all related, but each have different direct answers.


How do I check if the message author has an Admin role?

  • The GuildMember that sent a message is accessed via the Message#member property, as opposed to Message#author which returns a User. Remember, a member has roles and permissions, not a user.

  • A Collection of a member's roles can be retrieved with GuildMember#roles.

  • You can search for a role two main ways:

    1. ID: Map#has()
    2. Property: Collection#find()

So, tying this all together:

if (message.member.roles.has'roleIDHere')) console.log('User is an admin.');

or

if (message.member.roles.find(role => role.name === 'Admin')) console.log('User is an admin.');

How do I check if the message author's role has the Administrator permission?

  • Again, we need to use the GuildMember from Message#member.
  • And again, we need to use the GuildMember#roles collection.
  • And... déjà vu... you can search through a Collection with Collection#find().
  • This time, you should specifically check Role#hasPermission() in the predicate function.

For example:

if (message.member.roles.find(role => role.hasPermission('Administrator'))) console.log('User is an admin.');

You can apply this concept to any specific role, too.


Best method for this situation...

How do I check if the message author has the Administrator permission?

  • We continue to use Message#member to access the GuildMember.
  • However, you can check all of a member's permissions at once via the GuildMember#hasPermission() method.

Consider this short example:

if (message.member.hasPermission('ADMINISTRATOR')) console.log('User is an admin.');

Quick, right?


Make sure you check that the message your client receives is not a DM before attempting to check if the user is an Admin. Message#member is undefined when the message isn't sent in a guild, and trying to use properties of it will throw an error.

Use this condition, which will stop if the message is a DM:

if (!message.guild) return;

These answers seem outdated:

Use: msg.member.roles.cache.has(roleID)

On the GuildMember object, you have a hasPermission function available. So you can just do member.hasPermission('ADMINISTRATOR')

If you are interested in all the other strings, that are permission resolvable, you can find them in the discord.js docs.

发布评论

评论列表(0)

  1. 暂无评论