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

306
Visualizações
Discord.js update cache to pull all members that have role

Currently having issues pulling all members that have a certain role. I can currently pull a list of names but only those that are cached currently. If i leave my bot running for a few hrs it will pull more but it never pulls all members that have that role. Ive read a few different things about using the .fetch() command but still no dice.

const client = new Client({
    intents: [
        Intents.FLAGS.GUILDS,
        Intents.FLAGS.GUILD_MESSAGES,
        Intents.FLAGS.GUILD_VOICE_STATES,
        Intents.FLAGS.GUILD_MEMBERS
    ]
});


msg.channel.send('exporting all members with delcared role');

msg.guild.roles.fetch('guildId');//cache roles i think?
let role = msg.guild.roles.cache.find(r => r.name === 'Sysadmins');


msg.guild.members.fetch('roleId');//cache members i think?
let list = msg.guild.roles.cache.get(roleID).members.map(m => m.nickname)

console.log(role);
console.log(list);

The console.log(); returns my nickname as expected but only mine and i know there are at least 10 others with this role.

Other questions similar that I have already tried:

Similar Question #1

Similar Question #2

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

@DanLop your answer along with @MrMythical and a few other stack overflow questions helped me get this one sovled. I was able to use the async functions properly to pull all the data i need below is what i ended up with.

Solution:

client.on('messageCreate', async msg => {  //TODO here });

let list = client.guilds.cache.get(tempGuildId);

try {
    await list.members.fetch();

    let role1 = list.roles.cache.get(roleId).members.map(m => m.displayName);
    console.log(role1);
} catch (err) {
    console.error(err);
}
about 4 years ago · Juan Pablo Isaza Relatório

0

It's close, but much simpler, with 3 lines

await msg.guild.members.fetch() // fetch all members and cache them
const role = msg.guild.roles.cache.get("id") // get role from cache by ID (roles are always cached)
const list = role.members.map(m => m.nickname) // map members by nickname
about 4 years ago · Juan Pablo Isaza Relatório

0

Since you're giving the fetch() method a String argument, only the user matched with that String will be fetched, more like a get(). Also, the fetch() method returns a Promise, which means the method must be awaited to actually recieve the data.

Your fetch() method should look like this:

await msg.guild.members.fetch();

This way you tell Node.js it must wait until you recieve the data promised before continue.

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