1

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);
}
7
  • Verify your guild id
    – Cursed
    Commented Aug 5, 2020 at 15:28
  • i did but it still doesnt work
    – tobi
    Commented Aug 5, 2020 at 16:03
  • try to console.log(client.guild.cache.get("335507048017952771")) does it return anything?
    – Cursed
    Commented Aug 5, 2020 at 16:09
  • it returns undefined
    – tobi
    Commented Aug 5, 2020 at 16:11
  • Mistyped change client.guild to client.guilds
    – Cursed
    Commented Aug 5, 2020 at 16:37

2 Answers 2

1

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:

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.

13
  • members is still undefined
    – tobi
    Commented Aug 5, 2020 at 18:36
  • @tobi Where does receivedMessage come from? Commented Aug 5, 2020 at 18:39
  • client.on('message', (receivedMessage) => {
    – tobi
    Commented Aug 5, 2020 at 18:44
  • i just want the id. i get with the first line u send all the other informatuins nicknames ids etc. something like [0000000000, 000000000, 0000000000] or [Name1#1111, Name2#2222, Name3#3333]
    – tobi
    Commented Aug 5, 2020 at 18:45
  • 1
    @tobi Please can you copy your code onto the site hastebin or pastebin and add a link here. Remember to remove any tokens. Commented Aug 5, 2020 at 20:24
0

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());
2
  • guild.array() create this error: TypeError: Cannot read property 'array' of undefined
    – tobi
    Commented Aug 5, 2020 at 16:05
  • @tobi it looks like your bot can't fetch the guild, where does the ID come from? It has only access to guilds where it has been added as bot
    – Zer0
    Commented Aug 6, 2020 at 6:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.