Skip to content

Commit

Permalink
Merge branch 'maybe-feat/type'
Browse files Browse the repository at this point in the history
  • Loading branch information
MliKiowa committed Dec 14, 2024
2 parents dc0561d + a6adde7 commit dfc7996
Show file tree
Hide file tree
Showing 12 changed files with 180 additions and 169 deletions.
4 changes: 2 additions & 2 deletions src/onebot/action/OneBotAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class OB11Response {
}

export abstract class OneBotAction<PayloadType, ReturnDataType> {
actionName: ActionName = ActionName.Unknown;
actionName: typeof ActionName[keyof typeof ActionName] = ActionName.Unknown;
core: NapCatCore;
private validate: ValidateFunction<any> | undefined = undefined;
payloadSchema: any = undefined;
Expand Down Expand Up @@ -84,4 +84,4 @@ export abstract class OneBotAction<PayloadType, ReturnDataType> {
}

abstract _handle(payload: PayloadType, adaptername: string): PromiseLike<ReturnDataType>;
}
}
9 changes: 6 additions & 3 deletions src/onebot/action/extends/OCRImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ const SchemaData = Type.Object({

type Payload = Static<typeof SchemaData>;

export class OCRImage extends OneBotAction<Payload, any> {
actionName = ActionName.OCRImage;
class OCRImageBase extends OneBotAction<Payload, any> {
payloadSchema = SchemaData;

async _handle(payload: Payload) {
Expand All @@ -34,6 +33,10 @@ export class OCRImage extends OneBotAction<Payload, any> {
}
}

export class IOCRImage extends OCRImage {
export class OCRImage extends OCRImageBase {
actionName = ActionName.OCRImage;
}

export class IOCRImage extends OCRImageBase {
actionName = ActionName.IOCRImage;
}
10 changes: 7 additions & 3 deletions src/onebot/action/extends/SetGroupSign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ const SchemaData = Type.Object({

type Payload = Static<typeof SchemaData>;

export class SetGroupSign extends GetPacketStatusDepends<Payload, any> {
actionName = ActionName.SetGroupSign;
class SetGroupSignBase extends GetPacketStatusDepends<Payload, any> {
payloadSchema = SchemaData;

async _handle(payload: Payload) {
return await this.core.apis.PacketApi.pkt.operation.GroupSign(+payload.group_id);
}
}
export class SendGroupSign extends SetGroupSign {

export class SetGroupSign extends SetGroupSignBase {
actionName = ActionName.SendGroupSign;
}

export class SendGroupSign extends SetGroupSignBase {
actionName = ActionName.SendGroupSign;
}
4 changes: 2 additions & 2 deletions src/onebot/action/group/SendGroupMsg.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import SendMsg, { ContextMode } from '@/onebot/action/msg/SendMsg';
import {ContextMode, SendMsgBase} from '@/onebot/action/msg/SendMsg';
import { ActionName, BaseCheckResult } from '@/onebot/action/router';
import { OB11PostSendMsg } from '@/onebot/types';

// 未检测参数
class SendGroupMsg extends SendMsg {
class SendGroupMsg extends SendMsgBase {
actionName = ActionName.SendGroupMsg;
contextMode: ContextMode = ContextMode.Group;

Expand Down
43 changes: 20 additions & 23 deletions src/onebot/action/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,32 +218,29 @@ export function createActionMap(obContext: NapCatOneBot11Adapter, core: NapCatCo
new GetAiCharacters(obContext, core),
];

type ValueType<K> = K extends `${typeof actionHandlers[number]['actionName']}` | `${typeof actionHandlers[number]['actionName']}_async` | `${typeof actionHandlers[number]['actionName']}_rate_limited` ? typeof actionHandlers[number] : never;
type HandlerUnion = typeof actionHandlers[number];
type MapType = {
[H in HandlerUnion as H['actionName']]: H;
} & {
[H in HandlerUnion as `${H['actionName']}_async`]: H;
} & {
[H in HandlerUnion as `${H['actionName']}_rate_limited`]: H;
};

class TypedMap<K extends string> {
private map = new Map<K, ValueType<K>>();
const _map = new Map<keyof MapType, HandlerUnion>();

set(key: K, value: ValueType<K>): void {
this.map.set(key, value);
}
actionHandlers.forEach(h => {
_map.set(h.actionName as keyof MapType, h);
_map.set(`${h.actionName}_async` as keyof MapType, h);
_map.set(`${h.actionName}_rate_limited` as keyof MapType, h);
});

get(key: K): ValueType<K> | undefined {
return this.map.get(key);
}
function get<K extends keyof MapType>(key: K): MapType[K];
function get<K extends keyof MapType>(key: K): null;
function get<K extends keyof MapType>(key: K): HandlerUnion | null | MapType[K] {
return _map.get(key as keyof MapType) ?? null;
}

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

return actionMap;
return { get };
}
export type ActionMap = ReturnType<typeof createActionMap>
export type ActionMap = ReturnType<typeof createActionMap>
7 changes: 4 additions & 3 deletions src/onebot/action/msg/SendMsg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ function getSpecialMsgNum(payload: OB11PostSendMsg, msgType: OB11MessageDataType
return 0;
}

export class SendMsg extends OneBotAction<OB11PostSendMsg, ReturnDataType> {
actionName = ActionName.SendMsg;
export class SendMsgBase extends OneBotAction<OB11PostSendMsg, ReturnDataType> {
contextMode = ContextMode.Normal;

protected async check(payload: OB11PostSendMsg): Promise<BaseCheckResult> {
Expand Down Expand Up @@ -379,4 +378,6 @@ export class SendMsg extends OneBotAction<OB11PostSendMsg, ReturnDataType> {
}
}

export default SendMsg;
export default class SendMsg extends SendMsgBase {
actionName = ActionName.SendMsg;
}
4 changes: 2 additions & 2 deletions src/onebot/action/msg/SendPrivateMsg.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import SendMsg, { ContextMode } from './SendMsg';
import {ContextMode, SendMsgBase} from './SendMsg';
import { ActionName, BaseCheckResult } from '@/onebot/action/router';
import { OB11PostSendMsg } from '@/onebot/types';

// 未检测参数
class SendPrivateMsg extends SendMsg {
class SendPrivateMsg extends SendMsgBase {
actionName = ActionName.SendPrivateMsg;
contextMode: ContextMode = ContextMode.Private;

Expand Down
4 changes: 2 additions & 2 deletions src/onebot/action/packet/GetPacketStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { ActionName, BaseCheckResult } from '@/onebot/action/router';


export abstract class GetPacketStatusDepends<PT, RT> extends OneBotAction<PT, RT> {
actionName = ActionName.GetPacketStatus;

protected async check(payload: PT): Promise<BaseCheckResult>{
if (!this.core.apis.PacketApi.available) {
return {
Expand All @@ -18,6 +16,8 @@ export abstract class GetPacketStatusDepends<PT, RT> extends OneBotAction<PT, RT
}

export class GetPacketStatus extends GetPacketStatusDepends<any, null> {
actionName = ActionName.GetPacketStatus;

async _handle(payload: any) {
return null;
}
Expand Down
Loading

0 comments on commit dfc7996

Please sign in to comment.