From 06d2884a88ae2970dd811c12481ae77d07ca9a1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=8B=E7=93=9C=E4=B8=80=E5=8D=81=E9=9B=AA?= Date: Fri, 9 Aug 2024 18:44:14 +0800 Subject: [PATCH] chore config --- src/common/utils/ConfigBase.ts | 84 ++++++++++++++++++++++++++++++ src/onebot/helper/config.ts | 76 +++++++++++++++++++++++++++ src/onebot/server/postOB11Event.ts | 8 ++- 3 files changed, 163 insertions(+), 5 deletions(-) create mode 100644 src/common/utils/ConfigBase.ts create mode 100644 src/onebot/helper/config.ts diff --git a/src/common/utils/ConfigBase.ts b/src/common/utils/ConfigBase.ts new file mode 100644 index 00000000..7745806b --- /dev/null +++ b/src/common/utils/ConfigBase.ts @@ -0,0 +1,84 @@ +import path from 'node:path'; +import fs from 'node:fs'; +import { NapCatCore } from '@/core'; +export class ConfigBase { + public name: string = 'default_config'; + private pathName: string | null = null; // 本次读取的文件路径 + coreContext: NapCatCore; + configPath:string; + constructor(coreContext: NapCatCore,configPath:string) { + this.coreContext = coreContext; + this.configPath = configPath; + } + + protected getKeys(): string[] | null { + // 决定 key 在json配置文件中的顺序 + return null; + } + + getConfigDir() { + const configDir = path.resolve(this.configPath, 'config'); + fs.mkdirSync(configDir, { recursive: true }); + return configDir; + } + getConfigPath(pathName: string | null): string { + const suffix = pathName ? `_${pathName}` : ''; + const filename = `${this.name}${suffix}.json`; + return path.join(this.getConfigDir(), filename); + } + read() { + // 尝试加载当前账号配置 + if (this.read_from_file(this.coreContext.selfInfo.uin, false)) return this; + // 尝试加载默认配置 + return this.read_from_file('', true); + } + read_from_file(pathName: string, createIfNotExist: boolean) { + const logger = this.coreContext.context.logger; + const configPath = this.getConfigPath(pathName); + if (!fs.existsSync(configPath)) { + if (!createIfNotExist) return null; + this.pathName = pathName; // 记录有效的设置文件 + try { + fs.writeFileSync(configPath, JSON.stringify(this, this.getKeys(), 2)); + logger.log(`配置文件${configPath}已创建\n如果修改此文件后需要重启 NapCat 生效`); + } + catch (e: any) { + logger.logError(`创建配置文件 ${configPath} 时发生错误:`, e.message); + } + return this; + } + + try { + const data = JSON.parse(fs.readFileSync(configPath, 'utf-8')); + logger.logDebug(`配置文件${configPath}已加载`, data); + Object.assign(this, data); + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-expect-error + this.save(this); // 保存一次,让新版本的字段写入 + return this; + } catch (e: any) { + if (e instanceof SyntaxError) { + logger.logError(`配置文件 ${configPath} 格式错误,请检查配置文件:`, e.message); + } else { + logger.logError(`读取配置文件 ${configPath} 时发生错误:`, e.message); + } + return this; + } + } + + save(config: T, overwrite: boolean = false) { + Object.assign(this, config); + const logger = this.coreContext.context.logger; + const selfInfo = this.coreContext.selfInfo; + if (overwrite) { + // 用户要求强制写入,则变更当前文件为目标文件 + this.pathName = `${selfInfo.uin}`; + } + const configPath = this.getConfigPath(this.pathName); + try { + fs.writeFileSync(configPath, JSON.stringify(this, this.getKeys(), 2)); + } catch (e: any) { + logger.logError(`保存配置文件 ${configPath} 时发生错误:`, e.message); + } + } +} diff --git a/src/onebot/helper/config.ts b/src/onebot/helper/config.ts new file mode 100644 index 00000000..f1ce8615 --- /dev/null +++ b/src/onebot/helper/config.ts @@ -0,0 +1,76 @@ +import fs from 'node:fs'; +import path from 'node:path'; +import { ConfigBase } from '@/common/utils/ConfigBase'; + +// export interface OB11Config { +// http: { +// enable: boolean; +// host: string; +// port: number; +// secret: string; +// enableHeart: boolean; +// enablePost: boolean; +// postUrls: string[]; +// }; +// ws: { +// enable: boolean; +// host: string; +// port: number; +// }; +// reverseWs: { +// enable: boolean; +// urls: string[]; +// }; + +// debug: boolean; +// heartInterval: number; +// messagePostFormat: 'array' | 'string'; +// enableLocalFile2Url: boolean; +// musicSignUrl: string; +// reportSelfMessage: boolean; +// token: string; +// GroupLocalTime: { +// Record: boolean, +// RecordList: Array +// }, +// read(): OB11Config; + +// save(config: OB11Config): void; +// } + +export class OB11Config extends ConfigBase { + name: string = 'onebot11'; + http = { + enable: false, + host: '', + port: 3000, + secret: '', + enableHeart: false, + enablePost: false, + postUrls: [], + }; + ws = { + enable: false, + host: '', + port: 3001, + }; + reverseWs = { + enable: false, + urls: [], + }; + debug = false; + heartInterval = 30000; + messagePostFormat: 'array' | 'string' = 'array'; + enableLocalFile2Url = true; + musicSignUrl = ''; + reportSelfMessage = false; + token = ''; + GroupLocalTime = { + Record: false, + RecordList: [] as Array + }; + + protected getKeys(): string[] | null { + return null; + } +} diff --git a/src/onebot/server/postOB11Event.ts b/src/onebot/server/postOB11Event.ts index 6286de71..5a912d86 100644 --- a/src/onebot/server/postOB11Event.ts +++ b/src/onebot/server/postOB11Event.ts @@ -68,8 +68,6 @@ export function postWsEvent(event: QuickActionEvent) { } export function postOB11Event(msg: QuickActionEvent, reportSelf = false, postWs = true, coreContext: NapCatCore) { - const config = ob11Config; - // 判断msg是否是event if (!config.reportSelfMessage && !reportSelf) { if (msg.post_type === 'message' && (msg as OB11Message).user_id.toString() == coreContext.selfInfo.uin) { @@ -105,7 +103,7 @@ export function postOB11Event(msg: QuickActionEvent, reportSelf = false, postWs return; } try { - handleQuickOperation(msg as QuickActionEvent, resJson,coreContext).then().catch(logError); + handleQuickOperation(msg as QuickActionEvent, resJson, coreContext).then().catch(coreContext.context.logger.logError); } catch (e: any) { coreContext.context.logger.logError('新消息事件HTTP上报返回快速操作失败', e); } @@ -157,8 +155,8 @@ async function handleMsg(msg: OB11Message, quickAction: QuickAction, coreContext } } replyMessage = replyMessage.concat(normalize(reply, quickAction.auto_escape)); - const { sendElements, deleteAfterSentFiles } = await createSendElements(replyMessage, peer); - sendMsg(peer, sendElements, deleteAfterSentFiles, false).then().catch(logError); + const { sendElements, deleteAfterSentFiles } = await createSendElements(coreContext, replyMessage, peer); + sendMsg(coreContext, peer, sendElements, deleteAfterSentFiles, false).then().catch(coreContext.context.logger.logError); } } async function handleGroupRequest(request: OB11GroupRequestEvent, quickAction: QuickActionGroupRequest, coreContext: NapCatCore) {