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

女生的网名这么多〃 提交于 2020-01-16 04:25:27

问题


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.)


回答1:


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.get()
    2. Property: Collection.find()

So, tying this all together:

if (message.member.roles.get('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 it will throw errors.

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

if (!message.guild) return;



回答2:


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.



来源:https://stackoverflow.com/questions/56926998/how-can-i-check-if-the-message-author-has-an-admin-role-using-discord-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!