fix[default-config]config name check

This commit is contained in:
汉广 2024-07-22 20:12:24 +08:00
parent 0efdffd857
commit 03bc844ad0
3 changed files with 10 additions and 14 deletions

View File

@ -14,7 +14,8 @@ fs.mkdirSync(configDir, { recursive: true });
export class ConfigBase<T> {
public name: string = 'default_config'
private pathName: string | null = null // 本次读取的文件路径
constructor() {
}
@ -28,10 +29,11 @@ export class ConfigBase<T> {
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<T> {
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<T> {
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) {

View File

@ -13,6 +13,7 @@ export interface NapCatConfig {
}
class Config extends ConfigBase<NapCatConfig> implements NapCatConfig {
name: string = 'napcat'
fileLog = true;
consoleLog = true;
fileLogLevel = LogLevel.DEBUG;
@ -21,10 +22,6 @@ class Config extends ConfigBase<NapCatConfig> implements NapCatConfig {
constructor() {
super();
}
getConfigPath(pathName: string) {
const filename = `napcat${pathName ? "_" : ""}${pathName}.json`
return path.join(this.getConfigDir(), filename);
}
}
export const napCatConfig = new Config();

View File

@ -42,6 +42,7 @@ export interface OB11Config {
}
class Config extends ConfigBase<OB11Config> implements OB11Config {
name: string = 'onebot11'
http = {
enable: false,
host: '',
@ -72,11 +73,6 @@ class Config extends ConfigBase<OB11Config> implements OB11Config {
RecordList: [] as Array<string>
};
getConfigPath(pathName: string) {
const filename = `onebot11_${pathName ? "_" : ""}${pathName}.json`
return path.join(this.getConfigDir(), filename);
}
protected getKeys(): string[] | null {
return null;
}