Answer the question
In order to leave comments, you need to log in
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
}
}
})
}
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question