From 6758825895d2d1fda4c614ec859574a3fc11be8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=8B=E7=93=9C=E4=B8=80=E5=8D=81=E9=9B=AA?= Date: Thu, 5 Mar 2026 13:40:16 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=81=8A=E5=A4=A9=E8=AE=B0=E5=BD=95?= =?UTF-8?q?=E5=AF=B9=E8=81=8A=E5=A4=A9=E8=AE=B0=E5=BD=95=E5=90=88=E5=B9=B6?= =?UTF-8?q?=E8=BD=AC=E5=8F=91=E4=BF=AE=E5=A4=8D=20&=20fix=20#1673?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../packet/context/operationContext.ts | 13 +++++- .../napcat-core/packet/message/element.ts | 5 ++- .../transformer/message/UploadForwardMsgV2.ts | 7 ++-- packages/napcat-onebot/action/msg/SendMsg.ts | 40 +++++++++++++++++-- 4 files changed, 55 insertions(+), 10 deletions(-) diff --git a/packages/napcat-core/packet/context/operationContext.ts b/packages/napcat-core/packet/context/operationContext.ts index 34a2bde08..cb40e83f2 100644 --- a/packages/napcat-core/packet/context/operationContext.ts +++ b/packages/napcat-core/packet/context/operationContext.ts @@ -230,8 +230,8 @@ export class PacketOperationContext { } async UploadForwardMsgV2 (msg: UploadForwardMsgParams[], groupUin: number = 0) { //await this.SendPreprocess(msg, groupUin); - // 遍历上传资源 - await Promise.allSettled(msg.map(async (item) => { return await this.SendPreprocess(item.actionMsg, groupUin); })); + // 遍历上传资源(跳过已有原始数据的项) + await Promise.allSettled(msg.filter(item => item.actionMsg).map(async (item) => { return await this.SendPreprocess(item.actionMsg!, groupUin); })); const req = trans.UploadForwardMsgV2.build(this.context.napcore.basicInfo.uid, msg, groupUin); const resp = await this.context.client.sendOidbPacket(req, true); const res = trans.UploadForwardMsg.parse(resp); @@ -312,6 +312,15 @@ export class PacketOperationContext { } } + async FetchForwardMsgRaw (resId: string) { + const req = trans.DownloadForwardMsg.build(this.context.napcore.basicInfo.uid, resId); + const resp = await this.context.client.sendOidbPacket(req, true); + const res = trans.DownloadForwardMsg.parse(resp); + const inflate = gunzipSync(res.result.payload); + const result = new NapProtoMsg(LongMsgResult).decode(inflate); + return result.action; + } + async FetchForwardMsg (res_id: string): Promise { const req = trans.DownloadForwardMsg.build(this.context.napcore.basicInfo.uid, res_id); const resp = await this.context.client.sendOidbPacket(req, true); diff --git a/packages/napcat-core/packet/message/element.ts b/packages/napcat-core/packet/message/element.ts index d466391eb..5ae5f80a6 100644 --- a/packages/napcat-core/packet/message/element.ts +++ b/packages/napcat-core/packet/message/element.ts @@ -1,3 +1,4 @@ +import * as crypto from 'node:crypto'; import * as zlib from 'node:zlib'; import { NapProtoDecodeStructType, NapProtoEncodeStructType, NapProtoMsg } from 'napcat-protobuf'; import { @@ -665,11 +666,13 @@ export class PacketMsgMarkDownElement extends IPacketMsgElement { resid: string; message: PacketMsg[]; + uuid: string; constructor (rawElement: SendMultiForwardMsgElement, message?: PacketMsg[]) { super(rawElement); this.resid = rawElement.multiForwardMsgElement.resId; this.message = message ?? []; + this.uuid = rawElement.multiForwardMsgElement.fileName || crypto.randomUUID(); } override buildElement (): NapProtoEncodeStructType[] { @@ -677,7 +680,7 @@ export class PacketMultiMsgElement extends IPacketMsgElement[]; } class UploadForwardMsgV2 extends PacketTransformer { build (selfUid: string, msg: UploadForwardMsgParams[], groupUin: number = 0): OidbPacket { const reqdata = msg.map((item) => ({ actionCommand: item.actionCommand, actionData: { - msgBody: this.msgBuilder.buildFakeMsg(selfUid, item.actionMsg), + msgBody: item.actionMsgBody ?? this.msgBuilder.buildFakeMsg(selfUid, item.actionMsg ?? []), } })); const longMsgResultData = new NapProtoMsg(proto.LongMsgResult).encode( diff --git a/packages/napcat-onebot/action/msg/SendMsg.ts b/packages/napcat-onebot/action/msg/SendMsg.ts index 2c6afbc6a..60694ad5a 100644 --- a/packages/napcat-onebot/action/msg/SendMsg.ts +++ b/packages/napcat-onebot/action/msg/SendMsg.ts @@ -215,11 +215,11 @@ export class SendMsgBase extends OneBotAction { uuid?: string, packetMsg: PacketMsg[], deleteAfterSentFiles: string[], - innerPacketMsg?: Array<{ uuid: string, packetMsg: PacketMsg[]; }>; + innerPacketMsg?: Array<{ uuid: string, packetMsg?: PacketMsg[], actionMsgBody?: UploadForwardMsgParams['actionMsgBody']; }>; } | null> { const packetMsg: PacketMsg[] = []; const delFiles: string[] = []; - const innerMsg: Array<{ uuid: string, packetMsg: PacketMsg[]; }> = new Array(); + const innerMsg: Array<{ uuid: string, packetMsg?: PacketMsg[], actionMsgBody?: UploadForwardMsgParams['actionMsgBody']; }> = new Array(); for (const node of messageNodes) { if (dp >= 3) { this.core.context.logger.logWarn('转发消息深度超过3层,将停止解析!'); @@ -240,7 +240,7 @@ export class SendMsgBase extends OneBotAction { if (uploadReturnData?.uuid) { innerMsg.push({ uuid: uploadReturnData.uuid, packetMsg: uploadReturnData.packetMsg }); uploadReturnData.innerPacketMsg?.forEach(m => { - innerMsg.push({ uuid: m.uuid, packetMsg: m.packetMsg }); + innerMsg.push(m); }); } @@ -274,6 +274,37 @@ export class SendMsgBase extends OneBotAction { const msgCache = await this.core.apis.FileApi.downloadRawMsgMedia([msg]); delFiles.push(...msgCache); const transformedMsg = this.core.apis.PacketApi.pkt.msgConverter.rawMsgToPacketMsg(msg, msgPeer); + // 检测嵌套合并转发消息,拉取内层protobuf数据 + for (const element of msg.elements) { + let resId: string | undefined; + let uuid: string | undefined; + if (element.multiForwardMsgElement?.resId) { + resId = element.multiForwardMsgElement.resId; + uuid = element.multiForwardMsgElement.fileName; + } else if (element.arkElement?.bytesData) { + try { + const json = JSON.parse(element.arkElement.bytesData); + if (json.app === 'com.tencent.multimsg') { + resId = json.meta?.detail?.resid; + uuid = json.meta?.detail?.uniseq || json.extra?.filename; + } + } catch { /* not a valid json */ } + } + if (resId && uuid) { + try { + const rawActions = await this.core.apis.PacketApi.pkt.operation.FetchForwardMsgRaw(resId); + for (const action of rawActions) { + if (action.actionCommand === 'MultiMsg') { + innerMsg.push({ uuid, actionMsgBody: action.actionData.msgBody }); + } else { + innerMsg.push({ uuid: action.actionCommand, actionMsgBody: action.actionData.msgBody }); + } + } + } catch (e) { + this.core.context.logger.logError(`获取合并转发内层消息失败: ${(e as Error)?.stack}`); + } + } + } this.core.context.logger.logDebug(`handleForwardedNodesPacket[PureRaw] 转换为 ${stringifyWithBigInt(transformedMsg)}`); packetMsg.push(transformedMsg); } @@ -289,10 +320,11 @@ export class SendMsgBase extends OneBotAction { actionCommand: 'MultiMsg', actionMsg: packetMsg, }]; - innerMsg.forEach(({ uuid, packetMsg: msg }) => { + innerMsg.forEach(({ uuid, packetMsg: msg, actionMsgBody: raw }) => { uploadMsgData.push({ actionCommand: uuid, actionMsg: msg, + actionMsgBody: raw, }); }); const resid = await this.core.apis.PacketApi.pkt.operation.UploadForwardMsgV2(uploadMsgData, msgPeer.chatType === ChatType.KCHATTYPEGROUP ? +msgPeer.peerUid : 0);