mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2024-11-21 09:36:35 +00:00
feat: 配置热重载
This commit is contained in:
parent
17ebc01597
commit
f32a693393
@ -36,7 +36,11 @@ checkVersion().then((remoteVersion: string) => {
|
|||||||
}).catch((e) => {
|
}).catch((e) => {
|
||||||
logError('[NapCat] 检测更新失败');
|
logError('[NapCat] 检测更新失败');
|
||||||
});
|
});
|
||||||
new NapCatOnebot11();
|
// 不是很好待优化
|
||||||
|
let NapCat_OneBot11 = new NapCatOnebot11();
|
||||||
|
|
||||||
|
WebUiDataRuntime.setOB11ConfigCall(NapCat_OneBot11.SetConfig);
|
||||||
|
|
||||||
napCatCore.onLoginSuccess((uin, uid) => {
|
napCatCore.onLoginSuccess((uin, uid) => {
|
||||||
console.log('登录成功!');
|
console.log('登录成功!');
|
||||||
WebUiDataRuntime.setQQLoginStatus(true);
|
WebUiDataRuntime.setQQLoginStatus(true);
|
||||||
@ -64,7 +68,7 @@ napCatCore.getQuickLoginList().then((res) => {
|
|||||||
WebUiDataRuntime.setQQQuickLoginList(res.LocalLoginInfoList.filter((item) => item.isQuickLogin).map((item) => item.uin.toString()));
|
WebUiDataRuntime.setQQQuickLoginList(res.LocalLoginInfoList.filter((item) => item.isQuickLogin).map((item) => item.uin.toString()));
|
||||||
});
|
});
|
||||||
|
|
||||||
WebUiDataRuntime.setQQQuickLogin(async (uin: string) => {
|
WebUiDataRuntime.setQQQuickLoginCall(async (uin: string) => {
|
||||||
const QuickLogin: Promise<{ result: boolean, message: string }> = new Promise((resolve, reject) => {
|
const QuickLogin: Promise<{ result: boolean, message: string }> = new Promise((resolve, reject) => {
|
||||||
if (quickLoginQQ) {
|
if (quickLoginQQ) {
|
||||||
log('正在快速登录 ', quickLoginQQ);
|
log('正在快速登录 ', quickLoginQQ);
|
||||||
|
@ -11,7 +11,7 @@ import {
|
|||||||
GroupNotifyTypes,
|
GroupNotifyTypes,
|
||||||
RawMessage
|
RawMessage
|
||||||
} from '@/core/entities';
|
} from '@/core/entities';
|
||||||
import { ob11Config } from '@/onebot11/config';
|
import { OB11Config, ob11Config } from '@/onebot11/config';
|
||||||
import { httpHeart, ob11HTTPServer } from '@/onebot11/server/http';
|
import { httpHeart, ob11HTTPServer } from '@/onebot11/server/http';
|
||||||
import { ob11WebsocketServer } from '@/onebot11/server/ws/WebsocketServer';
|
import { ob11WebsocketServer } from '@/onebot11/server/ws/WebsocketServer';
|
||||||
import { ob11ReverseWebsockets } from '@/onebot11/server/ws/ReverseWebsocket';
|
import { ob11ReverseWebsockets } from '@/onebot11/server/ws/ReverseWebsocket';
|
||||||
@ -87,7 +87,7 @@ export class NapCatOnebot11 {
|
|||||||
logDebug('收到消息', msg);
|
logDebug('收到消息', msg);
|
||||||
for (const m of msg) {
|
for (const m of msg) {
|
||||||
// try: 减掉3s 试图修复消息半天收不到
|
// try: 减掉3s 试图修复消息半天收不到
|
||||||
if (this.bootTime - 3> parseInt(m.msgTime)) {
|
if (this.bootTime - 3 > parseInt(m.msgTime)) {
|
||||||
logDebug(`消息时间${m.msgTime}早于启动时间${this.bootTime},忽略上报`);
|
logDebug(`消息时间${m.msgTime}早于启动时间${this.bootTime},忽略上报`);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -189,7 +189,72 @@ export class NapCatOnebot11 {
|
|||||||
}).catch(e => logError('constructFriendAddEvent error: ', e));
|
}).catch(e => logError('constructFriendAddEvent error: ', e));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
async SetConfig(NewOb11: OB11Config) {
|
||||||
|
function isEqual(obj1: any, obj2: any) {
|
||||||
|
if (obj1 === obj2) return true;
|
||||||
|
if (obj1 == null || obj2 == null) return false;
|
||||||
|
if (typeof obj1 !== 'object' || typeof obj2 !== 'object') return obj1 === obj2;
|
||||||
|
|
||||||
|
const keys1 = Object.keys(obj1);
|
||||||
|
const keys2 = Object.keys(obj2);
|
||||||
|
|
||||||
|
if (keys1.length !== keys2.length) return false;
|
||||||
|
|
||||||
|
for (let key of keys1) {
|
||||||
|
if (!isEqual(obj1[key], obj2[key])) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
// if (!NewOb11 || typeof NewOb11 !== 'object') {
|
||||||
|
// throw new Error('Invalid configuration object');
|
||||||
|
// }
|
||||||
|
|
||||||
|
const isHttpChanged = !isEqual(NewOb11.httpPort, ob11Config.httpPort) && NewOb11.enableHttp;
|
||||||
|
const isWsChanged = !isEqual(NewOb11.wsPort, ob11Config.wsPort);
|
||||||
|
const isEnableWsChanged = !isEqual(NewOb11.enableWs, ob11Config.enableWs);
|
||||||
|
const isEnableWsReverseChanged = !isEqual(NewOb11.enableWsReverse, ob11Config.enableWsReverse);
|
||||||
|
const isWsReverseUrlsChanged = !isEqual(NewOb11.wsReverseUrls, ob11Config.wsReverseUrls);
|
||||||
|
|
||||||
|
if (isHttpChanged) {
|
||||||
|
ob11HTTPServer.restart(NewOb11.httpPort, NewOb11.httpHost);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!NewOb11.enableHttp) {
|
||||||
|
ob11HTTPServer.stop();
|
||||||
|
} else {
|
||||||
|
ob11HTTPServer.start(NewOb11.httpPort, NewOb11.httpHost);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isWsChanged) {
|
||||||
|
ob11WebsocketServer.restart(NewOb11.wsPort);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isEnableWsChanged) {
|
||||||
|
if (NewOb11.enableWs) {
|
||||||
|
ob11WebsocketServer.start(NewOb11.wsPort, NewOb11.wsHost);
|
||||||
|
} else {
|
||||||
|
ob11WebsocketServer.stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isEnableWsReverseChanged) {
|
||||||
|
if (NewOb11.enableWsReverse) {
|
||||||
|
ob11ReverseWebsockets.start();
|
||||||
|
} else {
|
||||||
|
ob11ReverseWebsockets.stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (NewOb11.enableWsReverse && isWsReverseUrlsChanged) {
|
||||||
|
logDebug('反向ws地址有变化, 重启反向ws服务');
|
||||||
|
ob11ReverseWebsockets.restart();
|
||||||
|
}
|
||||||
|
if (NewOb11.enableHttpHeart) {
|
||||||
|
httpHeart.start();
|
||||||
|
} else if (!NewOb11.enableHttpHeart) {
|
||||||
|
httpHeart.stop();
|
||||||
|
}
|
||||||
|
ob11Config.save(NewOb11);
|
||||||
|
}
|
||||||
async postGroupNotifies(notifies: GroupNotify[]) {
|
async postGroupNotifies(notifies: GroupNotify[]) {
|
||||||
for (const notify of notifies) {
|
for (const notify of notifies) {
|
||||||
try {
|
try {
|
||||||
|
@ -51,20 +51,36 @@ export const OB11SetConfigHandler: RequestHandler = async (req, res) => {
|
|||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
let configFilePath = resolve(__dirname, `./config/onebot11_${await WebUiDataRuntime.getQQLoginUin()}.json`);
|
let SetResult;
|
||||||
try {
|
try {
|
||||||
JSON.parse(req.body.config)
|
await WebUiDataRuntime.setOB11Config(JSON.parse(req.body.config));
|
||||||
readFileSync(configFilePath);
|
SetResult = true;
|
||||||
|
} catch (e) {
|
||||||
|
SetResult = false;
|
||||||
}
|
}
|
||||||
catch (e) {
|
|
||||||
//console.log(e);
|
// let configFilePath = resolve(__dirname, `./config/onebot11_${await WebUiDataRuntime.getQQLoginUin()}.json`);
|
||||||
configFilePath = resolve(__dirname, `./config/onebot11.json`);
|
// try {
|
||||||
|
// JSON.parse(req.body.config)
|
||||||
|
// readFileSync(configFilePath);
|
||||||
|
// }
|
||||||
|
// catch (e) {
|
||||||
|
// //console.log(e);
|
||||||
|
// configFilePath = resolve(__dirname, `./config/onebot11.json`);
|
||||||
|
// }
|
||||||
|
// //console.log(configFilePath,JSON.parse(req.body.config));
|
||||||
|
// writeFileSync(configFilePath, JSON.stringify(JSON.parse(req.body.config), null, 4));
|
||||||
|
if (SetResult) {
|
||||||
|
res.send({
|
||||||
|
code: 0,
|
||||||
|
message: 'success'
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
res.send({
|
||||||
|
code: -1,
|
||||||
|
message: 'Config Set Error'
|
||||||
|
});
|
||||||
}
|
}
|
||||||
//console.log(configFilePath,JSON.parse(req.body.config));
|
|
||||||
writeFileSync(configFilePath, JSON.stringify(JSON.parse(req.body.config), null, 4));
|
|
||||||
res.send({
|
|
||||||
code: 0,
|
|
||||||
message: 'success'
|
|
||||||
});
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
@ -1,3 +1,5 @@
|
|||||||
|
import { OB11Config } from "@/onebot11/config";
|
||||||
|
|
||||||
interface LoginRuntimeType {
|
interface LoginRuntimeType {
|
||||||
LoginCurrentTime: number;
|
LoginCurrentTime: number;
|
||||||
LoginCurrentRate: number;
|
LoginCurrentRate: number;
|
||||||
@ -5,7 +7,8 @@ interface LoginRuntimeType {
|
|||||||
QQQRCodeURL: string;
|
QQQRCodeURL: string;
|
||||||
QQLoginUin: string;
|
QQLoginUin: string;
|
||||||
NapCatHelper: {
|
NapCatHelper: {
|
||||||
CoreQuickLogin: (uin: string) => Promise<{ result: boolean, message: string }>;
|
CoreQuickLoginCall: (uin: string) => Promise<{ result: boolean, message: string }>;
|
||||||
|
SetOb11ConfigCall: (ob11: OB11Config) => Promise<void>;
|
||||||
QQLoginList: string[]
|
QQLoginList: string[]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -16,7 +19,8 @@ let LoginRuntime: LoginRuntimeType = {
|
|||||||
QQQRCodeURL: "",
|
QQQRCodeURL: "",
|
||||||
QQLoginUin: "",
|
QQLoginUin: "",
|
||||||
NapCatHelper: {
|
NapCatHelper: {
|
||||||
CoreQuickLogin: async (uin: string) => { return { result: false, message: '' }; },
|
SetOb11ConfigCall: async (ob11: OB11Config) => { return; },
|
||||||
|
CoreQuickLoginCall: async (uin: string) => { return { result: false, message: '' }; },
|
||||||
QQLoginList: []
|
QQLoginList: []
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -64,10 +68,16 @@ export const WebUiDataRuntime = {
|
|||||||
setQQQuickLoginList: async function (list: string[]): Promise<void> {
|
setQQQuickLoginList: async function (list: string[]): Promise<void> {
|
||||||
LoginRuntime.NapCatHelper.QQLoginList = list;
|
LoginRuntime.NapCatHelper.QQLoginList = list;
|
||||||
},
|
},
|
||||||
setQQQuickLogin(func: (uin: string) => Promise<{ result: boolean, message: string }>): void {
|
setQQQuickLoginCall(func: (uin: string) => Promise<{ result: boolean, message: string }>): void {
|
||||||
LoginRuntime.NapCatHelper.CoreQuickLogin = func;
|
LoginRuntime.NapCatHelper.CoreQuickLoginCall = func;
|
||||||
},
|
},
|
||||||
getQQQuickLogin: async function (uin: string): Promise<{ result: boolean, message: string }> {
|
getQQQuickLogin: async function (uin: string): Promise<{ result: boolean, message: string }> {
|
||||||
return await LoginRuntime.NapCatHelper.CoreQuickLogin(uin);
|
return await LoginRuntime.NapCatHelper.CoreQuickLoginCall(uin);
|
||||||
|
},
|
||||||
|
setOB11ConfigCall: async function (func: (ob11: OB11Config) => Promise<void>): Promise<void> {
|
||||||
|
LoginRuntime.NapCatHelper.SetOb11ConfigCall = func;
|
||||||
|
},
|
||||||
|
setOB11Config: async function (ob11: OB11Config): Promise<void> {
|
||||||
|
await LoginRuntime.NapCatHelper.SetOb11ConfigCall(ob11);
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user