From be605f11f25aeb495d8c2d5d85d8089cc828f426 Mon Sep 17 00:00:00 2001 From: Alen Date: Sat, 27 Jul 2024 15:01:08 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=8F=90=E4=BA=A4=E7=96=8F?= =?UTF-8?q?=E6=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修改在查询群历史消息时,如未提供msg_seq,则返回最新消息 --- src/common/utils/MessageUnique.ts | 23 +++++++++++++++++++ .../action/go-cqhttp/GetGroupMsgHistory.ts | 15 +++++++----- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/common/utils/MessageUnique.ts b/src/common/utils/MessageUnique.ts index 30c4d09d..ac6190ed 100644 --- a/src/common/utils/MessageUnique.ts +++ b/src/common/utils/MessageUnique.ts @@ -61,6 +61,10 @@ class LimitedHashTable { this.valueToKey.delete(value); } } + + getKeyList(): K[] { + return Array.from(this.keyToValue.keys()); + } } class MessageUniqueWrapper { @@ -111,6 +115,25 @@ class MessageUniqueWrapper { this.msgIdMap.resize(maxSize); this.msgDataMap.resize(maxSize); } + getNthLatestShortIdByPeer(peer: Peer, index: number = 1): number | undefined { + const peerUid = peer.peerUid; + const chatType = peer.chatType; + const keys = this.msgDataMap.getKeyList(); + const matches: number[] = []; + for (const key of keys) { + const [msgId, chatTypeStr, peerUidStr] = key.split('|'); + if (peerUidStr === peerUid && chatTypeStr === chatType.toString()) { + const shortId = this.msgDataMap.getValue(key); + if (shortId) { + matches.push(shortId); + } + } + } + if (matches.length >= index) { + return matches[matches.length - index]; + } + return undefined; + } } export const MessageUnique: MessageUniqueWrapper = new MessageUniqueWrapper(); \ No newline at end of file diff --git a/src/onebot11/action/go-cqhttp/GetGroupMsgHistory.ts b/src/onebot11/action/go-cqhttp/GetGroupMsgHistory.ts index c1f43021..a2e051af 100644 --- a/src/onebot11/action/go-cqhttp/GetGroupMsgHistory.ts +++ b/src/onebot11/action/go-cqhttp/GetGroupMsgHistory.ts @@ -18,7 +18,7 @@ const SchemaData = { message_seq: { type: 'number' }, count: { type: 'number' } }, - required: ['group_id', 'count'] + required: [ 'group_id' ] } as const satisfies JSONSchema; type Payload = FromSchema; @@ -31,13 +31,16 @@ export default class GoCQHTTPGetGroupMsgHistory extends BaseAction { msg.id = await MessageUnique.createMsg({ guildId: '', chatType: msg.chatType, peerUid: msg.peerUid }, msg.msgId);