Search code examples
javascriptdiscord.js

Bot sending [object, Object] when trying to send a random embed


I am trying to make it so when I say a command (ex. "!embedzay") the bot will send a random embed

I have tried pretty much all solutions from other posts I found on how to send random embeds or how to fix the [object Object] thing but they just give errors and the bot crashesenter image description here.

I have managed to get sending a single embed and sending random texts to work but I can't get sending random embeds to work.

A heads up that I am very new to JavaScript so if this is a stupid question I am very sorry. I am using JavaScript and node.js. If you need more information that what is below, please just tell me.

here is my code for random text that works:

//rtest - random test +
    if (command === 'rtest'){
        const options = [
            "message1",
            "message2",
            "message3",
        ]

        const random = options[Math.floor(Math.random() * options.length)]

        message.channel.send(`${random}`)
    }

my code for embeds that works:

//embed thingy - send and embed +
    if (command === 'embedz'){
        
        message.channel.send({
            embeds: [new EmbedBuilder()
                .setTitle('Some title')
                .setImage('https://media.discordapp.net/attachments/1044759244861345862/1045783400193204335/ezgif.com-gif-maker_1.gif')],
        });
    }

...And my code for random embeds that won't work:

attempt one - gives [object Object] error

    if (command === 'rtest'){
        const options = [
            {embeds: new EmbedBuilder().setTitle('rtfhgfh')},
            "message2",
            "message3",
        ]

        const random = options[Math.floor(Math.random() * options.length)]

        message.channel.send(`${random}`)
    }

attempt two - gives cannot send empty message error

// earlier embed attempt - cannot send empty message
    if (command === 'embedzay'){
        const embed3 = new EmbedBuilder()
        .setTitle('rtfhgfh')
        .setDescription('yhhfggf')

        const embed4 = new EmbedBuilder()
        .setTitle('haha')
        .setDescription('bunny')

        var embedArr = [embed3, embed4];
        let randomEmbed = embedArr[Math.floor(Math.random() * embedArr.length)];
        message.channel.send(randomEmbed);
    }

attempt three - back to [object Object] error

    if (command === 'embedzy'){

        const embed1 = new EmbedBuilder()
        .setTitle('Some title')
        .setImage('https://media.discordapp.net/attachments/1044759244861345862/1045783400193204335/ezgif.com-gif-maker_1.gif')

        const embed2 = new EmbedBuilder()
        .setTitle('Some titleuhhhhhh')
        .setImage('https://i.imgur.com/64l7KFc.png');

        const embeds = [embed1, embed2]

        const random = embeds[Math.floor(Math.random() * embeds.length)]

        message.channel.send(`${random}`)
    }

///

attempt four - kinda works? Using "const myJSON = JSON.stringify();" the correct way this time it kinda works but stuff shows up as "{"title":"Some titleuhhhhhh","image":{"url":"https://i.imgur.com/64l7KFc.png%22%7D%7D"

    if (command === 'embedzye'){

        const embed1 = new EmbedBuilder()
        .setTitle('Some title')
        .setImage('https://media.discordapp.net/attachments/1044759244861345862/1045783400193204335/ezgif.com-gif-maker_1.gif')

        const embed2 = new EmbedBuilder()
        .setTitle('Some titleuhhhhhh')
        .setImage('https://i.imgur.com/64l7KFc.png');

        const embeds = [embed1, embed2]

        const random = embeds[Math.floor(Math.random() * embeds.length)]

        message.channel.send(`${JSON.stringify(random)}`)
    }

attempt five - yes it works!!

    if (command === 'embedzyel'){

        const embed1 = new EmbedBuilder()
        .setTitle('Some title')
        .setImage('https://media.discordapp.net/attachments/1044759244861345862/1045783400193204335/ezgif.com-gif-maker_1.gif')

        const embed2 = new EmbedBuilder()
        .setTitle('Some titleuhhhhhh')
        .setImage('https://i.imgur.com/64l7KFc.png');

        const embeds = [embed1, embed2]

        const random = embeds[Math.floor(Math.random() * embeds.length)]


        message.channel.send({embeds: [random],});

    }

Solution

  •         message.channel.send({
                embeds: [new EmbedBuilder()
                    .setTitle('Some title')
                    .setImage('https://media.discordapp.net/attachments/1044759244861345862/1045783400193204335/ezgif.com-gif-maker_1.gif')],
            });
    

    This works because you send your embed packed in "embeds" object.

    Edit your code and add "embeds" object.

    const random = embeds[Math.floor(Math.random() * embeds.length)]
    
    
    message.channel.send({
            embeds: [random],
    });