refactor: selfMsgPost

This commit is contained in:
手瓜一十雪 2024-07-29 00:16:24 +08:00
parent 361a164f2a
commit cd256235da
4 changed files with 17 additions and 21 deletions

View File

@ -96,6 +96,9 @@ export class NTQQMsgApi {
static async getMsgsByMsgId(peer: Peer, msgIds: string[]) { static async getMsgsByMsgId(peer: Peer, msgIds: string[]) {
return await napCatCore.session.getMsgService().getMsgsByMsgId(peer, msgIds); return await napCatCore.session.getMsgService().getMsgsByMsgId(peer, msgIds);
} }
static async getSingleMsg(peer: Peer, seq: string) {
return await napCatCore.session.getMsgService().getSingleMsg(peer, seq);
}
static async queryMsgsWithFilterExWithSeq(peer: Peer, msgSeq: string, msgTime: string, senderUid: string) { static async queryMsgsWithFilterExWithSeq(peer: Peer, msgSeq: string, msgTime: string, senderUid: string) {
let ret = await napCatCore.session.getMsgService().queryMsgsWithFilterEx('0', msgTime, msgSeq, { let ret = await napCatCore.session.getMsgService().queryMsgsWithFilterEx('0', msgTime, msgSeq, {
chatInfo: peer, chatInfo: peer,

View File

@ -176,7 +176,7 @@ export interface NodeIKernelMsgService {
getMsgsBySeqList(peer: Peer, seqList: string[]): Promise<GeneralCallResult & { msgList: RawMessage[] }>; getMsgsBySeqList(peer: Peer, seqList: string[]): Promise<GeneralCallResult & { msgList: RawMessage[] }>;
getSingleMsg(Peer: Peer, msgSeq: string): unknown; getSingleMsg(Peer: Peer, msgSeq: string): Promise<GeneralCallResult & { msgList: RawMessage[] }>;
getSourceOfReplyMsg(...args: unknown[]): unknown; getSourceOfReplyMsg(...args: unknown[]): unknown;

View File

@ -139,6 +139,7 @@ export class OB11Constructor {
try { try {
//做这么多都是因为NC速度太快 可能nt还没有写入数据库 //做这么多都是因为NC速度太快 可能nt还没有写入数据库
let records = msg.records.find(msgRecord => msgRecord.msgId === element.replyElement.sourceMsgIdInRecords); let records = msg.records.find(msgRecord => msgRecord.msgId === element.replyElement.sourceMsgIdInRecords);
if (!records) { if (!records) {
throw new Error('Record筛选失败'); throw new Error('Record筛选失败');
} }
@ -147,18 +148,12 @@ export class OB11Constructor {
peerUid: msg.peerUid, peerUid: msg.peerUid,
guildId: '', guildId: '',
}; };
let replyMsg: RawMessage | undefined; let replyMsg: RawMessage | undefined;
replyMsg = (await NTQQMsgApi.getMsgsBySeqAndCount({ peerUid: msg.peerUid, guildId: '', chatType: msg.chatType }, element.replyElement.replayMsgSeq, 1, true, true)).msgList[0]; replyMsg = (await NTQQMsgApi.getMsgsBySeqAndCount({ peerUid: msg.peerUid, guildId: '', chatType: msg.chatType }, element.replyElement.replayMsgSeq, 1, true, true)).msgList[0];
if (!replyMsg || replyMsg.msgRandom !== records.msgRandom) { if (!replyMsg || replyMsg.msgRandom !== records.msgRandom) {
//logWarn(`消息比对失败,准备重新尝试 Info: CurrentMsgRandom:${replyMsg?.msgRandom}/TargetMsgRandom:${records.msgRandom}`);
await sleep(700);
replyMsg = (await NTQQMsgApi.getMsgsByMsgId(peer, MessageUnique.getRecentMsgIds(peer, 50))).msgList.find((msg) => msg.msgRandom == records.msgRandom && msg.msgSeq == element.replyElement.replayMsgSeq); replyMsg = (await NTQQMsgApi.getMsgsByMsgId(peer, MessageUnique.getRecentMsgIds(peer, 50))).msgList.find((msg) => msg.msgRandom == records.msgRandom && msg.msgSeq == element.replyElement.replayMsgSeq);
} }
if (!replyMsg || replyMsg.msgRandom !== records.msgRandom) { if (!replyMsg || replyMsg.msgRandom !== records.msgRandom) {
//logWarn(`消息比对失败,准备重新尝试 Info: CurrentMsgRandom:${replyMsg?.msgRandom}/TargetMsgRandom:${records.msgRandom}`);
await sleep(700);
replyMsg = (await NTQQMsgApi.queryMsgsWithFilterExWithSeq( replyMsg = (await NTQQMsgApi.queryMsgsWithFilterExWithSeq(
peer, peer,
element.replyElement.replayMsgSeq, element.replyElement.replayMsgSeq,
@ -166,15 +161,10 @@ export class OB11Constructor {
records.senderUid records.senderUid
)).msgList[0]; )).msgList[0];
} }
// 最后尝试第一次的方法
if (!replyMsg || replyMsg.msgRandom !== records.msgRandom) { if (!replyMsg || replyMsg.msgRandom !== records.msgRandom) {
//logWarn(`消息比对失败,准备重新尝试 Info: CurrentMsgRandom:${replyMsg?.msgRandom}/TargetMsgRandom:${records.msgRandom}`); replyMsg = (await NTQQMsgApi.getSingleMsg(peer, element.replyElement.replayMsgSeq)).msgList[0];
await sleep(700);
replyMsg = (await NTQQMsgApi.getMsgsBySeqAndCount({ peerUid: msg.peerUid, guildId: '', chatType: msg.chatType }, element.replyElement.replayMsgSeq, 1, true, true)).msgList[0];
} }
if (!replyMsg || replyMsg.msgRandom !== records.msgRandom) { if (!replyMsg || replyMsg.msgRandom !== records.msgRandom) {
//logWarn(`消息比对失败,准备重新尝试 Info: CurrentMsgRandom:${replyMsg?.msgRandom}/TargetMsgRandom:${records.msgRandom}`);
throw new Error('回复消息消息验证失败') throw new Error('回复消息消息验证失败')
} }
if (replyMsg) { if (replyMsg) {

View File

@ -240,8 +240,7 @@ export class NapCatOnebot11 {
}; };
msgListener.onMsgInfoListUpdate = (msgList) => { msgListener.onMsgInfoListUpdate = (msgList) => {
this.postRecallMsg(msgList).then().catch(logError); this.postRecallMsg(msgList).then().catch(logError);
}; for (const msg of msgList.filter(e => e.senderUin == selfInfo.uin)) {
msgListener.onAddSendMsg = (msg) => {
OB11Constructor.message(msg).then((_msg) => { OB11Constructor.message(msg).then((_msg) => {
_msg.target_id = parseInt(msg.peerUin); _msg.target_id = parseInt(msg.peerUin);
logMessage(_msg as OB11Message).then().catch(logError); logMessage(_msg as OB11Message).then().catch(logError);
@ -250,6 +249,10 @@ export class NapCatOnebot11 {
msg.id = MessageUnique.createMsg({ chatType: msg.chatType, peerUid: msg.peerUid, guildId: '' }, msg.msgId); msg.id = MessageUnique.createMsg({ chatType: msg.chatType, peerUid: msg.peerUid, guildId: '' }, msg.msgId);
this.postReceiveMsg([msg]).then().catch(logError); this.postReceiveMsg([msg]).then().catch(logError);
} }
}
};
msgListener.onAddSendMsg = (msg) => {
}; };
napCatCore.addListener(msgListener); napCatCore.addListener(msgListener);
logDebug('ob11 msg listener added'); logDebug('ob11 msg listener added');