How to Get a List of Members in a Guild Discord.js

JD M

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
Daemon Beast

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
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to get the ID of a guild on join in Discord.js

How do I list all Members with a Role In Discord.Js

how to get all members of a guild in discord.py?

How do I get a list of all of the members in a discord server using discord.py

How do I get a list of all members in a discord server using the new discord.py version?

How to get the user with guild.members.get()?

Discord.JS: How to get first channel of a guild?

How to get username of user not in Discord guild?

How to only get UserID from message.guild.members.fetch() in discord.js v12?

How do i get the guild ID of a joined user/member (Discord.js)

how to get all members id in discord.js

How to get ID from guild.fetch in discord.js v12?

How to get members of a discord guild(discord.py)?

Discord rest API: Kick members from guild

Discord PY how to get list of all server members?

Trying to show all members of guild by bot, Discord JS

Getting a list of all guild members and banning them in discord.js using node.js

Getting a list of all guild members

Discord.js - counter of sent messages, DM to all members of guild

Get guild members and filter them (Discord.js)

Discord Bot - Guild Members Showing in count... but not in guild.members

How to autoassign roles to new members joining Discord guild?

How to get member count of all members in every guild the bot is in

Discord JS Client only has itself in guild members cache

Discord.js: How to ban user that is not in the guild?

How to get owner of a guild in discord.js v13?

how to get the guild that have less than 10 members using discord.js

how to get the guild name that have less than 10 members using discord.js in v13

Discord.js Get the member in a guild by username