NodeJS Discord.JS v14 SlashCommandBuilder子命令函数出现错误

ogq8wdun  于 2023-03-01  发布在  Node.js
关注(0)|答案(1)|浏览(220)

我在discord.js v14中使用SlashCommandBuilder类创建了一个命令,但是当我尝试添加一个子命令时,我得到了这个错误。如果有人有任何信息,那就太好了。
我的代码:

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

我尝试添加子命令,但出现此错误

/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]

错误:

dbf7pr2w

dbf7pr2w1#

如果您检查错误,它会显示expected: [Function: SlashCommandChannelOption],但实际上是given: undefined。这意味着它需要SlashCommandChannelOption的示例,但收到的却是undefined
如果你检查回调函数,你会发现你没有返回任何东西,如果没有显式的return语句,函数会自动返回undefined
因此,您需要做的就是返回选项:

.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);
    });
});

当你使用箭头函数时,你也可以去掉花括号和return关键字:

.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),
    ),
);

相关问题