简体   繁体   中英

Add member counter to bot activity discord.js v.14

I tried to do that but it isn't working. How can I resolve this? I want that the activiy is the current member Count (refreshing every 5 minutes(or when its possible everytime a member joined))

module.exports = {
    name: 'ready',
    once: true,
    async execute(client) {

        setInterval(() => {
            let membersCount = client.guilds.cache.map(guild => guild.memberCount).reduce((a, b) => a + b, 0)

        }, 1000 * 60 * 5);
        const options = [
            {
                type: ActivityType.Playing,
                text: `mit [${membersCount} usern]`,
                status: "online"
            }
        ];
        await client.user.setPrecence({
            activities: [{
                name: [options].text,
                type: [options].type
                },
            ],
            status: [options].status
        }).catch(console.error);
    }
}

The problem with the code is just the positioning of the update function. What is happening right now is that, every five minutes, the setInterval() runs and updates the memberCount variable. But, the await client.user.setPresence() only runs one time when the async execute() function was first called. So to fix your code, you would just have to move the options variable and await client.user.setPresence() to inside the setInterval() function. Your fixed code would look like this:

module.exports = {
    name: 'ready',
    once: true,
    async execute(client) {

        setInterval(() => {
            let membersCount = client.guilds.cache.map(guild => guild.memberCount).reduce((a, b) => a + b, 0)
            const options = [
                {
                    type: ActivityType.Playing,
                    text: `mit [${membersCount} usern]`,
                    status: "online"
                }
            ];
            await client.user.setPrecence({
                activities: [{
                    name: options[0].text,
                    type: options[0].type
                }],
                status: options[0].status
            }).catch(console.error);
        }, 1000 * 60 * 5);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM