mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2024-11-21 09:36:35 +00:00
fix & refactor: no longer merge config data and loader; use static imports to represent config struct
This commit is contained in:
parent
12f6b1ca45
commit
ae981fe57d
@ -9,8 +9,7 @@ import { LegacyNTEventWrapper } from '@/common/framework/event-legacy';
|
|||||||
import { NTQQFileApi, NTQQFriendApi, NTQQGroupApi, NTQQMsgApi, NTQQSystemApi, NTQQUserApi, NTQQWebApi } from './apis';
|
import { NTQQFileApi, NTQQFriendApi, NTQQGroupApi, NTQQMsgApi, NTQQSystemApi, NTQQUserApi, NTQQWebApi } from './apis';
|
||||||
import os from 'node:os';
|
import os from 'node:os';
|
||||||
import { NTQQCollectionApi } from './apis/collection';
|
import { NTQQCollectionApi } from './apis/collection';
|
||||||
import { OB11Config } from '@/onebot/helper/config';
|
import { NapCatConfigLoader } from './helper/config';
|
||||||
import { NapCatConfig } from './helper/config';
|
|
||||||
|
|
||||||
export enum NapCatCoreWorkingEnv {
|
export enum NapCatCoreWorkingEnv {
|
||||||
Unknown = 0,
|
Unknown = 0,
|
||||||
@ -38,7 +37,7 @@ export class NapCatCore {
|
|||||||
// runtime info, not readonly
|
// runtime info, not readonly
|
||||||
selfInfo: SelfInfo;
|
selfInfo: SelfInfo;
|
||||||
util: NodeQQNTWrapperUtil;
|
util: NodeQQNTWrapperUtil;
|
||||||
config: any;
|
configLoader: NapCatConfigLoader;
|
||||||
|
|
||||||
// 通过构造器递过去的 runtime info 应该尽量少
|
// 通过构造器递过去的 runtime info 应该尽量少
|
||||||
constructor(context: InstanceContext, selfInfo: SelfInfo) {
|
constructor(context: InstanceContext, selfInfo: SelfInfo) {
|
||||||
@ -56,7 +55,7 @@ export class NapCatCore {
|
|||||||
UserApi: new NTQQUserApi(this.context, this),
|
UserApi: new NTQQUserApi(this.context, this),
|
||||||
GroupApi: new NTQQGroupApi(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');
|
this.NapCatDataPath = path.join(this.dataPath, 'NapCat');
|
||||||
fs.mkdirSync(this.NapCatDataPath, { recursive: true });
|
fs.mkdirSync(this.NapCatDataPath, { recursive: true });
|
||||||
this.NapCatTempPath = path.join(this.NapCatDataPath, 'temp');
|
this.NapCatTempPath = path.join(this.NapCatDataPath, 'temp');
|
||||||
@ -185,9 +184,9 @@ export class NapCatCore {
|
|||||||
}
|
}
|
||||||
checkAdminEvent(groupCode: string, memberNew: GroupMember, memberOld: GroupMember | undefined): boolean {
|
checkAdminEvent(groupCode: string, memberNew: GroupMember, memberOld: GroupMember | undefined): boolean {
|
||||||
if (memberNew.role !== memberOld?.role) {
|
if (memberNew.role !== memberOld?.role) {
|
||||||
this.context.logger.log(`群 ${groupCode} ${memberNew.nick} 角色变更为 ${memberNew.role === 3 ? '管理员' : '群员'}`);
|
this.context.logger.log(`群 ${groupCode} ${memberNew.nick} 角色变更为 ${memberNew.role === 3 ? '管理员' : '群员'}`);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,15 +1,11 @@
|
|||||||
import { ConfigBase } from "@/common/utils/ConfigBase";
|
import { ConfigBase } from "@/common/utils/ConfigBase";
|
||||||
import { LogLevel } from "@/common/utils/log";
|
import { LogLevel } from "@/common/utils/log";
|
||||||
|
import napCatDefaultConfig from '@/core/external/napcat.json';
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
|
||||||
export interface NapCatConfig {
|
export type NapCatConfig = typeof napCatDefaultConfig;
|
||||||
fileLog: boolean,
|
|
||||||
consoleLog: boolean,
|
|
||||||
fileLogLevel: LogLevel,
|
|
||||||
consoleLogLevel: LogLevel,
|
|
||||||
}
|
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
|
||||||
export class NapCatConfig extends ConfigBase<NapCatConfig> {
|
export class NapCatConfigLoader extends ConfigBase<NapCatConfig> {
|
||||||
name = 'napcat';
|
name = 'napcat';
|
||||||
}
|
}
|
||||||
|
@ -1,40 +1,10 @@
|
|||||||
import { ConfigBase } from '@/common/utils/ConfigBase';
|
import { ConfigBase } from '@/common/utils/ConfigBase';
|
||||||
|
import ob11DefaultConfig from '@/onebot/external/onebot11.json';
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
|
||||||
export interface OB11Config {
|
export type OB11Config = typeof ob11DefaultConfig;
|
||||||
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>
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
|
// eslint-disable-next-line @typescript-eslint/no-unsafe-declaration-merging
|
||||||
export class OB11Config extends ConfigBase<OB11Config> {
|
export class OB11ConfigLoader extends ConfigBase<OB11Config> {
|
||||||
name = 'onebot11';
|
name = 'onebot11';
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ import {
|
|||||||
NapCatCore,
|
NapCatCore,
|
||||||
RawMessage,
|
RawMessage,
|
||||||
} from '@/core';
|
} from '@/core';
|
||||||
import { OB11Config } from './helper/config';
|
import { OB11Config, OB11ConfigLoader } from './helper/config';
|
||||||
import { NapCatPathWrapper } from '@/common/framework/napcat';
|
import { NapCatPathWrapper } from '@/common/framework/napcat';
|
||||||
import { OneBotApiContextType } from '@/onebot/types';
|
import { OneBotApiContextType } from '@/onebot/types';
|
||||||
import { OneBotFriendApi, OneBotGroupApi, OneBotUserApi } from './api';
|
import { OneBotFriendApi, OneBotGroupApi, OneBotUserApi } from './api';
|
||||||
@ -39,7 +39,7 @@ export class NapCatOneBot11Adapter {
|
|||||||
readonly core: NapCatCore;
|
readonly core: NapCatCore;
|
||||||
readonly context: InstanceContext;
|
readonly context: InstanceContext;
|
||||||
|
|
||||||
config: OB11Config;
|
configLoader: OB11ConfigLoader;
|
||||||
apiContext: OneBotApiContextType;
|
apiContext: OneBotApiContextType;
|
||||||
networkManager: OB11NetworkManager;
|
networkManager: OB11NetworkManager;
|
||||||
|
|
||||||
@ -48,7 +48,7 @@ export class NapCatOneBot11Adapter {
|
|||||||
constructor(core: NapCatCore, context: InstanceContext, pathWrapper: NapCatPathWrapper) {
|
constructor(core: NapCatCore, context: InstanceContext, pathWrapper: NapCatPathWrapper) {
|
||||||
this.core = core;
|
this.core = core;
|
||||||
this.context = context;
|
this.context = context;
|
||||||
this.config = new OB11Config(core, pathWrapper.configPath);
|
this.configLoader = new OB11ConfigLoader(core, pathWrapper.configPath);
|
||||||
this.apiContext = {
|
this.apiContext = {
|
||||||
GroupApi: new OneBotGroupApi(this, core),
|
GroupApi: new OneBotGroupApi(this, core),
|
||||||
UserApi: new OneBotUserApi(this, core),
|
UserApi: new OneBotUserApi(this, core),
|
||||||
@ -61,7 +61,7 @@ export class NapCatOneBot11Adapter {
|
|||||||
async InitOneBot() {
|
async InitOneBot() {
|
||||||
const NTQQUserApi = this.core.apis.UserApi;
|
const NTQQUserApi = this.core.apis.UserApi;
|
||||||
const selfInfo = this.core.selfInfo;
|
const selfInfo = this.core.selfInfo;
|
||||||
const ob11Config = this.config.configData;
|
const ob11Config = this.configLoader.configData;
|
||||||
|
|
||||||
const serviceInfo = `
|
const serviceInfo = `
|
||||||
HTTP服务 ${ob11Config.http.enable ? '已启动' : '未启动'}, ${ob11Config.http.host}:${ob11Config.http.port}
|
HTTP服务 ${ob11Config.http.enable ? '已启动' : '未启动'}, ${ob11Config.http.host}:${ob11Config.http.port}
|
||||||
@ -113,7 +113,7 @@ export class NapCatOneBot11Adapter {
|
|||||||
await WebUiDataRuntime.setQQLoginUin(selfInfo.uin.toString());
|
await WebUiDataRuntime.setQQLoginUin(selfInfo.uin.toString());
|
||||||
await WebUiDataRuntime.setQQLoginStatus(true);
|
await WebUiDataRuntime.setQQLoginStatus(true);
|
||||||
await WebUiDataRuntime.setOB11ConfigCall(async (ob11: OB11Config) => {
|
await WebUiDataRuntime.setOB11ConfigCall(async (ob11: OB11Config) => {
|
||||||
this.config.save(ob11);
|
this.configLoader.save(ob11);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -157,10 +157,10 @@ export class NapCatOneBot11Adapter {
|
|||||||
// console.log(msg);
|
// console.log(msg);
|
||||||
if (msg.sendStatus == 2) {
|
if (msg.sendStatus == 2) {
|
||||||
// 完成后再post
|
// 完成后再post
|
||||||
OB11Constructor.message(this.core, msg, this.config.messagePostFormat)
|
OB11Constructor.message(this.core, msg, this.configLoader.configData.messagePostFormat)
|
||||||
.then((ob11Msg) => {
|
.then((ob11Msg) => {
|
||||||
ob11Msg.target_id = parseInt(msg.peerUin);
|
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);
|
msg.id = MessageUnique.createMsg({ chatType: msg.chatType, peerUid: msg.peerUid, guildId: '' }, msg.msgId);
|
||||||
this.emitMsg(msg);
|
this.emitMsg(msg);
|
||||||
} else {
|
} else {
|
||||||
@ -322,9 +322,9 @@ export class NapCatOneBot11Adapter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private async emitMsg(message: RawMessage) {
|
private async emitMsg(message: RawMessage) {
|
||||||
const { debug, reportSelfMessage } = this.config;
|
const { debug, reportSelfMessage, messagePostFormat } = this.configLoader.configData;
|
||||||
this.context.logger.logDebug('收到新消息', message);
|
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);
|
this.context.logger.logDebug('收到消息: ', ob11Msg);
|
||||||
if (debug) {
|
if (debug) {
|
||||||
ob11Msg.raw = message;
|
ob11Msg.raw = message;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user