Search code examples
javascriptnode.jsdiscorddiscord.js

How to get ID from guild.fetch in discord.js v12?


I'm trying to get the Guild id when my bot joins the Discord server for the first time. I need the ID so the bot can automatically set up the roles.

I'm using the guildCreate event, and inside the event, I'm using guild.fetch() to read the data of the collection. Now I only want the value of the id, how do I accomplish that?

Output of guild.fetch():

Guild {
  members: GuildMemberManager {
    cacheType: [Function: Collection],
    cache: Collection(3) [Map] {
      '36489644504160xxxx' => [GuildMember],
      '53517224246470xxxx' => [GuildMember],
      '53522344090743xxxx' => [GuildMember]
    },
    guild: [Circular *1]
  },
  channels: GuildChannelManager {
    cacheType: [Function: Collection],
    cache: Collection(4) [Map] {
      '71558241555944xxxx' => [CategoryChannel],
      '71558241555944xxxx' => [CategoryChannel],
      '71558241555944xxxx' => [TextChannel],
      '71558241555944xxxx' => [VoiceChannel]
    },
    guild: [Circular *1]
  },
  roles: RoleManager {
    cacheType: [Function: Collection],
    cache: Collection(5) [Map] {
      '71258641511471xxxx' => [Role],
      '75251159040894xxxx' => [Role],
      '75253768894955xxxx' => [Role],
      '75295113917353xxxx' => [Role],
      '75270698461845xxxx' => [Role]
    },
    guild: [Circular *1]
  },
  presences: PresenceManager {
    cacheType: [Function: Collection],
    cache: Collection(3) [Map] {
      '36489644504160xxxx' => [Presence],
      '52517224246470xxxx' => [Presence],
      '52522344090743xxxx' => [Presence]
    }
  },
  voiceStates: VoiceStateManager {
    cacheType: [Function: Collection],
    cache: Collection(0) [Map] {},
    guild: [Circular *1]
  },
  deleted: false,
  name: "attrib's server",
  icon: null,
  splash: null,
  discoverySplash: null,
  region: 'europe',
  memberCount: 3,
  large: false,
  features: [],
  applicationID: null,
  afkTimeout: 300,
  afkChannelID: null,
  systemChannelID: '71558641555944xxxx',
  embedEnabled: false,
  premiumTier: 0,
  premiumSubscriptionCount: 0,
  verificationLevel: 'NONE',
  explicitContentFilter: 'DISABLED',
  mfaLevel: 0,
  joinedTimestamp: 1600721822100,
  defaultMessageNotifications: 'ALL',
  systemChannelFlags: SystemChannelFlags { bitfield: 0 },
  maximumMembers: 250000,
  vanityURLCode: null,
  vanityURLUses: null,
  description: null,
  banner: null,
  id: '71558641511471xxxx',
  available: true,
  etc...

Solution

  • You can just get the id property from the guild object.

    guild.fetch().then((guild) => console.log(guild.id)).catch(console.error);