Get the list of all user on a server discord.js

I'm trying to get all users from my server with a bot using discord.js, I wrote this code but it's not working, it's telling me : TypeError: client.guilds.get is not a function. Here is my code :

'use strict'; const Discord = require('discord.js'); const client = new Discord.Client(); const list = client.guilds.get("myServerID"); list.members.forEach(member => console.log(member.user.username)); client.login('myTokenID'); 

Thanks

3

2 Answers

Since discord.js v12 you now need to access the guilds collection using .cache and I can also see that you're trying to access the members collection so your solution would be:

'use strict'; const Discord = require('discord.js'); const client = new Discord.Client(); const list = client.guilds.cache.get("myServerID"); list.members.cache.forEach(member => console.log(member.user.username)); client.login('myTokenID'); 
2

just use .fetch()

const guild = await client.guilds.fetch('your_id') const members = await guild.members.fetch() // returns Collection 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like