1

I have been trying to fetch the list of all members in my guild using the syntax as mentioned in the docs here. But am not receiving any output. Below is the code that I am using.

const { Client, Intents, Guild } = require("discord.js");
require("dotenv").config();
const commandHandler = require("./commands");
const fetch = require("node-fetch");

const client = new Client({
    intents: ["GUILDS", "GUILD_MESSAGES", "GUILD_PRESENCES"],
});

client.once("ready", async (data) => {
    console.log("Ready!");
    console.log(`Logged in as ${client.user.tag}!`);
    const Guilds = client.guilds.cache.map((guild) => guild);
    console.log(Guilds[0]);
    await Guilds[0].members.fetch().then(console.log).catch(console.error);
});

client.on("interactionCreate", async (interaction) => {
    if (!interaction.isCommand()) return;

    if (interaction.commandName === "ping") {
        await interaction.reply(client.user + "💖");
    }
});

client.on("messageCreate", commandHandler);
client.login(process.env.BOT_TOKEN); // Kushagra's bot

I am getting the information on Guild instance through the console.log on line 13. But do am not getting any output for line 15. However, I noticed that I was getting some output if I add options to fetch() command on line 14. I received some output when I added {query: 'random text'}.

Just wanted to understand why I am unable to get a list of all members. Thanks!🙂

1 Answer 1

4

I found that I had missed out on the Intent. It seems that in order to fetch members you need to add an intent GUILD_MEMBERS while creating a client instance.

So replacing the client creation with the one below will work.

const client = new Client({ 
    intents: ["GUILDS", "GUILD_MESSAGES", "GUILD_PRESENCES", "GUILD_MEMBERS"] 
});
1
  • Thanks so much. I was searching for a fix for hours until I realized I didn't add the intents.
    – Vince
    Commented Sep 25, 2021 at 4:55

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