diff --git a/src/onebot/action/OneBotAction.ts b/src/onebot/action/OneBotAction.ts index da453c24..b43191a2 100644 --- a/src/onebot/action/OneBotAction.ts +++ b/src/onebot/action/OneBotAction.ts @@ -29,7 +29,7 @@ export class OB11Response { } export abstract class OneBotAction { - actionName: ActionName = ActionName.Unknown; + actionName: typeof ActionName[keyof typeof ActionName] = ActionName.Unknown; core: NapCatCore; private validate: ValidateFunction | undefined = undefined; payloadSchema: any = undefined; @@ -84,4 +84,4 @@ export abstract class OneBotAction { } abstract _handle(payload: PayloadType, adaptername: string): PromiseLike; -} \ No newline at end of file +} diff --git a/src/onebot/action/extends/OCRImage.ts b/src/onebot/action/extends/OCRImage.ts index dc39b9e0..4957663a 100644 --- a/src/onebot/action/extends/OCRImage.ts +++ b/src/onebot/action/extends/OCRImage.ts @@ -10,8 +10,7 @@ const SchemaData = Type.Object({ type Payload = Static; -export class OCRImage extends OneBotAction { - actionName = ActionName.OCRImage; +class OCRImageBase extends OneBotAction { payloadSchema = SchemaData; async _handle(payload: Payload) { @@ -34,6 +33,10 @@ export class OCRImage extends OneBotAction { } } -export class IOCRImage extends OCRImage { +export class OCRImage extends OCRImageBase { + actionName = ActionName.OCRImage; +} + +export class IOCRImage extends OCRImageBase { actionName = ActionName.IOCRImage; } diff --git a/src/onebot/action/extends/SetGroupSign.ts b/src/onebot/action/extends/SetGroupSign.ts index e0a193a2..88ad0898 100644 --- a/src/onebot/action/extends/SetGroupSign.ts +++ b/src/onebot/action/extends/SetGroupSign.ts @@ -8,14 +8,18 @@ const SchemaData = Type.Object({ type Payload = Static; -export class SetGroupSign extends GetPacketStatusDepends { - actionName = ActionName.SetGroupSign; +class SetGroupSignBase extends GetPacketStatusDepends { 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; } diff --git a/src/onebot/action/group/SendGroupMsg.ts b/src/onebot/action/group/SendGroupMsg.ts index e4878e38..5345f4bb 100644 --- a/src/onebot/action/group/SendGroupMsg.ts +++ b/src/onebot/action/group/SendGroupMsg.ts @@ -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; diff --git a/src/onebot/action/index.ts b/src/onebot/action/index.ts index c3ddd832..b0475dea 100644 --- a/src/onebot/action/index.ts +++ b/src/onebot/action/index.ts @@ -218,32 +218,29 @@ export function createActionMap(obContext: NapCatOneBot11Adapter, core: NapCatCo new GetAiCharacters(obContext, core), ]; - type ValueType = 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 { - private map = new Map>(); + const _map = new Map(); - set(key: K, value: ValueType): 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 | undefined { - return this.map.get(key); - } + function get(key: K): MapType[K]; + function get(key: K): null; + function get(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 \ No newline at end of file +export type ActionMap = ReturnType diff --git a/src/onebot/action/msg/SendMsg.ts b/src/onebot/action/msg/SendMsg.ts index ed83239a..bf24a18a 100644 --- a/src/onebot/action/msg/SendMsg.ts +++ b/src/onebot/action/msg/SendMsg.ts @@ -88,8 +88,7 @@ function getSpecialMsgNum(payload: OB11PostSendMsg, msgType: OB11MessageDataType return 0; } -export class SendMsg extends OneBotAction { - actionName = ActionName.SendMsg; +export class SendMsgBase extends OneBotAction { contextMode = ContextMode.Normal; protected async check(payload: OB11PostSendMsg): Promise { @@ -379,4 +378,6 @@ export class SendMsg extends OneBotAction { } } -export default SendMsg; +export default class SendMsg extends SendMsgBase { + actionName = ActionName.SendMsg; +} diff --git a/src/onebot/action/msg/SendPrivateMsg.ts b/src/onebot/action/msg/SendPrivateMsg.ts index 57ad3468..0b6d9ee1 100644 --- a/src/onebot/action/msg/SendPrivateMsg.ts +++ b/src/onebot/action/msg/SendPrivateMsg.ts @@ -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; diff --git a/src/onebot/action/packet/GetPacketStatus.ts b/src/onebot/action/packet/GetPacketStatus.ts index d393c939..caab75ff 100644 --- a/src/onebot/action/packet/GetPacketStatus.ts +++ b/src/onebot/action/packet/GetPacketStatus.ts @@ -3,8 +3,6 @@ import { ActionName, BaseCheckResult } from '@/onebot/action/router'; export abstract class GetPacketStatusDepends extends OneBotAction { - actionName = ActionName.GetPacketStatus; - protected async check(payload: PT): Promise{ if (!this.core.apis.PacketApi.available) { return { @@ -18,6 +16,8 @@ export abstract class GetPacketStatusDepends extends OneBotAction { + actionName = ActionName.GetPacketStatus; + async _handle(payload: any) { return null; } diff --git a/src/onebot/action/router.ts b/src/onebot/action/router.ts index 05907312..6376202f 100644 --- a/src/onebot/action/router.ts +++ b/src/onebot/action/router.ts @@ -13,134 +13,134 @@ export interface InvalidCheckResult { [k: string | number]: any; } -export enum ActionName { +export const ActionName = { // onebot 11 - SendPrivateMsg = 'send_private_msg', - SendGroupMsg = 'send_group_msg', - SendMsg = 'send_msg', - DeleteMsg = 'delete_msg', - GetMsg = 'get_msg', - GoCQHTTP_GetForwardMsg = 'get_forward_msg', - SendLike = 'send_like', - SetGroupKick = 'set_group_kick', - SetGroupBan = 'set_group_ban', - // SetGroupAnoymousBan = 'set_group_anonymous_ban', - SetGroupWholeBan = 'set_group_whole_ban', - SetGroupAdmin = 'set_group_admin', - // SetGroupAnoymous = 'set_group_anonymous', - SetGroupCard = 'set_group_card', - SetGroupName = 'set_group_name', - SetGroupLeave = 'set_group_leave', - SetSpecialTittle = 'set_group_special_title', - SetFriendAddRequest = 'set_friend_add_request', - SetGroupAddRequest = 'set_group_add_request', - GetLoginInfo = 'get_login_info', - GoCQHTTP_GetStrangerInfo = 'get_stranger_info', - GetFriendList = 'get_friend_list', - GetGroupInfo = 'get_group_info', - GetGroupList = 'get_group_list', - GetGroupMemberInfo = 'get_group_member_info', - GetGroupMemberList = 'get_group_member_list', - GetGroupHonorInfo = 'get_group_honor_info', - GetCookies = 'get_cookies', - GetCSRF = 'get_csrf_token', - GetCredentials = 'get_credentials', - GetRecord = 'get_record', - GetImage = 'get_image', - CanSendImage = 'can_send_image', - CanSendRecord = 'can_send_record', - GetStatus = 'get_status', - GetVersionInfo = 'get_version_info', - // Reboot = 'set_restart', - // CleanCache = 'clean_cache', + SendPrivateMsg : 'send_private_msg', + SendGroupMsg : 'send_group_msg', + SendMsg : 'send_msg', + DeleteMsg : 'delete_msg', + GetMsg : 'get_msg', + GoCQHTTP_GetForwardMsg : 'get_forward_msg', + SendLike : 'send_like', + SetGroupKick : 'set_group_kick', + SetGroupBan : 'set_group_ban', + // SetGroupAnoymousBan : 'set_group_anonymous_ban', + SetGroupWholeBan : 'set_group_whole_ban', + SetGroupAdmin : 'set_group_admin', + // SetGroupAnoymous : 'set_group_anonymous', + SetGroupCard : 'set_group_card', + SetGroupName : 'set_group_name', + SetGroupLeave : 'set_group_leave', + SetSpecialTittle : 'set_group_special_title', + SetFriendAddRequest : 'set_friend_add_request', + SetGroupAddRequest : 'set_group_add_request', + GetLoginInfo : 'get_login_info', + GoCQHTTP_GetStrangerInfo : 'get_stranger_info', + GetFriendList : 'get_friend_list', + GetGroupInfo : 'get_group_info', + GetGroupList : 'get_group_list', + GetGroupMemberInfo : 'get_group_member_info', + GetGroupMemberList : 'get_group_member_list', + GetGroupHonorInfo : 'get_group_honor_info', + GetCookies : 'get_cookies', + GetCSRF : 'get_csrf_token', + GetCredentials : 'get_credentials', + GetRecord : 'get_record', + GetImage : 'get_image', + CanSendImage : 'can_send_image', + CanSendRecord : 'can_send_record', + GetStatus : 'get_status', + GetVersionInfo : 'get_version_info', + // Reboot : 'set_restart', + // CleanCache : 'clean_cache', // go-cqhttp - SetQQProfile = 'set_qq_profile', - // QidianGetAccountInfo = 'qidian_get_account_info', - GoCQHTTP_GetModelShow = '_get_model_show', - GoCQHTTP_SetModelShow = '_set_model_show', - GetOnlineClient = 'get_online_clients', - // GetUnidirectionalFriendList = 'get_unidirectional_friend_list', - GoCQHTTP_DeleteFriend = 'delete_friend', - // DeleteUnidirectionalFriendList = 'delete_unidirectional_friend', - GoCQHTTP_MarkMsgAsRead = 'mark_msg_as_read', - GoCQHTTP_SendGroupForwardMsg = 'send_group_forward_msg', - GoCQHTTP_SendPrivateForwardMsg = 'send_private_forward_msg', - GoCQHTTP_GetGroupMsgHistory = 'get_group_msg_history', - OCRImage = 'ocr_image', - IOCRImage = '.ocr_image', - GetGroupSystemMsg = 'get_group_system_msg', - GoCQHTTP_GetEssenceMsg = 'get_essence_msg_list', - GoCQHTTP_GetGroupAtAllRemain = 'get_group_at_all_remain', - SetGroupPortrait = 'set_group_portrait', - SetEssenceMsg = 'set_essence_msg', - DelEssenceMsg = 'delete_essence_msg', - GoCQHTTP_SendGroupNotice = '_send_group_notice', - GoCQHTTP_GetGroupNotice = '_get_group_notice', - GoCQHTTP_UploadGroupFile = 'upload_group_file', - GOCQHTTP_DeleteGroupFile = 'delete_group_file', - GoCQHTTP_CreateGroupFileFolder = 'create_group_file_folder', - GoCQHTTP_DeleteGroupFileFolder = 'delete_group_folder', - GoCQHTTP_GetGroupFileSystemInfo = 'get_group_file_system_info', - GoCQHTTP_GetGroupRootFiles = 'get_group_root_files', - GoCQHTTP_GetGroupFilesByFolder = 'get_group_files_by_folder', - GOCQHTTP_GetGroupFileUrl = 'get_group_file_url', - GOCQHTTP_UploadPrivateFile = 'upload_private_file', - // GOCQHTTP_ReloadEventFilter = 'reload_event_filter', - GoCQHTTP_DownloadFile = 'download_file', - GoCQHTTP_CheckUrlSafely = 'check_url_safely', - GoCQHTTP_GetWordSlices = '.get_word_slices', - GoCQHTTP_HandleQuickAction = '.handle_quick_operation', + SetQQProfile : 'set_qq_profile', + // QidianGetAccountInfo : 'qidian_get_account_info', + GoCQHTTP_GetModelShow : '_get_model_show', + GoCQHTTP_SetModelShow : '_set_model_show', + GetOnlineClient : 'get_online_clients', + // GetUnidirectionalFriendList : 'get_unidirectional_friend_list', + GoCQHTTP_DeleteFriend : 'delete_friend', + // DeleteUnidirectionalFriendList : 'delete_unidirectional_friend', + GoCQHTTP_MarkMsgAsRead : 'mark_msg_as_read', + GoCQHTTP_SendGroupForwardMsg : 'send_group_forward_msg', + GoCQHTTP_SendPrivateForwardMsg : 'send_private_forward_msg', + GoCQHTTP_GetGroupMsgHistory : 'get_group_msg_history', + OCRImage : 'ocr_image', + IOCRImage : '.ocr_image', + GetGroupSystemMsg : 'get_group_system_msg', + GoCQHTTP_GetEssenceMsg : 'get_essence_msg_list', + GoCQHTTP_GetGroupAtAllRemain : 'get_group_at_all_remain', + SetGroupPortrait : 'set_group_portrait', + SetEssenceMsg : 'set_essence_msg', + DelEssenceMsg : 'delete_essence_msg', + GoCQHTTP_SendGroupNotice : '_send_group_notice', + GoCQHTTP_GetGroupNotice : '_get_group_notice', + GoCQHTTP_UploadGroupFile : 'upload_group_file', + GOCQHTTP_DeleteGroupFile : 'delete_group_file', + GoCQHTTP_CreateGroupFileFolder : 'create_group_file_folder', + GoCQHTTP_DeleteGroupFileFolder : 'delete_group_folder', + GoCQHTTP_GetGroupFileSystemInfo : 'get_group_file_system_info', + GoCQHTTP_GetGroupRootFiles : 'get_group_root_files', + GoCQHTTP_GetGroupFilesByFolder : 'get_group_files_by_folder', + GOCQHTTP_GetGroupFileUrl : 'get_group_file_url', + GOCQHTTP_UploadPrivateFile : 'upload_private_file', + // GOCQHTTP_ReloadEventFilter : 'reload_event_filter', + GoCQHTTP_DownloadFile : 'download_file', + GoCQHTTP_CheckUrlSafely : 'check_url_safely', + GoCQHTTP_GetWordSlices : '.get_word_slices', + GoCQHTTP_HandleQuickAction : '.handle_quick_operation', // 以下为扩展napcat扩展 - Unknown = 'unknown', - SharePeer = 'ArkSharePeer', - ShareGroupEx = 'ArkShareGroup', - // RebootNormal = 'reboot_normal', //无快速登录重新启动 - GetRobotUinRange = 'get_robot_uin_range', - SetOnlineStatus = 'set_online_status', - GetFriendsWithCategory = 'get_friends_with_category', - SetQQAvatar = 'set_qq_avatar', - GetFile = 'get_file', - ForwardFriendSingleMsg = 'forward_friend_single_msg', - ForwardGroupSingleMsg = 'forward_group_single_msg', - TranslateEnWordToZn = 'translate_en2zh', - SetMsgEmojiLike = 'set_msg_emoji_like', - GoCQHTTP_SendForwardMsg = 'send_forward_msg', - MarkPrivateMsgAsRead = 'mark_private_msg_as_read', - MarkGroupMsgAsRead = 'mark_group_msg_as_read', - GetFriendMsgHistory = 'get_friend_msg_history', - CreateCollection = 'create_collection', - GetCollectionList = 'get_collection_list', - SetLongNick = 'set_self_longnick', - GetRecentContact = 'get_recent_contact', - _MarkAllMsgAsRead = '_mark_all_as_read', - GetProfileLike = 'get_profile_like', - FetchCustomFace = 'fetch_custom_face', - FetchEmojiLike = 'fetch_emoji_like', - SetInputStatus = 'set_input_status', - GetGroupInfoEx = 'get_group_info_ex', - GetGroupIgnoreAddRequest = 'get_group_ignore_add_request', - DelGroupNotice = '_del_group_notice', - FetchUserProfileLike = 'fetch_user_profile_like', - FriendPoke = 'friend_poke', - GroupPoke = 'group_poke', - GetPacketStatus = 'nc_get_packet_status', - GetUserStatus = 'nc_get_user_status', - GetRkey = 'nc_get_rkey', - GetGroupShutList = 'get_group_shut_list', + Unknown : 'unknown', + SharePeer : 'ArkSharePeer', + ShareGroupEx : 'ArkShareGroup', + // RebootNormal : 'reboot_normal', //无快速登录重新启动 + GetRobotUinRange : 'get_robot_uin_range', + SetOnlineStatus : 'set_online_status', + GetFriendsWithCategory : 'get_friends_with_category', + SetQQAvatar : 'set_qq_avatar', + GetFile : 'get_file', + ForwardFriendSingleMsg : 'forward_friend_single_msg', + ForwardGroupSingleMsg : 'forward_group_single_msg', + TranslateEnWordToZn : 'translate_en2zh', + SetMsgEmojiLike : 'set_msg_emoji_like', + GoCQHTTP_SendForwardMsg : 'send_forward_msg', + MarkPrivateMsgAsRead : 'mark_private_msg_as_read', + MarkGroupMsgAsRead : 'mark_group_msg_as_read', + GetFriendMsgHistory : 'get_friend_msg_history', + CreateCollection : 'create_collection', + GetCollectionList : 'get_collection_list', + SetLongNick : 'set_self_longnick', + GetRecentContact : 'get_recent_contact', + _MarkAllMsgAsRead : '_mark_all_as_read', + GetProfileLike : 'get_profile_like', + FetchCustomFace : 'fetch_custom_face', + FetchEmojiLike : 'fetch_emoji_like', + SetInputStatus : 'set_input_status', + GetGroupInfoEx : 'get_group_info_ex', + GetGroupIgnoreAddRequest : 'get_group_ignore_add_request', + DelGroupNotice : '_del_group_notice', + FetchUserProfileLike : 'fetch_user_profile_like', + FriendPoke : 'friend_poke', + GroupPoke : 'group_poke', + GetPacketStatus : 'nc_get_packet_status', + GetUserStatus : 'nc_get_user_status', + GetRkey : 'nc_get_rkey', + GetGroupShutList : 'get_group_shut_list', - GetGuildList = 'get_guild_list', - GetGuildProfile = 'get_guild_service_profile', + GetGuildList : 'get_guild_list', + GetGuildProfile : 'get_guild_service_profile', - GetGroupIgnoredNotifies = 'get_group_ignored_notifies', + GetGroupIgnoredNotifies : 'get_group_ignored_notifies', - SetGroupSign = "set_group_sign", - SendGroupSign = "send_group_sign", - - GetMiniAppArk = "get_mini_app_ark", - // UploadForwardMsg = "upload_forward_msg", - GetAiRecord = "get_ai_record", - GetAiCharacters = "get_ai_characters", - SendGroupAiRecord = "send_group_ai_record", -} + SetGroupSign : "set_group_sign", + SendGroupSign : "send_group_sign", + + GetMiniAppArk : "get_mini_app_ark", + // UploadForwardMsg : "upload_forward_msg", + GetAiRecord : "get_ai_record", + GetAiCharacters : "get_ai_characters", + SendGroupAiRecord : "send_group_ai_record", +} as const; diff --git a/src/onebot/action/system/CanSendImage.ts b/src/onebot/action/system/CanSendImage.ts index 592fa561..5ffd73c1 100644 --- a/src/onebot/action/system/CanSendImage.ts +++ b/src/onebot/action/system/CanSendImage.ts @@ -1,10 +1,10 @@ import { ActionName } from '@/onebot/action/router'; -import CanSendRecord from './CanSendRecord'; +import CanSendRecord, {CanSend} from './CanSendRecord'; interface ReturnType { yes: boolean; } -export default class CanSendImage extends CanSendRecord { +export default class CanSendImage extends CanSend { actionName = ActionName.CanSendImage; } diff --git a/src/onebot/action/system/CanSendRecord.ts b/src/onebot/action/system/CanSendRecord.ts index 48b1b5f8..d3dfc409 100644 --- a/src/onebot/action/system/CanSendRecord.ts +++ b/src/onebot/action/system/CanSendRecord.ts @@ -5,12 +5,15 @@ interface ReturnType { yes: boolean; } -export default class CanSendRecord extends OneBotAction { - actionName = ActionName.CanSendRecord; - +export class CanSend extends OneBotAction { async _handle(_payload: void): Promise { return { yes: true, }; } } + + +export default class CanSendRecord extends CanSend{ + actionName = ActionName.CanSendRecord; +} diff --git a/src/plugin/index.ts b/src/plugin/index.ts index d1eda518..616953a3 100644 --- a/src/plugin/index.ts +++ b/src/plugin/index.ts @@ -4,7 +4,10 @@ import { ActionMap } from "@/onebot/action"; export const plugin_onmessage = async (adapter: string, core: NapCatCore, obCore: NapCatOneBot11Adapter, message: OB11Message, action: ActionMap) => { if (message.raw_message === 'ping') { + // TODO: + // const a = action.get('fetch_user_profilqe_like'); + const ssa = action.get('fetch_user_profile_like'); 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 +}