简体   繁体   English

将会员计数器添加到机器人活动 discord.js v.14

[英]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))我希望 activiy 是当前成员计数(每 5 分钟刷新一次(或者每次有成员加入时刷新一次))

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.代码的问题只是更新 function 的定位。现在发生的是, setInterval()每五分钟运行一次并更新memberCount变量。 But, the await client.user.setPresence() only runs one time when the async execute() function was first called.但是, await client.user.setPresence()仅在第一次调用async execute() function 时运行一次。 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:因此,要修复您的代码,您只需将options变量和await client.user.setPresence()移动到setInterval() function 内部。您的修复代码如下所示:

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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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