如何列出特定服务器上的所有成员? [英] How to list all members from a specific server?

查看:32
本文介绍了如何列出特定服务器上的所有成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码是

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.

显示所有已连接的用户Discord.js 该问题的答案没有真的帮不了我.我确实尝试过使用 message.guild.users ,但这也无济于事.在 Discord上似乎找不到任何内容.js网站可以帮助我.

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"),您应该使用.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属性进行映射,如果要按id查找,则应使用get方法.有关详细信息,请参见 MDN .>

Guild 没有用户属性,因为它具有 成员 属性,该属性返回 Collection GuildMember .现在获取 用户名 ,可以从 user 属性.因此,您将需要遍历GuildMembers的集合,并获取< 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.

有几种方法可以做到这一点,我将使用 forEach() 方法.结果就是这样:

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)); 

这篇关于如何列出特定服务器上的所有成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆