Reputation: 2826
I have this code which works fine, I just need to check if a user has a role, it logs all the users, with their id, discriminator and username exc. I just can't get the roles. Can you guys please help?
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()
function is something that I made up. I jsut need some help.
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);
}
}
})
Upvotes: 1
Views: 8305
Reputation: 6620
To access roles you must approach it by guild, not user. (As mentioned by @Wright)
I'd do this using a map to save all the guild members, and then going through each member (that way when you come across one with a specific role name, you still have acess to the guildobject (as it is a 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'));
}
The output I get with this is (I censored out username's):
Upvotes: 4
Reputation: 1910
You could do something like:
// 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
Upvotes: 0