'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: [] });
                }
            });
        })
    })
})


Solution 1:[1]

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

Solution 2:[2]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Neenhila
Solution 2 crackheadakira