From a39b0a4a78b7c73c04a29100e39ccd4f0011ec41 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: Wed, 11 Dec 2024 17:07:21 +0800 Subject: [PATCH 1/3] feat: plugin --- src/core/plugin/index.ts | 10 ++++++++ src/onebot/config/config.ts | 3 ++- src/onebot/index.ts | 12 +++++++--- src/onebot/network/index.ts | 15 +++++++++--- src/onebot/network/plugin.ts | 44 ++++++++++++++++++++++++++++++++++++ 5 files changed, 77 insertions(+), 7 deletions(-) create mode 100644 src/core/plugin/index.ts create mode 100644 src/onebot/network/plugin.ts diff --git a/src/core/plugin/index.ts b/src/core/plugin/index.ts new file mode 100644 index 00000000..1a67f00e --- /dev/null +++ b/src/core/plugin/index.ts @@ -0,0 +1,10 @@ +import { OB11Message } from "@/onebot"; +import { NapCatCore } from ".."; +import { ActionMap } from "@/onebot/action"; + +export const plugin_onmessage = async (adapter: string, core: NapCatCore, action: ActionMap, message: OB11Message) => { + if (message.raw_message === 'ping') { + const ret = await action.get('send_group_msg')?.handle({ group_id: message.group_id, message: 'pong' }, adapter); + console.log(ret); + } +} \ No newline at end of file diff --git a/src/onebot/config/config.ts b/src/onebot/config/config.ts index d59247d6..82764b3a 100644 --- a/src/onebot/config/config.ts +++ b/src/onebot/config/config.ts @@ -28,6 +28,7 @@ interface v1Config { export interface AdapterConfigInner { name: string; enable: boolean; + } export type AdapterConfigWrap = AdapterConfigInner & Partial; @@ -127,7 +128,7 @@ export const mergeNetworkDefaultConfig = { websocketClients: websocketClientDefaultConfigs, } as const; -export type NetworkConfigAdapter = HttpServerConfig | HttpClientConfig | WebsocketServerConfig | WebsocketClientConfig; +export type NetworkConfigAdapter = HttpServerConfig | HttpClientConfig | WebsocketServerConfig | WebsocketClientConfig | AdapterConfig; type NetworkConfigKeys = keyof typeof mergeNetworkDefaultConfig; export function mergeOneBotConfigs( diff --git a/src/onebot/index.ts b/src/onebot/index.ts index 95035243..cb64fa6e 100644 --- a/src/onebot/index.ts +++ b/src/onebot/index.ts @@ -47,6 +47,7 @@ import { NodeIKernelRecentContactListener } from '@/core/listeners/NodeIKernelRe import { BotOfflineEvent } from './event/notice/BotOfflineEvent'; import { AdapterConfigWrap, mergeOneBotConfigs, migrateOneBotConfigsV1, NetworkConfigAdapter, OneBotConfig } from './config/config'; import { OB11Message } from './types'; +import { OB11PluginAdapter } from './network/plugin'; //OneBot实现类 export class NapCatOneBot11Adapter { @@ -106,7 +107,12 @@ export class NapCatOneBot11Adapter { const serviceInfo = await this.creatOneBotLog(ob11Config); this.context.logger.log(`[Notice] [OneBot11] ${serviceInfo}`); - // //创建NetWork服务 + //创建NetWork服务 + + // 注册Plugin + this.networkManager.registerAdapter( + new OB11PluginAdapter('plugin', this.core, this.actions) + ); for (const key of ob11Config.network.httpServers) { if (key.enable) { this.networkManager.registerAdapter( @@ -443,7 +449,7 @@ export class NapCatOneBot11Adapter { } private async emitMsg(message: RawMessage) { - const network = Object.values(this.configLoader.configData.network).flat() as Array; + const network = await this.networkManager.getAllConfig(); this.context.logger.logDebug('收到新消息 RawMessage', message); await Promise.allSettled([ this.handleMsg(message, network), @@ -483,7 +489,7 @@ export class NapCatOneBot11Adapter { ob11Msg.stringMsg.target_id = parseInt(message.peerUin); ob11Msg.arrayMsg.target_id = parseInt(message.peerUin); } - if (e.messagePostFormat == 'string') { + if ('messagePostFormat' in e && e.messagePostFormat == 'string') { msgMap.set(e.name, structuredClone(ob11Msg.stringMsg)); } else { msgMap.set(e.name, structuredClone(ob11Msg.arrayMsg)); diff --git a/src/onebot/network/index.ts b/src/onebot/network/index.ts index cbe1140b..8ace6024 100644 --- a/src/onebot/network/index.ts +++ b/src/onebot/network/index.ts @@ -34,7 +34,11 @@ export class OB11NetworkManager { } async emitEvent(event: OB11EmitEventContent) { - return Promise.all(Array.from(this.adapters.values()).map(adapter => adapter.onEvent(event))); + return Promise.all(Array.from(this.adapters.values()).map(adapter => { + if (adapter.isEnable) { + return adapter.onEvent(event); + } + })); } async emitEvents(events: OB11EmitEventContent[]) { @@ -44,19 +48,21 @@ export class OB11NetworkManager { async emitEventByName(names: string[], event: OB11EmitEventContent) { return Promise.all(names.map(name => { const adapter = this.adapters.get(name); - if (adapter) { + if (adapter && adapter.isEnable) { return adapter.onEvent(event); } })); } + async emitEventByNames(map: Map) { return Promise.all(Array.from(map.entries()).map(([name, event]) => { const adapter = this.adapters.get(name); - if (adapter) { + if (adapter && adapter.isEnable) { return adapter.onEvent(event); } })); } + registerAdapter(adapter: IOB11NetworkAdapter) { this.adapters.set(adapter.name, adapter); } @@ -104,6 +110,9 @@ export class OB11NetworkManager { async readloadSomeAdapters(configMap: Map) { await Promise.all(Array.from(configMap.entries()).map(([name, config]) => this.readloadAdapter(name, config))); } + async getAllConfig() { + return Array.from(this.adapters.values()).map(adapter => adapter.config); + } } export * from './active-http'; diff --git a/src/onebot/network/plugin.ts b/src/onebot/network/plugin.ts new file mode 100644 index 00000000..1c2c8134 --- /dev/null +++ b/src/onebot/network/plugin.ts @@ -0,0 +1,44 @@ +import { IOB11NetworkAdapter, OB11EmitEventContent, OB11NetworkReloadType } from './index'; +import { OB11Message } from '@/onebot'; +import { NapCatCore } from '@/core'; +import { ActionMap } from '../action'; +import { AdapterConfig } from '../config/config'; +import { plugin_onmessage } from '@/core/plugin'; + +export class OB11PluginAdapter implements IOB11NetworkAdapter { + isEnable: boolean = true; + public config: AdapterConfig; + + constructor( + public name: string, + public core: NapCatCore, + public actions: ActionMap, + ) { + // 基础配置 + this.config = { + name: name, + messagePostFormat: 'array', + reportSelfMessage: false, + enable: true, + debug: false, + } + } + + onEvent(event: T) { + if (event.post_type === 'message') { + plugin_onmessage(this.config.name, this.core, this.actions, event as OB11Message).then().catch(); + } + } + + open() { + + } + + async close() { + + } + + async reload() { + return OB11NetworkReloadType.Normal; + } +} From 002d135ef50bd5105ba668663255d451c558143c 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: Wed, 11 Dec 2024 18:26:26 +0800 Subject: [PATCH 2/3] fix --- src/core/plugin/index.ts | 8 ++++---- src/onebot/index.ts | 2 +- src/onebot/network/plugin.ts | 5 +++-- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/core/plugin/index.ts b/src/core/plugin/index.ts index 1a67f00e..bd8bd870 100644 --- a/src/core/plugin/index.ts +++ b/src/core/plugin/index.ts @@ -1,10 +1,10 @@ -import { OB11Message } from "@/onebot"; +import { NapCatOneBot11Adapter, OB11Message } from "@/onebot"; +import SendGroupMsg from "@/onebot/action/group/SendGroupMsg"; import { NapCatCore } from ".."; -import { ActionMap } from "@/onebot/action"; -export const plugin_onmessage = async (adapter: string, core: NapCatCore, action: ActionMap, message: OB11Message) => { +export const plugin_onmessage = async (adapter: string, core: NapCatCore, obCore: NapCatOneBot11Adapter, message: OB11Message) => { if (message.raw_message === 'ping') { - const ret = await action.get('send_group_msg')?.handle({ group_id: message.group_id, message: 'pong' }, adapter); + const ret = await new SendGroupMsg(obCore, core).handle({ group_id: String(message.group_id), message: 'pong' }, adapter); console.log(ret); } } \ No newline at end of file diff --git a/src/onebot/index.ts b/src/onebot/index.ts index cb64fa6e..029660b8 100644 --- a/src/onebot/index.ts +++ b/src/onebot/index.ts @@ -111,7 +111,7 @@ export class NapCatOneBot11Adapter { // 注册Plugin this.networkManager.registerAdapter( - new OB11PluginAdapter('plugin', this.core, this.actions) + new OB11PluginAdapter('plugin', this.core, this,this.actions) ); for (const key of ob11Config.network.httpServers) { if (key.enable) { diff --git a/src/onebot/network/plugin.ts b/src/onebot/network/plugin.ts index 1c2c8134..d16747c7 100644 --- a/src/onebot/network/plugin.ts +++ b/src/onebot/network/plugin.ts @@ -1,5 +1,5 @@ import { IOB11NetworkAdapter, OB11EmitEventContent, OB11NetworkReloadType } from './index'; -import { OB11Message } from '@/onebot'; +import { NapCatOneBot11Adapter, OB11Message } from '@/onebot'; import { NapCatCore } from '@/core'; import { ActionMap } from '../action'; import { AdapterConfig } from '../config/config'; @@ -12,6 +12,7 @@ export class OB11PluginAdapter implements IOB11NetworkAdapter { constructor( public name: string, public core: NapCatCore, + public obCore: NapCatOneBot11Adapter, public actions: ActionMap, ) { // 基础配置 @@ -26,7 +27,7 @@ export class OB11PluginAdapter implements IOB11NetworkAdapter { onEvent(event: T) { if (event.post_type === 'message') { - plugin_onmessage(this.config.name, this.core, this.actions, event as OB11Message).then().catch(); + plugin_onmessage(this.config.name, this.core, this.obCore, event as OB11Message).then().catch(); } } From 4fbd764ced4dca542ebd463034923e95a1b3cc70 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: Thu, 12 Dec 2024 09:34:13 +0800 Subject: [PATCH 3/3] fix --- src/onebot/index.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/onebot/index.ts b/src/onebot/index.ts index 029660b8..0c1f6951 100644 --- a/src/onebot/index.ts +++ b/src/onebot/index.ts @@ -109,10 +109,10 @@ export class NapCatOneBot11Adapter { //创建NetWork服务 - // 注册Plugin - this.networkManager.registerAdapter( - new OB11PluginAdapter('plugin', this.core, this,this.actions) - ); + // 注册Plugin 如果需要基于NapCat进行快速开发 + // this.networkManager.registerAdapter( + // new OB11PluginAdapter('plugin', this.core, this,this.actions) + // ); for (const key of ob11Config.network.httpServers) { if (key.enable) { this.networkManager.registerAdapter(