简体   繁体   English

discord.js client.guilds.cache.get(role).members | 创建一个包含公会所有成员的数组

[英]discord.js client.guilds.cache.get(role).members | Create a array with all members of a guild

I want to create an array with every user with a specific role.我想为每个具有特定角色的用户创建一个数组。 But I get the following error:但我收到以下错误:

TypeError: Cannot read property 'members' of undefined

The code that I am currently using:我目前使用的代码:

var role = receivedMessage.guild.roles.cache.find(role => role.name === "arole");
const guild = client.guilds.cache.get(role);
if (guild == "") {
    console.log("guild not found");
} else {
    const Members = client.guilds.cache.get(role).members.cache.map(member => member.id);
}

Seems like the guild is non-existent or your bot doesn't have acccess to it, you can simply check this by doing something like似乎公会不存在或者您的机器人无法访问它,您可以通过执行类似的操作来简单地检查这一点

const guild = client.guilds.cache.get("335507048017952771");
if (!guild) return console.log("guild not found :(");

//also use the built-in array() method
console.log(guild.array());

This code gets all members with a certain role in the server the message was sent in:此代码获取发送消息的服务器中具有特定角色的所有成员:

const guild = receivedMessage.guild;
if (!guild) return console.log("Couldn't get the guild.");

const members = guild.members.cache.filter(member => member.roles.cache.find(role => role.name === "arole")).map(member => member.id);

If you want to get all members with a certain role in a specific server, you can specify the guild ID:如果要获取特定服务器中具有特定角色的所有成员,可以指定公会 ID:

const guild = client.guilds.cache.get(/* Guild ID */);
if (!guild) return console.log("Couldn't get the guild.");

const members = guild.members.cache.filter(member => member.roles.cache.find(role => role.name === "arole")).map(member => member.id);

For more information on valid properties and methods, please read the Discord.js docs .有关有效属性和方法的更多信息,请阅读Discord.js 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM