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

by herminia_bruen , in category: Javascript , 2 months ago

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

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by orpha , 2 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.