我想让我的机器人活动说 观看 + 成员计数(不包括机器人)".
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);
}
});
这篇关于从客户端状态的成员计数中过滤掉机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
discord.js v12:我如何等待 DM 频道中的消息?discord.js v12: How do I await for messages in a DM channel?(discord.js v12:我如何等待 DM 频道中的消息?)
如何让我的机器人提及发出该机器人命令的人how to make my bot mention the person who gave that bot command(如何让我的机器人提及发出该机器人命令的人)
如何修复必须使用导入来加载 ES 模块 discord.jsHow to fix Must use import to load ES Module discord.js(如何修复必须使用导入来加载 ES 模块 discord.js)
如何列出来自特定服务器的所有成员?How to list all members from a specific server?(如何列出来自特定服务器的所有成员?)
Discord bot:修复“找不到 FFMPEG"Discord bot: Fix ‘FFMPEG not found’(Discord bot:修复“找不到 FFMPEG)
使用 discord.js 加入 discord 服务器时的欢迎消息Welcome message when joining discord Server using discord.js(使用 discord.js 加入 discord 服务器时的欢迎消息)