Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

118
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda