Указанный тип компонента недействителен в данном контексте — discord.js V13

avatar
Tyler2P
8 августа 2021 в 18:23
1437
1
-1

Я пытаюсь прикрепить кнопки к нижней части тестовой вставки, но получаю сообщение об ошибке.

Код, вызывающий ошибку:

module.exports = {
    name: 'test',
    description: 'A simple test command for MessageButtons',
    /**
     * @param {object} message The message that was sent
     * @param {string} prefix The servers prefix
     * @param {Client} client The bots client
     */
    async execute(message, _prefix, client) {
        const { MessageEmbed, MessageButton } = require('discord.js');

        let button1 = new MessageButton();
        button1.setLabel('Yes');
        button1.setStyle('PRIMARY');

        let button2 = new MessageButton()
        button2.setLabel('No');
        button2.setStyle('PRIMARY');

        let embed = new MessageEmbed()
            .setDescription('This is a test embed');

        message.channel.send({ embeds: [embed], components: [button1, button2] });
    }
}

Ошибка:

D:\Projects\Test\node_modules\discord.js\src\rest\RequestHandler.js:298
      throw new DiscordAPIError(data, res.status, request);
            ^

DiscordAPIError: Invalid Form Body
components[0]: The specified component type is invalid in this context
components[1]: The specified component type is invalid in this context
    at RequestHandler.execute (D:\Projects\Test\node_modules\discord.js\src\rest\RequestHandler.js:298:13)
    at runMicrotasks (<anonymous>)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async RequestHandler.push (D:\Projects\Test\node_modules\discord.js\src\rest\RequestHandler.js:50:14)
    at async TextChannel.send (D:\Projects\Test\node_modules\discord.js\src\structures\interfaces\TextBasedChannel.js:171:15)
    at async Object.execute (D:\Projects\Test\commands\test.js:14:13) {
  method: 'post',
  path: '/channels/826774490650247178/messages',
  code: 50035,
  httpStatus: 400,
  requestData: {
    json: {
      content: undefined,
      tts: false,
      nonce: undefined,
      embeds: [
        {
          title: null,
          type: 'rich',
          description: 'This is a test embed',
          url: null,
          timestamp: 0,
          color: null,
          fields: [],
          thumbnail: null,
          image: null,
          author: null,
          footer: null
        }
      ],
      components: [
        {
          custom_id: null,
          disabled: false,
          emoji: null,
          label: 'Yes',
          style: 1,
          type: 2,
          url: null
        },
        {
          custom_id: null,
          disabled: false,
          emoji: null,
          label: 'No',
          style: 1,
          type: 2,
          url: null
        }
      ],
      username: undefined,
      avatar_url: undefined,
      allowed_mentions: undefined,
      flags: undefined,
      message_reference: undefined,
      attachments: undefined,
      sticker_ids: undefined
    },
    files: []
  }
}
Источник

Ответы (1)

avatar
Jakye
8 августа 2021 в 18:40
2

MessageOptions#components accepts an array of MessageActionRow, not MessageButton.

Вам также необходимо установить пользовательский идентификатор для кнопки, используя MessageButton#setCustomId.


module.exports = {
    name: "test",
    description: "A simple test command for MessageButtons",
    /**
     * @param {object} message The message that was sent
     * @param {string} prefix The servers prefix
     * @param {Client} client The bots client
     */
    async execute(message, _prefix, client) {
        const { MessageEmbed, MessageButton, MessageActionRow } = require("discord.js");

        let button1 = new MessageButton();
        button1.setCustomId("PRIMARY");
        button1.setLabel("Yes");
        button1.setStyle("PRIMARY");

        let button2 = new MessageButton();
        button2.setCustomId("PRIMARY2");
        button2.setLabel("No");
        button2.setStyle("PRIMARY");

        const row = new MessageActionRow().addComponents([button1, button2]);

        let embed = new MessageEmbed().setDescription("This is a test embed");

        message.channel.send({ embeds: [embed], components: [row] });
    },
};