From f8b2be246f04fb0ed87cc9f89d1c32062124d34e Mon Sep 17 00:00:00 2001 From: idranme Date: Sat, 31 Aug 2024 22:55:26 +0800 Subject: [PATCH] optimize --- src/common/utils/messageUnique.ts | 2 +- src/ntqqapi/ntcall.ts | 17 +++++++-------- src/onebot11/entities.ts | 35 ++++++++++++++----------------- 3 files changed, 25 insertions(+), 29 deletions(-) diff --git a/src/common/utils/messageUnique.ts b/src/common/utils/messageUnique.ts index c733a76..3df7330 100644 --- a/src/common/utils/messageUnique.ts +++ b/src/common/utils/messageUnique.ts @@ -82,7 +82,7 @@ class MessageUniqueWrapper { return ret.map((t) => t?.MsgId).filter((t) => t !== undefined) } - createMsg(peer: Peer, msgId: string): number | undefined { + createMsg(peer: Peer, msgId: string): number { const key = `${msgId}|${peer.chatType}|${peer.peerUid}` const hash = createHash('md5').update(key).digest() //设置第一个bit为0 保证shortId为正数 diff --git a/src/ntqqapi/ntcall.ts b/src/ntqqapi/ntcall.ts index ea8248b..ee04296 100644 --- a/src/ntqqapi/ntcall.ts +++ b/src/ntqqapi/ntcall.ts @@ -136,25 +136,24 @@ export function invoke< R extends Awaited any ? NTService[S][M] : any>>, S extends keyof NTService = any, M extends keyof NTService[S] & string = any ->(method: `${unknown extends `${S}/${M}` ? `${S}/${M}` : string}`, args?: unknown[], options: InvokeOptions = {}) { +>(method: `${unknown extends `${S}/${M}` ? `${S}/${M}` : string}`, args: unknown[], options: InvokeOptions = {}) { const className = options.className ?? NTClass.NT_API const channel = options.channel ?? NTChannel.IPC_UP_2 const timeout = options.timeout ?? 5000 const afterFirstCmd = options.afterFirstCmd ?? true - const uuid = randomUUID() let eventName = className + '-' + channel[channel.length - 1] if (options.classNameIsRegister) { eventName += '-register' } - const apiArgs = [method, ...(args ?? [])] - //log('callNTQQApi', channel, eventName, apiArgs, uuid) - return new Promise((resolve: (data: R) => void, reject) => { + return new Promise((resolve, reject) => { + const apiArgs = [method, ...args] + const callbackId = randomUUID() let success = false if (!options.cbCmd) { // QQ后端会返回结果,并且可以根据uuid识别 - hookApiCallbacks[uuid] = (r: R) => { + hookApiCallbacks[callbackId] = res => { success = true - resolve(r) + resolve(res) } } else { @@ -177,7 +176,7 @@ export function invoke< }) } !afterFirstCmd && secondCallback() - hookApiCallbacks[uuid] = (result: GeneralCallResult) => { + hookApiCallbacks[callbackId] = (result: GeneralCallResult) => { if (result?.result === 0 || result === undefined) { //log(`${params.methodName} callback`, result) afterFirstCmd && secondCallback() @@ -203,7 +202,7 @@ export function invoke< }, }, }, - { type: 'request', callbackId: uuid, eventName }, + { type: 'request', callbackId, eventName }, apiArgs, ) }) diff --git a/src/onebot11/entities.ts b/src/onebot11/entities.ts index 003738e..fb9fad2 100644 --- a/src/onebot11/entities.ts +++ b/src/onebot11/entities.ts @@ -146,34 +146,31 @@ export namespace OB11Entities { message_data['data']['text'] = text } else if (element.replyElement) { - message_data['type'] = OB11MessageDataType.reply + const { replyElement } = element + const peer = { + chatType: msg.chatType, + peerUid: msg.peerUid, + guildId: '' + } try { - const records = msg.records.find(msgRecord => msgRecord.msgId === element.replyElement.sourceMsgIdInRecords) + const records = msg.records.find(msgRecord => msgRecord.msgId === replyElement.sourceMsgIdInRecords) if (!records) throw new Error('找不到回复消息') - let replyMsg = (await ctx.ntMsgApi.getMsgsBySeqAndCount({ - peerUid: msg.peerUid, - guildId: '', - chatType: msg.chatType, - }, element.replyElement.replayMsgSeq, 1, true, true))?.msgList[0] + let replyMsg = (await ctx.ntMsgApi.getMsgsBySeqAndCount(peer, replyElement.replayMsgSeq, 1, true, true)).msgList[0] if (!replyMsg || records.msgRandom !== replyMsg.msgRandom) { - const peer = { - chatType: msg.chatType, - peerUid: msg.peerUid, - guildId: '', - } - replyMsg = (await ctx.ntMsgApi.getSingleMsg(peer, element.replyElement.replayMsgSeq))?.msgList[0] + replyMsg = (await ctx.ntMsgApi.getSingleMsg(peer, replyElement.replayMsgSeq)).msgList[0] } // 284840486: 合并消息内侧 消息具体定位不到 if ((!replyMsg || records.msgRandom !== replyMsg.msgRandom) && msg.peerUin !== '284840486') { throw new Error('回复消息消息验证失败') } - message_data['data']['id'] = replyMsg && MessageUnique.createMsg({ - peerUid: msg.peerUid, - guildId: '', - chatType: msg.chatType, - }, replyMsg.msgId)?.toString() + message_data = { + type: OB11MessageDataType.reply, + data: { + id: MessageUnique.createMsg(peer, replyMsg ? replyMsg.msgId : records.msgId).toString() + } + } } catch (e: any) { - ctx.logger.error('获取不到引用的消息', e.stack, element.replyElement.replayMsgSeq) + ctx.logger.error('获取不到引用的消息', replyElement.replayMsgSeq, e.stack) continue } }