問題描述
我的代碼是
const list = client.guilds.find("id", "335507048017952771")
for (user of list.users){
console.log(user[1].username);
}
這實(shí)際上沒有任何作用.沒有錯(cuò)誤或任何東西.
This does literally nothing. There is no error or anything.
我只想讓機(jī)器人找到一個(gè)服務(wù)器,然后記錄來自該服務(wù)器的所有成員.
I just want the bot to find a server and then log all members from said server.
顯示所有連接的用戶 Discord.js 這個(gè)問題的答案沒有一點(diǎn)也幫不上我.我確實(shí)嘗試使用 message.guild.users
但這也沒有做任何事情.似乎在 Discord 上找不到任何內(nèi)容.js 網(wǎng)站 來幫助我.
Displaying all connected users Discord.js The answers in this question didn't really help me at all. I did try using message.guild.users
but that also did nothing. Can't seem to find anything on the Discord.js site to help me either.
推薦答案
首先,不要使用.find("id", "335507048017952771")
,你應(yīng)該使用.get("335507048017952771")
,正如它在 discord.js 文檔.
Firstly, don't use .find("id", "335507048017952771")
, you should be using .get("335507048017952771")
, as it says on the discord.js documentation.
Discord.js 中使用的所有集合都使用它們的 id 屬性進(jìn)行映射,如果你想通過 id 查找,你應(yīng)該使用 get 方法.有關(guān)詳細(xì)信息,請參閱 MDN.p>
All collections used in Discord.js are mapped using their id property, and if you want to find by id you should use the get method. See MDN for details.
一個(gè) Guild 沒有 users
屬性,因?yàn)樗幸粋€(gè) members
屬性,返回 Collectionusername
從每個(gè)成員你可以從 <GuildMember>.user.username
.
A Guild does not have a users
property, where as it has a members
property, which returns a Collection of GuildMembers. Now to get the username
from each member you can obtain that from the user
property of the GuildMember. So, you will need to iterate through the collection of GuildMembers, and get the <GuildMember>.user.username
.
有幾種方法可以做到這一點(diǎn),我將使用 forEach()
方法.結(jié)果如下:
There are several ways to do this, I will be using the forEach()
method. Here's what that would look like as a result:
// Get the Guild and store it under the variable "list"
const list = client.guilds.get("335507048017952771");
// Iterate through the collection of GuildMembers from the Guild getting the username property of each member
list.members.forEach(member => console.log(member.user.username));
這篇關(guān)于如何列出來自特定服務(wù)器的所有成員?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!