diff --git a/manifest.json b/manifest.json index 7f133d1..400682d 100644 --- a/manifest.json +++ b/manifest.json @@ -4,7 +4,7 @@ "name": "LLOneBot", "slug": "LLOneBot", "description": "LiteLoaderQQNT的OneBotApi", - "version": "3.0.3", + "version": "3.0.4", "thumbnail": "./icon.png", "authors": [{ "name": "linyuchen", diff --git a/src/common/data.ts b/src/common/data.ts index 57a9d8a..fff86d1 100644 --- a/src/common/data.ts +++ b/src/common/data.ts @@ -1,10 +1,30 @@ import { NTQQApi } from '../ntqqapi/ntcall'; import { Friend, Group, GroupMember, RawMessage, SelfInfo } from "../ntqqapi/types"; +import { log } from "./utils"; export let groups: Group[] = [] export let friends: Friend[] = [] 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> { let friend = friends.find(friend => friend.uin === qq) // if (!friend){ diff --git a/src/main/main.ts b/src/main/main.ts index d055e69..9edb41e 100644 --- a/src/main/main.ts +++ b/src/main/main.ts @@ -10,10 +10,9 @@ import { CHANNEL_LOG, CHANNEL_SET_CONFIG, } from "../common/channels"; -import { ConfigUtil } from "../common/config"; import { postMsg, startExpress } from "../onebot11/server"; 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 { OB11Constructor } from "../onebot11/constructor"; import { NTQQApi } from "../ntqqapi/ntcall"; @@ -48,7 +47,8 @@ function onLoad() { function postRawMsg(msgList: RawMessage[]) { const {debug, reportSelfMessage} = getConfigUtil().getConfig(); - for (const message of msgList) { + for (let message of msgList) { + addHistoryMsg(message) OB11Constructor.message(message).then((msg) => { if (debug) { msg.raw = message; @@ -86,8 +86,7 @@ function onLoad() { NTQQApi.getGroups(true).then() startExpress(getConfigUtil().getConfig().port) } - - async function getSelfInfo() { + const initLoop = setInterval(async ()=>{ try { const _ = await NTQQApi.getSelfInfo() Object.assign(selfInfo, _) @@ -95,7 +94,6 @@ function onLoad() { log("get self simple info", _) } catch (e) { log("retry get self info") - } if (selfInfo.uin) { try { @@ -104,22 +102,17 @@ function onLoad() { if (userInfo) { selfInfo.nick = userInfo.nick } else { - return setTimeout(() => { - getSelfInfo().then() - }, 100) + return } } catch (e) { - log("get self nickname failed", e.toString()) - return setTimeout(() => { - getSelfInfo().then() - }, 100) + return log("get self nickname failed", e.toString()) } + clearInterval(initLoop); start(); - } else { - setTimeout(() => { - getSelfInfo().then() - }, 100) } + }, 1000) + async function getSelfInfo() { + } getSelfInfo().then() diff --git a/src/ntqqapi/hook.ts b/src/ntqqapi/hook.ts index c458508..01646f0 100644 --- a/src/ntqqapi/hook.ts +++ b/src/ntqqapi/hook.ts @@ -3,7 +3,7 @@ import { getConfigUtil, log } from "../common/utils"; import { NTQQApi, NTQQApiClass, sendMessagePool } from "./ntcall"; import { Group, User } 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'; export let hookApiCallbacks: Record<string, (apiReturn: any) => void> = {} @@ -131,7 +131,7 @@ registerReceiveHook<{ registerReceiveHook<{ msgList: Array<RawMessage> }>(ReceiveCmd.UPDATE_MSG, (payload) => { 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) { log("收到新消息,push到历史记录", message) if (!msgHistory[message.msgId]) { - msgHistory[message.msgId] = message + addHistoryMsg(message) } else { Object.assign(msgHistory[message.msgId], message) } } const msgIds = Object.keys(msgHistory); - if (msgIds.length > 3000) { + if (msgIds.length > 30000) { delete msgHistory[msgIds.sort()[0]] } }) diff --git a/src/ntqqapi/types.ts b/src/ntqqapi/types.ts index 112364c..da86cf8 100644 --- a/src/ntqqapi/types.ts +++ b/src/ntqqapi/types.ts @@ -182,6 +182,7 @@ export interface PicElement { export interface RawMessage { msgId: string; + msgShortId?: number; // 自己维护的消息id msgTime: string; msgSeq: string; senderUin: string; // 发送者QQ号 diff --git a/src/onebot11/actions/DeleteMsg.ts b/src/onebot11/actions/DeleteMsg.ts index 37b7f36..7c999c3 100644 --- a/src/onebot11/actions/DeleteMsg.ts +++ b/src/onebot11/actions/DeleteMsg.ts @@ -1,21 +1,21 @@ import { ActionName } from "./types"; import BaseAction from "./BaseAction"; import { NTQQApi } from "../../ntqqapi/ntcall"; -import { msgHistory } from "../../common/data"; +import { getHistoryMsgByShortId, msgHistory } from "../../common/data"; interface Payload { - message_id: string + message_id: number } class DeleteMsg extends BaseAction<Payload, void> { actionName = ActionName.DeleteMsg protected async _handle(payload:Payload){ - let msg = msgHistory[payload.message_id] + let msg = getHistoryMsgByShortId(payload.message_id) await NTQQApi.recallMsg({ chatType: msg.chatType, peerUid: msg.peerUid - }, [payload.message_id]) + }, [msg.msgId]) } } diff --git a/src/onebot11/actions/GetMsg.ts b/src/onebot11/actions/GetMsg.ts index f1ff314..538c2be 100644 --- a/src/onebot11/actions/GetMsg.ts +++ b/src/onebot11/actions/GetMsg.ts @@ -1,4 +1,4 @@ -import { msgHistory } from "../../common/data"; +import { getHistoryMsgByShortId, msgHistory } from "../../common/data"; import { OB11Message } from '../types'; import { OB11Constructor } from "../constructor"; import { log } from "../../common/utils"; @@ -7,7 +7,7 @@ import { ActionName } from "./types"; export interface PayloadType { - message_id: string + message_id: number } export type ReturnDataType = OB11Message @@ -17,7 +17,7 @@ class GetMsg extends BaseAction<PayloadType, OB11Message> { protected async _handle(payload: PayloadType){ // log("history msg ids", Object.keys(msgHistory)); - const msg = msgHistory[payload.message_id.toString()] + const msg = getHistoryMsgByShortId(payload.message_id) if (msg) { const msgData = await OB11Constructor.message(msg); return msgData diff --git a/src/onebot11/actions/SendMsg.ts b/src/onebot11/actions/SendMsg.ts index edcd143..8c4fabf 100644 --- a/src/onebot11/actions/SendMsg.ts +++ b/src/onebot11/actions/SendMsg.ts @@ -1,5 +1,12 @@ 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 { NTQQApi } from "../../ntqqapi/ntcall"; import { Peer } from "../../ntqqapi/ntcall"; @@ -13,7 +20,7 @@ import { ActionName } from "./types"; import * as fs from "fs"; export interface ReturnDataType { - message_id: string + message_id: number } class SendMsg extends BaseAction<OB11PostSendMsg, ReturnDataType> { @@ -90,7 +97,7 @@ class SendMsg extends BaseAction<OB11PostSendMsg, ReturnDataType> { let replyMsgId = sendMsg.data.id; if (replyMsgId) { replyMsgId = replyMsgId.toString() - const replyMsg = msgHistory[replyMsgId] + const replyMsg = getHistoryMsgByShortId(replyMsgId) if (replyMsg) { 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) try { const returnMsg = await NTQQApi.sendMsg(peer, sendElements) + addHistoryMsg(returnMsg) deleteAfterSentFiles.map(f=>fs.unlink(f, ()=>{})) - return { message_id: returnMsg.msgId } + return { message_id: returnMsg.msgShortId } } catch (e) { throw(e.toString()) } diff --git a/src/onebot11/constructor.ts b/src/onebot11/constructor.ts index 56fe5c2..018462b 100644 --- a/src/onebot11/constructor.ts +++ b/src/onebot11/constructor.ts @@ -1,6 +1,6 @@ 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 { getFriend, getGroupMember, getHistoryMsgBySeq, selfInfo } from '../common/data'; +import { addHistoryMsg, getFriend, getGroupMember, getHistoryMsgBySeq, selfInfo } from '../common/data'; import {file2base64, getConfigUtil, log} from "../common/utils"; import { NTQQApi } from "../ntqqapi/ntcall"; @@ -13,7 +13,7 @@ export class OB11Constructor { self_id: selfInfo.uin, user_id: msg.senderUin, time: parseInt(msg.msgTime) || 0, - message_id: msg.msgId, + message_id: msg.msgShortId, real_id: msg.msgId, message_type: msg.chatType == ChatType.group ? "group" : "private", sender: { @@ -85,7 +85,7 @@ export class OB11Constructor { message_data["type"] = "reply" const replyMsg = getHistoryMsgBySeq(element.replyElement.replayMsgSeq) if (replyMsg) { - message_data["data"]["id"] = replyMsg.msgId + message_data["data"]["id"] = replyMsg.msgShortId } else{ continue diff --git a/src/onebot11/types.ts b/src/onebot11/types.ts index 513cbf0..e24166b 100644 --- a/src/onebot11/types.ts +++ b/src/onebot11/types.ts @@ -58,7 +58,7 @@ export enum OB11MessageType { export interface OB11Message { self_id?: string, time: number, - message_id: string, + message_id: number, real_id: string, user_id: string, group_id?: string,