From fba2078fc04b93eb3a54c2ee7d15035692fdf9f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=8B=E7=93=9C=E4=B8=80=E5=8D=81=E9=9B=AA?= Date: Mon, 12 Aug 2024 17:04:32 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E8=BF=87=E6=BB=A4=E9=87=8D=E5=A4=8D?= =?UTF-8?q?=E6=B6=88=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- manifest.json | 2 +- package.json | 2 +- src/common/framework/napcat.ts | 2 +- src/common/utils/LRU.ts | 31 +++++++++++++++++++ src/onebot/action/group/GetGroupMemberInfo.ts | 2 +- src/onebot/action/group/GetGroupMemberList.ts | 2 +- src/onebot/action/msg/DeleteMsg.ts | 2 +- .../msg/SendMsg/create-send-elements.ts | 4 +-- src/onebot/helper/data.ts | 2 +- src/onebot/index.ts | 7 +++-- src/webui/ui/NapCat.ts | 2 +- static/assets/renderer.js | 2 +- 12 files changed, 47 insertions(+), 13 deletions(-) create mode 100644 src/common/utils/LRU.ts diff --git a/manifest.json b/manifest.json index bf34e622..62ffa759 100644 --- a/manifest.json +++ b/manifest.json @@ -4,7 +4,7 @@ "name": "NapCat", "slug": "NapCat", "description": "OneBot v11 protocol implementation with NapCat logic", - "version": "2.0.1", + "version": "2.0.2", "icon": "./logo.png", "authors": [ { diff --git a/package.json b/package.json index 623fe379..ec32a9ac 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "napcat", "private": true, "type": "module", - "version": "2.0.1", + "version": "2.0.2", "scripts": { "build:framework": "vite build --mode framework", "build:shell": "vite build --mode shell", diff --git a/src/common/framework/napcat.ts b/src/common/framework/napcat.ts index d566fe43..ea332552 100644 --- a/src/common/framework/napcat.ts +++ b/src/common/framework/napcat.ts @@ -2,7 +2,7 @@ import path, { dirname } from 'path'; import { fileURLToPath } from 'url'; import fs from 'fs'; -export const napcat_version = '2.0.1'; +export const napcat_version = '2.0.2'; export class NapCatPathWrapper { binaryPath: string; diff --git a/src/common/utils/LRU.ts b/src/common/utils/LRU.ts new file mode 100644 index 00000000..1cb54212 --- /dev/null +++ b/src/common/utils/LRU.ts @@ -0,0 +1,31 @@ +export class LRUCache { + private capacity: number; + private cache: Map; + + constructor(capacity: number) { + this.capacity = capacity; + this.cache = new Map(); + } + + public get(key: K): V | undefined { + const value = this.cache.get(key); + if (value !== undefined) { + // Move the accessed key to the end to mark it as most recently used + this.cache.delete(key); + this.cache.set(key, value); + } + return value; + } + + public put(key: K, value: V): void { + if (this.cache.has(key)) { + // If the key already exists, move it to the end to mark it as most recently used + this.cache.delete(key); + } else if (this.cache.size >= this.capacity) { + // If the cache is full, remove the least recently used key (the first one in the map) + const firstKey = this.cache.keys().next().value; + this.cache.delete(firstKey); + } + this.cache.set(key, value); + } +} \ No newline at end of file diff --git a/src/onebot/action/group/GetGroupMemberInfo.ts b/src/onebot/action/group/GetGroupMemberInfo.ts index a0371b22..09fbd9e1 100644 --- a/src/onebot/action/group/GetGroupMemberInfo.ts +++ b/src/onebot/action/group/GetGroupMemberInfo.ts @@ -69,7 +69,7 @@ class GetGroupMemberInfo extends BaseAction { } } } else { - // Mlikiowa V2.0.1 Refactor Todo + // Mlikiowa V2.0.2 Refactor Todo // retMember.last_sent_time = parseInt((await getGroupMember(payload.group_id.toString(), retMember.user_id))?.lastSpeakTime || date.toString()); // retMember.join_time = parseInt((await getGroupMember(payload.group_id.toString(), retMember.user_id))?.joinTime || date.toString()); } diff --git a/src/onebot/action/group/GetGroupMemberList.ts b/src/onebot/action/group/GetGroupMemberList.ts index 7ad2b9b0..ce13f2bf 100644 --- a/src/onebot/action/group/GetGroupMemberList.ts +++ b/src/onebot/action/group/GetGroupMemberList.ts @@ -83,7 +83,7 @@ class GetGroupMemberList extends BaseAction { } } } else { - // Mlikiowa V2.0.1 Refactor Todo + // Mlikiowa V2.0.2 Refactor Todo // _groupMembers.forEach(async item => { // item.last_sent_time = parseInt((await getGroupMember(payload.group_id.toString(), item.user_id))?.lastSpeakTime || date.toString()); // item.join_time = parseInt((await getGroupMember(payload.group_id.toString(), item.user_id))?.joinTime || date.toString()); diff --git a/src/onebot/action/msg/DeleteMsg.ts b/src/onebot/action/msg/DeleteMsg.ts index 837b43d6..a7c6fece 100644 --- a/src/onebot/action/msg/DeleteMsg.ts +++ b/src/onebot/action/msg/DeleteMsg.ts @@ -44,7 +44,7 @@ class DeleteMsg extends BaseAction { await NTQQMsgApi.recallMsg(msg.Peer, [msg.MsgId]); const data = await ret; if (!data) { - throw new Error('Recall failed'); + //throw new Error('Recall failed'); } //await sleep(100); //await NTQQMsgApi.getMsgsByMsgId(msg.Peer, [msg.MsgId]); diff --git a/src/onebot/action/msg/SendMsg/create-send-elements.ts b/src/onebot/action/msg/SendMsg/create-send-elements.ts index 1a9df8f2..31f9109a 100644 --- a/src/onebot/action/msg/SendMsg/create-send-elements.ts +++ b/src/onebot/action/msg/SendMsg/create-send-elements.ts @@ -56,7 +56,7 @@ const _handlers: { if (atQQ === 'all') return SendMsgElementConstructor.at(coreContext, atQQ, atQQ, AtType.atAll, '全体成员'); // then the qq is a group member - // Mlikiowa V2.0.1 Refactor Todo + // Mlikiowa V2.0.2 Refactor Todo const uid = await coreContext.apis.UserApi.getUidByUinV2(atQQ); if (!uid) throw new Error('Get Uid Error'); return SendMsgElementConstructor.at(coreContext, atQQ, uid, AtType.atUser, ''); @@ -161,7 +161,7 @@ const _handlers: { } else { postData = data; } - // Mlikiowa V2.0.1 Refactor Todo + // Mlikiowa V2.0.2 Refactor Todo const signUrl = obContext.configLoader.configData.musicSignUrl; if (!signUrl) { if (data.type === 'qq') { diff --git a/src/onebot/helper/data.ts b/src/onebot/helper/data.ts index 763145ae..8f41f29e 100644 --- a/src/onebot/helper/data.ts +++ b/src/onebot/helper/data.ts @@ -408,7 +408,7 @@ export class OB11Constructor { return; } //log("group msg", msg); - // Mlikiowa V2.0.1 Refactor Todo + // Mlikiowa V2.0.2 Refactor Todo // if (msg.senderUin && msg.senderUin !== '0') { // const member = await getGroupMember(msg.peerUid, msg.senderUin); // if (member && member.cardName !== msg.sendMemberName) { diff --git a/src/onebot/index.ts b/src/onebot/index.ts index b3b5f60e..28cf6c2f 100644 --- a/src/onebot/index.ts +++ b/src/onebot/index.ts @@ -33,6 +33,7 @@ import { GroupDecreaseSubType, OB11GroupDecreaseEvent } from '@/onebot/event/not import { OB11GroupRequestEvent } from '@/onebot/event/request/OB11GroupRequest'; import { OB11FriendRecallNoticeEvent } from '@/onebot/event/notice/OB11FriendRecallNoticeEvent'; import { OB11GroupRecallNoticeEvent } from '@/onebot/event/notice/OB11GroupRecallNoticeEvent'; +import { LRUCache } from '@/common/utils/LRU'; //OneBot实现类 export class NapCatOneBot11Adapter { @@ -215,7 +216,6 @@ export class NapCatOneBot11Adapter { private initMsgListener() { const msgListener = new MsgListener(); - msgListener.onInputStatusPush = async data => { const uin = await this.core.apis.UserApi.getUinByUidV2(data.fromUin); this.context.logger.log(`[Notice] [输入状态] ${uin} ${data.statusText}`); @@ -245,12 +245,15 @@ export class NapCatOneBot11Adapter { .catch(e => this.context.logger.logError('处理消息失败', e)); } }; - + const msgIdSend = new LRUCache(100); msgListener.onMsgInfoListUpdate = async msgList => { this.emitRecallMsg(msgList) .catch(e => this.context.logger.logError('处理消息失败', e)); + for (const msg of msgList.filter(e => e.senderUin == this.core.selfInfo.uin)) { // console.log(msg); + if (!!msgIdSend.get(msg.msgId)) continue; + msgIdSend.put(msg.msgId, true); if (msg.sendStatus == 2) { // 完成后再post OB11Constructor.message(this.core, msg, this.configLoader.configData.messagePostFormat) diff --git a/src/webui/ui/NapCat.ts b/src/webui/ui/NapCat.ts index 17263258..265fc317 100644 --- a/src/webui/ui/NapCat.ts +++ b/src/webui/ui/NapCat.ts @@ -30,7 +30,7 @@ async function onSettingWindowCreated(view: Element) { SettingItem( 'Napcat', undefined, - SettingButton('V2.0.1', 'napcat-update-button', 'secondary'), + SettingButton('V2.0.2', 'napcat-update-button', 'secondary'), ), ]), SettingList([ diff --git a/static/assets/renderer.js b/static/assets/renderer.js index 2cbcdb4b..9564787d 100644 --- a/static/assets/renderer.js +++ b/static/assets/renderer.js @@ -164,7 +164,7 @@ async function onSettingWindowCreated(view) { SettingItem( 'Napcat', void 0, - SettingButton("V2.0.1", "napcat-update-button", "secondary") + SettingButton("V2.0.2", "napcat-update-button", "secondary") ) ]), SettingList([