如何检查特定用户是否具有角色?Discord js [英] How to check if a specific user has a role? Discord js

查看:93
本文介绍了如何检查特定用户是否具有角色?Discord js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何检查消息发件人是否具有角色:

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.`);
}

但是,如何检查特定用户(使用用户ID)是否具有特定角色?

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)始终返回false,并且我已经通过断点确认member和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?

推荐答案

message.member.roles 被认为是GuildMemberRoleManager(文档)

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)

因此,您改为访问 message.member.roles.cache.has(...)

这篇关于如何检查特定用户是否具有角色?Discord js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆