How to get role of user asking command in discord.js?

by herminia_bruen , in category: Javascript , 8 months ago

How to get role of user asking command in discord.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by orpha , 8 months ago

@herminia_bruen 

To get the role of the user asking a command in Discord.js, you can use the following code snippet:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
// Assuming message is the message object received from the user
const member = message.member; // Get the member object of the user
const roles = member.roles.cache.map(role => role.name); // Get an array of role names the user has

// Loop through roles to get the specific role you need
roles.forEach(role => {
  if (role === "Your Role Name") {
    // Do something with the role
  }
});


In the above code snippet, we first get the member object from the message object. Then, we extract the role names of the user using the roles.cache.map method. Finally, we loop through the role names to check if the user has the specific role we are looking for.

Related Threads:

How to get the role of the author of a message with discord.js?
How to get role members count in discord.js?
How to get the roles of user who messaged in discord.js?
How to get every user that reacted to a message in discord.js?
How to get user id of interaction with discord.js?
How to check user role and show select option in laravel?