title: Discord.js v13 Invite tracker
body: <p>Finally getting around to updating my bot from v12 to v13 and having an issue that I can't seem to solve. I have an invite tracker that works perfectly on v12 but will not function on v13. I have included the code below, the commented lines are the v12 code that I know needed changing for v13.</p>
  <pre class="lang-js prettyprint-override"><code>// const Discord = require('discord.js')
  const {
      Client,
      Intents
  } = require('discord.js')
  
  // const client = new Discord.Client({
  //     partials: ['MESSAGE', 'CHANNEL', 'REACTION']
  // })
  const client = new Client({
      intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MEMBERS, Intents.FLAGS.GUILD_BANS, Intents.FLAGS.GUILD_EMOJIS_AND_STICKERS, Intents.FLAGS.GUILD_INTEGRATIONS, Intents.FLAGS.GUILD_WEBHOOKS, Intents.FLAGS.GUILD_INVITES, Intents.FLAGS.GUILD_VOICE_STATES, Intents.FLAGS.GUILD_PRESENCES, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MESSAGE_REACTIONS, Intents.FLAGS.GUILD_MESSAGE_TYPING, Intents.FLAGS.DIRECT_MESSAGES, Intents.FLAGS.DIRECT_MESSAGE_REACTIONS, Intents.FLAGS.DIRECT_MESSAGE_TYPING],
      partials: ['MESSAGE', 'CHANNEL', 'REACTION']
  })
  const guildInvites = new Map()
  
  // client.on('inviteCreate', async invite =&gt; guildInvites.set(invite.guild.id, await invite.guild.fetchInvites()))
  client.on('inviteCreate', async invite =&gt; guildInvites.set(invite.guild.id, await invite.guild.invites.fetch()))
  
  client.once('ready', () =&gt; {
      client.guilds.cache.forEach(guild =&gt; {
          //guild.fetchInvites()
          guild.invites.fetch()
              .then(invites =&gt; guildInvites.set(guild.id, invites))
              .catch(err =&gt; {
                  console.log(err)
                  console.log(guildInvites)
                  client.channels.cache.get('channelId').send({
                      content: `${err}`
                  })
              })
      })
  })
  
  client.on('guildMemberAdd', async member =&gt; {
      const cachedInvites = guildInvites.get(member.guild.id)
      // const newInvites = await member.guild.fetchInvites()
      const newInvites = await member.guild.invites.fetch()
      guildInvites.set(member.guild.id, newInvites)
      try {
          const usedInvite = newInvites.find(inv =&gt; cachedInvites.get(inv.code).uses &lt; inv.uses)
          console.log(cachedInvites)
          console.log(newInvites)
          console.log(usedInvite)
          console.log(`The code ${usedInvite.code} was just used by ${member.user.username}.`)
      } catch (err) {
          console.log(err)
          client.channels.cache.get('channelId').send({
              content: `${err}`
          })
      }
  })
  </code></pre>
  <p>This is the console log result:</p>
  <p>console.log(cachedInvites) =&gt; returns below</p>
  <pre class="lang-js prettyprint-override"><code>Collection(2) [Map] {
      '11111111' =&gt; Invite {
        guild: Guild {
          id: 'xxxx',
          name: &quot;Testing Server&quot;,
          icon: null,
          features: [Array],
          commands: [GuildApplicationCommandManager],
          members: [GuildMemberManager],
          channels: [GuildChannelManager],
          bans: [GuildBanManager],
          roles: [RoleManager],
          presences: PresenceManager {},
          voiceStates: [VoiceStateManager],
          stageInstances: [StageInstanceManager],
          invites: [GuildInviteManager],
          deleted: false,
          available: true,
          shardId: 0,
          splash: null,
          banner: null,
          description: null,
          verificationLevel: 'LOW',
          vanityURLCode: null,
          nsfwLevel: 'DEFAULT',
          discoverySplash: null,
          memberCount: 3,
          large: false,
          applicationId: null,
          afkTimeout: 900,
          afkChannelId: 'xxxx',
          systemChannelId: 'xxxx',
          premiumTier: 'NONE',
          premiumSubscriptionCount: 0,
          explicitContentFilter: 'ALL_MEMBERS',
          mfaLevel: 'NONE',
          joinedTimestamp: 1633812225872,
          defaultMessageNotifications: 'ONLY_MENTIONS',
          systemChannelFlags: [SystemChannelFlags],
          maximumMembers: 250000,
          maximumPresences: null,
          approximateMemberCount: null,
          approximatePresenceCount: null,
          vanityURLUses: null,
          rulesChannelId: 'xxxx',
          publicUpdatesChannelId: 'xxxx',
          preferredLocale: 'en-US',
          ownerId: 'xxxx',
          emojis: [GuildEmojiManager],
          stickers: [GuildStickerManager]
        },
        code: '11111111',
        presenceCount: null,
        memberCount: null,
        temporary: false,
        maxAge: 604800,
        uses: 40,
        maxUses: 0,
        inviter: User {
          id: 'xxxx',
          bot: false,
          system: false,
          flags: [UserFlags],
          username: 'xxxx',
          discriminator: 'xxxx',
          avatar: 'xxxx',
          banner: undefined,
          accentColor: undefined
        },
        targetUser: null,
        targetApplication: null,
        targetType: null,
        channel: TextChannel {
          type: 'GUILD_TEXT',
          deleted: false,
          guild: [Guild],
          guildId: 'xxxx',
          parentId: 'xxxx',
          permissionOverwrites: [PermissionOverwriteManager],
          messages: [MessageManager],
          threads: [ThreadManager],
          nsfw: false,
          id: 'xxxx',
          name: 'welcome',
          rawPosition: 3,
          topic: null,
          lastMessageId: 'xxxx',
          rateLimitPerUser: 0
        },
        createdTimestamp: 1633922331801,
        _expiresTimestamp: null,
        stageInstance: null
      },
      '22222222' =&gt; Invite {
        guild: Guild {
          id: 'xxxx',
          name: &quot;Testing Server&quot;,
          icon: null,
          features: [Array],
          commands: [GuildApplicationCommandManager],
          members: [GuildMemberManager],
          channels: [GuildChannelManager],
          bans: [GuildBanManager],
          roles: [RoleManager],
          presences: PresenceManager {},
          voiceStates: [VoiceStateManager],
          stageInstances: [StageInstanceManager],
          invites: [GuildInviteManager],
          deleted: false,
          available: true,
          shardId: 0,
          splash: null,
          banner: null,
          description: null,
          verificationLevel: 'LOW',
          vanityURLCode: null,
          nsfwLevel: 'DEFAULT',
          discoverySplash: null,
          memberCount: 3,
          large: false,
          applicationId: null,
          afkTimeout: 900,
          afkChannelId: 'xxxx',
          systemChannelId: 'xxxx',
          premiumTier: 'NONE',
          premiumSubscriptionCount: 0,
          explicitContentFilter: 'ALL_MEMBERS',
          mfaLevel: 'NONE',
          joinedTimestamp: 1633812225872,
          defaultMessageNotifications: 'ONLY_MENTIONS',
          systemChannelFlags: [SystemChannelFlags],
          maximumMembers: 250000,
          maximumPresences: null,
          approximateMemberCount: null,
          approximatePresenceCount: null,
          vanityURLUses: null,
          rulesChannelId: 'xxxx',
          publicUpdatesChannelId: 'xxxx',
          preferredLocale: 'en-US',
          ownerId: 'xxxx',
          emojis: [GuildEmojiManager],
          stickers: [GuildStickerManager]
        },
        code: '22222222',
        presenceCount: null,
        memberCount: null,
        temporary: false,
        maxAge: 604800,
        uses: 21,
        maxUses: 0,
        inviter: User {
          id: 'xxxx',
          bot: false,
          system: false,
          flags: [UserFlags],
          username: 'xxxx',
          discriminator: 'xxxx',
          avatar: 'xxx',
          banner: undefined,
          accentColor: undefined
        },
        targetUser: null,
        targetApplication: null,
        targetType: null,
        channel: TextChannel {
          type: 'GUILD_TEXT',
          deleted: false,
          guild: [Guild],
          guildId: 'xxxxx',
          parentId: 'xxxx',
          permissionOverwrites: [PermissionOverwriteManager],
          messages: [MessageManager],
          threads: [ThreadManager],
          nsfw: false,
          id: 'xxxx',
          name: 'guest-chat',
          rawPosition: 48,
          topic: 'Guest Invitation Link',
          lastMessageId: null,
          rateLimitPerUser: 0
        },
        createdTimestamp: 1633969178889,
        _expiresTimestamp: null,
        stageInstance: null
      }
    }
  </code></pre>
  <p>console.log(newInvites) =&gt; returns below</p>
  <pre class="lang-js prettyprint-override"><code>Collection(2) [Map] {
      '11111111' =&gt; Invite {
        guild: Guild {
          id: 'xxxx',
          name: &quot;Testing Server&quot;,
          icon: null,
          features: [Array],
          commands: [GuildApplicationCommandManager],
          members: [GuildMemberManager],
          channels: [GuildChannelManager],
          bans: [GuildBanManager],
          roles: [RoleManager],
          presences: PresenceManager {},
          voiceStates: [VoiceStateManager],
          stageInstances: [StageInstanceManager],
          invites: [GuildInviteManager],
          deleted: false,
          available: true,
          shardId: 0,
          splash: null,
          banner: null,
          description: null,
          verificationLevel: 'LOW',
          vanityURLCode: null,
          nsfwLevel: 'DEFAULT',
          discoverySplash: null,
          memberCount: 3,
          large: false,
          applicationId: null,
          afkTimeout: 900,
          afkChannelId: 'xxxx',
          systemChannelId: 'xxxx',
          premiumTier: 'NONE',
          premiumSubscriptionCount: 0,
          explicitContentFilter: 'ALL_MEMBERS',
          mfaLevel: 'NONE',
          joinedTimestamp: 1633812225872,
          defaultMessageNotifications: 'ONLY_MENTIONS',
          systemChannelFlags: [SystemChannelFlags],
          maximumMembers: 250000,
          maximumPresences: null,
          approximateMemberCount: null,
          approximatePresenceCount: null,
          vanityURLUses: null,
          rulesChannelId: 'xxxx',
          publicUpdatesChannelId: 'xxxx',
          preferredLocale: 'en-US',
          ownerId: 'xxxx',
          emojis: [GuildEmojiManager],
          stickers: [GuildStickerManager]
        },
        code: '11111111',
        presenceCount: null,
        memberCount: null,
        temporary: false,
        maxAge: 604800,
        uses: 40,
        maxUses: 0,
        inviter: User {
          id: 'xxxx',
          bot: false,
          system: false,
          flags: [UserFlags],
          username: 'xxxx',
          discriminator: 'xxxx',
          avatar: 'xxxx',
          banner: undefined,
          accentColor: undefined
        },
        targetUser: null,
        targetApplication: null,
        targetType: null,
        channel: TextChannel {
          type: 'GUILD_TEXT',
          deleted: false,
          guild: [Guild],
          guildId: 'xxxx',
          parentId: 'xxxx',
          permissionOverwrites: [PermissionOverwriteManager],
          messages: [MessageManager],
          threads: [ThreadManager],
          nsfw: false,
          id: 'xxxx',
          name: 'welcome',
          rawPosition: 3,
          topic: null,
          lastMessageId: 'xxxx',
          rateLimitPerUser: 0
        },
        createdTimestamp: 1633922331801,
        _expiresTimestamp: null,
        stageInstance: null
      },
      '22222222' =&gt; Invite {
        guild: Guild {
          id: '878285237082271744',
          name: &quot;Testing Server&quot;,
          icon: null,
          features: [Array],
          commands: [GuildApplicationCommandManager],
          members: [GuildMemberManager],
          channels: [GuildChannelManager],
          bans: [GuildBanManager],
          roles: [RoleManager],
          presences: PresenceManager {},
          voiceStates: [VoiceStateManager],
          stageInstances: [StageInstanceManager],
          invites: [GuildInviteManager],
          deleted: false,
          available: true,
          shardId: 0,
          splash: null,
          banner: null,
          description: null,
          verificationLevel: 'LOW',
          vanityURLCode: null,
          nsfwLevel: 'DEFAULT',
          discoverySplash: null,
          memberCount: 3,
          large: false,
          applicationId: null,
          afkTimeout: 900,
          afkChannelId: 'xxxx',
          systemChannelId: 'xxxx',
          premiumTier: 'NONE',
          premiumSubscriptionCount: 0,
          explicitContentFilter: 'ALL_MEMBERS',
          mfaLevel: 'NONE',
          joinedTimestamp: 1633812225872,
          defaultMessageNotifications: 'ONLY_MENTIONS',
          systemChannelFlags: [SystemChannelFlags],
          maximumMembers: 250000,
          maximumPresences: null,
          approximateMemberCount: null,
          approximatePresenceCount: null,
          vanityURLUses: null,
          rulesChannelId: 'xxxx',
          publicUpdatesChannelId: 'xxxx',
          preferredLocale: 'en-US',
          ownerId: 'xxxx',
          emojis: [GuildEmojiManager],
          stickers: [GuildStickerManager]
        },
        code: '22222222',
        presenceCount: null,
        memberCount: null,
        temporary: false,
        maxAge: 604800,
        uses: 22,
        maxUses: 0,
        inviter: User {
          id: 'xxxx',
          bot: false,
          system: false,
          flags: [UserFlags],
          username: 'xxxx',
          discriminator: 'xxxx',
          avatar: 'xxxx',
          banner: undefined,
          accentColor: undefined
        },
        targetUser: null,
        targetApplication: null,
        targetType: null,
        channel: TextChannel {
          type: 'GUILD_TEXT',
          deleted: false,
          guild: [Guild],
          guildId: 'xxxx',
          parentId: 'xxxx',
          permissionOverwrites: [PermissionOverwriteManager],
          messages: [MessageManager],
          threads: [ThreadManager],
          nsfw: false,
          id: 'xxxx',
          name: 'guest-chat',
          rawPosition: 48,
          topic: 'Guest Invitation Link',
          lastMessageId: null,
          rateLimitPerUser: 0
        },
        createdTimestamp: 1633969178889,
        _expiresTimestamp: null,
        stageInstance: null
      }
    }
  </code></pre>
  <p>console.log(usedInvite) =&gt; returns undefined</p>
  <p>The error message I get is obviously:</p>
  <p><code>TypeError: Cannot read property 'code' of undefined</code></p>