From 6dc4f385815b88749faa0d010c37651b59683995 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=8B=E7=93=9C=E4=B8=80=E5=8D=81=E9=9B=AA?= Date: Sat, 16 Nov 2024 18:38:44 +0800 Subject: [PATCH] refactor: AdapterConfig --- src/onebot/action/go-cqhttp/GetFriendMsgHistory.ts | 3 ++- src/onebot/action/go-cqhttp/GetGroupMsgHistory.ts | 3 ++- src/onebot/action/group/GetGroupEssence.ts | 3 ++- src/onebot/action/msg/GetMsg.ts | 3 ++- src/onebot/action/user/GetRecentContact.ts | 3 ++- src/onebot/config/config.ts | 7 +++++-- src/onebot/index.ts | 10 +++++----- 7 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/onebot/action/go-cqhttp/GetFriendMsgHistory.ts b/src/onebot/action/go-cqhttp/GetFriendMsgHistory.ts index 15cc53cb..dd72e1bd 100644 --- a/src/onebot/action/go-cqhttp/GetFriendMsgHistory.ts +++ b/src/onebot/action/go-cqhttp/GetFriendMsgHistory.ts @@ -4,6 +4,7 @@ import { ActionName } from '../types'; import { ChatType } from '@/core/entities'; import { FromSchema, JSONSchema } from 'json-schema-to-ts'; import { MessageUnique } from '@/common/message-unique'; +import { AdapterConfigWrap } from '@/onebot/config/config'; interface Response { messages: OB11Message[]; @@ -45,7 +46,7 @@ export default class GetFriendMsgHistory extends BaseAction { await Promise.all(msgList.map(async msg => { msg.id = MessageUnique.createUniqueMsgId({ guildId: '', chatType: msg.chatType, peerUid: msg.peerUid }, msg.msgId); })); - const network = Object.values(this.obContext.configLoader.configData.network) as Array; + const network = Object.values(this.obContext.configLoader.configData.network) as Array; //烘焙消息 const ob11MsgList = (await Promise.all( msgList.map(msg => this.obContext.apis.MsgApi.parseMessage(msg, network.flat().find(e => e.name === adapter)?.messagePostFormat ?? 'array'))) diff --git a/src/onebot/action/go-cqhttp/GetGroupMsgHistory.ts b/src/onebot/action/go-cqhttp/GetGroupMsgHistory.ts index 63047433..3020a15e 100644 --- a/src/onebot/action/go-cqhttp/GetGroupMsgHistory.ts +++ b/src/onebot/action/go-cqhttp/GetGroupMsgHistory.ts @@ -4,6 +4,7 @@ import { ActionName } from '../types'; import { ChatType, Peer } from '@/core/entities'; import { FromSchema, JSONSchema } from 'json-schema-to-ts'; import { MessageUnique } from '@/common/message-unique'; +import { AdapterConfigWrap } from '@/onebot/config/config'; interface Response { messages: OB11Message[]; @@ -43,7 +44,7 @@ export default class GoCQHTTPGetGroupMsgHistory extends BaseAction { msg.id = MessageUnique.createUniqueMsgId({ guildId: '', chatType: msg.chatType, peerUid: msg.peerUid }, msg.msgId); })); - const network = Object.values(this.obContext.configLoader.configData.network) as Array; + const network = Object.values(this.obContext.configLoader.configData.network) as Array; //烘焙消息 const msgFormat = network.flat().find(e => e.name === adapter)?.messagePostFormat ?? 'array'; const ob11MsgList = (await Promise.all( diff --git a/src/onebot/action/group/GetGroupEssence.ts b/src/onebot/action/group/GetGroupEssence.ts index 374874b8..1895bda1 100644 --- a/src/onebot/action/group/GetGroupEssence.ts +++ b/src/onebot/action/group/GetGroupEssence.ts @@ -4,6 +4,7 @@ import { ActionName } from '../types'; import { FromSchema, JSONSchema } from 'json-schema-to-ts'; import { MessageUnique } from '@/common/message-unique'; import crypto from 'crypto'; +import { AdapterConfigWrap } from '@/onebot/config/config'; const SchemaData = { type: 'object', @@ -31,7 +32,7 @@ export class GetGroupEssence extends BaseAction { } async _handle(payload: Payload, adapter: string) { - const network = Object.values(this.obContext.configLoader.configData.network) as Array; + const network = Object.values(this.obContext.configLoader.configData.network) as Array; const msgFormat = network.flat().find(e => e.name === adapter)?.messagePostFormat ?? 'array'; const msglist = (await this.core.apis.WebApi.getGroupEssenceMsgAll(payload.group_id.toString())).flatMap((e) => e.data.msg_list); if (!msglist) { diff --git a/src/onebot/action/msg/GetMsg.ts b/src/onebot/action/msg/GetMsg.ts index 58f61eea..f5a85814 100644 --- a/src/onebot/action/msg/GetMsg.ts +++ b/src/onebot/action/msg/GetMsg.ts @@ -4,6 +4,7 @@ import { ActionName } from '../types'; import { FromSchema, JSONSchema } from 'json-schema-to-ts'; import { MessageUnique } from '@/common/message-unique'; import { RawMessage } from '@/core'; +import { AdapterConfigWrap } from '@/onebot/config/config'; export type ReturnDataType = OB11Message @@ -24,7 +25,7 @@ class GetMsg extends BaseAction { async _handle(payload: Payload, adapter: string) { // log("history msg ids", Object.keys(msgHistory)); - const network = Object.values(this.obContext.configLoader.configData.network) as Array; + const network = Object.values(this.obContext.configLoader.configData.network) as Array; const msgFormat = network.flat().find(e => e.name === adapter)?.messagePostFormat ?? 'array'; if (!payload.message_id) { throw Error('参数message_id不能为空'); diff --git a/src/onebot/action/user/GetRecentContact.ts b/src/onebot/action/user/GetRecentContact.ts index 8ce2c58d..05df160a 100644 --- a/src/onebot/action/user/GetRecentContact.ts +++ b/src/onebot/action/user/GetRecentContact.ts @@ -1,6 +1,7 @@ import { FromSchema, JSONSchema } from 'json-schema-to-ts'; import BaseAction from '../BaseAction'; import { ActionName } from '../types'; +import { AdapterConfigWrap } from '@/onebot/config/config'; const SchemaData = { type: 'object', @@ -17,7 +18,7 @@ export default class GetRecentContact extends BaseAction { async _handle(payload: Payload, adapter: string) { const ret = await this.core.apis.UserApi.getRecentContactListSnapShot(+(payload.count || 10)); - const network = Object.values(this.obContext.configLoader.configData.network) as Array; + const network = Object.values(this.obContext.configLoader.configData.network) as Array; //烘焙消息 const msgFormat = network.flat().find(e => e.name === adapter)?.messagePostFormat ?? 'array'; return await Promise.all(ret.info.changedList.map(async (t) => { diff --git a/src/onebot/config/config.ts b/src/onebot/config/config.ts index 0094f997..00e6d55c 100644 --- a/src/onebot/config/config.ts +++ b/src/onebot/config/config.ts @@ -25,10 +25,13 @@ interface v1Config { reportSelfMessage: boolean; token: string; } - -export interface AdapterConfig { +export interface AdapterConfigInner { name: string; enable: boolean; +} +export type AdapterConfigWrap = AdapterConfigInner & Partial; + +export interface AdapterConfig extends AdapterConfigInner { [key: string]: any; } diff --git a/src/onebot/index.ts b/src/onebot/index.ts index 6d290d39..ffd65702 100644 --- a/src/onebot/index.ts +++ b/src/onebot/index.ts @@ -47,7 +47,7 @@ import { OB11GroupRecallNoticeEvent } from '@/onebot/event/notice/OB11GroupRecal import { LRUCache } from '@/common/lru-cache'; import { NodeIKernelRecentContactListener } from '@/core/listeners/NodeIKernelRecentContactListener'; import { BotOfflineEvent } from './event/notice/BotOfflineEvent'; -import { mergeOneBotConfigs, migrateOneBotConfigsV1, NetworkConfigAdapter, OneBotConfig } from './config/config'; +import { AdapterConfigWrap, mergeOneBotConfigs, migrateOneBotConfigsV1, NetworkConfigAdapter, OneBotConfig } from './config/config'; import { OB11Message } from './types'; //OneBot实现类 @@ -543,7 +543,7 @@ export class NapCatOneBot11Adapter { } private async emitMsg(message: RawMessage) { - const network = Object.values(this.configLoader.configData.network) as Array; + const network = Object.values(this.configLoader.configData.network) as Array; this.context.logger.logDebug('收到新消息 RawMessage', message); try { const ob11Msg = await this.apis.MsgApi.parseMessageV2(message); @@ -573,7 +573,7 @@ export class NapCatOneBot11Adapter { ob11Msg.arrayMsg.user_id.toString() == this.core.selfInfo.uin; } - private createMsgMap(network: Array, ob11Msg: any, isSelfMsg: boolean, message: RawMessage): Map { + private createMsgMap(network: Array, ob11Msg: any, isSelfMsg: boolean, message: RawMessage): Map { const msgMap: Map = new Map(); network.flat().filter(e => e.enable).forEach(e => { if (e.messagePostFormat == 'string') { @@ -589,7 +589,7 @@ export class NapCatOneBot11Adapter { return msgMap; } - private handleDebugNetwork(network: Array, msgMap: Map, message: RawMessage) { + private handleDebugNetwork(network: Array, msgMap: Map, message: RawMessage) { const debugNetwork = network.flat().filter(e => e.enable && e.debug); if (debugNetwork.length > 0) { debugNetwork.forEach(adapter => { @@ -603,7 +603,7 @@ export class NapCatOneBot11Adapter { } } - private handleNotReportSelfNetwork(network: Array, msgMap: Map, isSelfMsg: boolean) { + private handleNotReportSelfNetwork(network: Array, msgMap: Map, isSelfMsg: boolean) { if (isSelfMsg) { const notReportSelfNetwork = network.flat().filter(e => e.enable && (('reportSelfMessage' in e && !e.reportSelfMessage) || !('reportSelfMessage' in e))); notReportSelfNetwork.forEach(adapter => {