R
R
rodiongoshev2020-08-23 12:39:14
JavaScript
rodiongoshev, 2020-08-23 12:39:14

Discord.js TypeError: Cannot read property 'forEach' of undefined how to fix?

module.exports = (bot) => {
    const invites = {} // { guildId: { memberId: count } }
  
    const getInviteCounts = async (guild) => {
      return await new Promise((resolve) => {
        guild.fetchInvites().then((invites) => {
          const inviteCounter = {} // { memberId: count }
  
          invites.forEach((invite) => {
            const { uses, inviter } = invite
            const { username, discriminator } = inviter
  
            const name = `${username}#${discriminator}`
  
            inviteCounter[name] = (inviteCounter[name] || 0) + uses
          })
  
          resolve(inviteCounter)
        })
      })
    }
  
    bot.guilds.cache.forEach(async (guild) => {
      invites[guild.id] = await getInviteCounts(guild)
    })
  
    bot.on('guildMemberAdd', async (member) => {
      const { guild, id } = member
  
      const invitesBefore = invites[guild.id]
      const invitesAfter = await getInviteCounts(guild)
  
      console.log('Было:', invitesBefore)
      console.log('Стало:', invitesAfter)
  
      for (const inviter in invitesAfter) {
        if (invitesBefore[inviter] === invitesAfter[inviter] - 1) {
          const channelId = '731801004462571602'
          const channel = guild.channels.cache.get(channelId)
          const count = invitesAfter[inviter]
          channel.send(
            `Добро пожаловать,<@${id}> на сервер. Был приглашён ${inviter} (${count} Приглашений.)`
          )
  
          invites[guild.id] = invitesAfter
          return
        }
      }
    })
  }

Line error
bot.guilds.cache.forEach(async (guild) => {
invites[guild.id] = await getInviteCounts(guild)
})

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2020-08-23
@rodiongoshev

you need to read this article , in particular, the first section.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question