问题
My code is
const list = client.guilds.find("id", "335507048017952771")
for (user of list.users){
console.log(user[1].username);
}
This does literally nothing. There is no error or anything.
I just want the bot to find a server and then log all members from said server.
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.
回答1:
Firstly, don't use .find("id", "335507048017952771")
, you should be using .get("335507048017952771")
, as it says on the discord.js documentation.
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.
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
.
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));
来源:https://stackoverflow.com/questions/50319939/how-to-list-all-members-from-a-specific-server