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

Discord JDA按钮触发Unknown Interaction报错求助

Discord按钮触发UNKNOWN INTERACTION错误的排查与解决

问题描述

我有一个用于呼叫授权工作人员的Discord按钮,每次触发都会抛出UNKNOWN INTERACTION错误,且消息无法发送。其他按钮功能正常,尝试多种方法仍未解决。注:ereignis是德语的“event”(事件)。

交互事件处理代码

public void onButtonInteraction(ButtonInteractionEvent ereignis) {
    if (ereignis.getButton().getId().equals("ssuuii")) {

        if (ereignis.getGuild().getId().equals("GUILD_ID")) {
            ereignis.deferReply().queue();
            ereignis.getGuild().getTextChannelById("TEXTCHANNEL_ID").sendMessage(ereignis.getGuild().getRoleById("ROLE_ID")
                    .getAsMention() + ", " + ereignis.getMember().getAsMention() + " möchte Support haben").queue();
        } else if (ereignis.getGuild().getId().equals("GUILD_ID_A")) {
            ereignis.deferReply().queue();
            ereignis.getGuild().getTextChannelById("TEXTCHANNEL_ID_A").sendMessage("exampleman, " +
                    ereignis.getMember().getAsMention() + " wants Support").queue();
        }

    }
}

按钮创建代码

e.getChannel().sendMessageEmbeds(s.build()).setActionRow(Button.success("ssuuii", "Call Support"))
.complete().delete().queueAfter(15, TimeUnit.SECONDS);

问题原因分析

  1. 按钮依附的消息被提前删除:按钮绑定在发送的消息上,这条消息会在15秒后自动删除。用户点击按钮时,若消息已被删除,Discord无法找到对应交互上下文,直接抛出UNKNOWN INTERACTION错误。
  2. 交互响应异步执行的超时风险:deferReply().queue()是异步操作,存在Discord在3秒内未收到响应的可能——Discord要求所有交互必须在触发后3秒内得到响应,超时就会触发该错误。
  3. 未处理空值异常:代码中getTextChannelById、getRoleById可能返回null,导致后续sendMessage失败,虽不直接触发UNKNOWN INTERACTION,但会导致功能失效。

解决办法

1. 调整消息删除逻辑

如果需要按钮长期可用,移除消息自动删除的代码:

// 保留带按钮的消息,移除自动删除逻辑
e.getChannel().sendMessageEmbeds(s.build()).setActionRow(Button.success("ssuuii", "Call Support")).queue();

若必须自动清理消息,可改为发送仅用户可见的临时私信,或在消息中明确提示用户15秒内完成操作。

2. 确保交互响应及时完成

将异步的deferReply().queue()改为同步执行,保证Discord在3秒内收到响应,还可设置为仅触发者可见的临时回复,避免公共频道冗余:

// 同步执行延迟回复,设置为仅触发者可见
ereignis.deferReply(true).complete();

3. 添加空值检查与异常处理

避免因空值导致后续逻辑失败,同时捕获交互处理中的异常:

public void onButtonInteraction(ButtonInteractionEvent ereignis) {
    if (!ereignis.getButton().getId().equals("ssuuii")) return;

    // 先同步响应交互,避免超时
    try {
        ereignis.deferReply(true).complete();
    } catch (Exception ex) {
        ex.printStackTrace();
        return;
    }

    Guild guild = ereignis.getGuild();
    if (guild == null) return;

    if (guild.getId().equals("GUILD_ID")) {
        TextChannel channel = guild.getTextChannelById("TEXTCHANNEL_ID");
        Role supportRole = guild.getRoleById("ROLE_ID");
        if (channel != null && supportRole != null) {
            channel.sendMessage(supportRole.getAsMention() + ", " + ereignis.getMember().getAsMention() + " möchte Support haben").queue();
        }
    } else if (guild.getId().equals("GUILD_ID_A")) {
        TextChannel channel = guild.getTextChannelById("TEXTCHANNEL_ID_A");
        if (channel != null) {
            channel.sendMessage("exampleman, " + ereignis.getMember().getAsMention() + " wants Support").queue();
        }
    }
}

4. 验证机器人权限

确保机器人拥有目标文本频道的发送消息权限、提及角色/用户的权限,以及读取消息、处理交互事件的权限。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 17:06:28