Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

207
Vistas
Discord js - Fetch Guild role list (array)

Trying to get roles from a discord server with discord.js

I started playing around with Discord bots using the discord.js library.

One of my objectives was to get the Guild roles in an array. I figured out a way to get it, but it is returning an array inside another array:

    const roles = client.guilds.cache.map(guild => {
        return guild.roles.cache.map(role=>role.name);
    });

    console.log(roles);

This is returning:

[ [ '@everyone', 'Demi God', 'Bot', 'God', 'Test' ] ]

I figured that there should be a better way? I figured I could also deconstruct the array but wanted to know if there was something I missed first.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

There are a couple of ways to get a single array.

Using flat() to flatten the array:

const roles = client.guilds.cache
  .map((guild) => guild.roles.cache.map((role) => role.name))
  .flat();

Using flatMap() that will return a single collection of roles, so you'll still need to use map() to return an array of role names:

const roles = client.guilds.cache
  .flatMap((guild) => guild.roles.cache)
  .map((role) => role.name);

Using reduce():

const roles = client.guilds.cache.reduce(
  (acc, guild) => [...acc, ...guild.roles.cache.map((role) => role.name)],
  [],
);

Using push():

const roles = [];
client.guilds.cache.forEach((guild) => {
  roles.push(...guild.roles.cache.map((role) => role.name));
});

If you only want to use the role names, you can use any of these. If you want to use roles, not just their names, maybe flatMap() is the best option as it returns a single collection, similar to guild.roles.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda