Message loops the same amount of times that the message reaction has been pressed

Viewed 30

I am working on a marriage system on my bot and I have encountered an odd bug while testing it. For context there is a "marry" and "divorce" command. When the user accepts the initial proposal everything works as intended, i.e. the bot updates the mongodb "spouse" section and the message embed announcing the marriage pops up in the channel. The bug occurs when a "married" user "divorces" someone and then "re-marries" so when they react to the "✅" to a new marriage proposal, the bot will send the "Congratulations embed message" for as many time as they have clicked it before.

So let's say a user gets married, then divorced, then married again. The bot will send the "you're married now" embed twice, since the user has clicked the ✅ twice in total.

How do I fix my code so it will no longer loop the message for the amount of times in total they have reacted to the ✅?

client.on('messageReactionAdd', async (reaction, user) => {
    if (reaction.message.partial) await reaction.message.fetch();
    if (reaction.partial) await reaction.fetch();
    if (user.bot) return;
    if (!reaction.message.guild) return;
    if (reaction.emoji.name === '✅') {
        await reaction.message.guild.members.cache.get(user.id).roles.add(marriedRole);
        await reaction.message.guild.members.cache.get(user.id).roles.remove(singleRole);
        await reaction.message.guild.members.cache.get(message.author.id).roles.add(marriedRole);
        await reaction.message.guild.members.cache.get(message.author.id).roles.remove(singleRole);
        let embed = new Discord.MessageEmbed()
            .setDescription(`**${user}** & **${message.author}** are now married!`)
            .setImage('https://64.media.tumblr.com/604a79e043662b7dc556919c502718df/tumblr_p0dsd3yYos1uxvvvzo1_500.gifv')
        messageEmbed = await message.channel.send({ embeds: [embed] });
        const response = await profileModel.replaceOne(
            { userID: message.author.id, spouse: null }, { userID: message.author.id, spouse: user.id },
        )
        const response2 = await profileModel.replaceOne(
            { userID: user.id, spouse: null }, { userID: user.id, spouse: message.author.id }
        )
    }
    if (reaction.emoji.name === '❌') {
        let embed = new Discord.MessageEmbed()
            .setDescription(`Sorry **${user}**, does not want to marry you.`);
        let messageEmbed = await message.channel.send({ embeds: [embed] });
    }
})
0 Answers
Related