You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用.addNumberOption创建Slash命令时遇类型错误求助

问题解决:Slash命令NumberOption类型不匹配错误

错误原因

你遇到的Option "minutes" is of type: 10; expected 3错误,核心是获取选项值的方法与选项类型不匹配:

  • 你用.addNumberOption定义了minutes为数值类型(对应类型标识10),但在execute方法里却用options.getString("minutes")获取——这个方法仅适用于字符串类型选项(对应类型标识3),因此触发类型校验错误。

修正方案

把获取minutes的方法替换为options.getNumber("minutes"),还可以根据需求给数值选项添加范围或整数限制:

修正后的代码片段:

data: new SlashCommandBuilder()
  .setName('maintenancemc-staff')
  .setDescription('Active ou désactive le mode maintenance du serveur Minecraft')
  .addStringOption((option) =>
    option
      .setName('raison')
      .setDescription('Raison du changement de statut')
      .setRequired(true)
  )
  .addNumberOption((option) =>
    option
      .setName('minutes')
      .setDescription("Décompte d'activation du mode maintenance en minute")
      .setRequired(true)
      .setInteger(true) // 可选:强制用户输入整数
      .setMinValue(1) // 可选:限制最小分钟数
      .setMaxValue(1440) // 可选:限制最大分钟数(比如一天)
  ),
async execute(interaction, client) {
  await interaction.deferReply({ fetchReply: true, ephemeral: true });
  const { options } = interaction; 
  const raison = options.getString("raison");
  const timer = options.getNumber("minutes"); // 替换为getNumber方法
  // 后续可按需处理数值,比如转整数:const minutes = Math.floor(timer);
...

内容的提问来源于stack exchange,提问作者Maxence

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 04:05:18