問題描述
我想讓我的機(jī)器人活動說 觀看 + 成員計(jì)數(shù)(不包括機(jī)器人)".
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);
});
我需要設(shè)置一個(gè)過濾器,它只計(jì)算成員,而不是機(jī)器人.
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
按您的意愿顯示用戶數(shù).你也可以Collection#filter()將成員集合過濾給人類,但是我在此示例中使用分區(qū)來在一個(gè)函數(shù)調(diào)用中訪問雙方.
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);
}
});
這篇關(guān)于從客戶端狀態(tài)的成員計(jì)數(shù)中過濾掉機(jī)器人的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!