fix: 打开插件设置界面导致插件多次监听

This commit is contained in:
linyuchen 2023-12-14 02:10:30 +08:00
parent f07f0111cd
commit 600addbf82
6 changed files with 62 additions and 32 deletions

View File

@ -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",

View File

@ -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"

1
src/global.d.ts vendored
View File

@ -45,6 +45,7 @@ declare var llonebot: {
setSelfInfo(selfInfo: SelfInfo):void;
downloadFile(arg: {uri: string, localFilePath: string}):Promise<string>;
deleteFile(path: string[]):Promise<void>;
getRunningStatus(): Promise<boolean>;
};
declare global {

View File

@ -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;
})
}

View File

@ -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,
});

View File

@ -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<Element>
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")