From 7ecdd63befc63c64ee3fc32d9fbb156a8c2be098 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: Sun, 13 Apr 2025 20:33:25 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=8C=BA=E5=88=86resId=E5=92=8C?= =?UTF-8?q?=E6=99=AE=E9=80=9A=E6=B6=88=E6=81=AFId?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/onebot/action/go-cqhttp/GetForwardMsg.ts | 64 +++++++++++--------- 1 file changed, 37 insertions(+), 27 deletions(-) diff --git a/src/onebot/action/go-cqhttp/GetForwardMsg.ts b/src/onebot/action/go-cqhttp/GetForwardMsg.ts index 89e9cb60..7cb5ac14 100644 --- a/src/onebot/action/go-cqhttp/GetForwardMsg.ts +++ b/src/onebot/action/go-cqhttp/GetForwardMsg.ts @@ -4,10 +4,11 @@ import { ActionName } from '@/onebot/action/router'; import { MessageUnique } from '@/common/message-unique'; import { ChatType, ElementType, MsgSourceType, NTMsgType, RawMessage } from '@/core'; import { z } from 'zod'; +import { isNumeric } from '@/common/helper'; const SchemaData = z.object({ - message_id: z.union([z.number(), z.string()]).optional(), - id: z.union([z.number(), z.string()]).optional(), + message_id: z.string().optional(), + id: z.string().optional(), }); type Payload = z.infer; @@ -52,19 +53,21 @@ export class GoCQHTTPGetForwardMsgAction extends OneBotAction { + // 2. 定义辅助函数 - 创建伪转发消息对象 + const createFakeForwardMsg = (resId: string): RawMessage => { return { chatType: ChatType.KCHATTYPEGROUP, elements: [{ elementType: ElementType.MULTIFORWARD, elementId: '', multiForwardMsgElement: { - resId: res_id, + resId: resId, fileName: '', xmlContent: '', } @@ -95,8 +98,9 @@ export class GoCQHTTPGetForwardMsgAction extends OneBotAction { - const ob = (await this.obContext.apis.MsgApi.parseMessageV2(fakeForwardMsg(res_id)))?.arrayMsg; + // 3. 定义协议回退逻辑函数 + const protocolFallbackLogic = async (resId: string) => { + const ob = (await this.obContext.apis.MsgApi.parseMessageV2(createFakeForwardMsg(resId)))?.arrayMsg; if (ob) { return { messages: (ob?.message?.[0] as OB11MessageForward)?.data?.content @@ -104,31 +108,37 @@ export class GoCQHTTPGetForwardMsgAction extends OneBotAction 0) { + const singleMsg = data.msgList[0]; + if (!singleMsg) { + throw new Error('消息不存在或已过期'); + } + // 6. 解析消息内容 + const resMsg = (await this.obContext.apis.MsgApi.parseMessageV2(singleMsg))?.arrayMsg; - // return { message: resMsg }; + const forwardContent = (resMsg?.message?.[0] as OB11MessageForward)?.data?.content; + if (forwardContent) { + return { messages: forwardContent }; + } + } + } + // 说明消息已过期或者为内层消息 NapCat 一次返回不处理内层消息 + throw new Error('消息已过期或者为内层消息,无法获取转发消息'); } }