From 600addbf82e68c436dde307937fa54b921d11459 Mon Sep 17 00:00:00 2001 From: linyuchen Date: Thu, 14 Dec 2023 02:10:30 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=89=93=E5=BC=80=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE=E7=95=8C=E9=9D=A2=E5=AF=BC=E8=87=B4=E6=8F=92?= =?UTF-8?q?=E4=BB=B6=E5=A4=9A=E6=AC=A1=E7=9B=91=E5=90=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- manifest.json | 2 +- src/common/IPCChannel.ts | 1 + src/global.d.ts | 1 + src/main/main.ts | 11 +++++-- src/preload.ts | 17 +++++++++-- src/renderer.ts | 62 +++++++++++++++++++++++----------------- 6 files changed, 62 insertions(+), 32 deletions(-) diff --git a/manifest.json b/manifest.json index de36a2a..231746d 100644 --- a/manifest.json +++ b/manifest.json @@ -4,7 +4,7 @@ "name": "LLOneBot", "slug": "LLOneBot", "description": "LiteLoaderQQNT的OneBotApi", - "version": "1.2.5", + "version": "1.2.6", "thumbnail": "./icon.png", "author": { "name": "linyuchen", diff --git a/src/common/IPCChannel.ts b/src/common/IPCChannel.ts index 1fe71ef..a45a084 100644 --- a/src/common/IPCChannel.ts +++ b/src/common/IPCChannel.ts @@ -10,3 +10,4 @@ export const CHANNEL_POST_ONEBOT_DATA = "llonebot_post_onebot_data" 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" \ No newline at end of file diff --git a/src/global.d.ts b/src/global.d.ts index 2dd4577..2ecbbb1 100644 --- a/src/global.d.ts +++ b/src/global.d.ts @@ -45,6 +45,7 @@ declare var llonebot: { setSelfInfo(selfInfo: SelfInfo):void; downloadFile(arg: {uri: string, localFilePath: string}):Promise; deleteFile(path: string[]):Promise; + getRunningStatus(): Promise; }; declare global { diff --git a/src/main/main.ts b/src/main/main.ts index 28bc98b..039426d 100644 --- a/src/main/main.ts +++ b/src/main/main.ts @@ -13,7 +13,7 @@ import { CHANNEL_SET_CONFIG, CHANNEL_START_HTTP_SERVER, CHANNEL_UPDATE_FRIENDS, - CHANNEL_UPDATE_GROUPS, CHANNEL_DELETE_FILE + CHANNEL_UPDATE_GROUPS, CHANNEL_DELETE_FILE, CHANNEL_GET_RUNNING_STATUS } from "../common/IPCChannel"; import {ConfigUtil} from "./config"; import {startExpress} from "./HttpServer"; @@ -22,10 +22,12 @@ import {friends, groups, selfInfo} from "./data"; const fs = require('fs'); +let running = false; + // 加载插件时触发 function onLoad(plugin: any) { - + log("main onLoaded"); function getConfigUtil() { const configFilePath = path.join(plugin.path.data, `config_${selfInfo.user_id}.json`) return new ConfigUtil(configFilePath) @@ -122,6 +124,7 @@ function onLoad(plugin: any) { ipcMain.handle(CHANNEL_SET_SELF_INFO, (event: any, arg: SelfInfo) => { selfInfo.user_id = arg.user_id; selfInfo.nickname = arg.nickname; + running = true; }) ipcMain.on(CHANNEL_DELETE_FILE, (event: any, arg: string[]) => { @@ -129,6 +132,10 @@ function onLoad(plugin: any) { fs.unlinkSync(path); } }) + + ipcMain.handle(CHANNEL_GET_RUNNING_STATUS, (event: any, arg: any) => { + return running; + }) } diff --git a/src/preload.ts b/src/preload.ts index 12c09b1..d33a06b7 100644 --- a/src/preload.ts +++ b/src/preload.ts @@ -3,10 +3,18 @@ import {Config, Group, PostDataSendMsg, SelfInfo, User} from "./common/types"; import { CHANNEL_DOWNLOAD_FILE, - CHANNEL_GET_CONFIG, CHANNEL_SET_SELF_INFO, CHANNEL_LOG, CHANNEL_POST_ONEBOT_DATA, - CHANNEL_RECALL_MSG, CHANNEL_SEND_MSG, + CHANNEL_GET_CONFIG, + CHANNEL_SET_SELF_INFO, + CHANNEL_LOG, + CHANNEL_POST_ONEBOT_DATA, + CHANNEL_RECALL_MSG, + CHANNEL_SEND_MSG, CHANNEL_SET_CONFIG, - CHANNEL_START_HTTP_SERVER, CHANNEL_UPDATE_FRIENDS, CHANNEL_UPDATE_GROUPS, CHANNEL_DELETE_FILE + CHANNEL_START_HTTP_SERVER, + CHANNEL_UPDATE_FRIENDS, + CHANNEL_UPDATE_GROUPS, + CHANNEL_DELETE_FILE, + CHANNEL_GET_RUNNING_STATUS } from "./common/IPCChannel"; @@ -55,6 +63,9 @@ contextBridge.exposeInMainWorld("llonebot", { }, deleteFile: async (localFilePath: string[]) => { ipcRenderer.send(CHANNEL_DELETE_FILE, localFilePath); + }, + getRunningStatus: () => { + return ipcRenderer.invoke(CHANNEL_GET_RUNNING_STATUS); } // startExpress, }); \ No newline at end of file diff --git a/src/renderer.ts b/src/renderer.ts index f3218bb..f329ce2 100644 --- a/src/renderer.ts +++ b/src/renderer.ts @@ -4,6 +4,7 @@ // const { ipcRenderer } = require('electron'); import {AtType, Group, MessageElement, OnebotGroupMemberRole, Peer, PostDataSendMsg, User} from "./common/types"; import * as stream from "stream"; +import {raw} from "express"; let self_qq: string = "" let groups: Group[] = [] @@ -30,6 +31,8 @@ async function getFriends() { } window.llonebot.updateFriends(friends) return friends + + } async function getFriend(qq: string) { @@ -254,7 +257,9 @@ async function listenSendMessage(postData: PostDataSendMsg) { console.log("发送消息", postData) window.LLAPI.sendMessage(peer, postData.params.message).then(res => { console.log("消息发送成功:", peer, postData.params.message) - window.llonebot.deleteFile(sendFiles); + if (sendFiles.length) { + window.llonebot.deleteFile(sendFiles); + } }, err => console.log("消息发送失败", postData, err)) } @@ -268,14 +273,11 @@ function recallMessage(msgId: string) { let chatListEle: HTMLCollectionOf -function onLoad() { - - window.llonebot.listenSendMessage((postData: PostDataSendMsg) => { - listenSendMessage(postData).then().catch(err => console.log("listenSendMessage err", err)) - }) - window.llonebot.listenRecallMessage((arg: { message_id: string }) => { - recallMessage(arg.message_id) - }) +async function onLoad(arg: any) { + let runningStatus = await window.llonebot.getRunningStatus(); + if (runningStatus) { + return; + } async function getGroupsMembers(groupsArg: Group[]) { // 批量获取群成员列表 @@ -292,9 +294,12 @@ function onLoad() { getGroupsMembers(failedGroups).then() }, 1000) } else { - console.log("全部群成员获取完毕", groups) + window.llonebot.log("全部群成员获取完毕") } } + await getFriends(); + await getGroups(); + await getGroupsMembers(groups); function onNewMessages(messages: MessageElement[]) { async function func(messages: MessageElement[]) { @@ -307,26 +312,31 @@ function onLoad() { func(messages).then(() => { }) - console.log("chatListEle", chatListEle) + // console.log("chatListEle", chatListEle) } + window.LLAPI.on("new-messages", onNewMessages); + window.LLAPI.on("new-send-messages", onNewMessages); - getFriends().then(); - getGroups().then(() => { - getGroupsMembers(groups).then(() => { - window.LLAPI.on("new-messages", onNewMessages); - window.LLAPI.on("new-send-messages", onNewMessages); - }) - }) + let accountInfo = await window.LLAPI.getAccountInfo(); + window.llonebot.log("getAccountInfo " + JSON.stringify(accountInfo)); + if (!accountInfo.uid) { + return; + } + let selfInfo = await window.LLAPI.getUserInfo(accountInfo.uid); + window.llonebot.setSelfInfo({ + user_id: accountInfo.uin, + nickname: selfInfo.nickName + }); + window.llonebot.log("selfInfo " + JSON.stringify(selfInfo)) + window.llonebot.startExpress(); - window.LLAPI.getAccountInfo().then(accountInfo => { - window.LLAPI.getUserInfo(accountInfo.uid).then(userInfo => { - window.llonebot.setSelfInfo({ - user_id: accountInfo.uin, - nickname: userInfo.nickName - }); - window.llonebot.startExpress(); - }) + window.llonebot.listenSendMessage((postData: PostDataSendMsg) => { + listenSendMessage(postData).then().catch(err => console.log("listenSendMessage err", err)) }) + window.llonebot.listenRecallMessage((arg: { message_id: string }) => { + recallMessage(arg.message_id) + }) + window.llonebot.log("llonebot loaded"); window.LLAPI.add_qmenu((qContextMenu: Node) => { let btn = document.createElement("a")