Search code examples
javascriptdiscorddiscord.js

DiscordJS v14 invalid form body


I have been trying to make a slash command with subcommand that sends message to specified channel. When i try to deploy it, i get:

DiscordAPIError[50035]: Invalid Form Body
options[1][APPLICATION_COMMAND_OPTIONS_TYPE_INVALID]: Sub-command and sub-command group option types are mutually exclusive to all other types
    at SequentialHandler.runRequest (D:\projs\cpp\tbot\node_modules\@discordjs\rest\dist\index.js:659:15)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async SequentialHandler.queueRequest (D:\projs\cpp\tbot\node_modules\@discordjs\rest\dist\index.js:458:14)
    at async REST.request (D:\projs\cpp\tbot\node_modules\@discordjs\rest\dist\index.js:902:22)
    at async D:\projs\cpp\tbot\deploy-commands.js:24:16 {
  requestBody: { files: undefined, json: [ [Object], [Object] ] },
  rawError: {
    code: 50035,
    errors: { options: [Object] },
    message: 'Invalid Form Body'
  },
  code: 50035,
  status: 400,
  method: 'PUT',
  url: 'https://discord.com/api/v10/applications/752621311494455367/guilds/1039125828790919240/commands'
}

My code:

const { SlashCommandBuilder } = require('discord.js');

const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({ intents: [GatewayIntentBits.Guilds] });

module.exports = {
    data: new SlashCommandBuilder()
        .setName('say')
        .setDescription('Make bot say anything the executor wants')
        .addSubcommand(subcommand =>
            subcommand
                .setName('channel')
                .setDescription('Says ')
             .addChannelOption(option =>
                  option
                    .setName('target_channel')
                    .setDescription('Channel to send message in.')
                    .setRequired(true)))
              .addStringOption(option =>
                option
                    .setName('text')
                    .setDescription("Message to be sent in specified channel")
                    .setRequired(true)),
    async execute(interaction) {
        const channel = interaction.options.getChannel('target_channel');
        const text = interaction.options.getString('text');

        client.channels.cache.get(channel).send(text);
    },
};

I have no clue about it. I didn't find anything about it anywhere. I expected it to deploy the commands via node deploy-commands.js but it resulted in error above.


Solution

  • The error is emitted when you attempt to add an option to a command which also has a subcommand. To illustrate this better I've create an ascii table, note how channel and text options are on the same level, which would result in an error.

    say
     |- channel: SUB_COMMAND
     |   |- name: STRING
     |   |- target_channel: TEXTCHANNEL
     |- text: STRING
     |   |- option: STRING
    

    If I've interpreted your code correctly you may want to add the string option to the subcommand, as so:

    data: new SlashCommandBuilder()
        .setName('say')
        .setDescription('Make bot say anything the executor wants')
        .addSubcommand(subcommand =>
            subcommand
                .setName('channel')
                .setDescription('Says ')
                .addStringOption(option =>
                    option
                        .setName('text')
                        .setDescription("Message to be sent in specified channel")
                        .setRequired(true)
                )
                .addChannelOption(option =>
                    option
                        .setName('target_channel')
                        .setDescription('Channel to send message in.')
                        .setRequired(true)
                )
        )