如何获取公会 Discord.js 中的成员列表 [英] How to Get a List of Members in a Guild Discord.js

查看:31
本文介绍了如何获取公会 Discord.js 中的成员列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Discord.js v12 让公会中的所有成员.这就是我所拥有的:

I am trying to get all the members in a guild using Discord.js v12. This is what I have:

    const list = client.guilds.cache.get("720352141709148200");
    list.members.forEach(member => {
       //do stuff with guild memebrs here
    }

我看过这个堆栈溢出问题 但我认为它已经过时了,因为它会抛出一个错误,指出 guilds 未定义.这是我在上面修改过的代码时遇到的错误:

I have looked at this stack overflow question but I believe it's out of date as it throws an error saying that guilds is undefined. This is the error I get with the modified-ish code I have above:

TypeError: list.members.forEach is not a function

推荐答案

正如Jakye指出的,你需要将list.members改为list.members.cache.

As pointed out by Jakye, you need to change list.members to list.members.cache.

但是,您不能使用 .forEach(),因为这是一个数组方法,并且 list.members.cache 返回一个 Discord 集合(Discord.Collection()).

However, you cannot use .forEach(), as that's an array method and list.members.cache returns a Discord collection (Discord.Collection()).

您可以使用 .each():

Instead of .forEach(), you can use .each():

list.members.cache.each(member => {
  // do stuff with guild members here
});

或者,您可以使用 .array() 然后使用 .forEach() :

Alternatively, you could convert the collection into an array of values using .array() and then using .forEach() on that:

list.members.cache.array().forEach(member => {
  // do stuff with guild members here
});

这篇关于如何获取公会 Discord.js 中的成员列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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