Discord bot not getting all users

I have written a discord bot using Discord.js and it used to get all the users of the discord server (I have the bot in one discord server only), but now it only gets 59 members. I have 300+ discord users in the server.

var Discord = require('discord.js');
var bot = new Discord.Client();

function getUsers() {
  let users = bot.users.array();

  for (let i = 0; i < users.length; i++) {
    let username = `${users[i].username}#${users[i].discriminator}`;
    console.log(`[${i}] ${username}`);
  }
}

Answer

You have to use the method .fetchMembers() for every discord server the bot is in.

Try to use the following code:

function getUsers() {
  let guilds = bot.guilds.array();

  for (let i = 0; i < guilds.length; i++) {
    bot.guilds.get(guilds[i].id).fetchMembers().then(r => {
      r.members.array().forEach(r => {
        let username = `${r.user.username}#${r.user.discriminator}`;
        console.log(`${username}`);
      });
    });
  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *