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

添加Ticket-System至Moderation Bot时遇getSubcommand函数错误

问题:TypeError: interaction.options.getSubcommand is not a function

我自己编译了一个全能型Moderation Bot,最近为它添加Ticket-System功能时出现了上述错误,试过网上能找到的所有修复方案都没解决。

相关代码:

ticket-config.js

const { Util, MessageEmbed } = require('discord.js');
const configOptions = require('../../configOptions');

module.exports = {
    name: "ticketconfig",
    description: "Configuration ticket system.",
    options: configOptions,
    permission: "ADMINISTRATOR",
    run: async(interaction, client) => {
        const replyMessage = {
            content: "Config has been set!"
        }
        if (interaction.options.getSubcommand() === 'message') {
            const message = interaction.options.getString('message');
            const content = interaction.options.getString('content') || null;
            let data = await client.db.get('config', interaction.guild.id);
            if (!data) data = {};
            data.message = message;
            data.content = content;
            await client.db.set('config', interaction.guild.id, data);
            return interaction.reply(replyMessage)
        }
    }
}

configOptions.js

module.exports = [
    {
        name: "message",
        description: "Configuration your ticket message",
        type: 1,
        options: [
            {
                name: "message",
                description: "The message to sent in ticket",
                type: 3,
                required: true
            },
            {
                name: "content",
                description: "content will be appeared above embed message, use /variables command to see all available variables.",
                type: 3
            }
        ]
    },
]

./handler/index.js

module.exports = async (client) => {

    const eventFiles = await globPromise(`${process.cwd()}/events/*.js`);
    eventFiles.map((value) => require(value));


    const slashCommands = await globPromise(
        `${process.cwd()}/SlashCommands/*/*.js`
    );

    const arrayOfSlashCommands = [];
    slashCommands.map((value) => {
        const file = require(value);
        if (!file?.name) return;
        client.slashCommands.set(file.name, file);

        if (["MESSAGE", "USER"].includes(file.type)) delete file.description;
        arrayOfSlashCommands.push(file);
    });
    client.on("ready", async () => {
        await client.application.commands.set(arrayOfSlashCommands);
    });
}

解决方案
  • 检查Discord.js版本:getSubcommand()是Discord.js v14及以上版本才新增的方法,如果你用的是v13或更低版本,会直接报错。执行npm install discord.js@latest升级到最新稳定版,之后重启机器人。

  • 验证子命令结构与注册逻辑:

    1. 确保configOptions里的子命令定义正确:type:1对应子命令(SUB_COMMAND),结构符合Discord API要求。
    2. 命令注册后,Discord服务器需要时间同步新命令,重启机器人后等待几分钟再测试;开发阶段可以改用guild.commands.set()针对单个服务器注册,同步速度更快。
  • 适配低版本写法(如果不想升级):
    若坚持使用v13,由于没有getSubcommand()方法,需要手动遍历options数组查找子命令:

    const subcommand = interaction.options.data.find(option => option.type === 'SUB_COMMAND');
    if (subcommand?.name === 'message') {
        // 后续业务逻辑
    }
    
  • 调试确认交互对象:在run函数开头添加console.log(interaction.options),查看options的实际结构。如果输出中没有getSubcommand方法,说明版本不兼容;如果结构异常,检查命令注册时是否正确传递了options参数。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 18:21:27