2

I am trying to do this to get all users (with a specific role) of a particular guild.

I have this, but the terminal says that "member is undefined". Any help is really appreciated.

var members;
const guild = client.guilds.fetch(process.env.GUILD_ID)
console.log(guild)
guild.then(()=>{
guild.members.fetch()
})
Aluan Haddad
  • 29,886
  • 8
  • 72
  • 84
  • 1
    See also: https://stackoverflow.com/questions/68517258/discord-js-get-all-current-guild-online-member-usernames – Raptor Apr 13 '22 at 09:42

1 Answers1

0

client.guilds.fetch will return a promise, not a value

You can modify then to get a guild response

const guildRequest = client.guilds.fetch(process.env.GUILD_ID)
const memberRequest = guildRequest.then((guild)=>{
  return guild.members.fetch()
})
Nick Vu
  • 14,512
  • 4
  • 21
  • 31
  • Hello, thank you for your help. I did that and this is an error that is shown. return new Promise((resolve, reject) => { ^ TypeError: Cannot read properties of undefined (reading 'send') – Jeremy Nguyen Apr 13 '22 at 04:43
  • I think it's expecting another promise return, could you do like this? `return guild.members.fetch()` @JeremyNguyen – Nick Vu Apr 13 '22 at 04:47
  • Hi! I did add the "return", but it still presents the same error – Jeremy Nguyen Apr 13 '22 at 04:49
  • Hmm I think it's related to discord.js particularly, could you share the whole function? @JeremyNguyen – Nick Vu Apr 13 '22 at 04:51
  • Another possibility is that your guild is not existing, it will cause `send` undefined @JeremyNguyen – Nick Vu Apr 13 '22 at 04:54
  • Yes sir, import { Client, Intents } from "discord.js"; import { CLinkedList, Node } from "./LinkedList"; const DiscordJS = require("discord.js") require("dotenv").config() //Creating the bot const client = new DiscordJS.Client({ intents: [Intents.FLAGS.GUILD_MEMBERS] }); const guildRequest = client.guilds.fetch(process.env.GUILD_ID) guildRequest.then((guild)=>{ return guild.members.fetch() }) client.on("ready", () => { console.log("The bot is ready") }) client.login(process.env.BOT_TOKEN) – Jeremy Nguyen Apr 13 '22 at 04:54
  • Oh, I see, though when I console.log(guild), it does display somethings – Jeremy Nguyen Apr 13 '22 at 04:55
  • what was the response there? @JeremyNguyen – Nick Vu Apr 13 '22 at 05:13
  • It was just displaying some information regarding the guild. That's all. "members" does not have anything. – Jeremy Nguyen Apr 13 '22 at 05:17
  • I think you don't need to call `guild.members.fetch()`, just return `guild.members`. You can check [this implementation](https://github.com/sizzlorox/Idle-RPG-Bot/blob/526c2518be325aa3d775e1d983d7b5cd9f40d74d/idle-rpg/v2/DiscordBot.js#L68) – Nick Vu Apr 13 '22 at 05:35
  • I just did it and console.log() it, it outputs guild.members as "undefined" – Jeremy Nguyen Apr 13 '22 at 05:56
  • I think you've not populated `members` properly @JeremyNguyen – Nick Vu Apr 13 '22 at 05:59
  • What does populate members mean? – Jeremy Nguyen Apr 13 '22 at 06:02
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/243848/discussion-between-nick-vu-and-jeremy-nguyen). – Nick Vu Apr 13 '22 at 08:52
  • what else do you have in `guild` data? @JeremyNguyen – Nick Vu Apr 13 '22 at 08:53
  • I have the same problem. No matter I added `Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MEMBERS` flags or enabled Privileged Gateway Intents (`GUILD_PRESENCES` and `GUILD_MEMBERS`), the `guild.members` object is still undefined. – Raptor Apr 13 '22 at 09:27
  • my `guild` OAuth2Guild object contains `id`, `name`, `icon`, `features`, `owner` and `permissions`, but does not have `members`. – Raptor Apr 13 '22 at 09:31
  • Have you found a solution yet@Raptor – Jeremy Nguyen Apr 13 '22 at 15:38
  • The link Raptor shared earlier is useful, you can check [this](https://stackoverflow.com/questions/68517258/discord-js-get-all-current-guild-online-member-usernames) out @JeremyNguyen – Nick Vu Apr 13 '22 at 15:43