11// 运行在 Electron 主进程 下的插件入口
22
33import * as path from "path" ;
4-
5- const fs = require ( 'fs' ) ;
64import { ipcMain } from 'electron' ;
75
86import { Config , Group , SelfInfo , User } from "../common/types" ;
97import {
108 CHANNEL_DOWNLOAD_FILE ,
11- CHANNEL_GET_CONFIG , CHANNEL_GET_SELF_INFO , CHANNEL_LOG , CHANNEL_POST_ONEBOT_DATA ,
9+ CHANNEL_GET_CONFIG ,
10+ CHANNEL_SET_SELF_INFO ,
11+ CHANNEL_LOG ,
12+ CHANNEL_POST_ONEBOT_DATA ,
1213 CHANNEL_SET_CONFIG ,
13- CHANNEL_START_HTTP_SERVER , CHANNEL_UPDATE_FRIENDS ,
14+ CHANNEL_START_HTTP_SERVER ,
15+ CHANNEL_UPDATE_FRIENDS ,
1416 CHANNEL_UPDATE_GROUPS
1517} from "../common/IPCChannel" ;
1618import { ConfigUtil } from "./config" ;
1719import { startExpress } from "./HttpServer" ;
1820import { log } from "./utils" ;
1921import { friends , groups , selfInfo } from "./data" ;
2022
23+ const fs = require ( 'fs' ) ;
2124
2225
2326// 加载插件时触发
2427function onLoad ( plugin : any ) {
2528
26- const configFilePath = path . join ( plugin . path . data , "config.json" )
27- let configUtil = new ConfigUtil ( configFilePath )
29+ function getConfigUtil ( ) {
30+ const configFilePath = path . join ( plugin . path . data , `config_${ selfInfo . user_id } .json` )
31+ return new ConfigUtil ( configFilePath )
32+ }
2833
2934 if ( ! fs . existsSync ( plugin . path . data ) ) {
3035 fs . mkdirSync ( plugin . path . data , { recursive : true } ) ;
3136 }
3237 ipcMain . handle ( CHANNEL_GET_CONFIG , ( event : any , arg : any ) => {
33- return configUtil . getConfig ( )
38+ return getConfigUtil ( ) . getConfig ( )
3439 } )
3540 ipcMain . handle ( CHANNEL_DOWNLOAD_FILE , async ( event : any , arg : { uri : string , localFilePath : string } ) => {
3641 let url = new URL ( arg . uri ) ;
@@ -51,11 +56,11 @@ function onLoad(plugin: any) {
5156 return arg . localFilePath ;
5257 } )
5358 ipcMain . on ( CHANNEL_SET_CONFIG , ( event : any , arg : Config ) => {
54- fs . writeFileSync ( configFilePath , JSON . stringify ( arg , null , 2 ) , "utf-8" )
59+ getConfigUtil ( ) . setConfig ( arg )
5560 } )
5661
5762 ipcMain . on ( CHANNEL_START_HTTP_SERVER , ( event : any , arg : any ) => {
58- startExpress ( configUtil . getConfig ( ) . port )
63+ startExpress ( getConfigUtil ( ) . getConfig ( ) . port )
5964 } )
6065
6166 ipcMain . on ( CHANNEL_UPDATE_GROUPS , ( event : any , arg : Group [ ] ) => {
@@ -89,7 +94,7 @@ function onLoad(plugin: any) {
8994 } )
9095
9196 ipcMain . on ( CHANNEL_POST_ONEBOT_DATA , ( event : any , arg : any ) => {
92- for ( const host of configUtil . getConfig ( ) . hosts ) {
97+ for ( const host of getConfigUtil ( ) . getConfig ( ) . hosts ) {
9398 try {
9499 fetch ( host , {
95100 method : "POST" ,
@@ -113,7 +118,7 @@ function onLoad(plugin: any) {
113118 log ( arg )
114119 } )
115120
116- ipcMain . on ( CHANNEL_GET_SELF_INFO , ( event : any , arg : SelfInfo ) => {
121+ ipcMain . handle ( CHANNEL_SET_SELF_INFO , ( event : any , arg : SelfInfo ) => {
117122 selfInfo . user_id = arg . user_id ;
118123 selfInfo . nickname = arg . nickname ;
119124 } )
0 commit comments