Discord.js Not updating server member count

Hello,

So I am trying to make my bot have an activity of “Watching memberCount Members!”. However the code I currently have works fine when the bot is first started but then won’t update after, until it’s restarted. Just wondered if my code below was wrong or if it’s some kind of limit to server size.

	setInterval(() => {
		client.user.setActivity(`${client.guilds.cache.get('907405033841238026').memberCount} Members!`, {type: ActivityType.Watching})
	}, 5000)

Thanks in advance!

@cookietechai,

Can you help @Max.Mobey with the original post?

Hi @Max.Mobey,

The code you provided looks fine to me. The issue may be due to Discord’s rate limit for activity updates. Discord has a rate limit of 2 updates per minute per user, so if you try to update the activity more frequently than that, it won’t work.

Also, if the bot is not part of the guild anymore or if it doesn’t have permission to view the member count, it won’t update the activity. You can check if the bot has the “View Channel” permission in the guild.

Try updating the interval timer to a longer time, such as 30 seconds or 1 minute, and see if it works. You can also try adding a catch block to handle any errors that may occur:

setInterval(() => {
	try {
		client.user.setActivity(`${client.guilds.cache.get('907405033841238026').memberCount} Members!`, {type: 'WATCHING'});
	} catch (error) {
		console.error(error);
	}
}, 30000); //change interval time here

Let me know if this helps!

Nope I have tried that and still the same thing, it continues to use the same member count it booted with.

@Max.Mobey,

If increasing the interval timer didn’t solve the issue, could you please try the following code where we are using Guild#fetch to get the refreshed member count?

const guildId = "Replace_Guild_ID";
let guild;

client.on('ready', () => {
    console.log(`Logged in as ${client.user.tag}`);
    guild = client.guilds.cache.get(guildId);
    setInterval(updateActivity, 5 * 60 * 1000);
});

async function updateActivity() {
    try {
        await guild.members.fetch();
        client.user.setActivity(`${guild.memberCount} Members!`, { type: 'WATCHING' });
        console.log(`Activity set. Current Member count: ${guild.memberCount}`);
    } catch (error) {
        console.error(error);
    }
}

Please replace Replace_Guild_ID with the ID number of the guild you want to track.

With this code, we are fetching the latest member count and we are updating the activity after every 5 minutes. You can change this interval to whatever interval time you want.

Let me know if this solves your issue.

That code logs the error:

Error [GuildMembersTimeout]: Members didn't arrive in time.
    at Timeout._onTimeout (C:\Users\maxmo\Desktop\NTSDBBot\node_modules\discord.js\src\managers\GuildMemberManager.js:536:16)
    at listOnTimeout (node:internal/timers:559:17)
    at processTimers (node:internal/timers:502:7) {
  code: 'GuildMembersTimeout'
}

I assume this isn’t possible due to the number of members and it isn’t able to count them in time, is that correct?

@cookietechai Both of those messages said “…”

1 Like

Might be because of this post

1 Like

Hello,

I worked it out, I forgot to add the intent for guild members :man_facepalming:

Anyways thanks for the help @happyshot10 and CookieAI

1 Like

This topic was automatically closed after 7 days. New replies are no longer allowed.