从客户端状态的成员计数中过滤掉机器人

我想让我的机器人活动说 观看 + 成员计数(不包括机器人)".

I want to make my bot activity say "Watching + Member Count (not include bots)".

我做了一些步骤,这是我的代码:

I did some steps, here is my code:

client.once('ready', () => { 
        setInterval(() => {
          targetGuild = client.guilds.cache.get('My Guild ID Here')
          client.user.setPresence({ 
            activities: [{ name: `${targetGuild.memberCount} Users`, type: 'WATCHING' }], 
            status: 'online'
            });
        }, 1000 * 60 * 5);
    
    });

我需要设置一个过滤器,它只计算成员,而不是机器人.

The thing that I need is to set a filter that it calculate members only, not bots.

推荐答案

使用 GuildMemberManager#fetch() 获取所有成员,然后使用 Collection#partition() 将成员集合拆分为 botshumans.使用 humans.size 按您的意愿显示用户数.你也可以Collection#filter()将成员集合过滤给人类,但是我在此示例中使用分区来在一个函数调用中访问双方.

Use GuildMemberManager#fetch() to fetch all members, then use Collection#partition() to split the member collection into bots and humans. Use humans.size to display the user count as you intend. You can also Collection#filter() to filter the member collection to just the humans, however I use partition in this example to have access to both parties in one function call.

client.once('ready', async() => { 
   targetGuild = client.guilds.cache.get('My Guild ID Here');
   try {
      const [bots, humans] = (await targetGuild.members.fetch())
         .partition(member => member.user.bot);
      setInterval(() => {
         client.user.setPresence({ 
            activities: [
               { 
                  name: `${humans.size} Users`, 
                  type: 'WATCHING' 
               }
            ], 
            status: 'online'
         });
      }, 1000 * 60 * 5);
   } catch (err) {
      console.error(err);
   }
});

相关文章