chore: sendMsg

This commit is contained in:
手瓜一十雪
2024-08-09 18:23:37 +08:00
parent 173f83808e
commit b18dfdb9ba
2 changed files with 26 additions and 27 deletions

View File

@@ -1,18 +1,15 @@
import { ChatType, ElementType, Group, NTQQMsgApi, Peer, RawMessage, SendMessageElement } from '@/core'; import { ChatType, ElementType, NapCatCore, Peer, RawMessage, SendMessageElement } from '@/core';
import { OB11MessageDataType, OB11MessageNode } from '@/onebot11/types';
import { selfInfo } from '@/core/data';
import createSendElements from '@/onebot11/action/msg/SendMsg/create-send-elements';
import { logDebug, logError } from '@/common/utils/log';
import { sleep } from '@/common/utils/helper';
import { normalize, sendMsg } from '@/onebot11/action/msg/SendMsg/index';
import { MessageUnique } from '@/common/utils/MessageUnique'; import { MessageUnique } from '@/common/utils/MessageUnique';
import { OB11MessageDataType, OB11MessageNode } from '@/onebot/types';
async function cloneMsg(msg: RawMessage): Promise<RawMessage | undefined> { import createSendElements from './create-send-elements';
import { normalize, sendMsg } from "../SendMsg/index"
async function cloneMsg(coreContext: NapCatCore, msg: RawMessage): Promise<RawMessage | undefined> {
const selfPeer = { const selfPeer = {
chatType: ChatType.friend, chatType: ChatType.friend,
peerUid: selfInfo.uid peerUid: coreContext.selfInfo.uid
}; };
const logger = coreContext.context.logger;
const NTQQMsgApi = coreContext.getApiContext().MsgApi;
//logDebug('克隆的目标消息', msg); //logDebug('克隆的目标消息', msg);
const sendElements: SendMessageElement[] = []; const sendElements: SendMessageElement[] = [];
@@ -22,29 +19,31 @@ async function cloneMsg(msg: RawMessage): Promise<RawMessage | undefined> {
} }
if (sendElements.length === 0) { if (sendElements.length === 0) {
logDebug('需要clone的消息无法解析将会忽略掉', msg); logger.logDebug('需要clone的消息无法解析将会忽略掉', msg);
} }
try { try {
const nodeMsg = await NTQQMsgApi.sendMsg(selfPeer, sendElements, true); const nodeMsg = await NTQQMsgApi.sendMsg(selfPeer, sendElements, true);
return nodeMsg; return nodeMsg;
} catch (e) { } catch (e) {
logError(e, '克隆转发消息失败,将忽略本条消息', msg); logger.logError(e, '克隆转发消息失败,将忽略本条消息', msg);
} }
} }
export async function handleForwardNode(destPeer: Peer, messageNodes: OB11MessageNode[]): Promise<RawMessage | null> { export async function handleForwardNode(coreContext: NapCatCore, destPeer: Peer, messageNodes: OB11MessageNode[]): Promise<RawMessage | null> {
const NTQQMsgApi = coreContext.getApiContext().MsgApi;
const selfPeer = { const selfPeer = {
chatType: ChatType.friend, chatType: ChatType.friend,
peerUid: selfInfo.uid peerUid: coreContext.selfInfo.uid
}; };
let nodeMsgIds: string[] = []; let nodeMsgIds: string[] = [];
const logger = coreContext.context.logger;
for (const messageNode of messageNodes) { for (const messageNode of messageNodes) {
const nodeId = messageNode.data.id; const nodeId = messageNode.data.id;
if (nodeId) { if (nodeId) {
//对Mgsid和OB11ID混用情况兜底 //对Mgsid和OB11ID混用情况兜底
const nodeMsg = MessageUnique.getMsgIdAndPeerByShortId(parseInt(nodeId)) || MessageUnique.getPeerByMsgId(nodeId); const nodeMsg = MessageUnique.getMsgIdAndPeerByShortId(parseInt(nodeId)) || MessageUnique.getPeerByMsgId(nodeId);
if (!nodeMsg) { if (!nodeMsg) {
logError('转发消息失败,未找到消息', nodeId); logger.logError('转发消息失败,未找到消息', nodeId);
continue; continue;
} }
nodeMsgIds.push(nodeMsg.MsgId); nodeMsgIds.push(nodeMsg.MsgId);
@@ -55,20 +54,20 @@ export async function handleForwardNode(destPeer: Peer, messageNodes: OB11Messag
//筛选node消息 //筛选node消息
let isNodeMsg = OB11Data.filter(e => e.type === OB11MessageDataType.node).length;//找到子转发消息 let isNodeMsg = OB11Data.filter(e => e.type === OB11MessageDataType.node).length;//找到子转发消息
if (isNodeMsg !== 0) { if (isNodeMsg !== 0) {
if (isNodeMsg !== OB11Data.length) { logError('子消息中包含非node消息 跳过不合法部分'); continue; } if (isNodeMsg !== OB11Data.length) { logger.logError('子消息中包含非node消息 跳过不合法部分'); continue; }
const nodeMsg = await handleForwardNode(selfPeer, OB11Data.filter(e => e.type === OB11MessageDataType.node)); const nodeMsg = await handleForwardNode(coreContext, selfPeer, OB11Data.filter(e => e.type === OB11MessageDataType.node));
if (nodeMsg) { nodeMsgIds.push(nodeMsg.msgId); MessageUnique.createMsg(selfPeer, nodeMsg.msgId) }; if (nodeMsg) { nodeMsgIds.push(nodeMsg.msgId); MessageUnique.createMsg(selfPeer, nodeMsg.msgId) };
//完成子卡片生成跳过后续 //完成子卡片生成跳过后续
continue; continue;
} }
const { sendElements } = await createSendElements(OB11Data, destPeer); const { sendElements } = await createSendElements(coreContext,OB11Data, destPeer);
//拆分消息 //拆分消息
let MixElement = sendElements.filter(element => element.elementType !== ElementType.FILE && element.elementType !== ElementType.VIDEO); let MixElement = sendElements.filter(element => element.elementType !== ElementType.FILE && element.elementType !== ElementType.VIDEO);
let SingleElement = sendElements.filter(element => element.elementType === ElementType.FILE || element.elementType === ElementType.VIDEO).map(e => [e]); let SingleElement = sendElements.filter(element => element.elementType === ElementType.FILE || element.elementType === ElementType.VIDEO).map(e => [e]);
let AllElement: SendMessageElement[][] = [MixElement, ...SingleElement].filter(e => e !== undefined && e.length !== 0); let AllElement: SendMessageElement[][] = [MixElement, ...SingleElement].filter(e => e !== undefined && e.length !== 0);
const MsgNodeList: Promise<RawMessage | undefined>[] = []; const MsgNodeList: Promise<RawMessage | undefined>[] = [];
for (const sendElementsSplitElement of AllElement) { for (const sendElementsSplitElement of AllElement) {
MsgNodeList.push(sendMsg(selfPeer, sendElementsSplitElement, [], true).catch(e => new Promise((resolve, reject) => { resolve(undefined) }))); MsgNodeList.push(sendMsg(coreContext,selfPeer, sendElementsSplitElement, [], true).catch(e => new Promise((resolve, reject) => { resolve(undefined) })));
} }
(await Promise.allSettled(MsgNodeList)).map((result) => { (await Promise.allSettled(MsgNodeList)).map((result) => {
if (result.status === 'fulfilled' && result.value) { if (result.status === 'fulfilled' && result.value) {
@@ -77,7 +76,7 @@ export async function handleForwardNode(destPeer: Peer, messageNodes: OB11Messag
} }
}); });
} catch (e) { } catch (e) {
logDebug('生成转发消息节点失败', e); logger.logDebug('生成转发消息节点失败', e);
} }
} }
} }
@@ -88,7 +87,7 @@ export async function handleForwardNode(destPeer: Peer, messageNodes: OB11Messag
for (let msgId of nodeMsgIds) { for (let msgId of nodeMsgIds) {
const nodeMsgPeer = MessageUnique.getPeerByMsgId(msgId); const nodeMsgPeer = MessageUnique.getPeerByMsgId(msgId);
if (!nodeMsgPeer) { if (!nodeMsgPeer) {
logError('转发消息失败,未找到消息', msgId); logger.logError('转发消息失败,未找到消息', msgId);
continue; continue;
} }
const nodeMsg = (await NTQQMsgApi.getMsgsByMsgId(nodeMsgPeer.Peer, [msgId])).msgList[0]; const nodeMsg = (await NTQQMsgApi.getMsgsByMsgId(nodeMsgPeer.Peer, [msgId])).msgList[0];
@@ -102,8 +101,8 @@ export async function handleForwardNode(destPeer: Peer, messageNodes: OB11Messag
let retMsgIds: string[] = []; let retMsgIds: string[] = [];
if (needSendSelf) { if (needSendSelf) {
for (const [index, msg] of nodeMsgArray.entries()) { for (const [index, msg] of nodeMsgArray.entries()) {
if (msg.peerUid === selfInfo.uid) continue; if (msg.peerUid === coreContext.selfInfo.uid) continue;
const ClonedMsg = await cloneMsg(msg); const ClonedMsg = await cloneMsg(coreContext, msg);
if (ClonedMsg) retMsgIds.push(ClonedMsg.msgId); if (ClonedMsg) retMsgIds.push(ClonedMsg.msgId);
} }
} else { } else {
@@ -111,10 +110,10 @@ export async function handleForwardNode(destPeer: Peer, messageNodes: OB11Messag
} }
if (nodeMsgIds.length === 0) throw Error('转发消息失败,生成节点为空'); if (nodeMsgIds.length === 0) throw Error('转发消息失败,生成节点为空');
try { try {
logDebug('开发转发', srcPeer, destPeer, nodeMsgIds); logger.logDebug('开发转发', srcPeer, destPeer, nodeMsgIds);
return await NTQQMsgApi.multiForwardMsg(srcPeer!, destPeer, nodeMsgIds); return await NTQQMsgApi.multiForwardMsg(srcPeer!, destPeer, nodeMsgIds);
} catch (e) { } catch (e) {
logError('forward failed', e); logger.logError('forward failed', e);
return null; return null;
} }
} }

View File

@@ -146,7 +146,7 @@ export class SendMsg extends BaseAction<OB11PostSendMsg, ReturnDataType> {
); );
if (getSpecialMsgNum(payload, OB11MessageDataType.node)) { if (getSpecialMsgNum(payload, OB11MessageDataType.node)) {
const returnMsg = await handleForwardNode(peer, messages as OB11MessageNode[]); const returnMsg = await handleForwardNode(this.CoreContext,peer, messages as OB11MessageNode[]);
if (returnMsg) { if (returnMsg) {
const msgShortId = MessageUnique.createMsg({ guildId: '', peerUid: peer.peerUid, chatType: peer.chatType }, returnMsg!.msgId); const msgShortId = MessageUnique.createMsg({ guildId: '', peerUid: peer.peerUid, chatType: peer.chatType }, returnMsg!.msgId);
return { message_id: msgShortId! }; return { message_id: msgShortId! };