会员计数器未更新会员计数 discord.js

Member counter is not updating members count discord.js

我创建了一个会员计数器,但它没有更新。一直卡在80585,更新的时候需要重启bot

我的代码:

client.on("ready", async() => {

    let servercount = await client.guilds.cache.reduce((a,b) => a+b.memberCount, 0 );
    
    const activities = [
        `${servercount} People !`,
        `d!help`
    ]
    setInterval(() => {
        const status = activities[Math.floor(Math.random()*activities.length)];
        client.user.setPresence({ status: "online", activities : [{name : `${status}` , type : "WATCHING",}]});
    }, 120000);
});

我正在使用 discord.js v13 和 node.js v16

When I want to update it, I need to restart the bot.

这就是您的代码存在的问题。查看 let servercount 行。具体来说,看看它在你的代码中的位置。成员数仅在机器人启动时更新,因为您仅在机器人启动时获取成员数。要解决该特定问题,只需将该行移至区间:

client.on("ready", async() => {

    setInterval(async () => {

        let servercount = await client.guilds.cache.reduce((a,b) => a+b.memberCount, 0 );
    
        const activities = [
            `${servercount} People !`,
            `d!help`
        ]

        const status = activities[Math.floor(Math.random()*activities.length)];
        client.user.setPresence({ status: "online", activities : [{name : `${status}` , type : "WATCHING",}]});
    }, 120000);
});

这至少应该可以解决未更新成员数的问题。但是,即使在此修复之后,成员数仍可能不是 up-to-date。您需要获取每个公会的成员集合,以便始终获得准确的数字。