Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

305
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!