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

后端 未结 3 497
挽巷
挽巷 2021-01-06 22:44

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\'v

相关标签:
3条回答
  • 2021-01-06 23:13

    These answers seem outdated:

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

    0 讨论(0)
  • 2021-01-06 23:23

    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 讨论(0)
  • 2021-01-06 23:25

    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;
    
    0 讨论(0)
提交回复
热议问题