fix: message_id过长导致koishi对接失败

perf: 初始化卡顿优化
This commit is contained in:
linyuchen 2024-02-13 21:17:16 +08:00
parent b28b812396
commit 282b2a0da0
10 changed files with 59 additions and 37 deletions

View File

@ -4,7 +4,7 @@
"name": "LLOneBot", "name": "LLOneBot",
"slug": "LLOneBot", "slug": "LLOneBot",
"description": "LiteLoaderQQNT的OneBotApi", "description": "LiteLoaderQQNT的OneBotApi",
"version": "3.0.3", "version": "3.0.4",
"thumbnail": "./icon.png", "thumbnail": "./icon.png",
"authors": [{ "authors": [{
"name": "linyuchen", "name": "linyuchen",

View File

@ -1,10 +1,30 @@
import { NTQQApi } from '../ntqqapi/ntcall'; import { NTQQApi } from '../ntqqapi/ntcall';
import { Friend, Group, GroupMember, RawMessage, SelfInfo } from "../ntqqapi/types"; import { Friend, Group, GroupMember, RawMessage, SelfInfo } from "../ntqqapi/types";
import { log } from "./utils";
export let groups: Group[] = [] export let groups: Group[] = []
export let friends: Friend[] = [] export let friends: Friend[] = []
export let msgHistory: Record<string, RawMessage> = {} // msgId: RawMessage export let msgHistory: Record<string, RawMessage> = {} // msgId: RawMessage
let globalMsgId = Date.now()
export function addHistoryMsg(msg: RawMessage){
let existMsg = msgHistory[msg.msgId]
if (existMsg){
Object.assign(existMsg, msg)
msg.msgShortId = existMsg.msgShortId;
return
}
msg.msgShortId = ++globalMsgId
msgHistory[msg.msgId] = msg
}
export function getHistoryMsgByShortId(shortId: number | string){
log("getHistoryMsgByShortId", shortId, Object.values(msgHistory).map(m=>m.msgShortId))
return Object.values(msgHistory).find(msg => msg.msgShortId.toString() == shortId.toString())
}
export async function getFriend(qq: string): Promise<Friend | undefined> { export async function getFriend(qq: string): Promise<Friend | undefined> {
let friend = friends.find(friend => friend.uin === qq) let friend = friends.find(friend => friend.uin === qq)
// if (!friend){ // if (!friend){

View File

@ -10,10 +10,9 @@ import {
CHANNEL_LOG, CHANNEL_LOG,
CHANNEL_SET_CONFIG, CHANNEL_SET_CONFIG,
} from "../common/channels"; } from "../common/channels";
import { ConfigUtil } from "../common/config";
import { postMsg, startExpress } from "../onebot11/server"; import { postMsg, startExpress } from "../onebot11/server";
import { CONFIG_DIR, getConfigUtil, log } from "../common/utils"; import { CONFIG_DIR, getConfigUtil, log } from "../common/utils";
import { friends, groups, msgHistory, selfInfo } from "../common/data"; import { addHistoryMsg, selfInfo } from "../common/data";
import { hookNTQQApiReceive, ReceiveCmd, registerReceiveHook } from "../ntqqapi/hook"; import { hookNTQQApiReceive, ReceiveCmd, registerReceiveHook } from "../ntqqapi/hook";
import { OB11Constructor } from "../onebot11/constructor"; import { OB11Constructor } from "../onebot11/constructor";
import { NTQQApi } from "../ntqqapi/ntcall"; import { NTQQApi } from "../ntqqapi/ntcall";
@ -48,7 +47,8 @@ function onLoad() {
function postRawMsg(msgList: RawMessage[]) { function postRawMsg(msgList: RawMessage[]) {
const {debug, reportSelfMessage} = getConfigUtil().getConfig(); const {debug, reportSelfMessage} = getConfigUtil().getConfig();
for (const message of msgList) { for (let message of msgList) {
addHistoryMsg(message)
OB11Constructor.message(message).then((msg) => { OB11Constructor.message(message).then((msg) => {
if (debug) { if (debug) {
msg.raw = message; msg.raw = message;
@ -86,8 +86,7 @@ function onLoad() {
NTQQApi.getGroups(true).then() NTQQApi.getGroups(true).then()
startExpress(getConfigUtil().getConfig().port) startExpress(getConfigUtil().getConfig().port)
} }
const initLoop = setInterval(async ()=>{
async function getSelfInfo() {
try { try {
const _ = await NTQQApi.getSelfInfo() const _ = await NTQQApi.getSelfInfo()
Object.assign(selfInfo, _) Object.assign(selfInfo, _)
@ -95,7 +94,6 @@ function onLoad() {
log("get self simple info", _) log("get self simple info", _)
} catch (e) { } catch (e) {
log("retry get self info") log("retry get self info")
} }
if (selfInfo.uin) { if (selfInfo.uin) {
try { try {
@ -104,22 +102,17 @@ function onLoad() {
if (userInfo) { if (userInfo) {
selfInfo.nick = userInfo.nick selfInfo.nick = userInfo.nick
} else { } else {
return setTimeout(() => { return
getSelfInfo().then()
}, 100)
} }
} catch (e) { } catch (e) {
log("get self nickname failed", e.toString()) return log("get self nickname failed", e.toString())
return setTimeout(() => {
getSelfInfo().then()
}, 100)
} }
clearInterval(initLoop);
start(); start();
} else {
setTimeout(() => {
getSelfInfo().then()
}, 100)
} }
}, 1000)
async function getSelfInfo() {
} }
getSelfInfo().then() getSelfInfo().then()

View File

@ -3,7 +3,7 @@ import { getConfigUtil, log } from "../common/utils";
import { NTQQApi, NTQQApiClass, sendMessagePool } from "./ntcall"; import { NTQQApi, NTQQApiClass, sendMessagePool } from "./ntcall";
import { Group, User } from "./types"; import { Group, User } from "./types";
import { RawMessage } from "./types"; import { RawMessage } from "./types";
import { friends, groups, msgHistory } from "../common/data"; import { addHistoryMsg, friends, groups, msgHistory } from "../common/data";
import { v4 as uuidv4 } from 'uuid'; import { v4 as uuidv4 } from 'uuid';
export let hookApiCallbacks: Record<string, (apiReturn: any) => void> = {} export let hookApiCallbacks: Record<string, (apiReturn: any) => void> = {}
@ -131,7 +131,7 @@ registerReceiveHook<{
registerReceiveHook<{ msgList: Array<RawMessage> }>(ReceiveCmd.UPDATE_MSG, (payload) => { registerReceiveHook<{ msgList: Array<RawMessage> }>(ReceiveCmd.UPDATE_MSG, (payload) => {
for (const message of payload.msgList) { for (const message of payload.msgList) {
msgHistory[message.msgId] = message; addHistoryMsg(message)
} }
}) })
@ -139,13 +139,13 @@ registerReceiveHook<{ msgList: Array<RawMessage> }>(ReceiveCmd.NEW_MSG, (payload
for (const message of payload.msgList) { for (const message of payload.msgList) {
log("收到新消息push到历史记录", message) log("收到新消息push到历史记录", message)
if (!msgHistory[message.msgId]) { if (!msgHistory[message.msgId]) {
msgHistory[message.msgId] = message addHistoryMsg(message)
} else { } else {
Object.assign(msgHistory[message.msgId], message) Object.assign(msgHistory[message.msgId], message)
} }
} }
const msgIds = Object.keys(msgHistory); const msgIds = Object.keys(msgHistory);
if (msgIds.length > 3000) { if (msgIds.length > 30000) {
delete msgHistory[msgIds.sort()[0]] delete msgHistory[msgIds.sort()[0]]
} }
}) })

View File

@ -182,6 +182,7 @@ export interface PicElement {
export interface RawMessage { export interface RawMessage {
msgId: string; msgId: string;
msgShortId?: number; // 自己维护的消息id
msgTime: string; msgTime: string;
msgSeq: string; msgSeq: string;
senderUin: string; // 发送者QQ号 senderUin: string; // 发送者QQ号

View File

@ -1,21 +1,21 @@
import { ActionName } from "./types"; import { ActionName } from "./types";
import BaseAction from "./BaseAction"; import BaseAction from "./BaseAction";
import { NTQQApi } from "../../ntqqapi/ntcall"; import { NTQQApi } from "../../ntqqapi/ntcall";
import { msgHistory } from "../../common/data"; import { getHistoryMsgByShortId, msgHistory } from "../../common/data";
interface Payload { interface Payload {
message_id: string message_id: number
} }
class DeleteMsg extends BaseAction<Payload, void> { class DeleteMsg extends BaseAction<Payload, void> {
actionName = ActionName.DeleteMsg actionName = ActionName.DeleteMsg
protected async _handle(payload:Payload){ protected async _handle(payload:Payload){
let msg = msgHistory[payload.message_id] let msg = getHistoryMsgByShortId(payload.message_id)
await NTQQApi.recallMsg({ await NTQQApi.recallMsg({
chatType: msg.chatType, chatType: msg.chatType,
peerUid: msg.peerUid peerUid: msg.peerUid
}, [payload.message_id]) }, [msg.msgId])
} }
} }

View File

@ -1,4 +1,4 @@
import { msgHistory } from "../../common/data"; import { getHistoryMsgByShortId, msgHistory } from "../../common/data";
import { OB11Message } from '../types'; import { OB11Message } from '../types';
import { OB11Constructor } from "../constructor"; import { OB11Constructor } from "../constructor";
import { log } from "../../common/utils"; import { log } from "../../common/utils";
@ -7,7 +7,7 @@ import { ActionName } from "./types";
export interface PayloadType { export interface PayloadType {
message_id: string message_id: number
} }
export type ReturnDataType = OB11Message export type ReturnDataType = OB11Message
@ -17,7 +17,7 @@ class GetMsg extends BaseAction<PayloadType, OB11Message> {
protected async _handle(payload: PayloadType){ protected async _handle(payload: PayloadType){
// log("history msg ids", Object.keys(msgHistory)); // log("history msg ids", Object.keys(msgHistory));
const msg = msgHistory[payload.message_id.toString()] const msg = getHistoryMsgByShortId(payload.message_id)
if (msg) { if (msg) {
const msgData = await OB11Constructor.message(msg); const msgData = await OB11Constructor.message(msg);
return msgData return msgData

View File

@ -1,5 +1,12 @@
import { AtType, ChatType, Group } from "../../ntqqapi/types"; import { AtType, ChatType, Group } from "../../ntqqapi/types";
import { friends, getGroup, getStrangerByUin, msgHistory } from "../../common/data"; import {
addHistoryMsg,
friends,
getGroup,
getHistoryMsgByShortId,
getStrangerByUin,
msgHistory
} from "../../common/data";
import { OB11MessageData, OB11MessageDataType, OB11PostSendMsg } from '../types'; import { OB11MessageData, OB11MessageDataType, OB11PostSendMsg } from '../types';
import { NTQQApi } from "../../ntqqapi/ntcall"; import { NTQQApi } from "../../ntqqapi/ntcall";
import { Peer } from "../../ntqqapi/ntcall"; import { Peer } from "../../ntqqapi/ntcall";
@ -13,7 +20,7 @@ import { ActionName } from "./types";
import * as fs from "fs"; import * as fs from "fs";
export interface ReturnDataType { export interface ReturnDataType {
message_id: string message_id: number
} }
class SendMsg extends BaseAction<OB11PostSendMsg, ReturnDataType> { class SendMsg extends BaseAction<OB11PostSendMsg, ReturnDataType> {
@ -90,7 +97,7 @@ class SendMsg extends BaseAction<OB11PostSendMsg, ReturnDataType> {
let replyMsgId = sendMsg.data.id; let replyMsgId = sendMsg.data.id;
if (replyMsgId) { if (replyMsgId) {
replyMsgId = replyMsgId.toString() replyMsgId = replyMsgId.toString()
const replyMsg = msgHistory[replyMsgId] const replyMsg = getHistoryMsgByShortId(replyMsgId)
if (replyMsg) { if (replyMsg) {
sendElements.push(SendMsgElementConstructor.reply(replyMsg.msgSeq, replyMsgId, replyMsg.senderUin, replyMsg.senderUin)) sendElements.push(SendMsgElementConstructor.reply(replyMsg.msgSeq, replyMsgId, replyMsg.senderUin, replyMsg.senderUin))
} }
@ -119,8 +126,9 @@ class SendMsg extends BaseAction<OB11PostSendMsg, ReturnDataType> {
// log("send msg:", peer, sendElements) // log("send msg:", peer, sendElements)
try { try {
const returnMsg = await NTQQApi.sendMsg(peer, sendElements) const returnMsg = await NTQQApi.sendMsg(peer, sendElements)
addHistoryMsg(returnMsg)
deleteAfterSentFiles.map(f=>fs.unlink(f, ()=>{})) deleteAfterSentFiles.map(f=>fs.unlink(f, ()=>{}))
return { message_id: returnMsg.msgId } return { message_id: returnMsg.msgShortId }
} catch (e) { } catch (e) {
throw(e.toString()) throw(e.toString())
} }

View File

@ -1,6 +1,6 @@
import {OB11MessageDataType, OB11GroupMemberRole, OB11Message, OB11MessageData, OB11Group, OB11GroupMember, OB11User} from "./types"; import {OB11MessageDataType, OB11GroupMemberRole, OB11Message, OB11MessageData, OB11Group, OB11GroupMember, OB11User} from "./types";
import { AtType, ChatType, Group, GroupMember, IMAGE_HTTP_HOST, RawMessage, SelfInfo, User } from '../ntqqapi/types'; import { AtType, ChatType, Group, GroupMember, IMAGE_HTTP_HOST, RawMessage, SelfInfo, User } from '../ntqqapi/types';
import { getFriend, getGroupMember, getHistoryMsgBySeq, selfInfo } from '../common/data'; import { addHistoryMsg, getFriend, getGroupMember, getHistoryMsgBySeq, selfInfo } from '../common/data';
import {file2base64, getConfigUtil, log} from "../common/utils"; import {file2base64, getConfigUtil, log} from "../common/utils";
import { NTQQApi } from "../ntqqapi/ntcall"; import { NTQQApi } from "../ntqqapi/ntcall";
@ -13,7 +13,7 @@ export class OB11Constructor {
self_id: selfInfo.uin, self_id: selfInfo.uin,
user_id: msg.senderUin, user_id: msg.senderUin,
time: parseInt(msg.msgTime) || 0, time: parseInt(msg.msgTime) || 0,
message_id: msg.msgId, message_id: msg.msgShortId,
real_id: msg.msgId, real_id: msg.msgId,
message_type: msg.chatType == ChatType.group ? "group" : "private", message_type: msg.chatType == ChatType.group ? "group" : "private",
sender: { sender: {
@ -85,7 +85,7 @@ export class OB11Constructor {
message_data["type"] = "reply" message_data["type"] = "reply"
const replyMsg = getHistoryMsgBySeq(element.replyElement.replayMsgSeq) const replyMsg = getHistoryMsgBySeq(element.replyElement.replayMsgSeq)
if (replyMsg) { if (replyMsg) {
message_data["data"]["id"] = replyMsg.msgId message_data["data"]["id"] = replyMsg.msgShortId
} }
else{ else{
continue continue

View File

@ -58,7 +58,7 @@ export enum OB11MessageType {
export interface OB11Message { export interface OB11Message {
self_id?: string, self_id?: string,
time: number, time: number,
message_id: string, message_id: number,
real_id: string, real_id: string,
user_id: string, user_id: string,
group_id?: string, group_id?: string,