mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2026-02-04 14:41:14 +00:00
Refactor type definitions and payload schemas in actions
Standardized type usage and improved type safety across multiple OneBot action files. Updated payload schemas to use string types for IDs and flags, refined return types, and enhanced message content typing. Added error handling for missing parameters in SetGroupTodo.
This commit is contained in:
parent
075047d790
commit
e562a57713
@ -29,7 +29,7 @@ export default class GetGroupAddRequest extends OneBotAction<void, ReturnType> {
|
||||
const NTQQUserApi = this.core.apis.UserApi;
|
||||
const NTQQGroupApi = this.core.apis.GroupApi;
|
||||
const ignoredNotifies = await NTQQGroupApi.getSingleScreenNotifies(true, 10);
|
||||
const retData: any[] = [];
|
||||
const retData: ReturnType = [];
|
||||
|
||||
const notifyPromises = ignoredNotifies
|
||||
.filter(notify => notify.type === 7)
|
||||
|
||||
@ -1,6 +1,8 @@
|
||||
import { OneBotAction } from '@/napcat-onebot/action/OneBotAction';
|
||||
import { ActionName } from '@/napcat-onebot/action/router';
|
||||
import { Static, Type } from '@sinclair/typebox';
|
||||
import { NTQQWebApi } from 'napcat-core/apis';
|
||||
|
||||
const PayloadSchema = Type.Object({
|
||||
group_id: Type.String({ description: '群号' }),
|
||||
});
|
||||
@ -9,14 +11,14 @@ type PayloadType = Static<typeof PayloadSchema>;
|
||||
|
||||
const ReturnSchema = Type.Array(Type.Any(), { description: '群相册列表' });
|
||||
|
||||
type ReturnType = Static<typeof ReturnSchema>;
|
||||
type GetQunAlbumListReturn = Awaited<globalThis.ReturnType<NTQQWebApi['getAlbumListByNTQQ']>>['response']['album_list'];
|
||||
|
||||
export class GetQunAlbumList extends OneBotAction<PayloadType, ReturnType> {
|
||||
export class GetQunAlbumList extends OneBotAction<PayloadType, GetQunAlbumListReturn> {
|
||||
override actionName = ActionName.GetQunAlbumList;
|
||||
override payloadSchema = PayloadSchema;
|
||||
override returnSchema = ReturnSchema;
|
||||
|
||||
async _handle (payload: PayloadType) {
|
||||
async _handle (payload: PayloadType): Promise<GetQunAlbumListReturn> {
|
||||
return (await this.core.apis.WebApi.getAlbumListByNTQQ(payload.group_id)).response.album_list;
|
||||
}
|
||||
}
|
||||
|
||||
@ -42,7 +42,13 @@ export class GetUnidirectionalFriendList extends OneBotAction<void, ReturnType>
|
||||
const rsq = { cmd: 'MQUpdateSvc_com_qq_ti.web.OidbSvc.0xe17_0', data: data as PacketBuf };
|
||||
const rsp_data = await this.core.apis.PacketApi.pkt.operation.sendPacket(rsq, true);
|
||||
const block_json = ProtoBuf(class extends ProtoBufBase { data = PBString(4); }).decode(rsp_data);
|
||||
const block_list: any[] = JSON.parse(block_json.data).rpt_block_list;
|
||||
const block_list = JSON.parse(block_json.data).rpt_block_list as {
|
||||
uint64_uin: number;
|
||||
str_uid: string;
|
||||
bytes_nick: string;
|
||||
uint32_age: number;
|
||||
bytes_source: string;
|
||||
}[];
|
||||
|
||||
return block_list.map((block) => ({
|
||||
uin: block.uint64_uin,
|
||||
|
||||
@ -2,17 +2,19 @@ import { ContextMode, normalize, ReturnDataType, SendMsgBase, SendMsgPayload } f
|
||||
import { ActionName } from '@/napcat-onebot/action/router';
|
||||
|
||||
// 未验证
|
||||
type GoCQHTTPSendForwardMsgPayload = SendMsgPayload & { messages?: any; };
|
||||
|
||||
export class GoCQHTTPSendForwardMsgBase extends SendMsgBase {
|
||||
protected override async check (payload: SendMsgPayload) {
|
||||
if ((payload as any).messages) payload.message = normalize((payload as any).messages);
|
||||
protected override async check (payload: GoCQHTTPSendForwardMsgPayload) {
|
||||
if (payload.messages) payload.message = normalize(payload.messages);
|
||||
return super.check(payload);
|
||||
}
|
||||
}
|
||||
export class GoCQHTTPSendForwardMsg extends GoCQHTTPSendForwardMsgBase {
|
||||
override actionName = ActionName.GoCQHTTP_SendForwardMsg;
|
||||
|
||||
protected override async check (payload: SendMsgPayload) {
|
||||
if ((payload as any).messages) payload.message = normalize((payload as any).messages);
|
||||
protected override async check (payload: GoCQHTTPSendForwardMsgPayload) {
|
||||
if (payload.messages) payload.message = normalize(payload.messages);
|
||||
return super.check(payload);
|
||||
}
|
||||
}
|
||||
|
||||
@ -5,6 +5,7 @@ import { MessageUnique } from 'napcat-common/src/message-unique';
|
||||
import crypto from 'crypto';
|
||||
import { Static, Type } from '@sinclair/typebox';
|
||||
import { NetworkAdapterConfig } from '@/napcat-onebot/config/config';
|
||||
import { OB11MessageData, OB11MessageDataType } from '@/napcat-onebot/types';
|
||||
|
||||
const PayloadSchema = Type.Object({
|
||||
group_id: Type.String({ description: '群号' }),
|
||||
@ -58,12 +59,12 @@ export class GetGroupEssence extends OneBotAction<PayloadType, ReturnType> {
|
||||
if (msgOriginData) {
|
||||
const { id: message_id, msg: rawMessage } = msgOriginData;
|
||||
const parsed = await this.obContext.apis.MsgApi.parseMessage(rawMessage, config.messagePostFormat);
|
||||
let content: any[] = [];
|
||||
let content: OB11MessageData[] = [];
|
||||
if (parsed) {
|
||||
if (Array.isArray(parsed.message)) {
|
||||
content = parsed.message;
|
||||
} else {
|
||||
content = [{ type: 'text', data: { text: parsed.message } }];
|
||||
content = [{ type: OB11MessageDataType.text, data: { text: parsed.message } }];
|
||||
}
|
||||
}
|
||||
return {
|
||||
@ -99,24 +100,25 @@ export class GetGroupEssence extends OneBotAction<PayloadType, ReturnType> {
|
||||
operator_nick: msg.add_digest_nick,
|
||||
message_id: shortId,
|
||||
operator_time: msg.add_digest_time,
|
||||
content: msg.msg_content.map((msg) => {
|
||||
content: msg.msg_content.map((msg): OB11MessageData | undefined => {
|
||||
if (msg.msg_type === 1) {
|
||||
return {
|
||||
type: 'text',
|
||||
type: OB11MessageDataType.text,
|
||||
data: {
|
||||
text: msg?.text,
|
||||
text: msg?.text ?? '',
|
||||
},
|
||||
};
|
||||
} else if (msg.msg_type === 3) {
|
||||
return {
|
||||
type: 'image',
|
||||
type: OB11MessageDataType.image,
|
||||
data: {
|
||||
file: '',
|
||||
url: msg?.image_url,
|
||||
},
|
||||
};
|
||||
}
|
||||
return undefined;
|
||||
}).filter((e): e is any => e !== undefined),
|
||||
}).filter((e): e is OB11MessageData => e !== undefined),
|
||||
};
|
||||
}));
|
||||
}
|
||||
|
||||
@ -4,7 +4,7 @@ import { ActionName } from '@/napcat-onebot/action/router';
|
||||
import { Static, Type } from '@sinclair/typebox';
|
||||
|
||||
const PayloadSchema = Type.Object({
|
||||
flag: Type.Union([Type.String(), Type.Number()], { description: '请求flag' }),
|
||||
flag: Type.String({ description: '请求flag' }),
|
||||
approve: Type.Optional(Type.Union([Type.Boolean(), Type.String()], { description: '是否同意' })),
|
||||
reason: Type.Optional(Type.Union([Type.String({ default: ' ' }), Type.Null()], { description: '拒绝理由' })),
|
||||
count: Type.Optional(Type.Number({ default: 100, description: '搜索通知数量' })),
|
||||
|
||||
@ -6,8 +6,8 @@ import { ActionName } from '../router';
|
||||
|
||||
export const SetGroupTodoPayloadSchema = Type.Object({
|
||||
group_id: Type.Union([Type.String(), Type.Number()], { description: '群号' }),
|
||||
message_id: Type.Union([Type.String(), Type.Number()], { description: '消息ID' }),
|
||||
message_seq: Type.Optional(Type.Union([Type.String(), Type.Number()], { description: '消息Seq (可选)' })),
|
||||
message_id: Type.Optional(Type.String({ description: '消息ID' })),
|
||||
message_seq: Type.Optional(Type.String({ description: '消息Seq (可选)' })),
|
||||
});
|
||||
|
||||
export type SetGroupTodoPayload = Static<typeof SetGroupTodoPayloadSchema>;
|
||||
@ -19,6 +19,9 @@ export class SetGroupTodo extends GetPacketStatusDepends<SetGroupTodoPayload, vo
|
||||
if (payload.message_seq) {
|
||||
return await this.core.apis.PacketApi.pkt.operation.SetGroupTodo(+payload.group_id, payload.message_seq.toString());
|
||||
}
|
||||
if (!payload.message_id) {
|
||||
throw new Error('缺少参数 message_id 或 message_seq');
|
||||
}
|
||||
const peer: Peer = {
|
||||
chatType: ChatType.KCHATTYPEGROUP,
|
||||
peerUid: payload.group_id.toString(),
|
||||
|
||||
@ -3,7 +3,7 @@ import { ActionName } from '@/napcat-onebot/action/router';
|
||||
import { Static, Type } from '@sinclair/typebox';
|
||||
|
||||
export const SetFriendAddRequestPayloadSchema = Type.Object({
|
||||
flag: Type.Union([Type.String(), Type.Number()], { description: '加好友请求的 flag (需从上报中获取)' }),
|
||||
flag: Type.String({ description: '加好友请求的 flag (需从上报中获取)' }),
|
||||
approve: Type.Optional(Type.Union([Type.String(), Type.Boolean()], { description: '是否同意请求' })),
|
||||
remark: Type.Optional(Type.String({ description: '添加后的好友备注' })),
|
||||
});
|
||||
|
||||
@ -3,7 +3,7 @@ import { ActionName } from '@/napcat-onebot/action/router';
|
||||
import { Static, Type } from '@sinclair/typebox';
|
||||
|
||||
export const SetFriendRemarkPayloadSchema = Type.Object({
|
||||
user_id: Type.Union([Type.String(), Type.Number()], { description: '好友 QQ 号' }),
|
||||
user_id: Type.String({ description: '好友 QQ 号' }),
|
||||
remark: Type.String({ description: '备注' }),
|
||||
});
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user