mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2025-07-19 12:03:37 +00:00
feat: plugin
This commit is contained in:
10
src/core/plugin/index.ts
Normal file
10
src/core/plugin/index.ts
Normal file
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
@@ -28,6 +28,7 @@ interface v1Config {
|
|||||||
export interface AdapterConfigInner {
|
export interface AdapterConfigInner {
|
||||||
name: string;
|
name: string;
|
||||||
enable: boolean;
|
enable: boolean;
|
||||||
|
|
||||||
}
|
}
|
||||||
export type AdapterConfigWrap = AdapterConfigInner & Partial<NetworkConfigAdapter>;
|
export type AdapterConfigWrap = AdapterConfigInner & Partial<NetworkConfigAdapter>;
|
||||||
|
|
||||||
@@ -127,7 +128,7 @@ export const mergeNetworkDefaultConfig = {
|
|||||||
websocketClients: websocketClientDefaultConfigs,
|
websocketClients: websocketClientDefaultConfigs,
|
||||||
} as const;
|
} as const;
|
||||||
|
|
||||||
export type NetworkConfigAdapter = HttpServerConfig | HttpClientConfig | WebsocketServerConfig | WebsocketClientConfig;
|
export type NetworkConfigAdapter = HttpServerConfig | HttpClientConfig | WebsocketServerConfig | WebsocketClientConfig | AdapterConfig;
|
||||||
type NetworkConfigKeys = keyof typeof mergeNetworkDefaultConfig;
|
type NetworkConfigKeys = keyof typeof mergeNetworkDefaultConfig;
|
||||||
|
|
||||||
export function mergeOneBotConfigs(
|
export function mergeOneBotConfigs(
|
||||||
|
@@ -47,6 +47,7 @@ import { NodeIKernelRecentContactListener } from '@/core/listeners/NodeIKernelRe
|
|||||||
import { BotOfflineEvent } from './event/notice/BotOfflineEvent';
|
import { BotOfflineEvent } from './event/notice/BotOfflineEvent';
|
||||||
import { AdapterConfigWrap, mergeOneBotConfigs, migrateOneBotConfigsV1, NetworkConfigAdapter, OneBotConfig } from './config/config';
|
import { AdapterConfigWrap, mergeOneBotConfigs, migrateOneBotConfigsV1, NetworkConfigAdapter, OneBotConfig } from './config/config';
|
||||||
import { OB11Message } from './types';
|
import { OB11Message } from './types';
|
||||||
|
import { OB11PluginAdapter } from './network/plugin';
|
||||||
|
|
||||||
//OneBot实现类
|
//OneBot实现类
|
||||||
export class NapCatOneBot11Adapter {
|
export class NapCatOneBot11Adapter {
|
||||||
@@ -106,7 +107,12 @@ export class NapCatOneBot11Adapter {
|
|||||||
const serviceInfo = await this.creatOneBotLog(ob11Config);
|
const serviceInfo = await this.creatOneBotLog(ob11Config);
|
||||||
this.context.logger.log(`[Notice] [OneBot11] ${serviceInfo}`);
|
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) {
|
for (const key of ob11Config.network.httpServers) {
|
||||||
if (key.enable) {
|
if (key.enable) {
|
||||||
this.networkManager.registerAdapter(
|
this.networkManager.registerAdapter(
|
||||||
@@ -443,7 +449,7 @@ export class NapCatOneBot11Adapter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private async emitMsg(message: RawMessage) {
|
private async emitMsg(message: RawMessage) {
|
||||||
const network = Object.values(this.configLoader.configData.network).flat() as Array<AdapterConfigWrap>;
|
const network = await this.networkManager.getAllConfig();
|
||||||
this.context.logger.logDebug('收到新消息 RawMessage', message);
|
this.context.logger.logDebug('收到新消息 RawMessage', message);
|
||||||
await Promise.allSettled([
|
await Promise.allSettled([
|
||||||
this.handleMsg(message, network),
|
this.handleMsg(message, network),
|
||||||
@@ -483,7 +489,7 @@ export class NapCatOneBot11Adapter {
|
|||||||
ob11Msg.stringMsg.target_id = parseInt(message.peerUin);
|
ob11Msg.stringMsg.target_id = parseInt(message.peerUin);
|
||||||
ob11Msg.arrayMsg.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));
|
msgMap.set(e.name, structuredClone(ob11Msg.stringMsg));
|
||||||
} else {
|
} else {
|
||||||
msgMap.set(e.name, structuredClone(ob11Msg.arrayMsg));
|
msgMap.set(e.name, structuredClone(ob11Msg.arrayMsg));
|
||||||
|
@@ -34,7 +34,11 @@ export class OB11NetworkManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async emitEvent(event: OB11EmitEventContent) {
|
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[]) {
|
async emitEvents(events: OB11EmitEventContent[]) {
|
||||||
@@ -44,19 +48,21 @@ export class OB11NetworkManager {
|
|||||||
async emitEventByName(names: string[], event: OB11EmitEventContent) {
|
async emitEventByName(names: string[], event: OB11EmitEventContent) {
|
||||||
return Promise.all(names.map(name => {
|
return Promise.all(names.map(name => {
|
||||||
const adapter = this.adapters.get(name);
|
const adapter = this.adapters.get(name);
|
||||||
if (adapter) {
|
if (adapter && adapter.isEnable) {
|
||||||
return adapter.onEvent(event);
|
return adapter.onEvent(event);
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
async emitEventByNames(map: Map<string, OB11EmitEventContent>) {
|
async emitEventByNames(map: Map<string, OB11EmitEventContent>) {
|
||||||
return Promise.all(Array.from(map.entries()).map(([name, event]) => {
|
return Promise.all(Array.from(map.entries()).map(([name, event]) => {
|
||||||
const adapter = this.adapters.get(name);
|
const adapter = this.adapters.get(name);
|
||||||
if (adapter) {
|
if (adapter && adapter.isEnable) {
|
||||||
return adapter.onEvent(event);
|
return adapter.onEvent(event);
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
registerAdapter(adapter: IOB11NetworkAdapter) {
|
registerAdapter(adapter: IOB11NetworkAdapter) {
|
||||||
this.adapters.set(adapter.name, adapter);
|
this.adapters.set(adapter.name, adapter);
|
||||||
}
|
}
|
||||||
@@ -104,6 +110,9 @@ export class OB11NetworkManager {
|
|||||||
async readloadSomeAdapters<T>(configMap: Map<string, T>) {
|
async readloadSomeAdapters<T>(configMap: Map<string, T>) {
|
||||||
await Promise.all(Array.from(configMap.entries()).map(([name, config]) => this.readloadAdapter(name, config)));
|
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';
|
export * from './active-http';
|
||||||
|
44
src/onebot/network/plugin.ts
Normal file
44
src/onebot/network/plugin.ts
Normal file
@@ -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<T extends OB11EmitEventContent>(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;
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user