How to check if a specific user has a role? Discord js

泪湿孤枕 提交于 2020-12-26 08:39:09

问题


I know how to check if the message sender has a role:

if(message.member.roles.has(role.id)) {
  console.log(`Yay, the author of the message has the role!`);
} else {
  console.log(`Nope, noppers, nadda.`);
}

However, how can I check if specific user (using user id) has a specific role?

    var authorID = "111111111111111111"

    //Find role
    var role = message.guild.roles.find(role => role.name === "Private Splash Ping");
    //Find member
    let member = message.guild.members.get(authorID);

    console.log(member.roles.has(role))
    if(member.roles.has(role)) {
        roleadded = "User already has Private spalsh role."
    } else {
        message.member.addRole(role);
        roleadded = "Added private splash role."
    }

member.roles.has(role) always returns false and I've confirmed both member and role variables are correct via breakpoints.

Is there something I'm missing here?


回答1:


message.member.roles is considered a GuildMemberRoleManager (Documentation), to access the collection of roles you must access the property .cache which is a collection. This collection then has the .has(...) method function. (Documentation)

Thus, you instead want to access message.member.roles.cache.has(...)




回答2:


There are many more Changes in v12.0.x (current v12.1.1). A major one is message.channel.send command and cache.

One of them is this.

I am sorry that I am late.



来源:https://stackoverflow.com/questions/61088275/how-to-check-if-a-specific-user-has-a-role-discord-js

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