From 3c3114b6abf836ae8ab27a0ff1e8e438076416a5 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: Fri, 13 Dec 2024 23:23:58 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=94=AF=E6=8C=81=E7=B1=BB=E5=9E=8B?= =?UTF-8?q?=E6=8E=A8=E5=AF=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/onebot/action/index.ts | 32 +++++++++++++++++++++++++------- src/onebot/network/plugin.ts | 4 ++-- src/plugin/index.ts | 10 +++++----- 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/onebot/action/index.ts b/src/onebot/action/index.ts index 9c6be913..c3ddd832 100644 --- a/src/onebot/action/index.ts +++ b/src/onebot/action/index.ts @@ -103,10 +103,7 @@ import { GetAiCharacters } from "@/onebot/action/extends/GetAiCharacters"; import { GetGuildList } from './guild/GetGuildList'; import { GetGuildProfile } from './guild/GetGuildProfile'; - -export type ActionMap = Map>; - -export function createActionMap(obContext: NapCatOneBot11Adapter, core: NapCatCore): ActionMap { +export function createActionMap(obContext: NapCatOneBot11Adapter, core: NapCatCore) { const actionHandlers = [ new GetGroupInfoEx(obContext, core), @@ -220,12 +217,33 @@ export function createActionMap(obContext: NapCatOneBot11Adapter, core: NapCatCo new SendGroupAiRecord(obContext, core), new GetAiCharacters(obContext, core), ]; - const actionMap = new Map(); + + type ValueType = K extends `${typeof actionHandlers[number]['actionName']}` | `${typeof actionHandlers[number]['actionName']}_async` | `${typeof actionHandlers[number]['actionName']}_rate_limited` ? typeof actionHandlers[number] : never; + + class TypedMap { + private map = new Map>(); + + set(key: K, value: ValueType): void { + this.map.set(key, value); + } + + get(key: K): ValueType | undefined { + return this.map.get(key); + } + } + + const actionMap = new TypedMap< + `${typeof actionHandlers[number]['actionName']}` | + `${typeof actionHandlers[number]['actionName']}_async` | + `${typeof actionHandlers[number]['actionName']}_rate_limited` + >(); + for (const action of actionHandlers) { actionMap.set(action.actionName, action); - actionMap.set(action.actionName + '_async', action); - actionMap.set(action.actionName + '_rate_limited', action); + actionMap.set(`${action.actionName}_async`, action); + actionMap.set(`${action.actionName}_rate_limited`, action); } return actionMap; } +export type ActionMap = ReturnType \ No newline at end of file diff --git a/src/onebot/network/plugin.ts b/src/onebot/network/plugin.ts index 62b08d37..d34d605a 100644 --- a/src/onebot/network/plugin.ts +++ b/src/onebot/network/plugin.ts @@ -1,9 +1,9 @@ import { IOB11NetworkAdapter, OB11EmitEventContent, OB11NetworkReloadType } from './index'; import { NapCatOneBot11Adapter, OB11Message } from '@/onebot'; import { NapCatCore } from '@/core'; -import { ActionMap } from '../action'; import { AdapterConfig } from '../config/config'; import { plugin_onmessage } from '@/plugin'; +import { ActionMap } from '../action'; export class OB11PluginAdapter implements IOB11NetworkAdapter { isEnable: boolean = true; @@ -27,7 +27,7 @@ export class OB11PluginAdapter implements IOB11NetworkAdapter { onEvent(event: T) { if (event.post_type === 'message') { - plugin_onmessage(this.config.name, this.core, this.obCore, event as OB11Message).then().catch(); + plugin_onmessage(this.config.name, this.core, this.obCore, event as OB11Message,this.actions).then().catch(); } } diff --git a/src/plugin/index.ts b/src/plugin/index.ts index dc54d84d..d1eda518 100644 --- a/src/plugin/index.ts +++ b/src/plugin/index.ts @@ -1,10 +1,10 @@ import { NapCatOneBot11Adapter, OB11Message } from "@/onebot"; import { NapCatCore } from "../core"; -import { SetSpecialTittle } from "@/onebot/action/extends/SetSpecialTittle"; +import { ActionMap } from "@/onebot/action"; -export const plugin_onmessage = async (adapter: string, core: NapCatCore, obCore: NapCatOneBot11Adapter, message: OB11Message) => { - if (message.raw_message.startsWith('设置头衔') && message.group_id) { - const ret = await new SetSpecialTittle(obCore, core) - .handle({ group_id: message.group_id, user_id: message.user_id, special_title: message.raw_message.replace('设置头衔', '') }, adapter); +export const plugin_onmessage = async (adapter: string, core: NapCatCore, obCore: NapCatOneBot11Adapter, message: OB11Message, action: ActionMap) => { + if (message.raw_message === 'ping') { + const ret = await action.get('send_group_msg')?.handle({ group_id: String(message.group_id), message: 'pong' }, adapter); + console.log(ret); } } \ No newline at end of file