mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2024-11-21 09:36:35 +00:00
Merge branch 'main' of https://github.com/NapNeko/NapCatQQ
This commit is contained in:
commit
0efdffd857
@ -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);
|
||||
@ -27,13 +28,20 @@ export class ConfigBase<T>{
|
||||
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)) {
|
||||
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);
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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 {
|
||||
|
Loading…
x
Reference in New Issue
Block a user