如何列出来自特定服务器的所有成员?

How to list all members from a specific server?(如何列出来自特定服务器的所有成员?)
本文介绍了如何列出来自特定服务器的所有成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我的代码是

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.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.

一个 Guild 没有 users 属性,因为它有一个 members 属性,返回 Collection.现在获取 username 从每个成员你可以从 user 属性.因此,您需要遍历 GuildMember 的集合,并获取 <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)); 

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

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
Ordinals in words javascript(javascript中的序数)
getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)