avisk
avisk

Reputation: 390

Discord.js any way to get all roles

How would you get all the roles in a server using Discord.js? I've message.guild.roles but from there it's a mystery to me.

Upvotes: 0

Views: 21214

Answers (2)

JackRed
JackRed

Reputation: 1204

As stated by the other answer and your question message.guild indeed has a property roles:

.roles
A collection of roles that are in this guild. The key is the role's ID, the value is the role
Type: Collection <Snowflake, Role>

A collection extends the JavaScript type map and add some function for you to use.

You indeed have all the roles of the guild (server) with message.guild.roles.
Now if you want to do anything with it, for example list them, check the function in Collection / the type map.

message.guild.roles.forEach(role => console.log(role.name, role.id))

will print each role name + id in the console.

Upvotes: 1

RealPenguin
RealPenguin

Reputation: 89

 let rolemap = message.guild.roles.cache
            .sort((a, b) => b.position - a.position)
            .map(r => r)
            .join(",");
            if (rolemap.length > 1024) rolemap = "To many roles to display";
            if (!rolemap) rolemap = "No roles";
    const embed = Discord.MessageEmbed()
    .addField("Role List" , rolemap)
    message.channel.send(embed);

Here's how to get all roles in a guild in discord.js v12

Upvotes: 3

Related Questions