Home > Mobile >  How to fetch a members roles without message? Discord.js
How to fetch a members roles without message? Discord.js

Time:06-07

is there a way to check if a user has a role / fetch a users roles without him / anyone sending a message? Thanks.

CodePudding user response:

You can use client.guilds.cache for guilds, guild.members.fetch() for members and member.roles.cache for roles. For example(with ready event and if you want to check every 'x' seconds change setInterval's time(in milliseconds)):

setInterval(() => {
    client.guilds.cache.forEach(async(guild) => {
        if (guild.name.toLowerCase() === 'guildname') {
            (await guild.members.fetch()).forEach((member) => {
                if (member.user.username.toLowerCase() === 'membername') {
                    member.roles.cache.forEach((role) => {
                        if (role.name.toLowerCase() === 'rolename') {
                            // do something
                            console.log(member.user.username);
                        }
                    });
                }
            });
        }
    });
}, 5000);

Use what you want for if. guild.name: guild name, guild.id: guild id, member.user.username: username, member.user.id: user id, role.name: role name, role.id: role id.

CodePudding user response:

To find a role it is done by:

let role = message.guild.roles.cache.get(`role_Id`)

But it can also be done by name:

let role = message.guild.roles.cache.get(r => r.name === `role_Name`)

Then you have to check if the user has that role you can use:

message.member.roles.cache.has(role)
  • Related