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

查看:44
本文介绍了如何在Guild 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集合(

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

代替 .forEach(),您可以使用

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

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

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

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

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

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