chore: config

This commit is contained in:
手瓜一十雪
2024-08-10 22:25:25 +08:00
parent 6e125f15a4
commit 15f4841328
2 changed files with 23 additions and 39 deletions

View File

@@ -3,11 +3,15 @@ import fs from 'node:fs';
import type { NapCatCore } from '@/core'; import type { NapCatCore } from '@/core';
export class ConfigBase<T> { export class ConfigBase<T> {
public name: string = 'default_config'; name: string = this.getConfigName();
private pathName: string | null = null; // 本次读取的文件路径 pathName: string | null = null; // 本次读取的文件路径
coreContext: NapCatCore; coreContext: NapCatCore;
configPath: string; configPath: string;
config: { [key: string]: any } = {}; configData: T = {} as T;
getConfigName() {
return this.constructor.name
}
constructor(coreContext: NapCatCore, configPath: string) { constructor(coreContext: NapCatCore, configPath: string) {
this.coreContext = coreContext; this.coreContext = coreContext;
@@ -22,62 +26,43 @@ export class ConfigBase<T> {
} }
getConfigPath(pathName: string | null): string { getConfigPath(pathName: string | undefined): string {
const suffix = pathName ? `_${pathName}` : ''; const suffix = pathName ? `_${pathName}` : '';
const filename = `${this.name}${suffix}.json`; const filename = `${this.name}${suffix}.json`;
return path.join(this.configPath, filename); return path.join(this.configPath, filename);
} }
read() { read(): T {
// 尝试加载当前账号配置
if (this.read_from_file(this.coreContext.selfInfo.uin, false)) return this.config;
// 尝试加载默认配置
return this.read_from_file('', true);
}
getConfig(): T {
return this.config as T;
}
read_from_file(pathName: string, createIfNotExist: boolean) {
const logger = this.coreContext.context.logger; const logger = this.coreContext.context.logger;
const configPath = this.getConfigPath(pathName); const configPath = this.getConfigPath(this.coreContext.selfInfo.uin);
if (!fs.existsSync(configPath)) { if (!fs.existsSync(configPath)) {
if (!createIfNotExist) return null; this.pathName = configPath; // 记录有效的设置文件
this.pathName = pathName; // 记录有效的设置文件
try { try {
fs.writeFileSync(configPath, JSON.stringify(this.config, this.getKeys(), 2)); fs.writeFileSync(configPath, fs.readFileSync(this.getConfigPath(undefined), 'utf-8'));
logger.log(`配置文件${configPath}创建\n如果修改此文件后需要重启 NapCat 生效`); logger.log(`配置文件${configPath}创建成功!\n`);
} catch (e: any) { } catch (e: any) {
logger.logError(`创建配置文件 ${configPath} 时发生错误:`, e.message); logger.logError(`创建配置文件 ${configPath} 时发生错误:`, e.message);
} }
return this.config;
} }
try { try {
this.config = JSON.parse(fs.readFileSync(configPath, 'utf-8')); this.configData = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
logger.logDebug(`配置文件${configPath}已加载`, this.config); logger.logDebug(`配置文件${configPath}已加载`, this.configData);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment return this.configData;
this.save(); // 保存一次,让新版本的字段写入
return this.config;
} catch (e: any) { } catch (e: any) {
if (e instanceof SyntaxError) { if (e instanceof SyntaxError) {
logger.logError(`配置文件 ${configPath} 格式错误,请检查配置文件:`, e.message); logger.logError(`配置文件 ${configPath} 格式错误,请检查配置文件:`, e.message);
} else { } else {
logger.logError(`读取配置文件 ${configPath} 时发生错误:`, e.message); logger.logError(`读取配置文件 ${configPath} 时发生错误:`, e.message);
} }
return {}; return {} as T;
} }
} }
save(configData: T = this.config as T, overwrite: boolean = false) {
save(configData: T = this.configData as T) {
const logger = this.coreContext.context.logger; const logger = this.coreContext.context.logger;
const selfInfo = this.coreContext.selfInfo; const selfInfo = this.coreContext.selfInfo;
if (overwrite) { const configPath = this.getConfigPath(selfInfo.uin);
// 用户要求强制写入,则变更当前文件为目标文件
this.pathName = `${selfInfo.uin}`;
}
const configPath = this.getConfigPath(this.pathName);
try { try {
fs.writeFileSync(configPath, JSON.stringify(configData, this.getKeys(), 2)); fs.writeFileSync(configPath, JSON.stringify(configData, this.getKeys(), 2));
} catch (e: any) { } catch (e: any) {

View File

@@ -34,9 +34,8 @@ export interface OB11Config {
} }
export class OB11Config extends ConfigBase<OB11Config> { export class OB11Config extends ConfigBase<OB11Config> {
name = 'onebot11'; getConfigName() {
return 'onebot11';
}
protected getKeys(): string[] | null {
return null;
}
} }