Jack
Jack

Reputation: 15

how to make an online member counter to update on the moment?

how to make an online member counter to update on the moment?

client.on('ready', () => {
    var guild = client.guilds.cache.get('705684700982673428');
    var onlineCount = guild.members.cache.filter(m => m.presence.status === 'online').size
    client.user.setActivity("Online: " + onlineCount)
    .then(console.log)
    .catch(console.error);}, 3000);

Upvotes: 0

Views: 62

Answers (2)

Jakye
Jakye

Reputation: 6625

Since you are setting your activity status on client initialization, it won't be updated in the future. I recommend you use presenceUpdate, which will fire each time a member's presence is updated. (Yes, this includes the online status.)

client.on("presenceUpdate", (oldPresence, newPresence) => {
    const Guild = client.guilds.cache.get("705684700982673428");
    if (newPresence.guild.id !== Guild.id) return false;
    client.user.setActivity(`${Guild.members.cache.filter(member => member.presence.status !== "offline").size} online member${Guild.members.cache.filter(member => member.presence.status !== "offline").size == 1 ? '' : 's'}.`, {type: "WATCHING"})
});

Upvotes: 0

Niklas Ziermann
Niklas Ziermann

Reputation: 86

I'd recommend using the fetch api to tell the server whenever someone new joins so that all other users can ask for the current user count in an interval

Upvotes: 1

Related Questions