Skip to content

activities of bot update informations

I put it in the status to show how many members are online of the total number, but it does not update the real number, only when the bot is restarted, is there any way that when this activity arrives, the information appears with the new data?

const server = client.guilds.cache.get("ID OF GUILD")
let onlineMembers = 0;
  server.members.cache.forEach((m) => {
    if (m.presence.status === "online" || m.presence.status === "dnd" || m.presence.status === "idle")  {
      onlineMembers++;
    }
  });
let activities = [
`${onlineMembers}/${server.members.cache.filter(member => !member.user.bot).size} members online`,
]
i = 0;
  setInterval( () => client.user.setActivity(`${activities[i++ % activities.length]}`, {
        type: "PLAYING"
      }), 10000 * 60); 

Answer

You need to regenerate the Strings in the Array every run:

let i = 0;
setInterval( () => {
    const server = client.guilds.cache.get("ID OF GUILD")
    let onlineMembers = 0;
    server.members.cache.forEach((m) => {
        if (m.presence.status === "online" || m.presence.status === "dnd" || m.presence.status === "idle")  {
            onlineMembers++;
        }
    });
    let activities = [
         `${onlineMembers}/${server.members.cache.filter(member => !member.user.bot).size} members online`,
    ];
    client.user.setActivity(`${activities[i++ % activities.length]}`, {
        type: "PLAYING"
    });
}, 10000 * 60);