admin管理员组文章数量:1293049
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?
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?
Share Improve this question asked Apr 7, 2020 at 19:49 EdwinEdwin 731 gold badge1 silver badge7 bronze badges 2-
Not familiar with discord.js, but if
roles
is an array, could you usemember.roles.indexOf(role)
instead of the.has
method? – R Greenstreet Commented Apr 7, 2020 at 19:52 - Are you receiving any errors? What version of discord.js are you using? – Syntle Commented Apr 7, 2020 at 19:56
1 Answer
Reset to default 6message.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(...)
本文标签: javascriptHow to check if a specific user has a role Discord jsStack Overflow
版权声明:本文标题:javascript - How to check if a specific user has a role? Discord js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741570488a2385982.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论