【问题标题】:The specified component type is invalid in this context - discord.js V13指定的组件类型在此上下文中无效 - discord.js V13
【发布时间】:2021-08-09 02:40:40
【问题描述】:

我正在尝试将按钮附加到我的测试嵌入的底部,但是我收到了错误。

导致错误的代码:

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: []
  }
}

【问题讨论】:

    标签: node.js discord.js


    【解决方案1】:

    MessageOptions#components 接受 MessageActionRow 的数组,而不是 MessageButton

    您还需要使用 MessageButton#setCustomId 为按钮设置自定义 ID。


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

    【讨论】:

      猜你喜欢
      • 2021-12-12
      • 1970-01-01
      • 2021-12-25
      • 2018-05-07
      • 2020-04-16
      • 2021-11-09
      • 2016-03-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多