Reputation: 93
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!🙂
Upvotes: 1
Views: 4750
Reputation: 93
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"]
});
Upvotes: 4