This commit is contained in:
手瓜一十雪 2024-08-12 01:15:00 +08:00
commit c7669777cb
6 changed files with 35 additions and 21 deletions

View File

@ -3,12 +3,13 @@ import fs from 'node:fs';
import type { NapCatCore } from '@/core';
export abstract class ConfigBase<T> {
abstract name: string;
name: string;
coreContext: NapCatCore;
configPath: string;
configData: T = {} as T;
constructor(coreContext: NapCatCore, configPath: string) {
protected constructor(name: string, coreContext: NapCatCore, configPath: string) {
this.name = name;
this.coreContext = coreContext;
this.configPath = configPath;
fs.mkdirSync(this.configPath, { recursive: true });

View File

@ -68,7 +68,7 @@ export class LogWrapper {
this.setLogSelfInfo({ nick: '', uin: '', uid: '' });
}
setLogLevel(fileLogLevel: LogLevel, consoleLogLevel: LogLevel) {
setFileAndConsoleLogLevel(fileLogLevel: LogLevel, consoleLogLevel: LogLevel) {
this.logConfig.categories.file.level = fileLogLevel;
this.logConfig.categories.console.level = consoleLogLevel;
log4js.configure(this.logConfig);
@ -81,13 +81,12 @@ export class LogWrapper {
this.loggerDefault.addContext('userInfo', userInfo);
}
enableFileLog(enable: boolean) {
this.fileLogEnabled = enable;
setFileLogEnabled(isEnabled: boolean) {
this.fileLogEnabled = isEnabled;
}
enableConsoleLog(enable: boolean) {
this.consoleLogEnabled = enable;
setConsoleLogEnabled(isEnabled: boolean) {
this.consoleLogEnabled = isEnabled;
}
formatMsg(msg: any[]) {

View File

@ -10,6 +10,7 @@ import { NTQQFileApi, NTQQFriendApi, NTQQGroupApi, NTQQMsgApi, NTQQSystemApi, NT
import os from 'node:os';
import { NTQQCollectionApi } from './apis/collection';
import { NapCatConfigLoader } from './helper/config';
import { LogLevel } from '@/common/utils/log';
export enum NapCatCoreWorkingEnv {
Unknown = 0,
@ -55,7 +56,7 @@ export class NapCatCore {
UserApi: new NTQQUserApi(this.context, this),
GroupApi: new NTQQGroupApi(this.context, this),
};
this.configLoader = new NapCatConfigLoader(this,this.context.pathWrapper.configPath);
this.configLoader = new NapCatConfigLoader(this, this.context.pathWrapper.configPath);
this.NapCatDataPath = path.join(this.dataPath, 'NapCat');
fs.mkdirSync(this.NapCatDataPath, { recursive: true });
this.NapCatTempPath = path.join(this.NapCatDataPath, 'temp');
@ -64,6 +65,17 @@ export class NapCatCore {
fs.mkdirSync(this.NapCatTempPath, { recursive: true });
}
this.initNapCatCoreListeners().then().catch(this.context.logger.logError);
this.context.logger.setFileLogEnabled(
this.configLoader.configData.fileLog,
);
this.context.logger.setConsoleLogEnabled(
this.configLoader.configData.consoleLog,
);
this.context.logger.setFileAndConsoleLogLevel(
this.configLoader.configData.fileLogLevel as LogLevel,
this.configLoader.configData.consoleLogLevel as LogLevel,
);
}
get dataPath(): string {

View File

@ -1,11 +1,13 @@
import { ConfigBase } from "@/common/utils/ConfigBase";
import { LogLevel } from "@/common/utils/log";
import napCatDefaultConfig from '@/core/external/napcat.json';
import { NapCatCore } from '@/core';
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
export type NapCatConfig = typeof napCatDefaultConfig;
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
export class NapCatConfigLoader extends ConfigBase<NapCatConfig> {
name = 'napcat';
constructor(coreContext: NapCatCore, configPath: string) {
super('napcat', coreContext, configPath);
}
}

View File

@ -1,10 +1,11 @@
import { ConfigBase } from '@/common/utils/ConfigBase';
import ob11DefaultConfig from '@/onebot/external/onebot11.json';
import { NapCatCore } from '@/core';
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
export type OB11Config = typeof ob11DefaultConfig;
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
export class OB11ConfigLoader extends ConfigBase<OB11Config> {
name = 'onebot11';
constructor(coreContext: NapCatCore, configPath: string) {
super('onebot11', coreContext, configPath);
}
}

View File

@ -26,7 +26,6 @@ import qrcode from 'qrcode-terminal';
import { NapCatOneBot11Adapter } from '@/onebot';
import { InitWebUi } from '@/webui';
import { WebUiDataRuntime } from '@/webui/src/helper/Data';
import { promisify } from 'util';
program.option('-q, --qq [number]', 'QQ号').parse(process.argv);
const cmdOptions = program.opts();
@ -151,7 +150,7 @@ export async function NCoreInitShell() {
});
WebUiDataRuntime.setQQQuickLoginCall(async (uin: string) => {
const QuickLogin: Promise<{ result: boolean, message: string }> = new Promise((resolve, reject) => {
return await new Promise((resolve) => {
if (uin) {
logger.log('正在快速登录 ', uin);
loginService.quickLoginWithUin(uin).then(res => {
@ -167,8 +166,6 @@ export async function NCoreInitShell() {
resolve({ result: false, message: '快速登录失败' });
}
});
const result = await QuickLogin;
return result;
});
if (quickLoginUin && historyLoginList.some(u => u.uin === quickLoginUin)) {
@ -186,9 +183,11 @@ export async function NCoreInitShell() {
} else {
logger.log('没有 -q 指令指定快速登录,或未曾登录过这个 QQ将使用二维码登录方式');
if (historyLoginList.length > 0) {
logger.log(`可用于快速登录的 QQ\n${historyLoginList.map((u, index) => `${index + 1}. ${u.uin} ${u.nickName}`)
.join('\n')
}`);
logger.log(`可用于快速登录的 QQ\n${
historyLoginList
.map((u, index) => `${index + 1}. ${u.uin} ${u.nickName}`)
.join('\n')
}`);
}
loginService.getQRCodePicture();
}