Whenever I react to an embed, It sends double messages. DISCORDJS

I have a waifu command where it gets an image and a name and puts it in a embed, it also then reacts with the :sparkling_heart: emoji. I wanted to make it so the first person who clicked the emoji would claim the waifu.

const { Client, MessageEmbed, ReactionCollector} = require('discord.js');
const {
    prefix
} = require('../../config');
const superagent = require('superagent');
const {
    urlencoded
} = require('body-parser');


module.exports = {
    name: 'waifu',
    category: 'waifu',
    description: 'Random Waifu',
    usage: `${prefix}waifu`,
    perms: 'Send Messages',
    cooldown: 5,
    run: async (bot, message, args) => {

        const rating1 = 10
        const rating2 = Math.floor(Math.random() * rating1)
        var rating = rating2

        const decimals1 = 100
        const decimals2 = Math.floor(Math.random() * decimals1)
        var decimals = decimals2

        const compatibility1 = 100
        const compatibility2 = Math.floor(Math.random() * compatibility1)
        var compatibility = compatibility2

        const {
            waifuID
        } = require("../../Database/WaifuNameDB.json")

        let randW = Math.floor(Math.random() * Object.keys(waifuID).length)
        let randomWaifu = waifuID[randW]

        let embed2 = new MessageEmbed()
            .setTitle(`🎀${randomWaifu.names}🎀`)
            .addField("Claims:", `${randomWaifu.claims}`)
            .addField("Rating:", `${rating}.${decimals}/10 ⭐`)
            .setImage(`${randomWaifu.img_url}`, innerHeight = '500', innerWidth = '500')
            .setColor('#f095d1')
            .setFooter(`| Powered by: @Twintails🎀 API `, `https://64.media.tumblr.com/1a1c3bcc08b5a048b90139a56fe7f415/tumblr_o9ku1rVS8z1vnqjx7o2_250.png`)

        var mg = await message.channel.send(embed2);
        
        mg.react('💖')

        message.delete()

        bot.on('messageReactionAdd', async (reaction, user) => {
            if (reaction.mg) await reaction.mg.fetch();
            if (reaction) await reaction.fetch()
            if (user.bot) return;
            if (reaction.emoji.name == "💖") {

                message.channel.send(`${randomWaifu.names} was claimed!`)

                mg.delete()
            if(user = message.author) return;

            }
        })
        
    }   
}

It works, but if for example I do /waifu, it sends the embed and says (waifuname) was claimed, but whenever I do /waifu again, everything is the same, but when I click the react button, it says that the previous and the current waifu was claimed.

(Umaru Doma was the previous waifu I rolled)

It also gives an error:
(node:9964) UnhandledPromiseRejectionWarning: DiscordAPIError: Unknown Message

Please help if you can!

I think it has something to do with the message.delete().

didn’t seem to fix anything

Try replacing mg.delete() with message.delete().

1 Like

Didn’t work :c

your var mg = await message.channel.send(embed2); is not set up correctly and in turn is using message.channel.send(embed2) witch will resend your embed twice. hope this helped and sorry this is late

1 Like

omg, thanks!!!

How would I fix this? What’s wrong with the mg?

Okay here JavaScript comes, so what’s the problem? Like what is current behavior and what’s the expected behavior?

the only reason I know this is because I ran into the same problem in my code. Im still getting help form some of my friends and I will respond when I get a responce. Sorry about that. I will respond when I get answer.

try to put the var witch I would suggest changing to let or const somewhere else like in a if statment. my code that I am having a problem with I cant move anything because im also using a embed but cant move the embed for that will cause another problem and I cant move my mg react because it is on a .then so if you are able to move it into a if statment that should fix it. if not try adding it to a else statment. Hope this helped

Ok, thanks, ill try that.

Another way to fix I would to just get ride of the code that sends the embed in the first place and use your var mg = await message.channel.send(embed2); for both sending the embed and then also making it a var agin you should change this to let or const.

1 Like

I changed most of my code, so can you tell me what to remove and what to keep?

const {
    Client,
    MessageEmbed,
    ReactionCollector
} = require('discord.js');
const config = require('../../config.json')
const talkedRecently = new Set();


module.exports = {
    name: 'waifus',
    category: 'waifu',
    description: 'Random Waifu',
    usage: `${config.prefix}waifu`,
    perms: 'Send Messages',
    aliases: ['w', 'waifus'],
    cooldown: 5,
    run: async (bot, message, args) => {

        try {

        if (talkedRecently.has(message.author.id)) {
            message.channel.send("**Wait 3 seconds.**")
        } else {

            const rating1 = 10
            const rating2 = Math.floor(Math.random() * rating1)
            var rating = rating2

            const decimals1 = 100
            const decimals2 = Math.floor(Math.random() * decimals1)
            var decimals = decimals2

            const {
                waifuID
            } = require("../../Database/WaifuNameDB.json")

            let randW = Math.floor(Math.random() * Object.keys(waifuID).length)
            let randomWaifu = waifuID[randW]

            let embed2 = new MessageEmbed()
                .setTitle(`🎀${randomWaifu.names}🎀`)
                .addField("Claims:", `${randomWaifu.claims}`)
                .addField("Rating:", `${rating}.${decimals}/10 ⭐`)
                .addField("Anime:", `${randomWaifu.anime}`)
                .setImage(`${randomWaifu.img_url}`, innerHeight = '500', innerWidth = '500')
                .setColor("#f089e4")
                .setFooter(`| Powered by: @Twintails🎀 API `, `https://64.media.tumblr.com/1a1c3bcc08b5a048b90139a56fe7f415/tumblr_o9ku1rVS8z1vnqjx7o2_250.png`)

            var mg = await message.channel.send(embed2);

            mg.react('💖')

            bot.on('messageReactionAdd', (messageReaction, user) => {

                if (user.bot) return;
                const {
                    message,
                    emoji
                } = messageReaction;

                if (emoji.name === "💖") {
                    if (!message.id === mg.id) return;
                    if (message.id === mg.id) {

                        let claimarray = [
                            `**${user.username}** claimed **${randomWaifu.names}**! 💕`,
                            `**${user.username}** drove off with **${randomWaifu.names}**! 🚗`,
                            `**${user.username}** loves **${randomWaifu.names}**! 🎆`,
                            `💖**${user.username}** and **${randomWaifu.names}** are now married! 💖`
                        ]

                        let claim = Math.floor(Math.random() * claimarray.length)
                        let randomclaim = claimarray[claim]

                        mg.delete()
                        message.channel.send(randomclaim)
                        if(messageReaction.count < 2) return; 
                    }
                }
            });

            talkedRecently.add(message.author.id);
            setTimeout(() => {
                // Removes the user from the set after a minute
                talkedRecently.delete(message.author.id);
            }, 3000);
        }

    } catch (err) {
        console.log(null)
      }


    }
}

add await in front of mg.react(‘:sparkling_heart:’) and also go into all of your code it looks like you have more than one folder and fine were u send the embed2 for the first time. because if your getting 2 responses your getting one from you await and one from something else and just altogether get rid of the first one

you haven’t said if it worked or not so im going to explain a little more. This is my code.
channel.send( <@&738598824385445938> <@&738597461597356053> \n ${message.author} application, exampleEmbed);
and after that is my
var msg = await message.channel.send( <@&738598824385445938> <@&738597461597356053> \n ${message.author} application, exampleEmbed); and after that is my msg.react(’’)
What i ended up doing is removing my channel.send( <@&738598824385445938> <@&738597461597356053> \n ${message.author} application , exampleEmbed);
and kept my message await witch also sent the message. the only problem was then my reaction wouldn’t send so in front of your mg.react you need to add await mg.react witch should send it and fix your code. sorry that this is so long and I hoped it worked. -Carson

I haven’t got to testing it out yet but thanks!