Discordjs检查用户是否有角色

时间:2017-12-29 22:29:41

标签: node.js discord discord.js

我有这个代码工作正常,我只需要检查用户是否有角色,它记录所有用户,他们的id,鉴别器和用户名exc。我无法获得这些角色。你能帮忙吗?

client.on('ready', () => {
  console.log(`logged in as ${client.user.username}`);

    var Count;
    for(Count in client.users.array()){
       var User = client.users.array()[Count];
       if(User.username == "someUsername"){
         //User.sendMessage("you");
       }
       //User.checkRole("Admin");

    }
}

checkRole()函数是我编写的。我需要一些帮助。

client.on('ready', () => {
    console.log(`logged in as ${client.user.username}`);

    var Count;
    for(Count in client.users.array()){
       var User = client.users.array()[Count];
       if(User.hasRole("Admin")){
          console.log(User.username);
       }
    }
})

2 个答案:

答案 0 :(得分:3)

要访问角色,您必须通过公会,不是用户来接近它。 (正如@Wright所说)

我会使用地图来保存所有公会成员,然后通过每个成员(当你遇到一个具有特定角色名称的那个时,你仍然可以访问guildobject(因为它是guildmember object)

m = client.guilds.map(function (obj) {
    return obj.members;
});
for (var i = 0; i < m.length; i++) {
    console.log("\n\nNew Guild: ");        
    console.log(m[i].map(function (obj) {
        return obj.guild.name + " , " + obj.user.username + "  :  " + obj._roles;
    }).join('\n'));
}

我得到的输出是(我审查了用户名): Output

答案 1 :(得分:0)

您可以执行以下操作:

// assuming role.id is an actual ID of a valid role:
if(message.member.roles.has(role.id)) {
  // user has that role
} else {
  //user doesn't have that role
}

https://anidiotsguide.gitbooks.io/discord-js-bot-guide/information/understanding-roles.html