DiscordAPIError: Invalid Form Body data.embeds[0].description: This field is required Error

Viewed 2934

I'm trying to make a command that sends an embed. But for some reason, it keeps saying DiscordAPIError: Invalid Form Body data.embeds[0].description: This field is required Error even tho the description isn't whitespace.

I'm hosting it in heroku so the error is from the logs.

Command Script:

module.exports = {
    name: "test",
    desc: "A test command",
    cmd: async (interaction, utils) => {
        await interaction.user.fetch()
            .then(async ({ hexAccentColor }) => {
                interaction.reply({ embeds: [
                    utils.embed(
                        "Hello!",
                        interaction.member.displayAvatarURL(),
                        `Your name is <@${interaction.member.id}> and you're now in the special list! (jk)`,
                        "",
                        hexAccentColor
                    )
                ] })
            })
    }
}

Utils:

embed: async (title, thumbnail, desc, footer, color) => {
                const { MessageEmbed } = require("discord.js")

                await client.user.fetch()
                    .then(({ hexAccentColor }) => {
                        const embed = new MessageEmbed()
                            .setAuthor("GND Bot", client.user.avatarURL())
                            .setTitle(title)
                            .setColor(color || hexAccentColor)
                            .setDescription(desc)
                            .setThumbnail(thumbnail || client.user.avatarURL())
                            .setFooter(footer)
                        return embed
                    })
}

Error:

2021-11-08T10:20:12.649708+00:00 app[worker.1]: /app/node_modules/discord.js/src/rest/RequestHandler.js:349
2021-11-08T10:20:12.649731+00:00 app[worker.1]:       throw new DiscordAPIError(data, res.status, request);
2021-11-08T10:20:12.649732+00:00 app[worker.1]:             ^
2021-11-08T10:20:12.649732+00:00 app[worker.1]: 
2021-11-08T10:20:12.649732+00:00 app[worker.1]: DiscordAPIError: Invalid Form Body
2021-11-08T10:20:12.649733+00:00 app[worker.1]: data.embeds[0].description: This field is required
2021-11-08T10:20:12.649733+00:00 app[worker.1]:     at RequestHandler.execute (/app/node_modules/discord.js/src/rest/RequestHandler.js:349:13)
2021-11-08T10:20:12.649734+00:00 app[worker.1]:     at processTicksAndRejections (node:internal/process/task_queues:96:5)
2021-11-08T10:20:12.649734+00:00 app[worker.1]:     at async RequestHandler.push (/app/node_modules/discord.js/src/rest/RequestHandler.js:50:14)
2021-11-08T10:20:12.649735+00:00 app[worker.1]:     at async CommandInteraction.reply (/app/node_modules/discord.js/src/structures/interfaces/InteractionResponses.js:98:5) {
2021-11-08T10:20:12.649735+00:00 app[worker.1]:   method: 'post'
3 Answers

I found the problem. I was returning the embed to the callback in the .then() so it was returning undefined.

I was using the EmbedBuilder like this:

const createEmbed = () =>  {
 const result = new EmbedBuilder()
        .setColor(0x0099FF)
        .setTitle('Some title')
        .setURL('https://discord.js.org/')
        .setAuthor({ name: 'Some name', iconURL: 'https://i.imgur.com/AfFp7pu.png', url: 'https://discord.js.org' })
        .setDescription('Some description here')
        .setThumbnail('https://i.imgur.com/AfFp7pu.png')
        .setTimestamp()
        .setFooter({ text: 'Some footer text here', iconURL: 'https://i.imgur.com/AfFp7pu.png' });

  return result;
}

and in another function:

let embed = createEmbed();                           
console.log(embed);
interaction.channel.send({embeds: [embed]});

However embed when printed to console looked like this:

EmbedBuilder {
  data: {
    color: 39423,
    title: 'Some title',
    description: 'Some description here'
    ...

So the actual embed was under the data property. So I added the embed.data to the code, like this:

let embed = createEmbed();                           
console.log(embed);
interaction.channel.send({embeds: [embed.data]});

When getting the embed object through a function, it calls an APIError when you call the function inside of the embeds array, you need to put the embed into a variable before sending it in a message.

Related