chore: 过滤重复消息

This commit is contained in:
手瓜一十雪 2024-08-12 17:04:32 +08:00
parent 20a37fe2de
commit fba2078fc0
12 changed files with 47 additions and 13 deletions

View File

@ -4,7 +4,7 @@
"name": "NapCat", "name": "NapCat",
"slug": "NapCat", "slug": "NapCat",
"description": "OneBot v11 protocol implementation with NapCat logic", "description": "OneBot v11 protocol implementation with NapCat logic",
"version": "2.0.1", "version": "2.0.2",
"icon": "./logo.png", "icon": "./logo.png",
"authors": [ "authors": [
{ {

View File

@ -2,7 +2,7 @@
"name": "napcat", "name": "napcat",
"private": true, "private": true,
"type": "module", "type": "module",
"version": "2.0.1", "version": "2.0.2",
"scripts": { "scripts": {
"build:framework": "vite build --mode framework", "build:framework": "vite build --mode framework",
"build:shell": "vite build --mode shell", "build:shell": "vite build --mode shell",

View File

@ -2,7 +2,7 @@ import path, { dirname } from 'path';
import { fileURLToPath } from 'url'; import { fileURLToPath } from 'url';
import fs from 'fs'; import fs from 'fs';
export const napcat_version = '2.0.1'; export const napcat_version = '2.0.2';
export class NapCatPathWrapper { export class NapCatPathWrapper {
binaryPath: string; binaryPath: string;

31
src/common/utils/LRU.ts Normal file
View File

@ -0,0 +1,31 @@
export class LRUCache<K, V> {
private capacity: number;
private cache: Map<K, V>;
constructor(capacity: number) {
this.capacity = capacity;
this.cache = new Map<K, V>();
}
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);
}
}

View File

@ -69,7 +69,7 @@ class GetGroupMemberInfo extends BaseAction<Payload, OB11GroupMember> {
} }
} }
} else { } 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.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()); // retMember.join_time = parseInt((await getGroupMember(payload.group_id.toString(), retMember.user_id))?.joinTime || date.toString());
} }

View File

@ -83,7 +83,7 @@ class GetGroupMemberList extends BaseAction<Payload, OB11GroupMember[]> {
} }
} }
} else { } else {
// Mlikiowa V2.0.1 Refactor Todo // Mlikiowa V2.0.2 Refactor Todo
// _groupMembers.forEach(async item => { // _groupMembers.forEach(async item => {
// item.last_sent_time = parseInt((await getGroupMember(payload.group_id.toString(), item.user_id))?.lastSpeakTime || date.toString()); // 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()); // item.join_time = parseInt((await getGroupMember(payload.group_id.toString(), item.user_id))?.joinTime || date.toString());

View File

@ -44,7 +44,7 @@ class DeleteMsg extends BaseAction<Payload, void> {
await NTQQMsgApi.recallMsg(msg.Peer, [msg.MsgId]); await NTQQMsgApi.recallMsg(msg.Peer, [msg.MsgId]);
const data = await ret; const data = await ret;
if (!data) { if (!data) {
throw new Error('Recall failed'); //throw new Error('Recall failed');
} }
//await sleep(100); //await sleep(100);
//await NTQQMsgApi.getMsgsByMsgId(msg.Peer, [msg.MsgId]); //await NTQQMsgApi.getMsgsByMsgId(msg.Peer, [msg.MsgId]);

View File

@ -56,7 +56,7 @@ const _handlers: {
if (atQQ === 'all') return SendMsgElementConstructor.at(coreContext, atQQ, atQQ, AtType.atAll, '全体成员'); if (atQQ === 'all') return SendMsgElementConstructor.at(coreContext, atQQ, atQQ, AtType.atAll, '全体成员');
// then the qq is a group member // 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); const uid = await coreContext.apis.UserApi.getUidByUinV2(atQQ);
if (!uid) throw new Error('Get Uid Error'); if (!uid) throw new Error('Get Uid Error');
return SendMsgElementConstructor.at(coreContext, atQQ, uid, AtType.atUser, ''); return SendMsgElementConstructor.at(coreContext, atQQ, uid, AtType.atUser, '');
@ -161,7 +161,7 @@ const _handlers: {
} else { } else {
postData = data; postData = data;
} }
// Mlikiowa V2.0.1 Refactor Todo // Mlikiowa V2.0.2 Refactor Todo
const signUrl = obContext.configLoader.configData.musicSignUrl; const signUrl = obContext.configLoader.configData.musicSignUrl;
if (!signUrl) { if (!signUrl) {
if (data.type === 'qq') { if (data.type === 'qq') {

View File

@ -408,7 +408,7 @@ export class OB11Constructor {
return; return;
} }
//log("group msg", msg); //log("group msg", msg);
// Mlikiowa V2.0.1 Refactor Todo // Mlikiowa V2.0.2 Refactor Todo
// if (msg.senderUin && msg.senderUin !== '0') { // if (msg.senderUin && msg.senderUin !== '0') {
// const member = await getGroupMember(msg.peerUid, msg.senderUin); // const member = await getGroupMember(msg.peerUid, msg.senderUin);
// if (member && member.cardName !== msg.sendMemberName) { // if (member && member.cardName !== msg.sendMemberName) {

View File

@ -33,6 +33,7 @@ import { GroupDecreaseSubType, OB11GroupDecreaseEvent } from '@/onebot/event/not
import { OB11GroupRequestEvent } from '@/onebot/event/request/OB11GroupRequest'; import { OB11GroupRequestEvent } from '@/onebot/event/request/OB11GroupRequest';
import { OB11FriendRecallNoticeEvent } from '@/onebot/event/notice/OB11FriendRecallNoticeEvent'; import { OB11FriendRecallNoticeEvent } from '@/onebot/event/notice/OB11FriendRecallNoticeEvent';
import { OB11GroupRecallNoticeEvent } from '@/onebot/event/notice/OB11GroupRecallNoticeEvent'; import { OB11GroupRecallNoticeEvent } from '@/onebot/event/notice/OB11GroupRecallNoticeEvent';
import { LRUCache } from '@/common/utils/LRU';
//OneBot实现类 //OneBot实现类
export class NapCatOneBot11Adapter { export class NapCatOneBot11Adapter {
@ -215,7 +216,6 @@ export class NapCatOneBot11Adapter {
private initMsgListener() { private initMsgListener() {
const msgListener = new MsgListener(); const msgListener = new MsgListener();
msgListener.onInputStatusPush = async data => { msgListener.onInputStatusPush = async data => {
const uin = await this.core.apis.UserApi.getUinByUidV2(data.fromUin); const uin = await this.core.apis.UserApi.getUinByUidV2(data.fromUin);
this.context.logger.log(`[Notice] [输入状态] ${uin} ${data.statusText}`); this.context.logger.log(`[Notice] [输入状态] ${uin} ${data.statusText}`);
@ -245,12 +245,15 @@ export class NapCatOneBot11Adapter {
.catch(e => this.context.logger.logError('处理消息失败', e)); .catch(e => this.context.logger.logError('处理消息失败', e));
} }
}; };
const msgIdSend = new LRUCache<string, boolean>(100);
msgListener.onMsgInfoListUpdate = async msgList => { msgListener.onMsgInfoListUpdate = async msgList => {
this.emitRecallMsg(msgList) this.emitRecallMsg(msgList)
.catch(e => this.context.logger.logError('处理消息失败', e)); .catch(e => this.context.logger.logError('处理消息失败', e));
for (const msg of msgList.filter(e => e.senderUin == this.core.selfInfo.uin)) { for (const msg of msgList.filter(e => e.senderUin == this.core.selfInfo.uin)) {
// console.log(msg); // console.log(msg);
if (!!msgIdSend.get(msg.msgId)) continue;
msgIdSend.put(msg.msgId, true);
if (msg.sendStatus == 2) { if (msg.sendStatus == 2) {
// 完成后再post // 完成后再post
OB11Constructor.message(this.core, msg, this.configLoader.configData.messagePostFormat) OB11Constructor.message(this.core, msg, this.configLoader.configData.messagePostFormat)

View File

@ -30,7 +30,7 @@ async function onSettingWindowCreated(view: Element) {
SettingItem( SettingItem(
'<span id="napcat-update-title">Napcat</span>', '<span id="napcat-update-title">Napcat</span>',
undefined, undefined,
SettingButton('V2.0.1', 'napcat-update-button', 'secondary'), SettingButton('V2.0.2', 'napcat-update-button', 'secondary'),
), ),
]), ]),
SettingList([ SettingList([

View File

@ -164,7 +164,7 @@ async function onSettingWindowCreated(view) {
SettingItem( SettingItem(
'<span id="napcat-update-title">Napcat</span>', '<span id="napcat-update-title">Napcat</span>',
void 0, void 0,
SettingButton("V2.0.1", "napcat-update-button", "secondary") SettingButton("V2.0.2", "napcat-update-button", "secondary")
) )
]), ]),
SettingList([ SettingList([