From 9411993d8aeeddb66f95ea9b498953dd5901a957 Mon Sep 17 00:00:00 2001 From: linyuchen Date: Tue, 6 Feb 2024 18:29:03 +0800 Subject: [PATCH] fix: report self --- README.md | 10 ++-------- src/common/channels.ts | 3 ++- src/common/data.ts | 2 +- src/global.d.ts | 7 ++++--- src/main/main.ts | 38 +++++++++++++++++++++++++++++++++++--- src/ntqqapi/hook.ts | 8 +++++--- src/onebot11/construct.ts | 7 +------ src/preload.ts | 8 ++++---- src/renderer.ts | 38 ++++++++++++++++++++++---------------- src/server/httpserver.ts | 3 +-- 10 files changed, 77 insertions(+), 47 deletions(-) diff --git a/README.md b/README.md index c3bfa56..67e18b4 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # LLOneBot API -将NTQQLiteLoaderAPI封装成OneBot11/12标准的API, V12没有完整测试 +将NTQQLiteLoaderAPI封装成OneBot11标准的API *注意:本文档对应的是 LiteLoader 1.0.0及以上版本,如果你使用的是旧版本请切换到本项目v1分支查看文档* @@ -51,6 +51,7 @@ - [x] get_group_member_list - [x] get_group_member_info - [x] get_friend_list +- [x] get_msg **自己发送成功的消息也会上报,可以用于获取需要撤回消息的id** @@ -90,13 +91,6 @@
-
- onebot 12对接不了 -
- onebot 12只写了部分兼容,没有完整测试,不保证能用,慎用 -
-
-
QQ变得很卡
diff --git a/src/common/channels.ts b/src/common/channels.ts index c505539..70ac91a 100644 --- a/src/common/channels.ts +++ b/src/common/channels.ts @@ -11,4 +11,5 @@ export const CHANNEL_SET_SELF_INFO= "llonebot_set_self_info" export const CHANNEL_DOWNLOAD_FILE= "llonebot_download_file" export const CHANNEL_DELETE_FILE= "llonebot_delete_file" export const CHANNEL_GET_RUNNING_STATUS= "llonebot_get_running_status" -export const CHANNEL_FILE2BASE64= "llonebot_file2base64" \ No newline at end of file +export const CHANNEL_FILE2BASE64= "llonebot_file2base64" +export const CHANNEL_GET_HISTORY_MSG= "llonebot_get_history_msg" \ No newline at end of file diff --git a/src/common/data.ts b/src/common/data.ts index c9aa843..8c7606c 100644 --- a/src/common/data.ts +++ b/src/common/data.ts @@ -2,6 +2,7 @@ import {Group, MessageElement, RawMessage, SelfInfo, User} from "./types"; export let groups: Group[] = [] export let friends: User[] = [] +export let msgHistory: Record = {} // msgId: RawMessage export function getFriend(qq: string): User | undefined { return friends.find(friend => friend.uin === qq) @@ -23,7 +24,6 @@ export let selfInfo: SelfInfo = { nickname: "" } -export let msgHistory: Record = {} export function getHistoryMsgBySeq(seq: string) { return Object.values(msgHistory).find(msg => msg.msgSeq === seq) diff --git a/src/global.d.ts b/src/global.d.ts index 646642c..b035248 100644 --- a/src/global.d.ts +++ b/src/global.d.ts @@ -4,13 +4,13 @@ import { GroupMemberInfo, MessageElement, Peer, - PostDataSendMsg, PttElement, + PostDataSendMsg, PttElement, RawMessage, SelfInfo, User } from "./common/types"; -import {OB11Return, OB11MessageData} from "./onebot11/types"; +import {OB11Return, OB11MessageData, OB11SendMsgReturn} from "./onebot11/types"; declare var LLAPI: { @@ -49,8 +49,9 @@ declare var llonebot: { downloadFile(arg: {uri: string, fileName: string}):Promise<{errMsg: string, path: string}>; deleteFile(path: string[]):Promise; getRunningStatus(): Promise; - sendSendMsgResult(sessionId: string, msgResult: OB11Return): void; + sendSendMsgResult(sessionId: string, msgResult: OB11SendMsgReturn): void; file2base64(path: string): Promise<{err: string, data: string}>; + getHistoryMsg(msgId: string): Promise; }; declare global { diff --git a/src/main/main.ts b/src/main/main.ts index b7e1f25..14b7223 100644 --- a/src/main/main.ts +++ b/src/main/main.ts @@ -14,7 +14,7 @@ import { CHANNEL_SET_CONFIG, CHANNEL_START_HTTP_SERVER, CHANNEL_UPDATE_FRIENDS, - CHANNEL_UPDATE_GROUPS, CHANNEL_DELETE_FILE, CHANNEL_GET_RUNNING_STATUS, CHANNEL_FILE2BASE64 + CHANNEL_UPDATE_GROUPS, CHANNEL_DELETE_FILE, CHANNEL_GET_RUNNING_STATUS, CHANNEL_FILE2BASE64, CHANNEL_GET_HISTORY_MSG } from "../common/channels"; import {ConfigUtil} from "../common/config"; import {postMsg, startExpress} from "../server/httpserver"; @@ -167,12 +167,44 @@ function onLoad() { return await file2base64(path); }) - registerReceiveHook<{ msgList: Array }>(ReceiveCmd.NEW_MSG, (payload) => { - for (const message of payload.msgList) { + ipcMain.handle(CHANNEL_GET_HISTORY_MSG, (event: any, arg: string): RawMessage | undefined => { + return msgHistory[arg] || null; + }) + + function postRawMsg(msgList:RawMessage[]) { + const {debug, reportSelfMessage} = getConfigUtil().getConfig(); + for (const message of msgList) { OB11Construct.constructMessage(message).then((msg) => { + if (debug) { + msg.raw = message; + } + if (msg.user_id == selfInfo.user_id && !reportSelfMessage) { + return + } postMsg(msg); }).catch(e=>log("constructMessage error: ", e.toString())); } + } + + registerReceiveHook<{ msgList: Array }>(ReceiveCmd.NEW_MSG, (payload) => { + try { + postRawMsg(payload.msgList); + } catch (e) { + log("report message error: ", e.toString()) + } + }) + + registerReceiveHook<{ msgRecord: RawMessage }>(ReceiveCmd.SELF_SEND_MSG, (payload) => { + const {reportSelfMessage} = getConfigUtil().getConfig() + if (!reportSelfMessage) { + return + } + log("reportSelfMessage", payload) + try { + postRawMsg([payload.msgRecord]); + } catch (e) { + log("report self message error: ", e.toString()) + } }) } diff --git a/src/ntqqapi/hook.ts b/src/ntqqapi/hook.ts index 7be53ac..40013fd 100644 --- a/src/ntqqapi/hook.ts +++ b/src/ntqqapi/hook.ts @@ -1,12 +1,13 @@ import {BrowserWindow} from 'electron'; -import {log} from "../common/utils"; +import {getConfigUtil, log} from "../common/utils"; import {NTQQApiClass} from "./ntcall"; import {RawMessage} from "../common/types"; import {msgHistory} from "../common/data"; export enum ReceiveCmd { UPDATE_MSG = "nodeIKernelMsgListener/onMsgInfoListUpdate", - NEW_MSG = "nodeIKernelMsgListener/onRecvMsg" + NEW_MSG = "nodeIKernelMsgListener/onRecvMsg", + SELF_SEND_MSG = "nodeIKernelMsgListener/onAddSendMsg" } interface NTQQApiReturnData extends Array { @@ -66,4 +67,5 @@ registerReceiveHook<{ msgList: Array }>(ReceiveCmd.NEW_MSG, (payload log("收到新消息,push到历史记录", message) msgHistory[message.msgId] = message; } -}) \ No newline at end of file +}) + diff --git a/src/onebot11/construct.ts b/src/onebot11/construct.ts index 7037408..0e3f9ef 100644 --- a/src/onebot11/construct.ts +++ b/src/onebot11/construct.ts @@ -6,7 +6,7 @@ import {file2base64, getConfigUtil} from "../common/utils"; export class OB11Construct { static async constructMessage(msg: RawMessage): Promise { - const {debug, enableBase64} = getConfigUtil().getConfig() + const {enableBase64} = getConfigUtil().getConfig() const message_type = msg.chatType == ChatType.group ? "group" : "private"; const resMsg: OB11Message = { self_id: selfInfo.user_id, @@ -42,11 +42,6 @@ export class OB11Construct { resMsg.sub_type = "group" } - - if (debug) { - resMsg.raw = msg - } - for (let element of msg.elements) { let message_data: any = { data: {}, diff --git a/src/preload.ts b/src/preload.ts index 06922ab..a8d591a 100644 --- a/src/preload.ts +++ b/src/preload.ts @@ -1,6 +1,6 @@ // Electron 主进程 与 渲染进程 交互的桥梁 -import {Config, Group, PostDataSendMsg, SelfInfo, User} from "./common/types"; +import {Config, Group, PostDataSendMsg, RawMessage, SelfInfo, User} from "./common/types"; import { CHANNEL_DOWNLOAD_FILE, CHANNEL_GET_CONFIG, @@ -14,7 +14,7 @@ import { CHANNEL_UPDATE_FRIENDS, CHANNEL_UPDATE_GROUPS, CHANNEL_DELETE_FILE, - CHANNEL_GET_RUNNING_STATUS, CHANNEL_FILE2BASE64 + CHANNEL_GET_RUNNING_STATUS, CHANNEL_FILE2BASE64, CHANNEL_GET_HISTORY_MSG } from "./common/channels"; @@ -74,8 +74,8 @@ contextBridge.exposeInMainWorld("llonebot", { getRunningStatus: () => { return ipcRenderer.invoke(CHANNEL_GET_RUNNING_STATUS); }, - file2base64: (localFilePath: string) => { - return ipcRenderer.invoke(CHANNEL_FILE2BASE64, localFilePath); + getHistoryMsg: async (msgId: string):Promise => { + return await ipcRenderer.invoke(CHANNEL_GET_HISTORY_MSG, msgId) } // startExpress, }); \ No newline at end of file diff --git a/src/renderer.ts b/src/renderer.ts index 041ed23..565d1e5 100644 --- a/src/renderer.ts +++ b/src/renderer.ts @@ -1,21 +1,14 @@ /// -import { - AtType, - ChatType, - Group, - MessageElement, Peer, - PostDataSendMsg, - User -} from "./common/types"; +import {AtType, ChatType, Group, MessageElement, Peer, PostDataSendMsg, RawMessage, User} from "./common/types"; -import {OB11Return, OB11SendMsgReturn, OB11MessageDataType} from "./onebot11/types"; +import {OB11SendMsgReturn} from "./onebot11/types"; +import {ipcRenderer} from "electron"; +import {CHANNEL_GET_HISTORY_MSG} from "./common/channels"; -let self_qq: string = "" let groups: Group[] = [] let friends: User[] = [] -let msgHistory: MessageElement[] = [] let uid_maps: Record = {} // 一串加密的字符串 -> qq号 function getStrangerByUin(uin: string) { @@ -26,6 +19,10 @@ function getStrangerByUin(uin: string) { } } +async function getHistoryMsg(msgId: string) { + return await window.llonebot.getHistoryMsg(msgId); +} + async function getUserInfo(uid: string): Promise { let user = uid_maps[uid] if (!user) { @@ -236,9 +233,11 @@ async function listenSendMessage(postData: PostDataSendMsg) { message.file = localFilePath } else if (message.type == "reply") { let msgId = message.data?.id || message.msgId - let replyMessage = msgHistory.find(msg => msg.raw.msgId == msgId) - message.msgId = msgId - message.msgSeq = replyMessage?.raw.msgSeq || "" + const rawMsg: RawMessage = await getHistoryMsg(msgId) + if (rawMsg){ + message.msgId = msgId + message.msgSeq = rawMsg.msgSeq + } } } console.log("发送消息", postData) @@ -270,8 +269,15 @@ async function listenSendMessage(postData: PostDataSendMsg) { } function recallMessage(msgId: string) { - let msg = msgHistory.find(msg => msg.raw.msgId == msgId) - window.LLAPI.recallMessage(msg.peer, [msgId]).then() + getHistoryMsg(msgId).then((msg: RawMessage)=>{ + const peer: Peer ={ + chatType: msg.chatType, + name: "", + uid: msg.peerUin + } + window.LLAPI.recallMessage(peer, [msgId]).then() + }) + } let chatListEle: HTMLCollectionOf diff --git a/src/server/httpserver.ts b/src/server/httpserver.ts index 5204853..112b528 100644 --- a/src/server/httpserver.ts +++ b/src/server/httpserver.ts @@ -153,7 +153,7 @@ function handlePost(jsonData: any, handleSendResult: (data: OB11Return) => } }) } else if (jsonData.action == "delete_msg") { - sendIPCRecallQQMsg(String(jsonData.message_id)) + sendIPCRecallQQMsg(jsonData.message_id) } return resData } @@ -287,5 +287,4 @@ export function postMsg(msg: OB11Message) { log(`新消息事件上报失败: ${host} ` + err + JSON.stringify(msg)); }); } - } \ No newline at end of file