Skip to content

Commit

Permalink
fix & refactor: no longer merge config data and loader; use static im…
Browse files Browse the repository at this point in the history
…ports to represent config struct
  • Loading branch information
Wesley-Young committed Aug 11, 2024
1 parent 12f6b1c commit ae981fe
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 56 deletions.
13 changes: 6 additions & 7 deletions src/core/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import { LegacyNTEventWrapper } from '@/common/framework/event-legacy';
import { NTQQFileApi, NTQQFriendApi, NTQQGroupApi, NTQQMsgApi, NTQQSystemApi, NTQQUserApi, NTQQWebApi } from './apis';
import os from 'node:os';
import { NTQQCollectionApi } from './apis/collection';
import { OB11Config } from '@/onebot/helper/config';
import { NapCatConfig } from './helper/config';
import { NapCatConfigLoader } from './helper/config';

export enum NapCatCoreWorkingEnv {
Unknown = 0,
Expand Down Expand Up @@ -38,7 +37,7 @@ export class NapCatCore {
// runtime info, not readonly
selfInfo: SelfInfo;
util: NodeQQNTWrapperUtil;
config: any;
configLoader: NapCatConfigLoader;

// 通过构造器递过去的 runtime info 应该尽量少
constructor(context: InstanceContext, selfInfo: SelfInfo) {
Expand All @@ -56,7 +55,7 @@ export class NapCatCore {
UserApi: new NTQQUserApi(this.context, this),
GroupApi: new NTQQGroupApi(this.context, this),
};
this.config = new NapCatConfig(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');
Expand Down Expand Up @@ -185,9 +184,9 @@ export class NapCatCore {
}
checkAdminEvent(groupCode: string, memberNew: GroupMember, memberOld: GroupMember | undefined): boolean {
if (memberNew.role !== memberOld?.role) {
this.context.logger.log(`群 ${groupCode} ${memberNew.nick} 角色变更为 ${memberNew.role === 3 ? '管理员' : '群员'}`);
return true;
this.context.logger.log(`群 ${groupCode} ${memberNew.nick} 角色变更为 ${memberNew.role === 3 ? '管理员' : '群员'}`);
return true;
}
return false;
}
}
}
10 changes: 3 additions & 7 deletions src/core/helper/config.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import { ConfigBase } from "@/common/utils/ConfigBase";
import { LogLevel } from "@/common/utils/log";
import napCatDefaultConfig from '@/core/external/napcat.json';

// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
export interface NapCatConfig {
fileLog: boolean,
consoleLog: boolean,
fileLogLevel: LogLevel,
consoleLogLevel: LogLevel,
}
export type NapCatConfig = typeof napCatDefaultConfig;

// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
export class NapCatConfig extends ConfigBase<NapCatConfig> {
export class NapCatConfigLoader extends ConfigBase<NapCatConfig> {
name = 'napcat';
}
36 changes: 3 additions & 33 deletions src/onebot/helper/config.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,10 @@
import { ConfigBase } from '@/common/utils/ConfigBase';
import ob11DefaultConfig from '@/onebot/external/onebot11.json';

// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
export interface OB11Config {
http: {
enable: boolean;
host: string;
port: number;
secret: string;
enableHeart: boolean;
enablePost: boolean;
postUrls: string[];
};
ws: {
enable: boolean;
host: string;
port: number;
};
reverseWs: {
enable: boolean;
urls: string[];
};

debug: boolean;
heartInterval: number;
messagePostFormat: 'array' | 'string';
enableLocalFile2Url: boolean;
musicSignUrl: string;
reportSelfMessage: boolean;
token: string;
GroupLocalTime: {
Record: boolean,
RecordList: Array<string>
};
}
export type OB11Config = typeof ob11DefaultConfig;

// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
export class OB11Config extends ConfigBase<OB11Config> {
export class OB11ConfigLoader extends ConfigBase<OB11Config> {
name = 'onebot11';
}
18 changes: 9 additions & 9 deletions src/onebot/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
NapCatCore,
RawMessage,
} from '@/core';
import { OB11Config } from './helper/config';
import { OB11Config, OB11ConfigLoader } from './helper/config';
import { NapCatPathWrapper } from '@/common/framework/napcat';
import { OneBotApiContextType } from '@/onebot/types';
import { OneBotFriendApi, OneBotGroupApi, OneBotUserApi } from './api';
Expand Down Expand Up @@ -39,7 +39,7 @@ export class NapCatOneBot11Adapter {
readonly core: NapCatCore;
readonly context: InstanceContext;

config: OB11Config;
configLoader: OB11ConfigLoader;
apiContext: OneBotApiContextType;
networkManager: OB11NetworkManager;

Expand All @@ -48,7 +48,7 @@ export class NapCatOneBot11Adapter {
constructor(core: NapCatCore, context: InstanceContext, pathWrapper: NapCatPathWrapper) {
this.core = core;
this.context = context;
this.config = new OB11Config(core, pathWrapper.configPath);
this.configLoader = new OB11ConfigLoader(core, pathWrapper.configPath);
this.apiContext = {
GroupApi: new OneBotGroupApi(this, core),
UserApi: new OneBotUserApi(this, core),
Expand All @@ -61,7 +61,7 @@ export class NapCatOneBot11Adapter {
async InitOneBot() {
const NTQQUserApi = this.core.apis.UserApi;
const selfInfo = this.core.selfInfo;
const ob11Config = this.config.configData;
const ob11Config = this.configLoader.configData;

const serviceInfo = `
HTTP服务 ${ob11Config.http.enable ? '已启动' : '未启动'}, ${ob11Config.http.host}:${ob11Config.http.port}
Expand Down Expand Up @@ -113,7 +113,7 @@ export class NapCatOneBot11Adapter {
await WebUiDataRuntime.setQQLoginUin(selfInfo.uin.toString());
await WebUiDataRuntime.setQQLoginStatus(true);
await WebUiDataRuntime.setOB11ConfigCall(async (ob11: OB11Config) => {
this.config.save(ob11);
this.configLoader.save(ob11);
});
}

Expand Down Expand Up @@ -157,10 +157,10 @@ export class NapCatOneBot11Adapter {
// console.log(msg);
if (msg.sendStatus == 2) {
// 完成后再post
OB11Constructor.message(this.core, msg, this.config.messagePostFormat)
OB11Constructor.message(this.core, msg, this.configLoader.configData.messagePostFormat)
.then((ob11Msg) => {
ob11Msg.target_id = parseInt(msg.peerUin);
if (this.config.reportSelfMessage) {
if (this.configLoader.configData.reportSelfMessage) {
msg.id = MessageUnique.createMsg({ chatType: msg.chatType, peerUid: msg.peerUid, guildId: '' }, msg.msgId);
this.emitMsg(msg);
} else {
Expand Down Expand Up @@ -322,9 +322,9 @@ export class NapCatOneBot11Adapter {
}

private async emitMsg(message: RawMessage) {
const { debug, reportSelfMessage } = this.config;
const { debug, reportSelfMessage, messagePostFormat } = this.configLoader.configData;
this.context.logger.logDebug('收到新消息', message);
OB11Constructor.message(this.core, message, this.config.messagePostFormat).then((ob11Msg) => {
OB11Constructor.message(this.core, message, messagePostFormat).then((ob11Msg) => {
this.context.logger.logDebug('收到消息: ', ob11Msg);
if (debug) {
ob11Msg.raw = message;
Expand Down

0 comments on commit ae981fe

Please sign in to comment.