Find out if someone has a role

前端 未结 6 1864
萌比男神i
萌比男神i 2020-12-06 11:47

I made a simple quote bot for a server, but the admin only wants mod+ people to be able to add quotes to avoid spam. I went to the documentation and did everything, but I ca

相关标签:
6条回答
  • 2020-12-06 12:08

    The discord.js api has been updated and there is a better way since .exists() has been deprecated.

    if (message.member.roles.some(role => role.name === 'Whatever')) {}

    This is better than .find() because .find() returns the role object (or undefined) which is then converted into a boolean. The .some() method returns a boolean by default.

    0 讨论(0)
  • 2020-12-06 12:09

    I'm probably wayy to late with this, but I just had the same problem, and the way i solved it is to search for the role, and then check if the variable has anything on it. Say i wanna check for if the author of the message has admin role:

    client.on('message',message =>{
    
         adminrole = message.member.roles.cache.find(role => role.name == "Admin");
         if(adminrole != null){memberIsAdmin = true;}else{memberIsAdmin = false;}
    
    }
    

    Maybe this will help someone, note that I'm not very experienced with js tho :) Have a nice day y'all

    0 讨论(0)
  • 2020-12-06 12:21

    The Map.has method checks for keys, not values, so you'll have to use the roles' ids instead.

    message.member.roles.has(adminRole.id)

    message.member.roles.has(modRole.id)

    0 讨论(0)
  • 2020-12-06 12:22

    This worked for me with version 12.2.0

    if(message.member.roles.cache.find(r => r.name === "Admin")) {
        // Your code
    }
    

    You can also use r.id to check with the role id

    0 讨论(0)
  • 2020-12-06 12:22

    if(message.guild.roles.find(role => role.name === "VIP"))

    0 讨论(0)
  • 2020-12-06 12:23

    message.member.roles is a collection. Instead of getting the roles object, then looking for it, just look for the role directly in the collection. Try this:

    else if (command === "addquote" && arg) {
        if(message.member.roles.find(r => r.name === "Admin") || message.member.roles.find(r => rname === "Mod")){
            //Rest of your code
        }
    

    Note, the role name must be the name you put in the find including any emojis if there's any in the role name.

    0 讨论(0)
提交回复
热议问题