chore: config

This commit is contained in:
手瓜一十雪
2024-08-09 22:29:01 +08:00
parent c271a4b2cb
commit 3f6f1dcd78
2 changed files with 48 additions and 81 deletions

View File

@@ -6,9 +6,11 @@ export class ConfigBase<T> {
private pathName: string | null = null; // 本次读取的文件路径 private pathName: string | null = null; // 本次读取的文件路径
coreContext: NapCatCore; coreContext: NapCatCore;
configPath: string; configPath: string;
config: { [key: string]: any } = {};
constructor(coreContext: NapCatCore, configPath: string) { constructor(coreContext: NapCatCore, configPath: string) {
this.coreContext = coreContext; this.coreContext = coreContext;
this.configPath = configPath; this.configPath = configPath;
this.read();
} }
protected getKeys(): string[] | null { protected getKeys(): string[] | null {
@@ -32,6 +34,9 @@ export class ConfigBase<T> {
// 尝试加载默认配置 // 尝试加载默认配置
return this.read_from_file('', true); return this.read_from_file('', true);
} }
getConfig(): T {
return this.config as T;
}
read_from_file(pathName: string, createIfNotExist: boolean) { 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(pathName);
@@ -39,35 +44,32 @@ export class ConfigBase<T> {
if (!createIfNotExist) return null; if (!createIfNotExist) return null;
this.pathName = pathName; // 记录有效的设置文件 this.pathName = pathName; // 记录有效的设置文件
try { try {
fs.writeFileSync(configPath, JSON.stringify(this, this.getKeys(), 2)); fs.writeFileSync(configPath, JSON.stringify(this.config, this.getKeys(), 2));
logger.log(`配置文件${configPath}已创建\n如果修改此文件后需要重启 NapCat 生效`); logger.log(`配置文件${configPath}已创建\n如果修改此文件后需要重启 NapCat 生效`);
} }
catch (e: any) { catch (e: any) {
logger.logError(`创建配置文件 ${configPath} 时发生错误:`, e.message); logger.logError(`创建配置文件 ${configPath} 时发生错误:`, e.message);
} }
return this; return this.config;
} }
try { try {
const data = JSON.parse(fs.readFileSync(configPath, 'utf-8')); this.config = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
logger.logDebug(`配置文件${configPath}已加载`, data); logger.logDebug(`配置文件${configPath}已加载`, this.config);
Object.assign(this, data);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error this.save(); // 保存一次,让新版本的字段写入
this.save(this); // 保存一次,让新版本的字段写入 return this.config;
return this;
} 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 this; return {};
} }
} }
save(config: T, overwrite: boolean = false) { save(configData: T = this.config as T, overwrite: boolean = false) {
Object.assign(this, config);
const logger = this.coreContext.context.logger; const logger = this.coreContext.context.logger;
const selfInfo = this.coreContext.selfInfo; const selfInfo = this.coreContext.selfInfo;
if (overwrite) { if (overwrite) {
@@ -76,7 +78,7 @@ export class ConfigBase<T> {
} }
const configPath = this.getConfigPath(this.pathName); const configPath = this.getConfigPath(this.pathName);
try { try {
fs.writeFileSync(configPath, JSON.stringify(this, this.getKeys(), 2)); fs.writeFileSync(configPath, JSON.stringify(configData, this.getKeys(), 2));
} catch (e: any) { } catch (e: any) {
logger.logError(`保存配置文件 ${configPath} 时发生错误:`, e.message); logger.logError(`保存配置文件 ${configPath} 时发生错误:`, e.message);
} }

View File

@@ -1,75 +1,40 @@
import fs from 'node:fs';
import path from 'node:path';
import { ConfigBase } from '@/common/utils/ConfigBase'; import { ConfigBase } from '@/common/utils/ConfigBase';
// export interface OB11Config { export interface OB11Config {
// http: { http: {
// enable: boolean; enable: boolean;
// host: string; host: string;
// port: number; port: number;
// secret: string; secret: string;
// enableHeart: boolean; enableHeart: boolean;
// enablePost: boolean; enablePost: boolean;
// postUrls: string[]; postUrls: string[];
// }; };
// ws: { ws: {
// enable: boolean; enable: boolean;
// host: string; host: string;
// port: number; port: number;
// }; };
// reverseWs: { reverseWs: {
// enable: boolean; enable: boolean;
// urls: string[]; urls: string[];
// }; };
// debug: boolean; debug: boolean;
// heartInterval: number; heartInterval: number;
// messagePostFormat: 'array' | 'string'; messagePostFormat: 'array' | 'string';
// enableLocalFile2Url: boolean; enableLocalFile2Url: boolean;
// musicSignUrl: string; musicSignUrl: string;
// reportSelfMessage: boolean; reportSelfMessage: boolean;
// token: string; token: string;
// GroupLocalTime: { GroupLocalTime: {
// Record: boolean, Record: boolean,
// RecordList: Array<string> RecordList: Array<string>
// }, }
// read(): OB11Config; }
// save(config: OB11Config): void;
// }
export class OB11Config extends ConfigBase<OB11Config> { export class OB11Config extends ConfigBase<OB11Config> {
name: string = 'onebot11'; name = 'onebot11';
http = {
enable: false,
host: '',
port: 3000,
secret: '',
enableHeart: false,
enablePost: false,
postUrls: [],
};
ws = {
enable: false,
host: '',
port: 3001,
};
reverseWs = {
enable: false,
urls: [],
};
debug = false;
heartInterval = 30000;
messagePostFormat: 'array' | 'string' = 'array';
enableLocalFile2Url = true;
musicSignUrl = '';
reportSelfMessage = false;
token = '';
GroupLocalTime = {
Record: false,
RecordList: [] as Array<string>
};
protected getKeys(): string[] | null { protected getKeys(): string[] | null {
return null; return null;
} }