This commit is contained in:
手瓜一十雪 2024-07-22 18:49:36 +08:00
commit 0efdffd857
3 changed files with 23 additions and 12 deletions

View File

@ -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<T>{
export class ConfigBase<T> {
constructor() {
}
@ -22,19 +23,26 @@ export class ConfigBase<T>{
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<T>{
}
return this;
}
try {
const data = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
logDebug(`配置文件${configPath}已加载`, data);

View File

@ -12,7 +12,7 @@ export interface NapCatConfig {
consoleLogLevel: LogLevel,
}
class Config extends ConfigBase<NapCatConfig> implements NapCatConfig{
class Config extends ConfigBase<NapCatConfig> implements NapCatConfig {
fileLog = true;
consoleLog = true;
fileLogLevel = LogLevel.DEBUG;
@ -21,8 +21,9 @@ class Config extends ConfigBase<NapCatConfig> implements NapCatConfig{
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);
}
}

View File

@ -72,8 +72,9 @@ class Config extends ConfigBase<OB11Config> implements OB11Config {
RecordList: [] as Array<string>
};
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 {