From a1972784d69e1584a8edefefe49684a02771d4b5 Mon Sep 17 00:00:00 2001 From: AnnaofArendelle Date: Sat, 15 Aug 2026 04:33:36 +0800 Subject: [PATCH] =?UTF-8?q?fix(entities):=20=E4=BF=AE=E5=A4=8D=20InterActi?= =?UTF-8?q?on.getEnvType=20=E5=AF=B9=20chat=5Ftype=20=E7=9A=84=E7=A9=BA?= =?UTF-8?q?=E6=8C=87=E9=92=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit chat_type 声明为 Integer,getEnvType() 中的 `chat_type == 0` 会触发拆箱。 QQ 下发的 InterAction 事件并非总是携带该字段,此时抛出 NullPointerException。 崩溃点在 Events.onEvent 的日志格式化路径: logger.info(String.format("Bot(%s) post(%s) from %s", ..., event, ...)); -> BaseInterActionEvent.toString() -> InterAction.getCid() -> getEnvType() java.lang.NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because "this.chat_type" is null at io.github.kloping.qqbot.entities.qqpd.InterAction.getEnvType(InterAction.java:75) at io.github.kloping.qqbot.entities.qqpd.InterAction.getCid(InterAction.java:65) at io.github.kloping.qqbot.impl.BaseInterActionEvent.toString(BaseInterActionEvent.java:69) at io.github.kloping.qqbot.network.Events.onEvent(Events.java:115) 事件本身已分发完毕,功能不受影响,但每次都会向 stderr 打印一次堆栈。 改为先判空;chat_type 缺失时回退到 scene 字段——按官方文档,二者承载同一信息 (guild / group / c2c)。两者皆缺失时按群聊处理,与 chat_type 非 0 时的既有行为一致。 chat_type 非空时行为完全不变。 Co-Authored-By: Claude Opus 5 (1M context) --- .../kloping/qqbot/entities/qqpd/InterAction.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/github/kloping/qqbot/entities/qqpd/InterAction.java b/src/main/java/io/github/kloping/qqbot/entities/qqpd/InterAction.java index 4e9f57f..ea27f58 100644 --- a/src/main/java/io/github/kloping/qqbot/entities/qqpd/InterAction.java +++ b/src/main/java/io/github/kloping/qqbot/entities/qqpd/InterAction.java @@ -27,6 +27,11 @@ */ @Data public class InterAction implements SenderV2, SenderAndCidMidGetter { + /** + * {@link #scene} 的频道场景取值,用于 {@link #chat_type} 缺失时判定环境 + */ + private static final String SCENE_GUILD = "guild"; + private String id; private Integer type; private String scene; @@ -72,7 +77,12 @@ public String getMid() { @Override public EnvType getEnvType() { - return chat_type == 0 ? EnvType.GUILD : EnvType.GROUP; + if (chat_type != null) { + return chat_type == 0 ? EnvType.GUILD : EnvType.GROUP; + } + // chat_type 并非每次都会下发;scene 字段承载同一信息(guild / group / c2c), + // 两者皆缺失时按群聊处理,与 chat_type 非 0 时的既有行为一致。 + return SCENE_GUILD.equals(scene) ? EnvType.GUILD : EnvType.GROUP; } @Setter