fix: 支持类型推导

This commit is contained in:
手瓜一十雪
2024-12-13 23:23:58 +08:00
parent 5cdbf58f59
commit 3c3114b6ab
3 changed files with 32 additions and 14 deletions

View File

@@ -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<string, OneBotAction<any, any>>;
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> = K extends `${typeof actionHandlers[number]['actionName']}` | `${typeof actionHandlers[number]['actionName']}_async` | `${typeof actionHandlers[number]['actionName']}_rate_limited` ? typeof actionHandlers[number] : never;
class TypedMap<K extends string> {
private map = new Map<K, ValueType<K>>();
set(key: K, value: ValueType<K>): void {
this.map.set(key, value);
}
get(key: K): ValueType<K> | 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<typeof createActionMap>

View File

@@ -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<T extends OB11EmitEventContent>(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();
}
}

View File

@@ -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);
}
}