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

discord.js v14报错:interaction.options.getSubCommand不是函数求助

问题解决:discord.js v14中interaction.options.getSubCommand is not a function错误

核心问题分析

discord.js v14对命令交互的类型定义和处理逻辑做了调整,你的代码存在几个关键问题:

  • 类型判断方法调用错误:interaction.isChatInputCommand是方法,需加括号执行
  • 子命令类型定义不规范:使用数字而非官方枚举值,易引发版本兼容问题
  • 未提前校验子命令存在性:直接调用getSubCommand()可能触发无定义错误
  • 异步逻辑处理遗漏:部分异步操作未加await,还有代码细节错误

修正后的代码

1. interactionCreate.js

const { Client, ChatInputCommandInteraction } = require("discord.js");
module.exports = {
  name: "interactionCreate",
  /**
   * @param {Client} client
   * @param {ChatInputCommandInteraction} interaction
   */
  async execute(interaction, client) {
    if (interaction.isChatInputCommand()) {
      const command = client.commands.get(interaction.commandName);
      if (!command) {
        return interaction.reply({ content: "Error", ephemeral: true });
      }
      await command.execute(interaction, client);
    }
  }
};
  • 替换为更精准的ChatInputCommandInteraction类型
  • 移除无效的&& client.commands.delete(...)逻辑(return后代码不会执行)

2. soundeffect.js

const { ApplicationCommandType, ApplicationCommandOptionType, ChatInputCommandInteraction, Client } = require("discord.js");
module.exports = {
  name: "soundeffect",
  description: "Plays a sound effect in a voice channel.",
  type: ApplicationCommandType.ChatInput,
  options: [
    {
      type: ApplicationCommandOptionType.Subcommand,
      name: "bruh",
      description: "Plays the bruh sound effect"
    },
    {
      type: ApplicationCommandOptionType.Subcommand,
      name: "whatthedogdoing",
      description: "Plays the what the dog doing sound effect"
    },
    {
      type: ApplicationCommandOptionType.Subcommand,
      name: "ohmygod",
      description: "Plays the oh my god sound effect"
    }
  ],
  /**
   * @param {ChatInputCommandInteraction} interaction 
   * @param {Client} client
   */
  async execute(interaction, client) {
    if (!interaction.isChatInputCommand()) return;
    if (!interaction.member.voice.channelId) return await interaction.reply("You must be in a voice channel!");
    
    if (interaction.options.hasSubCommand()) {
      const subCommand = interaction.options.getSubCommand();
      if (subCommand === 'bruh') await require('./soundEffect/playBruhSound')(interaction);
    }
  }
};
  • 导入ApplicationCommandOptionType枚举,替换数字1为规范枚举值
  • 调用isChatInputCommand()时添加括号执行判断
  • 先通过hasSubCommand()校验子命令存在性,再获取子命令名称
  • 给子命令处理函数添加await确保异步逻辑完成

3. playBruhSound.js

const { ChatInputCommandInteraction } = require("discord.js");
const { joinVoiceChannel, createAudioPlayer, createAudioResource } = require('@discordjs/voice');
const path = require('path');

/**
 * @param {ChatInputCommandInteraction} interaction 
 */
module.exports = async (interaction) => {
  const player = createAudioPlayer();
  // 使用path.resolve确保音频文件路径正确
  player.play(createAudioResource(path.resolve(__dirname, '../files/movie_1.mp3')));

  joinVoiceChannel({
    channelId: interaction.member.voice.channelId,
    guildId: interaction.guild.id,
    adapterCreator: interaction.guild.voiceAdapterCreator
  }).subscribe(player);

  // 修复消息标签的括号闭合问题
  await interaction.reply(`Played bruh sound effect in <#${interaction.member.voice.channelId}>`);
};
  • 添加async修饰符支持await调用
  • 使用path.resolve修正相对路径问题,避免文件找不到
  • 修复回复消息中未闭合的<#>标签

额外注意事项

  • 修改命令定义后,需重新同步应用命令到Discord服务器,否则新定义不会生效
  • 确保@discordjs/voice版本与discord.js v14兼容,建议使用最新稳定版

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 12:54:24