将成员计数器添加到bot活动discord.js中

时间:2020-07-06 17:41:34

标签: javascript node.js discord discord.js

我试图这样做,但是没有用。我该如何解决?

const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
  console.log(`Logged in as ${client.user.tag}!`);
  client.user.setActivity(`[${member.guild.memberCount}] Players`, {type: "PLAYING"});
});
client.on("guildMemberAdd", member => {
    client.user.setActivity(`[${member.guild.memberCount}] Players`, {type: "PLAYING"});
});
client.on("guildMemberRemove", member => {
    client.user.setActivity(`[${member.guild.memberCount}] Players`, {type: "PLAYING"});
});

1 个答案:

答案 0 :(得分:0)

您可以映射客户协会,以获取membersCount值,然后使用reduce方法获取成员的总和。

此代码将每1分钟更新一次成员计数。

const Discord = require('discord.js')
const token = require('./token.json').token
const client = new Discord.Client();

client.on('ready', () => {
    setInterval(() => {
        let membersCount = client.guilds.cache.map(guild => guild.memberCount).reduce((a, b) => a + b, 0)
        client.user.setActivity(`[${membersCount}] Players`, {type: "PLAYING"});
    }, 1000 * 60);

});

client.login(token)