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

JDA开发Discord模态框(斜杠命令)仅调试模式可用问题求助

问题解决方案:JDA开发的Discord模态框斜杠命令导出运行无响应

核心问题分析

调试模式下斜杠命令正常,但导出运行时Discord提示"The application is not responding",核心原因通常是运行时异常未被捕获或代码隐性错误,导致Bot无法处理交互请求。结合你提供的代码,可定位几个明确问题:

1. 变量名错误(直接触发异常)

在TestCommand类的模态框创建代码中,你定义的文本输入变量是test_name,但构建ActionRow时误用了未定义的name变量:

// 定义的变量是test_name
TextInput test_name = TextInput.create("test-name", "Name", TextInputStyle.SHORT)...build();

// 错误使用未定义的name变量
Modal modal = Modal.create("test-modal", "test text")
        .addActionRows(ActionRow.of(name), ActionRow.of(message))
        .build();

这个错误会直接触发NullPointerException:调试模式下IDE会捕获异常并提示,但导出运行时异常会导致事件处理线程崩溃,Bot无法向Discord返回响应,从而出现"应用无响应"的提示。

2. 缺少异常处理机制

事件监听器未捕获异常,一旦出错,事件线程会直接终止,后续所有交互请求都无法被处理。调试模式下IDE会拦截异常,线程不会终止,因此命令能正常工作。

3. 依赖打包不完整(潜在问题)

若导出jar时未将JDA及其依赖全部打包,运行时会出现ClassNotFoundException或NoClassDefFoundError,同样会导致事件处理失败。


修复步骤

步骤1:修正变量名错误

将模态框构建代码中的name替换为test_name:

Modal modal = Modal.create("test-modal", "test text")
        .addActionRows(ActionRow.of(test_name), ActionRow.of(message))
        .build();

步骤2:添加异常捕获与处理

在onSlashCommandInteraction方法中加入try-catch块,捕获所有异常并向用户反馈,同时避免事件线程崩溃:

@Override
public void onSlashCommandInteraction(@NotNull SlashCommandInteractionEvent event) {
    try {
        if (event.getName().equals("test")){
            TextInput test_name = TextInput.create("test-name", "Name", TextInputStyle.SHORT)
                    .setPlaceholder("test text")
                    .setMinLength(1)
                    .setRequired(true)
                    .build();

            TextInput message = TextInput.create("test-message","Message", TextInputStyle.PARAGRAPH)
                    .setMinLength(10)
                    .setMaxLength(100)
                    .setRequired(true)
                    .setValue("test text.")
                    .build();

            Modal modal = Modal.create("test-modal", "test text")
                    .addActionRows(ActionRow.of(test_name), ActionRow.of(message))
                    .build();

            event.replyModal(modal).queue();
        }
    } catch (Exception e) {
        e.printStackTrace(); // 打印错误日志用于排查
        if (!event.isAcknowledged()) {
            // 向用户发送仅自己可见的错误提示
            event.reply("命令执行出错,请重试").setEphemeral(true).queue();
        }
    }
}

步骤3:确保依赖完整打包

使用构建工具的fat jar插件打包:

  • Maven:配置maven-shade-plugin,将所有依赖合并到一个jar中。
  • Gradle:执行shadowJar命令打包。
    避免手动导出jar时遗漏依赖,确保运行时classpath包含所有必要类文件。

步骤4:优化命令注册逻辑

将命令注册移到ReadyEvent中执行,确保Guild完全加载后再注册命令:

JDA jda = JDABuilder.createDefault(token)
        .setActivity(Activity.listening("TEST"))
        .addEventListeners(new TestCommand(), new ListenerAdapter() {
            @Override
            public void onReady(@NotNull ReadyEvent event) {
                Guild guild = event.getJDA().getGuildById("SERVER_ID");
                if (guild != null) {
                    guild.upsertCommand("test", "test command").queue();
                }
            }
        })
        .build().awaitReady();

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 19:00:09