Home > database >  DiscordJS: Why is the bot sending more messages as I run the command multiple times?
DiscordJS: Why is the bot sending more messages as I run the command multiple times?

Time:06-24

The code below is wrapped in a class that gets called when specific commands is ran, for example !dm. When I run !dm after starting the bot, it normally sends a message and waits for the user to reply back to the sent message(code below). When I run !dm the second time, it sends a message two times. If I run !dm once more, it sends the message 3 times and so on... Why is this happening and how can I fix it?

this.client.on("messageCreate", function (msg) {
            if (msg.author.bot || msg.channel.type !== "DM") return
            console.log("something")
})

(This is the shortened code)

Full code: PasteHub

CodePudding user response:

I think that would be the default behaviour, because the bot will always be listening to the messageCreate event. I think you would need some kind of state for the bot, for eg:

If user1 typed !DM, you would store that info somewhere, like:

const botStates = [];

// Message event closure {
botStates.push({
    user: user.name, // example
    bot_state: 'waiting',
})
// }

And then you could check that state each time the event triggers the closure, and if the state for that user is waiting, you would not send the message.

CodePudding user response:

This might be due to you storing two client events inside of the each other.

Make sure that you are not storing your messageCreate inside of another event.

  • Related