Online member fetch
hi im trying to make a command where its ".members" and it displays the online members, members in the server, and the server boosts. i got the server boosts, and server members working but cant seem to fight off this error that is persistent with the online members, does anyone know the right code on how to fix this error?


14 Replies
- What's your exact discord.js
npm list discord.js
and node node -v
version?
- Not a discord.js issue? Check out #other-js-ts.
- Consider reading #how-to-get-help to improve your question!
- Explain what exactly your issue is.
- Post the full error stack trace, not just the top part!
- Show your code!
- Issue solved? Press the button!import { Client, GatewayIntentBits, EmbedBuilder } from 'discord.js';
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers,
GatewayIntentBits.GuildPresences,
],
});
export const membersCommand = {
name: 'members',
description: 'Displays server member statistics.',
cooldown: 10,
async execute(message) {
try {
const requiredRoleId = '1317994382451806319';
if (!message.member.roles.cache.has(requiredRoleId)) {
const errorMessage = await message.reply('You do not have permission to use this command.');
setTimeout(() => errorMessage.delete().catch(() => {}), 7000);
return;
}
await message.delete().catch(() => {});
const loadingMessage = await message.channel.send(':clock_w: Loading member statistics...');
const guild = message.guild;
console.log('Fetching all members...');
const fetchedMembers = await guild.members.fetch();
console.log(
let onlineMembers = (await guild.members.fetch()).filter((member) => !member.user.bot && member.user.presence.status == 'online'); console.log(
Fetched ${fetchedMembers.size} members.
);
let onlineMembers = (await guild.members.fetch()).filter((member) => !member.user.bot && member.user.presence.status == 'online'); console.log(
Online members: ${onlineMembers}
);
const totalMembers = guild.memberCount;
const serverBoosts = guild.premiumSubscriptionCount || 0;
let boostLevel = 'Level 0';
if (serverBoosts >= 14) boostLevel = 'Level 3';
else if (serverBoosts >= 7) boostLevel = 'Level 2';
else if (serverBoosts >= 2) boostLevel = 'Level 1';
const embed = new EmbedBuilder()
.setAuthor({
name: 'Lake Stevens Roleplay',
iconURL: 'https://cdn.discordapp.com/icons/1317933545267662929/e507c5b9d75d855f42e885bbfc2eb1e5.png',
})
.setColor('#0893ff')
.addFields(
{ name: ':Member: Members:', value: ${totalMembers}
, inline: true },
{ name: ':Online: Online Members:', value: ${onlineMembers}
, inline: true },
{ name: ':booter: Server Boosts:', value: ${serverBoosts} (${boostLevel})
, inline: true }
)
.setImage(
'https://media.discordapp.net/attachments/1327900739661467739/1332764232785068083/image.png'
);
await loadingMessage.edit({ content: null, embeds: [embed] });
} catch (error) {
console.error('An error occurred while executing the members command:', error);
}
},
};
this is my current code ^^Don't fetch members, just filter presences
You need
GuildPresences
intent btw
So what is the code and do you have
GuildPresences
intent enabled?yes i do
You didn't use this
hang on
ok ive removed the intents
what do i need to import from there
ok done
ok it fetched the online members but only got about half

i have around 100 members online
Others probably have dnd, idle status
ok
alright the command works now
thank you guys for your help