Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

124
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda