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.
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:
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.
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'
}