【问题标题】:Discord Slash Command: Error while sending EmbedsDiscord Slash 命令:发送嵌入时出错
【发布时间】:2021-06-28 15:26:34
【问题描述】:

我正在为我的机器人创建一个斜杠命令。所以我尝试了这个

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

这很好用。
所以我尝试发送一个嵌入并在下面尝试这些(2)代码

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

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

这些都不起作用。

那么我做错了什么?
或者,如何发送带有斜杠的嵌入命令?

编辑:这就是我定义exampleEmbed的方式

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

【问题讨论】:

    标签: javascript node.js discord discord.js


    【解决方案1】:

    它接受一个嵌入数组,称为embeds 属性。

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

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

    【讨论】:

      【解决方案2】:

      对于任何使用我使用的 discord.js API 的人:

          const embed = new MessageEmbed().setTitle('testing');
      
          const messageId = await interaction.reply({ embeds: [ embed ] });
      

      【讨论】:

        【解决方案3】:

        我认为您需要使用embeds 属性将embeds 作为数组发送到data 中,但根据文档“目前不支持所有消息字段。”

        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]
                }
            }})
        })
        

        【讨论】:

          【解决方案4】:

          对于使用 discord.js 进行嵌入的任何人,您必须在嵌入上设置 type 属性!否则你会得到一个错误 400。

          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]
                  }
              }})
          })
          

          【讨论】:

            猜你喜欢
            • 2021-05-05
            • 2021-02-05
            • 1970-01-01
            • 2021-04-06
            • 1970-01-01
            • 2019-04-19
            • 2022-11-28
            • 2020-10-09
            • 2023-01-30
            相关资源
            最近更新 更多