From c510f4acdc29f76b348a3dc446572cd38c622eea Mon Sep 17 00:00:00 2001 From: zhangzemeng Date: Mon, 12 Feb 2024 09:13:06 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=8A=BD=E7=A6=BB=20action=20=E5=85=AC?= =?UTF-8?q?=E5=85=B1=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/onebot11/actions/ActionTemplate.ts | 21 +++--------------- src/onebot11/actions/BaseAction.ts | 25 ++++++++++++++++++++++ src/onebot11/actions/GetFriendList.ts | 19 ++-------------- src/onebot11/actions/GetGroupInfo.ts | 19 ++-------------- src/onebot11/actions/GetGroupList.ts | 19 ++-------------- src/onebot11/actions/GetGroupMemberInfo.ts | 19 ++-------------- src/onebot11/actions/GetGroupMemberList.ts | 19 ++-------------- src/onebot11/actions/GetLoginInfo.ts | 19 ++-------------- src/onebot11/actions/GetMsg.ts | 19 ++-------------- src/onebot11/actions/SendGroupMsg.ts | 20 ++--------------- src/onebot11/actions/SendMsg.ts | 19 ++-------------- src/onebot11/actions/SendPrivateMsg.ts | 20 ++--------------- 12 files changed, 48 insertions(+), 190 deletions(-) create mode 100644 src/onebot11/actions/BaseAction.ts diff --git a/src/onebot11/actions/ActionTemplate.ts b/src/onebot11/actions/ActionTemplate.ts index fa2e4cd..8ecaa68 100644 --- a/src/onebot11/actions/ActionTemplate.ts +++ b/src/onebot11/actions/ActionTemplate.ts @@ -1,6 +1,5 @@ -// import { OB11Response } from "./utils"; -// import { BaseCheckResult } from "./types"; // import { OB11Return } from '../types'; +// import BaseAction from "./BaseAction"; // export type ActionType = '' @@ -16,25 +15,11 @@ // } -// class ActionTemplate { +// class ActionTemplate extends BaseAction { // static ACTION_TYPE: ActionType = '' -// async check(jsonData: any): Promise { -// return { -// valid: true, -// } -// } - -// async handle(jsonData: any) { -// const result = await this.check(jsonData) -// if (!result.valid) { -// return OB11Response.error(result.message) -// } -// const resData = await this._handle(jsonData) -// return resData -// } - // async _handle(payload: PayloadType): Promise> { +// return // } // } diff --git a/src/onebot11/actions/BaseAction.ts b/src/onebot11/actions/BaseAction.ts new file mode 100644 index 0000000..1d2c5e7 --- /dev/null +++ b/src/onebot11/actions/BaseAction.ts @@ -0,0 +1,25 @@ +import { BaseCheckResult } from "./types" +import { OB11Response } from "./utils" + +class BaseAction { + async check(jsonData: any): Promise { + return { + valid: true, + } + } + + async handle(jsonData: any) { + const result = await this.check(jsonData) + if (!result.valid) { + return OB11Response.error(result.message) + } + const resData = await this._handle(jsonData) + return resData + } + + async _handle(payload: unknown): Promise { + return + } +} + +export default BaseAction \ No newline at end of file diff --git a/src/onebot11/actions/GetFriendList.ts b/src/onebot11/actions/GetFriendList.ts index ec6418b..49b44da 100644 --- a/src/onebot11/actions/GetFriendList.ts +++ b/src/onebot11/actions/GetFriendList.ts @@ -1,8 +1,8 @@ import { OB11Response } from "./utils"; -import { BaseCheckResult } from "./types"; import { OB11Return, OB11User } from '../types'; import { OB11Constructor } from "../constructor"; import { friends } from "../../common/data"; +import BaseAction from "./BaseAction"; export type ActionType = 'get_friend_list' @@ -12,24 +12,9 @@ export interface PayloadType { export type ReturnDataType = OB11User[] -class GetFriendList { +class GetFriendList extends BaseAction { static ACTION_TYPE: ActionType = 'get_friend_list' - async check(jsonData: any): Promise { - return { - valid: true, - } - } - - async handle(jsonData: any) { - const result = await this.check(jsonData) - if (!result.valid) { - return OB11Response.error(result.message) - } - const resData = await this._handle(jsonData) - return resData - } - async _handle(payload: PayloadType): Promise> { return OB11Response.ok(OB11Constructor.friends(friends)); } diff --git a/src/onebot11/actions/GetGroupInfo.ts b/src/onebot11/actions/GetGroupInfo.ts index 06335f1..9ac11be 100644 --- a/src/onebot11/actions/GetGroupInfo.ts +++ b/src/onebot11/actions/GetGroupInfo.ts @@ -1,8 +1,8 @@ import { OB11Response } from "./utils"; -import { BaseCheckResult } from "./types"; import { OB11Group, OB11Return } from '../types'; import { getGroup, groups } from "../../common/data"; import { OB11Constructor } from "../constructor"; +import BaseAction from "./BaseAction"; export type ActionType = 'get_group_info' @@ -13,24 +13,9 @@ export interface PayloadType { export type ReturnDataType = OB11Group[] -class GetGroupInfo { +class GetGroupInfo extends BaseAction { static ACTION_TYPE: ActionType = 'get_group_info' - async check(jsonData: any): Promise { - return { - valid: true, - } - } - - async handle(jsonData: any) { - const result = await this.check(jsonData) - if (!result.valid) { - return OB11Response.error(result.message) - } - const resData = await this._handle(jsonData) - return resData - } - async _handle(payload: PayloadType): Promise> { const group = await getGroup(payload.group_id.toString()) if (group) { diff --git a/src/onebot11/actions/GetGroupList.ts b/src/onebot11/actions/GetGroupList.ts index fdeaed2..16eba73 100644 --- a/src/onebot11/actions/GetGroupList.ts +++ b/src/onebot11/actions/GetGroupList.ts @@ -1,8 +1,8 @@ import { OB11Response } from "./utils"; -import { BaseCheckResult } from "./types"; import { OB11Group, OB11Return } from '../types'; import { OB11Constructor } from "../constructor"; import { groups } from "../../common/data"; +import BaseAction from "./BaseAction"; export type ActionType = 'get_group_list' @@ -12,24 +12,9 @@ export interface PayloadType { export type ReturnDataType = OB11Group[] -class GetGroupList { +class GetGroupList extends BaseAction { static ACTION_TYPE: ActionType = 'get_group_list' - async check(jsonData: any): Promise { - return { - valid: true, - } - } - - async handle(jsonData: any) { - const result = await this.check(jsonData) - if (!result.valid) { - return OB11Response.error(result.message) - } - const resData = await this._handle(jsonData) - return resData - } - async _handle(payload: PayloadType): Promise> { return OB11Response.ok(OB11Constructor.groups(groups)); } diff --git a/src/onebot11/actions/GetGroupMemberInfo.ts b/src/onebot11/actions/GetGroupMemberInfo.ts index 3e8d80f..696ecb9 100644 --- a/src/onebot11/actions/GetGroupMemberInfo.ts +++ b/src/onebot11/actions/GetGroupMemberInfo.ts @@ -1,8 +1,8 @@ import { OB11Response } from "./utils"; -import { BaseCheckResult } from "./types"; import { OB11GroupMember, OB11Return } from '../types'; import { getGroupMember } from "../../common/data"; import { OB11Constructor } from "../constructor"; +import BaseAction from "./BaseAction"; export type ActionType = 'get_group_member_info' @@ -14,24 +14,9 @@ export interface PayloadType { export type ReturnDataType = OB11GroupMember -class GetGroupMemberInfo { +class GetGroupMemberInfo extends BaseAction { static ACTION_TYPE: ActionType = 'get_group_member_info' - async check(jsonData: any): Promise { - return { - valid: true, - } - } - - async handle(jsonData: any) { - const result = await this.check(jsonData) - if (!result.valid) { - return OB11Response.error(result.message) - } - const resData = await this._handle(jsonData) - return resData - } - async _handle(payload: PayloadType): Promise> { const member = await getGroupMember(payload.group_id.toString(), payload.user_id.toString()) if (member) { diff --git a/src/onebot11/actions/GetGroupMemberList.ts b/src/onebot11/actions/GetGroupMemberList.ts index 4ae7b42..04abedc 100644 --- a/src/onebot11/actions/GetGroupMemberList.ts +++ b/src/onebot11/actions/GetGroupMemberList.ts @@ -1,9 +1,9 @@ import { OB11Response } from "./utils"; -import { BaseCheckResult } from "./types"; import { OB11GroupMember, OB11Return } from '../types'; import { getGroup } from "../../common/data"; import { NTQQApi } from "../../ntqqapi/ntcall"; import { OB11Constructor } from "../constructor"; +import BaseAction from "./BaseAction"; export type ActionType = 'get_group_member_list' @@ -14,24 +14,9 @@ export interface PayloadType { export type ReturnDataType = OB11GroupMember[] -class GetGroupMemberList { +class GetGroupMemberList extends BaseAction { static ACTION_TYPE: ActionType = 'get_group_member_list' - async check(jsonData: any): Promise { - return { - valid: true, - } - } - - async handle(jsonData: any) { - const result = await this.check(jsonData) - if (!result.valid) { - return OB11Response.error(result.message) - } - const resData = await this._handle(jsonData) - return resData - } - async _handle(payload: PayloadType): Promise> { const group = await getGroup(payload.group_id.toString()); if (group) { diff --git a/src/onebot11/actions/GetLoginInfo.ts b/src/onebot11/actions/GetLoginInfo.ts index 78eb21a..6303bb6 100644 --- a/src/onebot11/actions/GetLoginInfo.ts +++ b/src/onebot11/actions/GetLoginInfo.ts @@ -1,8 +1,8 @@ import { OB11Response } from "./utils"; -import { BaseCheckResult } from "./types"; import { OB11Return, OB11User } from '../types'; import { OB11Constructor } from "../constructor"; import { selfInfo } from "../../common/data"; +import BaseAction from "./BaseAction"; export type ActionType = 'get_login_info' @@ -12,24 +12,9 @@ export interface PayloadType { export type ReturnDataType = OB11User -class GetLoginInfo { +class GetLoginInfo extends BaseAction { static ACTION_TYPE: ActionType = 'get_login_info' - async check(jsonData: any): Promise { - return { - valid: true, - } - } - - async handle(jsonData: any) { - const result = await this.check(jsonData) - if (!result.valid) { - return OB11Response.error(result.message) - } - const resData = await this._handle(jsonData) - return resData - } - async _handle(payload: PayloadType): Promise> { return OB11Response.ok(OB11Constructor.selfInfo(selfInfo)); } diff --git a/src/onebot11/actions/GetMsg.ts b/src/onebot11/actions/GetMsg.ts index f700825..ab78177 100644 --- a/src/onebot11/actions/GetMsg.ts +++ b/src/onebot11/actions/GetMsg.ts @@ -1,9 +1,9 @@ import { OB11Response } from "./utils"; -import { BaseCheckResult } from "./types"; import { msgHistory } from "../../common/data"; import { OB11Message, OB11Return } from '../types'; import { OB11Constructor } from "../constructor"; import { log } from "../../common/utils"; +import BaseAction from "./BaseAction"; export type ActionType = 'get_msg' @@ -14,24 +14,9 @@ export interface PayloadType { export type ReturnDataType = OB11Message -class GetMsg { +class GetMsg extends BaseAction { static ACTION_TYPE: ActionType = 'get_msg' - async check(jsonData: any): Promise { - return { - valid: true, - } - } - - async handle(jsonData: any) { - const result = await this.check(jsonData) - if (!result.valid) { - return OB11Response.error(result.message) - } - const resData = await this._handle(jsonData) - return resData - } - async _handle(payload: PayloadType): Promise> { log("history msg ids", Object.keys(msgHistory)); const msg = msgHistory[payload.message_id.toString()] diff --git a/src/onebot11/actions/SendGroupMsg.ts b/src/onebot11/actions/SendGroupMsg.ts index 147390d..3b8919b 100644 --- a/src/onebot11/actions/SendGroupMsg.ts +++ b/src/onebot11/actions/SendGroupMsg.ts @@ -1,7 +1,6 @@ import { OB11PostSendMsg, OB11Return } from '../types'; -import { OB11Response } from "./utils"; -import { BaseCheckResult } from "./types"; import SendMsg from "./SendMsg"; +import BaseAction from './BaseAction'; export type ActionType = 'send_group_msg' @@ -13,24 +12,9 @@ export interface ReturnDataType { message_id: string } -class SendGroupMsg { +class SendGroupMsg extends BaseAction { static ACTION_TYPE: ActionType = 'send_group_msg' - async check(jsonData: any): Promise { - return { - valid: true, - } - } - - async handle(jsonData: any) { - const result = await this.check(jsonData) - if (!result.valid) { - return OB11Response.error(result.message) - } - const resData = await this._handle(jsonData) - return resData - } - async _handle(payload: PayloadType): Promise> { // 偷懒借用现有逻辑 return new SendMsg()._handle(payload as any) diff --git a/src/onebot11/actions/SendMsg.ts b/src/onebot11/actions/SendMsg.ts index 766d306..981a581 100644 --- a/src/onebot11/actions/SendMsg.ts +++ b/src/onebot11/actions/SendMsg.ts @@ -9,7 +9,7 @@ import { uri2local } from "../utils"; import { OB11Response } from "./utils"; import { v4 as uuid4 } from 'uuid'; import { log } from "../../common/utils"; -import { BaseCheckResult } from "./types"; +import BaseAction from "./BaseAction"; export type ActionType = 'send_msg' @@ -21,24 +21,9 @@ export interface ReturnDataType { message_id: string } -class SendMsg { +class SendMsg extends BaseAction { static ACTION_TYPE: ActionType = 'send_msg' - async check(jsonData: any): Promise { - return { - valid: true, - } - } - - async handle(jsonData: any) { - const result = await this.check(jsonData) - if (!result.valid) { - return OB11Response.error(result.message) - } - const resData = await this._handle(jsonData) - return resData - } - async _handle(payload: PayloadType): Promise> { const peer: Peer = { chatType: ChatType.friend, diff --git a/src/onebot11/actions/SendPrivateMsg.ts b/src/onebot11/actions/SendPrivateMsg.ts index f12bfd5..93c5af6 100644 --- a/src/onebot11/actions/SendPrivateMsg.ts +++ b/src/onebot11/actions/SendPrivateMsg.ts @@ -1,7 +1,6 @@ import { OB11PostSendMsg, OB11Return } from '../types'; -import { OB11Response } from "./utils"; -import { BaseCheckResult } from "./types"; import SendMsg from "./SendMsg"; +import BaseAction from './BaseAction'; export type ActionType = 'send_private_msg' @@ -13,24 +12,9 @@ export interface ReturnDataType { message_id: string } -class SendPrivateMsg { +class SendPrivateMsg extends BaseAction { static ACTION_TYPE: ActionType = 'send_private_msg' - async check(jsonData: any): Promise { - return { - valid: true, - } - } - - async handle(jsonData: any) { - const result = await this.check(jsonData) - if (!result.valid) { - return OB11Response.error(result.message) - } - const resData = await this._handle(jsonData) - return resData - } - async _handle(payload: PayloadType): Promise> { // 偷懒借用现有逻辑 return new SendMsg()._handle(payload as any)