How to fetch all online users in a guild, including idle and dnd

See title Code: guild.members.cache.filter(member => member.presence?.status === 'online' && !member.user.bot).size; DiscordJs Ver: 14.7.1 Rest Ver: 10 Node Ver: 18.14.1
9 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
ShompiFlen
ShompiFlen2y ago
fetch them from the guild first const members = await guild.members.fetch() then filter that list
V3RM1N
V3RM1NOP2y ago
Problem is my function is not async
setInterval(() => {
client.guilds.cache.forEach(guild =>{
if (fs.existsSync('./config/' + guild.id + '.json') === false) return;
//Get all online users from guild
const members = await guild.members.fetch();
console.log(members)
let onlineUsers = members.filter(member => member.presence?.status === ('online' || 'dnd' || 'idle') && !member.user.bot).size;
let config = JSON.parse(fs.readFileSync('./config/' + guild.id + '.json', 'utf8'));
if (!config.onlineChannel) return;
guild.channels.edit(config.onlineChannel, {name: `Online: ${onlineUsers}`});
if (!config.allChannel) return;
guild.channels.edit(config.allChannel, {name: `Members: ${guild.memberCount}`});
if (!config.botChannel) return;
guild.channels.edit(config.botChannel, {name: `Bots: ${guild.members.cache.filter(member => member.user.bot).size}`});
})
}, 100 * 15);
setInterval(() => {
client.guilds.cache.forEach(guild =>{
if (fs.existsSync('./config/' + guild.id + '.json') === false) return;
//Get all online users from guild
const members = await guild.members.fetch();
console.log(members)
let onlineUsers = members.filter(member => member.presence?.status === ('online' || 'dnd' || 'idle') && !member.user.bot).size;
let config = JSON.parse(fs.readFileSync('./config/' + guild.id + '.json', 'utf8'));
if (!config.onlineChannel) return;
guild.channels.edit(config.onlineChannel, {name: `Online: ${onlineUsers}`});
if (!config.allChannel) return;
guild.channels.edit(config.allChannel, {name: `Members: ${guild.memberCount}`});
if (!config.botChannel) return;
guild.channels.edit(config.botChannel, {name: `Bots: ${guild.members.cache.filter(member => member.user.bot).size}`});
})
}, 100 * 15);
ShompiFlen
ShompiFlen2y ago
then make it async by the way why would you need to fetch all members every 1.5 seconds
V3RM1N
V3RM1NOP2y ago
just for the test
ShompiFlen
ShompiFlen2y ago
spamming the api "just for testing" is not justifiable tho
V3RM1N
V3RM1NOP2y ago
good point Well doesnt work idk why Had to change the loop to a for loop and made the execution async (commeted out the interval)
for (const guild of client.guilds.cache) {
if (fs.existsSync('./config/' + guild.id + '.json') === false) continue;
//Get all online users from guild
const members = await guild.members.fetch();
console.log(members)
let onlineUsers = members.filter(member => member.presence?.status === ('online' || 'dnd' || 'idle') && !member.user.bot).size;
let config = JSON.parse(fs.readFileSync('./config/' + guild.id + '.json', 'utf8'));
if (!config.onlineChannel) continue;
guild.channels.edit(config.onlineChannel, {name: `Online: ${onlineUsers}`});
if (!config.allChannel) continue;
guild.channels.edit(config.allChannel, {name: `Members: ${guild.memberCount}`});
if (!config.botChannel) continue;
guild.channels.edit(config.botChannel, {name: `Bots: ${guild.members.cache.filter(member => member.user.bot).size}`});
}
for (const guild of client.guilds.cache) {
if (fs.existsSync('./config/' + guild.id + '.json') === false) continue;
//Get all online users from guild
const members = await guild.members.fetch();
console.log(members)
let onlineUsers = members.filter(member => member.presence?.status === ('online' || 'dnd' || 'idle') && !member.user.bot).size;
let config = JSON.parse(fs.readFileSync('./config/' + guild.id + '.json', 'utf8'));
if (!config.onlineChannel) continue;
guild.channels.edit(config.onlineChannel, {name: `Online: ${onlineUsers}`});
if (!config.allChannel) continue;
guild.channels.edit(config.allChannel, {name: `Members: ${guild.memberCount}`});
if (!config.botChannel) continue;
guild.channels.edit(config.botChannel, {name: `Bots: ${guild.members.cache.filter(member => member.user.bot).size}`});
}
but the looks like the isnt coming to action cuz the console.log(members) isnt called
ShompiFlen
ShompiFlen2y ago
if you have a lot of guilds this is even worse than using an interval. Do you have the Guilds and the GuildMembers intent enabled in your bot? the Guild Members intent has to also be enabled in your developers portal
V3RM1N
V3RM1NOP2y ago
Is enables in developer portal and intents are right, and its just my testbot on my test server so only one, but i need the loop for my main bot with 10+ servers Found a solution doing it this way:
let onlineUsers = guild.members.cache.filter(member => (member.presence?.status === 'online' ||
member.presence?.status === 'idle' ||
member.presence?.status === 'dnd') &&
!member.user.bot)
.size;
let onlineUsers = guild.members.cache.filter(member => (member.presence?.status === 'online' ||
member.presence?.status === 'idle' ||
member.presence?.status === 'dnd') &&
!member.user.bot)
.size;
Thanks for your help
Want results from more Discord servers?
Add your server