3

I am trying to get all the members in a guild using Discord.js v12. This is what I have:

    const list = client.guilds.cache.get("720352141709148200");
    list.members.forEach(member => {
       //do stuff with guild memebrs here
    }

I have looked at this stack overflow question but I believe it's out of date as it throws an error saying that guilds is undefined. This is the error I get with the modified-ish code I have above:

TypeError: list.members.forEach is not a function

2 Answers 2

7

As pointed out by Jakye, you need to change list.members to list.members.cache.

However, you cannot use .forEach(), as that's an array method and list.members.cache returns a Discord collection (Discord.Collection()).

Instead of .forEach(), you can use .each():

list.members.cache.each(member => {
  // do stuff with guild members here
});

Alternatively, you could convert the collection into an array of values using .array() and then using .forEach() on that:

list.members.cache.array().forEach(member => {
  // do stuff with guild members here
});
1

Since you are using Discord JS v12, instead of list.members.forEach() you need to use list.members.cache.forEach().

Not the answer you're looking for? Browse other questions tagged or ask your own question.