From 03bc844ad088c6b8432927c16755170c6f48f20f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B1=89=E5=B9=BF?= Date: Mon, 22 Jul 2024 20:12:24 +0800 Subject: [PATCH] fix[default-config]config name check --- src/common/utils/ConfigBase.ts | 13 ++++++++----- src/core/src/utils/config.ts | 5 +---- src/onebot11/config.ts | 6 +----- 3 files changed, 10 insertions(+), 14 deletions(-) diff --git a/src/common/utils/ConfigBase.ts b/src/common/utils/ConfigBase.ts index 50aeefb0..fbf0b7d0 100644 --- a/src/common/utils/ConfigBase.ts +++ b/src/common/utils/ConfigBase.ts @@ -14,7 +14,8 @@ fs.mkdirSync(configDir, { recursive: true }); export class ConfigBase { - + public name: string = 'default_config' + private pathName: string | null = null // 本次读取的文件路径 constructor() { } @@ -28,10 +29,11 @@ export class ConfigBase { fs.mkdirSync(configDir, { recursive: true }); return configDir; } - getConfigPath(pathName: string): string { - throw new Error('Method not implemented.'); + 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(selfInfo.uin, false)) return this @@ -42,6 +44,7 @@ export class ConfigBase { 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)); log(`配置文件${configPath}已创建\n如果修改此文件后需要重启 NapCat 生效`); @@ -72,7 +75,7 @@ export class ConfigBase { save(config: T) { Object.assign(this, config); - const configPath = this.getConfigPath(); + const configPath = this.getConfigPath(this.pathName); try { fs.writeFileSync(configPath, JSON.stringify(this, this.getKeys(), 2)); } catch (e: any) { diff --git a/src/core/src/utils/config.ts b/src/core/src/utils/config.ts index 5ee7b302..5686e429 100644 --- a/src/core/src/utils/config.ts +++ b/src/core/src/utils/config.ts @@ -13,6 +13,7 @@ export interface NapCatConfig { } class Config extends ConfigBase implements NapCatConfig { + name: string = 'napcat' fileLog = true; consoleLog = true; fileLogLevel = LogLevel.DEBUG; @@ -21,10 +22,6 @@ class Config extends ConfigBase implements NapCatConfig { constructor() { super(); } - getConfigPath(pathName: string) { - const filename = `napcat${pathName ? "_" : ""}${pathName}.json` - return path.join(this.getConfigDir(), filename); - } } export const napCatConfig = new Config(); diff --git a/src/onebot11/config.ts b/src/onebot11/config.ts index dc26b110..8bce5c1b 100644 --- a/src/onebot11/config.ts +++ b/src/onebot11/config.ts @@ -42,6 +42,7 @@ export interface OB11Config { } class Config extends ConfigBase implements OB11Config { + name: string = 'onebot11' http = { enable: false, host: '', @@ -72,11 +73,6 @@ class Config extends ConfigBase implements OB11Config { RecordList: [] as Array }; - getConfigPath(pathName: string) { - const filename = `onebot11_${pathName ? "_" : ""}${pathName}.json` - return path.join(this.getConfigDir(), filename); - } - protected getKeys(): string[] | null { return null; }