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)
})