Skip to content

Commit

Permalink
throw a tips exception while msgId is not int32
Browse files Browse the repository at this point in the history
  • Loading branch information
MrXiaoM committed Mar 7, 2024
1 parent 0c664fb commit 501e27b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public MessageEvent deserialize(JsonElement json, Type typeOfT, JsonDeserializat
String message = messageElement.isJsonPrimitive() ? messageElement.getAsJsonPrimitive().getAsString() : gson.toJson(messageElement);
String rawMessage = obj.get("raw_message").getAsString();
int font = obj.get("font").getAsInt();

switch (messageType) {
case "group": {
e = groupMessageEvent(new JsonsObject(obj));
Expand All @@ -39,7 +40,7 @@ public MessageEvent deserialize(JsonElement json, Type typeOfT, JsonDeserializat
break;
}
}
if (e != null){
if (e != null) {
e.setPostType(postType);
e.setTime(time);
e.setSelfId(selfId);
Expand All @@ -53,7 +54,7 @@ public MessageEvent deserialize(JsonElement json, Type typeOfT, JsonDeserializat
}

private MessageEvent groupMessageEvent(JsonsObject obj) {
int messageId = obj.optInt("message_id");
int messageId = MsgAdapter.getMessageId(obj.get());
String subType = obj.optString("sub_type");
long groupId = obj.optLong("group_id");
Anonymous anonymous = gson.fromJson(obj.get().get("anonymous"), Anonymous.class);
Expand All @@ -62,7 +63,7 @@ private MessageEvent groupMessageEvent(JsonsObject obj) {
}

private MessageEvent privateMessageEvent(JsonsObject obj) {
int messageId = obj.optInt("message_id");
int messageId = MsgAdapter.getMessageId(obj.get());
String subType = obj.optString("sub_type");
PrivateMessageEvent.PrivateSender sender = gson.fromJson(obj.get().get("sender"), PrivateMessageEvent.PrivateSender.class);
return new PrivateMessageEvent(messageId, subType, sender);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class MsgAdapter implements JsonDeserializer<GetMsgResp> {
public GetMsgResp deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) {
JsonObject obj = json.getAsJsonObject();

int messageId = obj.get("message_id").getAsInt();
int messageId = getMessageId(obj);
int realId = obj.get("real_id").getAsInt();
GetMsgResp.Sender sender = gson.fromJson(obj.get("sender"), GetMsgResp.Sender.class);
int time = obj.get("time").getAsInt();
Expand All @@ -24,4 +24,17 @@ public GetMsgResp deserialize(JsonElement json, Type typeOfT, JsonDeserializatio

return new GetMsgResp(messageId, realId, sender, time, message, rawMessage, peerId, groupId, targetId);
}

public static int getMessageId(JsonObject obj) {
String msgId = obj.get("message_id").getAsString();
try {
return Integer.parseInt(msgId);
} catch (NumberFormatException ignored) {
throw new NumberFormatException(
"无法将消息ID `" + msgId + "` 的类型转换为 int32," +
"请向你所使用的 Onebot 实现维护者报告该问题," +
"不要将该问题反馈到 Overflow。"
);
}
}
}

0 comments on commit 501e27b

Please sign in to comment.