mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2026-02-05 15:11:15 +00:00
Introduced explicit payloadSchema and returnSchema definitions for all OneBotAction classes using @sinclair/typebox. This improves type safety, API documentation, and validation for action payloads and return values. Also refactored method signatures and types for consistency across the codebase.
46 lines
1.6 KiB
TypeScript
46 lines
1.6 KiB
TypeScript
import { ActionName } from '@/napcat-onebot/action/router';
|
|
import { OneBotAction } from '@/napcat-onebot/action/OneBotAction';
|
|
import { MessageUnique } from 'napcat-common/src/message-unique';
|
|
import { Static, Type } from '@sinclair/typebox';
|
|
|
|
const PayloadSchema = Type.Object({
|
|
message_id: Type.Union([Type.Number(), Type.String()], { description: '消息ID' }),
|
|
emoji_id: Type.Union([Type.Number(), Type.String()], { description: '表情ID' }),
|
|
set: Type.Optional(Type.Union([Type.Boolean(), Type.String()], { description: '是否设置' })),
|
|
});
|
|
|
|
type PayloadType = Static<typeof PayloadSchema>;
|
|
|
|
const ReturnSchema = Type.Any({ description: '操作结果' });
|
|
|
|
type ReturnType = Static<typeof ReturnSchema>;
|
|
|
|
export class SetMsgEmojiLike extends OneBotAction<PayloadType, ReturnType> {
|
|
override actionName = ActionName.SetMsgEmojiLike;
|
|
override payloadSchema = PayloadSchema;
|
|
override returnSchema = ReturnSchema;
|
|
|
|
async _handle (payload: PayloadType) {
|
|
const msg = MessageUnique.getMsgIdAndPeerByShortId(+payload.message_id);
|
|
if (!msg) {
|
|
throw new Error('msg not found');
|
|
}
|
|
if (!payload.emoji_id) {
|
|
throw new Error('emojiId not found');
|
|
}
|
|
payload.set = payload.set ?? true;
|
|
|
|
const msgData = (await this.core.apis.MsgApi.getMsgsByMsgId(msg.Peer, [msg.MsgId])).msgList;
|
|
if (!msgData || msgData.length === 0 || !msgData[0]?.msgSeq) {
|
|
throw new Error('find msg by msgid error');
|
|
}
|
|
|
|
return await this.core.apis.MsgApi.setEmojiLike(
|
|
msg.Peer,
|
|
msgData[0].msgSeq,
|
|
payload.emoji_id.toString(),
|
|
typeof payload.set === 'string' ? payload.set === 'true' : !!payload.set
|
|
);
|
|
}
|
|
}
|