Discordjs check if a user has a role

turmuka

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);
       }
    }
})
Monacraft

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): Output

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

DiscordJS Check if command user has a role

Check if User has a certain role

DiscordJS: add role to array of user

How to check if a user has a specific role in Meteor

How to check is an user has a role on discord

How to check whether user has any role?

Check if User has Role - Parse Cloud Code

How to check if user has different role in mvc

Check if user has role Discord.net

Check if a user has a role in a completely different server

How to check if a member chosen from the getUser option has a specific role in discordjs

Symfony2 - check if user has role at authentication

How to check if a specific user has a role? Discord js

Discord.js how to check if user has a role on a specific server

How can you check if a user has a particular role in a particular server

How can I check if the user has a role for one to one relationship?

To check if a mentioned user has the role or not in discord.js

How to check if a user has a specific role in on_message?

How to check if a user has a role from another Discord server

discord.py check if a user has a particular role

How do I check if a user has a role in discord.py?

How to check if bot has higher role than mentioned user

Jhipster - check Role of a User

Check the role of a user in a database

How to remove role from user in guild discordjs V12?

How can I add a role to a user on DiscordJS v13?

In Angular -> how to check if the user has permission using role-based access with the role save in the data base

Discord.js-commando how would I check if a mentioned user has a role, like a muted role for example?

Adding role discordjs 14