Akio
Akio

Reputation: 723

Discord Slash Command: Error while sending Embeds

I was creating a slash command for my bot. So I tried this

Client.ws.on('INTERACTION_CREATE', async interaction => {
    Client.api.interactions(interaction.id, interaction.token).callback.post({data: {
        type: 4,
        data: {
            content: 'hello world!'
        }
    }})
})

And this works fine.
So I tried sending an embed with it and tried these (2) code below

Client.ws.on('INTERACTION_CREATE', async interaction => {
    Client.api.interactions(interaction.id, interaction.token).callback.post({data: {
        type: 4,
        data: {
            content: {
                embed: exampleEmbed
            }
        }
    }})
})

and

Client.ws.on('INTERACTION_CREATE', async interaction => {
    Client.api.interactions(interaction.id, interaction.token).callback.post({data: {
        type: 4,
        data: {
            embed: exampleEmbed
        }
    }})
})

And none of these works.

So what am I doing wrong ?
Or, How can I send an embed with slash command?

Edit: This is how I define exampleEmbed

const exampleEmbed = {
    color: 0x0099ff,
    title: 'Hello world',
    thumbnail: {
        url: 'https://i.imgur.com/wSTFkRM.png',
    },
    image: {
        url: 'https://i.imgur.com/wSTFkRM.png',
    }
};

Upvotes: 1

Views: 11890

Answers (4)

Mark Andrew
Mark Andrew

Reputation: 168

For anyone using the discord.js API I use:

    const embed = new MessageEmbed().setTitle('testing');

    const messageId = await interaction.reply({ embeds: [ embed ] });

Upvotes: 6

Glup3
Glup3

Reputation: 152

For anyone who is using discord.js for the embeds, you have to set the type property on the embed! You will get an error 400 otherwise.

import { MessageEmbed } from 'discord.js'

const exampleEmbed = new MessageEmbed({
    title: 'Error occurred',
    description: description,
    type: 'rich',
  });

Client.ws.on('INTERACTION_CREATE', async interaction => {
    Client.api.interactions(interaction.id, interaction.token).callback.post({data: {
        type: 4,
        data: {
            embeds: [exampleEmbed]
        }
    }})
})

Upvotes: 0

Zsolt Meszaros
Zsolt Meszaros

Reputation: 23161

I think you'll need to send embeds inside data as an array using the embeds property, but according to the docs "Not all message fields are currently supported."

Client.ws.on('INTERACTION_CREATE', async interaction => {
    Client.api.interactions(interaction.id, interaction.token).callback.post({data: {
        type: 4,
        data: {
            content: 'hello world!',
            embeds: [exampleEmbed]
        }
    }})
})

Upvotes: 1

PiggyPlex
PiggyPlex

Reputation: 703

It accepts an array of embeds, known as embeds property.

Client.ws.on('INTERACTION_CREATE', async interaction => {
    Client.api.interactions(interaction.id, interaction.token).callback.post({data: {
        type: 4,
        data: {
            embeds: [ exampleEmbed ]
        }
    }})
})

From https://discord.com/developers/docs/interactions/slash-commands#responding-to-an-interaction

Upvotes: 5

Related Questions