Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

205
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda