From 6ca768c3ee9eda22d504d5eca29d3b6c7026d834 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B1=89=E5=B9=BF?= Date: Sat, 20 Jul 2024 23:43:32 +0800 Subject: [PATCH] feat[config]support use default-template --- src/common/utils/ConfigBase.ts | 19 ++++++++++++++----- src/core/src/utils/config.ts | 11 ++++++----- src/onebot11/config.ts | 5 +++-- 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/common/utils/ConfigBase.ts b/src/common/utils/ConfigBase.ts index 6ea6956b..50aeefb0 100644 --- a/src/common/utils/ConfigBase.ts +++ b/src/common/utils/ConfigBase.ts @@ -3,6 +3,7 @@ import fs from 'node:fs'; import { log, logDebug, logError } from '@/common/utils/log'; import { dirname } from 'node:path'; import { fileURLToPath } from 'node:url'; +import { selfInfo } from '@/core/data'; const __filename = fileURLToPath(import.meta.url); @@ -12,7 +13,7 @@ const configDir = path.resolve(__dirname, 'config'); fs.mkdirSync(configDir, { recursive: true }); -export class ConfigBase{ +export class ConfigBase { constructor() { } @@ -22,19 +23,26 @@ export class ConfigBase{ return null; } - getConfigDir(){ + getConfigDir() { const configDir = path.resolve(__dirname, 'config'); fs.mkdirSync(configDir, { recursive: true }); return configDir; } - getConfigPath(): string { + getConfigPath(pathName: string): string { throw new Error('Method not implemented.'); } read() { - const configPath = this.getConfigPath(); + // 尝试加载当前账号配置 + if (this.read_from_file(selfInfo.uin, false)) return this + // 尝试加载默认配置 + return this.read_from_file('', true) + } + read_from_file(pathName: string, createIfNotExist: boolean) { + const configPath = this.getConfigPath(pathName); if (!fs.existsSync(configPath)) { - try{ + if (!createIfNotExist) return null + try { fs.writeFileSync(configPath, JSON.stringify(this, this.getKeys(), 2)); log(`配置文件${configPath}已创建\n如果修改此文件后需要重启 NapCat 生效`); } @@ -43,6 +51,7 @@ export class ConfigBase{ } return this; } + try { const data = JSON.parse(fs.readFileSync(configPath, 'utf-8')); logDebug(`配置文件${configPath}已加载`, data); diff --git a/src/core/src/utils/config.ts b/src/core/src/utils/config.ts index d7e90ca7..5ee7b302 100644 --- a/src/core/src/utils/config.ts +++ b/src/core/src/utils/config.ts @@ -12,17 +12,18 @@ export interface NapCatConfig { consoleLogLevel: LogLevel, } -class Config extends ConfigBase implements NapCatConfig{ +class Config extends ConfigBase implements NapCatConfig { fileLog = true; consoleLog = true; - fileLogLevel = LogLevel.DEBUG; - consoleLogLevel = LogLevel.INFO; + fileLogLevel = LogLevel.DEBUG; + consoleLogLevel = LogLevel.INFO; constructor() { super(); } - getConfigPath() { - return path.join(this.getConfigDir(), `napcat_${selfInfo.uin}.json`); + getConfigPath(pathName: string) { + const filename = `napcat${pathName ? "_" : ""}${pathName}.json` + return path.join(this.getConfigDir(), filename); } } diff --git a/src/onebot11/config.ts b/src/onebot11/config.ts index 50ece431..dc26b110 100644 --- a/src/onebot11/config.ts +++ b/src/onebot11/config.ts @@ -72,8 +72,9 @@ class Config extends ConfigBase implements OB11Config { RecordList: [] as Array }; - getConfigPath() { - return path.join(this.getConfigDir(), `onebot11_${selfInfo.uin}.json`); + getConfigPath(pathName: string) { + const filename = `onebot11_${pathName ? "_" : ""}${pathName}.json` + return path.join(this.getConfigDir(), filename); } protected getKeys(): string[] | null {