Search code examples
javascriptasync-awaitbotsembeddiscord.js

Bot doesn't wait for reaction and also he doesn't send message when collected because of Emded error


So my bot should wait for a reaction of the user and then he should send a message, sometimes he waits (i think because of the internet connection),but mostly not as you can see in this screenshot: https://ibb.co/9gmfcr8 what is a problem, but even when i would suggest something, then he doesn't send the message to the specific channel, instead i get the error: RangeError [EMBED_FIELD_VALUE]: MessageEmbed field values may not be empty./UnhandledPromiseRejectionWarning: RangeError [EMBED_FIELD_VALUE]: MessageEmbed field values may not be empty. Idk why he doesn't wait for the reaction or why he wouldn't send the suggestion. I would be thankful for any advice/solution.

module.exports = {
    name: 'suggest',
    aliases: ['sug', 'suggestion'],
    description: 'Suggest something for the Bot',
    async execute(message, client, args) {
        try {
        const { MessageEmbed } = require('discord.js');

        const Embed = new MessageEmbed()
            .setColor('0x0099ff')
            .setDescription(`Suggestion categories`)
            .addField(`For what you want to suggest something?`, `\nA: I want to suggest something for the Website/Servers/Discord Server\nB: I want to suggest something for the CloudX Bot \n\nPlease react to this message with A or B`)


        message.channel.send(Embed).then(async function (message) {
            await message.react("🇦")
            await message.react("🇧")
            const filter = (reaction, user) => {
                return ['🇦', '🇧'].includes(reaction.emoji.name) && user.id;
        }

        await message.awaitReactions(filter, { max: 1 })
            .then(async collected => {
                const reaction = collected.first();

                if (reaction.emoji.name === '🇦') {
                    const filter = m => m.author.id === message.author.id;

                    await message.channel.send(`Please provide a suggestion for the Website/Servers/Discord Server or cancel this command with "cancel"!`)

                    message.channel.awaitMessages(filter, { max: 1, })
                        .then(async (collected) => {
                            try{
                            if (collected.first().content.toLowerCase() === 'cancel') {
                                message.reply("Your suggestion has been cancelled.")
                            }
                            else {
                                const embed1 = new MessageEmbed()
                                    .setColor('0x0099ff')
                                    .setAuthor(message.author.tag)
                                    .addField(`New Suggestion:`, `${collected.first().content}`)
                                    .setFooter(client.user.username, "attachment://CloudX.png")
                                    .setTimestamp();

                                const channel = await client.channels.fetch("705781201469964308")
                                await channel.send({embed: embed1, files: [{
                                    attachment:'CloudX.png',
                                    name:'CloudX.png'
                                }]})

                                await message.channel.send(`Your suggestion has been filled to the staff team. Thank you!`)
                                }
                            } catch (error) {
                                console.log(error)
                            } 
                        })
                }
                if (reaction.emoji.name === '🇧') {
                    const filter = m => m.author.id === message.author.id;

                    await message.channel.send(`Please provide a suggestion for the CloudX Bot or cancel this command with "cancel"!`)

                    message.channel.awaitMessages(filter, { max: 1, })
                        .then(async (collected) => {
                            try{
                            if (collected.first().content.toLowerCase() === 'cancel') {
                                message.reply("Your suggestion has been cancelled.")
                            }
                            else {
                                const embed2 = new MessageEmbed()
                                    .setColor('0x0099ff')
                                    .setAuthor(message.author.tag)
                                    .addField(`New Suggestion:`, `${collected.first().content}`)
                                    .setFooter(client.user.username, "attachment://CloudX.png")
                                    .setTimestamp();

                                const channel = await client.channels.fetch("702825446248808519")
                                await channel.send({embed: embed2, files: [{
                                    attachment:'CloudX.png',
                                    name:'CloudX.png'
                                }]})

                                await message.channel.send(`Your suggestion has been filled to the staff team. Thank you!`)
                            }
                            } catch (error) {
                            console.log(error)
                            } 
                        })
                }    
            })
        })
        } catch (error) {
            console.log(error)
        }
    },
    catch(err) {
        console.log(err)
    }
};

Solution

  • This code should work for what I assume you want,

    Tested and working as of : Tue May 5 2:52 AM

        const { MessageEmbed } = require('discord.js')
    
    
        // call the lord EmojiMessageMan to handle reactions
        async function emojiMessageMan(message, validReactions) {
    
            // validReactions = ["one", "two"]
            for (const reaction of validReactions) await message.react(reaction);
    
            // filter
            const filter = (reaction, user) => validReactions.includes(reaction.emoji.name) && (!user.bot)
    
            // returns emoji the user reacted with
            return message
                .awaitReactions(filter, {
                    max: 1,
                })
                .then(collected => collected.first() && collected.first().emoji.name);
        }
    
        // define embeds
        const mainEmbed = new MessageEmbed()
            .setColor('0x0099ff')
            .setDescription(`Suggestion categories`)
            .addField(`For what you want to suggest something?`, `\nA: I want to suggest something for the Website/Servers/Discord Server\nB: I want to suggest something for the CloudX Bot \n\nPlease react to this message with A or B`)
    
    
        // sends mainEmbed and await for reactions
    
        let mainEmbedMsg = "";
        await message.channel.send(mainEmbed).then((message) => {
            mainEmbedMsg = message
        })
        let mainEmbedEmoji = await emojiMessageMan(mainEmbedMsg, ["🇦", "🇧"])
        if (mainEmbedEmoji == "🇦") {
            let promptMessage = await message.channel.send(`Please provide a suggestion for the Website/Servers/Discord Server or cancel this command with "cancel"!`)
    
            const Messagefilter = m => m.author.id === message.author.id;
            var userResponse;
            var userMsg;
            await message.channel.awaitMessages(Messagefilter, { max: 1, }).then((collected) => {
                userResponse = collected.first().content.toLowerCase()
                userMsg = collected.first()
            })
    
            if (userResponse == "cancel") {
                message.channel.send("Your Suggestion Has Been Cancelled").then((msg) => {
                    msg.delete(5000)
                })
                mainEmbedMsg.delete()
                userMsg.delete()
                message.delete()
                promptMessage.delete()
                return;
            }
    
    
            const bigA = new MessageEmbed()
                .setColor('0x0099ff')
                .setAuthor(message.author.tag)
                .addField(`New Suggestion:`, `${userMsg}`)
                .setFooter(client.user.username, "attachment://CloudX.png")
                .setTimestamp();
    
            const channel = await client.channels.fetch("705781201469964308")
    
            await channel.send({
                embed: bigA,
                files: [{
                    attachment: 'CloudX.png',
                    name: 'CloudX.png'
                }]
            })
    
            await message.channel.send(`Your suggestion has been filled to the staff team. Thank you!`).then((msg) => {
                msg.delete(5000)
            })
    
        } else if (mainEmbedEmoji == "🇧") {
    
            let promptMessage = await message.channel.send(`Please provide a suggestion for the CloudX Bot or cancel this command with "cancel"!`)
            const Messagefilter = m => m.author.id === message.author.id;
    
            var userResponse;
            var userMsg;
            await message.channel.awaitMessages(Messagefilter, { max: 1, }).then((collected) => {
                userResponse = collected.first().content.toLowerCase()
                userMsg = collected.first()
            })
    
            if (userResponse == "cancel") {
                message.channel.send("Your Suggestion Has Been Cancelled").then((msg) => {
                    msg.delete(5000)
                })
                mainEmbedMsg.delete()
                userMsg.delete()
                message.delete()
                promptMessage.delete()
                return;
            }
    
            const bigB = new MessageEmbed()
                .setColor('0x0099ff')
                .setAuthor(message.author.tag)
                .addField(`New Suggestion:`, `${userResponse}`)
                .setFooter(client.user.username, "attachment://CloudX.png")
                .setTimestamp();
    
            const channel = await client.channels.fetch("702825446248808519")
    
            await channel.send({
                embed: bigB,
                files: [{
                    attachment: 'CloudX.png',
                    name: 'CloudX.png'
                }]
            })
            await message.channel.send(`Your suggestion has been filled to the staff team. Thank you!`).then(msg => {
                msg.delete(5000)
            })
    
        }
    }