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() 将成员集合拆分为 bots 和 humans.使用 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);
}
});
这篇关于从客户端状态的成员计数中过滤掉机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:从客户端状态的成员计数中过滤掉机器人
基础教程推荐
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01
