mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2025-07-19 12:03:37 +00:00
feat: 回归ajv
This commit is contained in:
@@ -4,14 +4,14 @@ import { ActionName } from '@/onebot/action/router';
|
|||||||
import { MessageUnique } from '@/common/message-unique';
|
import { MessageUnique } from '@/common/message-unique';
|
||||||
import { Static, Type } from '@sinclair/typebox';
|
import { Static, Type } from '@sinclair/typebox';
|
||||||
import { ChatType, ElementType, MsgSourceType, NTMsgType, RawMessage } from '@/core';
|
import { ChatType, ElementType, MsgSourceType, NTMsgType, RawMessage } from '@/core';
|
||||||
|
import { isNumeric } from '@/common/helper';
|
||||||
|
|
||||||
const SchemaData = Type.Object({
|
const SchemaData = Type.Object({
|
||||||
message_id: Type.Optional(Type.Union([Type.Number(), Type.String()])),
|
message_id: Type.Optional(Type.String()),
|
||||||
id: Type.Optional(Type.Union([Type.Number(), Type.String()])),
|
id: Type.Optional(Type.String()),
|
||||||
});
|
});
|
||||||
|
|
||||||
type Payload = Static<typeof SchemaData>;
|
type Payload = Static<typeof SchemaData>;
|
||||||
|
|
||||||
export class GoCQHTTPGetForwardMsgAction extends OneBotAction<Payload, {
|
export class GoCQHTTPGetForwardMsgAction extends OneBotAction<Payload, {
|
||||||
messages: OB11Message[] | undefined;
|
messages: OB11Message[] | undefined;
|
||||||
}> {
|
}> {
|
||||||
@@ -53,19 +53,21 @@ export class GoCQHTTPGetForwardMsgAction extends OneBotAction<Payload, {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async _handle(payload: Payload) {
|
async _handle(payload: Payload) {
|
||||||
|
// 1. 检查消息ID是否存在
|
||||||
const msgId = payload.message_id || payload.id;
|
const msgId = payload.message_id || payload.id;
|
||||||
if (!msgId) {
|
if (!msgId) {
|
||||||
throw new Error('message_id is required');
|
throw new Error('message_id is required');
|
||||||
}
|
}
|
||||||
|
|
||||||
const fakeForwardMsg = (res_id: string) => {
|
// 2. 定义辅助函数 - 创建伪转发消息对象
|
||||||
|
const createFakeForwardMsg = (resId: string): RawMessage => {
|
||||||
return {
|
return {
|
||||||
chatType: ChatType.KCHATTYPEGROUP,
|
chatType: ChatType.KCHATTYPEGROUP,
|
||||||
elements: [{
|
elements: [{
|
||||||
elementType: ElementType.MULTIFORWARD,
|
elementType: ElementType.MULTIFORWARD,
|
||||||
elementId: '',
|
elementId: '',
|
||||||
multiForwardMsgElement: {
|
multiForwardMsgElement: {
|
||||||
resId: res_id,
|
resId: resId,
|
||||||
fileName: '',
|
fileName: '',
|
||||||
xmlContent: '',
|
xmlContent: '',
|
||||||
}
|
}
|
||||||
@@ -96,8 +98,9 @@ export class GoCQHTTPGetForwardMsgAction extends OneBotAction<Payload, {
|
|||||||
} as RawMessage;
|
} as RawMessage;
|
||||||
};
|
};
|
||||||
|
|
||||||
const protocolFallbackLogic = async (res_id: string) => {
|
// 3. 定义协议回退逻辑函数
|
||||||
const ob = (await this.obContext.apis.MsgApi.parseMessageV2(fakeForwardMsg(res_id)))?.arrayMsg;
|
const protocolFallbackLogic = async (resId: string) => {
|
||||||
|
const ob = (await this.obContext.apis.MsgApi.parseMessageV2(createFakeForwardMsg(resId)))?.arrayMsg;
|
||||||
if (ob) {
|
if (ob) {
|
||||||
return {
|
return {
|
||||||
messages: (ob?.message?.[0] as OB11MessageForward)?.data?.content
|
messages: (ob?.message?.[0] as OB11MessageForward)?.data?.content
|
||||||
@@ -105,31 +108,37 @@ export class GoCQHTTPGetForwardMsgAction extends OneBotAction<Payload, {
|
|||||||
}
|
}
|
||||||
throw new Error('protocolFallbackLogic: 找不到相关的聊天记录');
|
throw new Error('protocolFallbackLogic: 找不到相关的聊天记录');
|
||||||
};
|
};
|
||||||
|
// 4. 尝试通过正常渠道获取消息
|
||||||
|
// 如果是数字ID,优先使用getMsgsByMsgId获取
|
||||||
|
if (!isNumeric(msgId)) {
|
||||||
|
let ret = await protocolFallbackLogic(msgId);
|
||||||
|
if (ret.messages) {
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
throw new Error('ResId无效: 找不到相关的聊天记录');
|
||||||
|
}
|
||||||
const rootMsgId = MessageUnique.getShortIdByMsgId(msgId.toString());
|
const rootMsgId = MessageUnique.getShortIdByMsgId(msgId.toString());
|
||||||
const rootMsg = MessageUnique.getMsgIdAndPeerByShortId(rootMsgId ?? +msgId);
|
const rootMsg = MessageUnique.getMsgIdAndPeerByShortId(rootMsgId ?? +msgId);
|
||||||
if (!rootMsg) {
|
|
||||||
return await protocolFallbackLogic(msgId.toString());
|
|
||||||
}
|
|
||||||
const data = await this.core.apis.MsgApi.getMsgsByMsgId(rootMsg.Peer, [rootMsg.MsgId]);
|
|
||||||
|
|
||||||
if (!data || data.result !== 0) {
|
if (rootMsg) {
|
||||||
return await protocolFallbackLogic(msgId.toString());
|
// 5. 获取消息内容
|
||||||
}
|
const data = await this.core.apis.MsgApi.getMsgsByMsgId(rootMsg.Peer, [rootMsg.MsgId]);
|
||||||
|
|
||||||
const singleMsg = data.msgList[0];
|
if (data && data.result === 0 && data.msgList.length > 0) {
|
||||||
if (!singleMsg) {
|
const singleMsg = data.msgList[0];
|
||||||
return await protocolFallbackLogic(msgId.toString());
|
if (!singleMsg) {
|
||||||
}
|
throw new Error('消息不存在或已过期');
|
||||||
const resMsg = (await this.obContext.apis.MsgApi.parseMessageV2(singleMsg))?.arrayMsg;//强制array 以便处理
|
}
|
||||||
if (!(resMsg?.message?.[0] as OB11MessageForward)?.data?.content) {
|
// 6. 解析消息内容
|
||||||
return await protocolFallbackLogic(msgId.toString());
|
const resMsg = (await this.obContext.apis.MsgApi.parseMessageV2(singleMsg))?.arrayMsg;
|
||||||
}
|
|
||||||
return {
|
|
||||||
messages: (resMsg?.message?.[0] as OB11MessageForward)?.data?.content
|
|
||||||
};
|
|
||||||
//}
|
|
||||||
|
|
||||||
// return { message: resMsg };
|
const forwardContent = (resMsg?.message?.[0] as OB11MessageForward)?.data?.content;
|
||||||
|
if (forwardContent) {
|
||||||
|
return { messages: forwardContent };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 说明消息已过期或者为内层消息 NapCat 一次返回不处理内层消息
|
||||||
|
throw new Error('消息已过期或者为内层消息,无法获取转发消息');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user