Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

319
Views
DiscordJS: Why is the bot sending more messages as I run the command multiple times?

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

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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.

about 4 years ago · Juan Pablo Isaza Report

0

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!