問題描述
我希望機器人狀態顯示當前在線的人數.我一直在嘗試使用此代碼,但它一直在說:
I want the bots status to show the amount of people that currently are online. I have been trying with this code but it keeps saying:
TypeError:client.guilds.get 不是函數
//Checks if the bot is online
client.once('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
//Activity
var guild = client.guilds.get('id here');
var onlineCount = guild.membersCount.filter(m => m.presence.status === 'online').size
client.user.setActivity('games with ' + onlineCount + ' people' , { type: 'PLAYING' });
});
推薦答案
由于 discord.js v12 你現在需要使用 cache
屬性來訪問 guilds
集合,所以您需要將 var guild = client.guilds.get('id here');
替換為 var guild = client.guilds.cache.get('id here');
Since discord.js v12 you now need to use the cache
property to access guilds
collection, so you need to replace var guild = client.guilds.get('id here');
with var guild = client.guilds.cache.get('id here');
與問題無關:您正在獲取 guild
的 memberCount
并對其進行過濾以獲取該公會中的在線用戶數,問題在于 memberCount
返回一個 number
不是 GuildMember
的集合a>s,您需要使用的是 members
屬性,要訪問該集合,您需要再次使用 cache
:
Unrelated to the question:
You are getting the guild
's memberCount
and filtering it to get the number of online users in that guild, the problem with that is that memberCount
returns a number
not a collection of GuildMember
s, what you need to use is the members
property instead, which to access that collection, you need to use cache
again:
var onlineCount = guild.members.cache.filter(m => m.presence.status === 'online').size
這篇關于將 Discord Bot 活動設置為在線用戶數.不和諧.js的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!