Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
181
rated 0 times [  185] [ 4]  / answers: 1 / hits: 18054  / 4 Years ago, tue, april 7, 2020, 12:00:00

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?


More From » bots

 Answers
13

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


[#51066] Monday, March 30, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jocelynkarsynr

Total Points: 472
Total Questions: 98
Total Answers: 96

Location: Macau
Member since Mon, Nov 16, 2020
4 Years ago
jocelynkarsynr questions
Tue, Feb 8, 22, 00:00, 2 Years ago
Sat, Jul 11, 20, 00:00, 4 Years ago
Sun, May 10, 20, 00:00, 4 Years ago
Sat, Jan 18, 20, 00:00, 4 Years ago
;