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

Filter out bots from member count in client status(从客户端状态的成员计数中过滤掉机器人)
本文介绍了从客户端状态的成员计数中过滤掉机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

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

这篇关于从客户端状态的成员计数中过滤掉机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Should I use window.navigate or document.location in JavaScript?(我应该在 JavaScript 中使用 window.navigate 还是 document.location?)
Power BI - Get data from post request(Power BI - 从发布请求中获取数据)
Embed a Power BI report in React JS to get report instance(在 React JS 中嵌入 Power BI 报表以获取报表实例)
Create Report in Embed View via PowerBI API(通过 PowerBI API 在嵌入视图中创建报表)
Interactive Dialog Box in PowerBI(PowerBI 中的交互式对话框)
Print/Generate PDF of embedded power bi report(打印/生成嵌入式 power bi 报告的 PDF)