diff --git a/src/core/src/apis/msg.ts b/src/core/src/apis/msg.ts index fb0fafe4..1067a020 100644 --- a/src/core/src/apis/msg.ts +++ b/src/core/src/apis/msg.ts @@ -110,7 +110,9 @@ export class NTQQMsgApi { }); return ret; } - static async getMsgsByMsgId(peer: Peer, msgIds: string[]) { + static async getMsgsByMsgId(peer: Peer | undefined, msgIds: string[] | undefined) { + if (!peer) throw new Error('peer is not allowed'); + if (!msgIds) throw new Error('msgIds is not allowed'); return await napCatCore.session.getMsgService().getMsgsByMsgId(peer, msgIds); } static async getSingleMsg(peer: Peer, seq: string) { diff --git a/src/onebot11/action/msg/SendMsg/create-send-elements.ts b/src/onebot11/action/msg/SendMsg/create-send-elements.ts index 84d39636..fe917dcd 100644 --- a/src/onebot11/action/msg/SendMsg/create-send-elements.ts +++ b/src/onebot11/action/msg/SendMsg/create-send-elements.ts @@ -18,7 +18,7 @@ import { uri2local } from '@/common/utils/file'; import { ob11Config } from '@/onebot11/config'; import { RequestUtil } from '@/common/utils/request'; import { MessageUnique } from '@/common/utils/MessageUnique'; - +console.log(process.pid) export type MessageContext = { deleteAfterSentFiles: string[], peer:Peer diff --git a/src/onebot11/action/msg/SendMsg/handle-forward-node.ts b/src/onebot11/action/msg/SendMsg/handle-forward-node.ts index 387ddec7..c01c26e0 100644 --- a/src/onebot11/action/msg/SendMsg/handle-forward-node.ts +++ b/src/onebot11/action/msg/SendMsg/handle-forward-node.ts @@ -32,7 +32,7 @@ async function cloneMsg(msg: RawMessage): Promise { } } -export async function handleForwardNode(destPeer: Peer, messageNodes: OB11MessageNode[], inputPeer: Peer): Promise { +export async function handleForwardNode(destPeer: Peer, messageNodes: OB11MessageNode[]): Promise { const selfPeer = { chatType: ChatType.friend, peerUid: selfInfo.uid @@ -56,24 +56,24 @@ export async function handleForwardNode(destPeer: Peer, messageNodes: OB11Messag let isNodeMsg = OB11Data.filter(e => e.type === OB11MessageDataType.node).length;//找到子转发消息 if (isNodeMsg !== 0) { if (isNodeMsg !== OB11Data.length) { logError('子消息中包含非node消息 跳过不合法部分'); continue; } - const nodeMsg = await handleForwardNode(destPeer, OB11Data.filter(e => e.type === OB11MessageDataType.node), inputPeer); - if (nodeMsg) nodeMsgIds.push(nodeMsg.msgId); + const nodeMsg = await handleForwardNode(selfPeer, OB11Data.filter(e => e.type === OB11MessageDataType.node)); + if (nodeMsg) { nodeMsgIds.push(nodeMsg.msgId); MessageUnique.createMsg(selfPeer, nodeMsg.msgId) }; //完成子卡片生成跳过后续 continue; } - const { sendElements } = await createSendElements(OB11Data, inputPeer); + const { sendElements } = await createSendElements(OB11Data, destPeer); //拆分消息 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 AllElement: SendMessageElement[][] = [MixElement, ...SingleElement]; + let AllElement: SendMessageElement[][] = [MixElement, ...SingleElement].filter(e => e !== undefined && e.length !== 0); const MsgNodeList: Promise[] = []; for (const sendElementsSplitElement of AllElement) { MsgNodeList.push(sendMsg(selfPeer, sendElementsSplitElement, [], true).catch(e => new Promise((resolve, reject) => { resolve(undefined) }))); - await sleep(10); } (await Promise.allSettled(MsgNodeList)).map((result) => { if (result.status === 'fulfilled' && result.value) { nodeMsgIds.push(result.value.msgId); + MessageUnique.createMsg(selfPeer, result.value.msgId); } }); } catch (e) { @@ -82,13 +82,16 @@ export async function handleForwardNode(destPeer: Peer, messageNodes: OB11Messag } } const nodeMsgArray: Array = []; - let srcPeer: Peer | undefined = undefined; let needSendSelf = false; //检测是否处于同一个Peer 不在同一个peer则全部消息由自身发送 for (let msgId of nodeMsgIds) { const nodeMsgPeer = MessageUnique.getPeerByMsgId(msgId); - const nodeMsg = (await NTQQMsgApi.getMsgsByMsgId(nodeMsgPeer?.Peer!, [msgId])).msgList[0]; + if (!nodeMsgPeer) { + logError('转发消息失败,未找到消息', msgId); + continue; + } + const nodeMsg = (await NTQQMsgApi.getMsgsByMsgId(nodeMsgPeer.Peer, [msgId])).msgList[0]; srcPeer = srcPeer ?? { chatType: nodeMsg.chatType, peerUid: nodeMsg.peerUid }; if (srcPeer.peerUid !== nodeMsg.peerUid) { needSendSelf = true; @@ -96,12 +99,15 @@ export async function handleForwardNode(destPeer: Peer, messageNodes: OB11Messag nodeMsgArray.push(nodeMsg); } nodeMsgIds = nodeMsgArray.map(msg => msg.msgId); + let retMsgIds: string[] = []; if (needSendSelf) { for (const [index, msg] of nodeMsgArray.entries()) { if (msg.peerUid === selfInfo.uid) continue; - const clonedMsg = await cloneMsg(msg); - nodeMsgIds[index] = clonedMsg?.msgId || ""; + const ClonedMsg = await cloneMsg(msg); + if (ClonedMsg) retMsgIds.push(ClonedMsg.msgId); } + } else { + retMsgIds = nodeMsgIds; } if (nodeMsgIds.length === 0) throw Error('转发消息失败,生成节点为空'); try { diff --git a/src/onebot11/action/msg/SendMsg/index.ts b/src/onebot11/action/msg/SendMsg/index.ts index 6eb51400..94255d01 100644 --- a/src/onebot11/action/msg/SendMsg/index.ts +++ b/src/onebot11/action/msg/SendMsg/index.ts @@ -140,7 +140,7 @@ export class SendMsg extends BaseAction { ); if (getSpecialMsgNum(payload, OB11MessageDataType.node)) { - const returnMsg = await handleForwardNode(peer, messages as OB11MessageNode[], peer); + const returnMsg = await handleForwardNode(peer, messages as OB11MessageNode[]); if (returnMsg) { const msgShortId = MessageUnique.createMsg({ guildId: '', peerUid: peer.peerUid, chatType: peer.chatType }, returnMsg!.msgId); return { message_id: msgShortId! };