PrezOfTheUnitedIdiots
PrezOfTheUnitedIdiots

Reputation: 181

Invites command bot reply sending message multiple times

I am using the invite tracker from the WOK tutorials, and even though the code works perfectly fine, and im using a command handler that has 0 issues, for some reason, the bot replies with multiple messages instead of 1 as seen in this image:

enter image description here

Here is the code that I used for the invites command:

const Discord = require("discord.js")

module.exports = {
    commands: 'invites',
    callback: (message) => {
        const { guild } = message
        guild.fetchInvites().then((invites) => {
            const inviteCounter = {}

            invites.forEach((invite) => {
                const { uses, inviter } = invite
                const { username, discriminator } = inviter

                const name = `${username}#${discriminator}`

                inviteCounter[name] = (inviteCounter[name] || 0) + uses
            })

            let replyText = ''

            const sortedInvites = Object.keys(inviteCounter).sort((a, b) => inviteCounter[b] - inviteCounter[a])

            for (const invite of sortedInvites) {
                const count = inviteCounter[invite]
                replyText += `\n${invite} has invited ${count} member(s)!`

                const embed = new Discord.MessageEmbed()
                embed.setTitle('Invites: ')
                embed.setDescription(replyText)
                message.reply(embed)
            }
        })
    }
}

Fyi last time i posted this i forgot to post the code so yeah that was dumb my bad.

Upvotes: 0

Views: 85

Answers (1)

MrMythical
MrMythical

Reputation: 9041

This is because you have message.reply in a for...of loop.

for (const invite of sortedInvites) {
                const count = inviteCounter[invite]
                replyText += `\n${invite} has invited ${count} member(s)!`

                const embed = new Discord.MessageEmbed()
                embed.setTitle('Invites: ')
                embed.setDescription(replyText)
                message.reply(embed) //here
            }

You probably meant to put it outside of the loop so that it sends the resulting embed after all the iterations.

Upvotes: 1

Related Questions