#unable to fetch members
35 messages · Page 1 of 1 (latest)
"discord.js": "^14.9.0",
node: v18.16.0
Not all members are cached. If it's a voice based channel, then add GuildVoiceStates inetent. Else fetch all members of the guild.
replaced the code with
const guild = client.guilds.cache.first();
const members = guild.members.fetch();
console.log(`Found members: ${members.map(member => member.displayName).join(", ")}`);
still no output in the terminal
its giving undefined
Fetch returns a promise
Wdym
Adding Await
import dotenv from "dotenv";
import { Client, GatewayIntentBits } from "discord.js";
dotenv.config();
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.MessageContent
]
});
const channelID = '{channelID-here}'; //general
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}`);
setInterval(runFunction, 1);
});
async function runFunction() {
const channel = client.channels.fetch(channelID);
const guild = client.guilds.cache.first();
const members = await guild.members.fetch();
console.log(`Found ${members.size} members in ${guild.name}:`);
client.login(process.env.BOT_TOKEN);
console.log('bot is online');
does it have to do with something related to the client?
No GuildMembers intent
hrow new DiscordjsError(unrecoverableErrorCodeMap[error.code]);
^
Error [DisallowedIntents]: Privileged intent provided is not enabled or whitelisted.
Go whitelist it on the dev portal
import dotenv from "dotenv";
import { Client, GatewayIntentBits } from "discord.js";
dotenv.config();
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMessages,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.MessageContent
]
});
const channelID = '1100170161694183427'; //general
const targetChannelID = '1101092357635579944'; //test
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}`);
setInterval(runFunction, 60);
});
async function runFunction() {
const channel = client.channels.fetch(channelID);
const guild = client.guilds.cache.first();
const members = await guild.members.fetch();
const validMembers = members.filter(member => !member.user.bot && member.lastMessageChannelID === channel.id).values();
if (validMembers.length > 0) {
const randomIndex = Math.floor(Math.random() * validMembers.length);
let randomMember = validMembers[randomIndex];
console.log(`${randomMember.displayName}`);
// await channel.send(`Congratulations to <@${randomMember}> for being selected randomly!`);
}
}
client.login(process.env.BOT_TOKEN);
console.log('bot is online');
i updated the code and to convert validMembers to an array i used values() and now i'm being shown this Error [GuildMembersTimeout]: Members didn't arrive in time.
did i do the array conversion right?
There’s a lot wrong with this code
SetInterval every 60?!?! milliseconds…??
Log typeof valid members to make sure it’s an array
It’s an asynchronous function but you just call it using runFunction which isn’t valid js
fetch for the channel returns a promise but you don’t resolve it, but you also don’t need to fetch channels with the Guilds intent
60 is just for testing purposes
on it
That’s another level api spam, don’t do it
You only need to fetch all members once (if you have GuildMembers intent, which you need anyway for that to work). After that the cache will be kept up-to-date by gateway events
validmembers is an object, rest i think i fixed
hey i'm sorry i'm new to js
const members = guild.members.cache; is that what you meant?
here's the updated code