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

123
Views
My bot sending multiple messages, discord.js

To explain briefly, the code is to make already existing buttons after restart of the bot usable again, I store for this the channelId and the messageId. However, several messages are stored, therefore the foreach(), as long as only one message is stored, everything is fine when I then try to use a button, but if there are 2 or more messages, the message is sent more often, how do I fix this?

I hope anyone can help me <3

fs.readdir(folder, (err, files) => {
    files.forEach(file => {
        const r = fs.readFileSync(`${folder}/${file}`, { encoding: "utf-8" })
        const data = JSON.parse(r)

        const channel = client.channels.cache.get(data.channelId)
        channel.messages.fetch(data.messageId).then(async (message) => {
            const collector = message.channel.createMessageComponentCollector();

            collector.on('collect', async i => {
                if (i.customId === 'upvote') {
                    return i.channel.send({ content: 'A button was clicked!', components: [] });
                } else if (i.customId === 'downvote') {
                    return i.channel.send({ content: 'A button was clicked!', components: [] });
                }
            });
        })
    })
})
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You're running your token in several times. Try to reset your token and run it on one platform.

about 4 years ago · Juan Pablo Isaza Report

0

I would assume it's because you have the code executing inside the forEach, so possibly have some sort of array outside of it and push the messages to the array, and then try to send the message channel.

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!