Search code examples
javascriptnode.jsdiscord.js

Discord.JS v14 SlashCommandBuilder Subcommand Function is giving an err


I created a command using the SlashCommandBuilder class in discord.js v14 but when I try to add a Subcommand I get this error. If anyone has any information it would be nice.

My Code:

.addSubcommand(subcommand => {
    subcommand
        .setName("channel")
        .setDescription("Sets the channel to send earthquake logs.")
        .addChannelOption(option => {
            option
                .setName("channel")
                .setDescription("Channel to send earthquake logs.")
                .setRequired(true)
        })
})

I'm trying to add subcommand but it gives me this err

/Users/yscnpkr/Desktop/mira v2/node_modules/@sapphire/shapeshift/dist/index.js:41 
  throw this.error; 

ExpectedValidationError: Expected 
  at InstanceValidator.handle (/Users/yscnpkr/Desktop/mira v2/node_modules/@sapphire/shapeshift/dist/index.js:714:75) 
  at InstanceValidator.parse (/Users/yscnpkr/Desktop/mira v2/node_modules/@sapphire/shapeshift/dist/index.js:201:88) 
  at assertReturnOfBuilder (/Users/yscnpkr/Desktop/mira v2/node_modules/@discordjs/builders/dist/index.js:942:53) 
  at MixedClass._sharedAddOptionMethod (/Users/yscnpkr/Desktop/mira v2/node_modules/@discordjs/builders/dist/index.js:1347:51 
  at MixedClass.addChannelOption (/Users/yscnpkr/Desktop/mira v2/node_modules/@discordjs/builders/dist/index.js:1323:171 
  at /Users/yscnpkr/Desktop/mira v2/src/commands/deprem.js:20:26 
  at MixedClass.addSubcommand (/Users/yscnpkr/Desktop/mira v2/node_modules/@discordjs/builders/dist/index.js:1362:501 
  at /Users/yscnpkr/Desktop/mira v2/src/commands/deprem.js:14:18 
  at MixedClass.addSubcommandGroup (/Users/yscnpkr/Desktop/mira v2/node_modules/@discordjs/builders/dist/index.js:1441:501 
  at Object.<anonymous> (/Users/yscnpkr/Desktop/mira v2/src/commands/deprem.js:8:101 { 
 validator: 's.instance(V)', 
 given: undefined, 
 expected: [Function: SlashCommandChannelOption] 

Error: Console Error is here


Solution

  • If you check the error it says expected: [Function: SlashCommandChannelOption] but given: undefined. It means it expected an instance of SlashCommandChannelOption but received undefined.

    If you check your callback functions, you can see that you don't return anything. If there is no explicit return statement, the function automatically returns undefined.

    So, all you need to do is to return options:

    .addSubcommand((subcommand) => {
      return subcommand
        .setName('channel')
        .setDescription('Sets the channel to send earthquake logs.')
        .addChannelOption((option) => {
          return option
            .setName('channel')
            .setDescription('Channel to send earthquake logs.')
            .setRequired(true);
        });
    });
    

    As you're using arrow functions, you could also just get rid of the curly brackets and the return keyword:

    .addSubcommand((subcommand) =>
      subcommand
        .setName('channel')
        .setDescription('Sets the channel to send earthquake logs.')
        .addChannelOption((option) =>
          option
            .setName('channel')
            .setDescription('Channel to send earthquake logs.')
            .setRequired(true),
        ),
    );