refactor: type-check (#586)

* refactor: type-check

* fix: default

* refactor: type-check
This commit is contained in:
手瓜一十雪
2024-12-01 12:41:51 +08:00
committed by GitHub
parent fa78e6567c
commit fb3370ccf9
83 changed files with 566 additions and 943 deletions

View File

@@ -1,17 +1,13 @@
import { OneBotAction } from '@/onebot/action/OneBotAction';
import { ActionName } from '@/onebot/action/router';
import { FromSchema, JSONSchema } from 'json-schema-to-ts';
import { Static, Type } from '@sinclair/typebox';
const SchemaData = {
type: 'object',
properties: {
user_id: { type: ['number', 'string'] },
times: { type: ['number', 'string'] },
},
required: ['user_id', 'times'],
} as const satisfies JSONSchema;
const SchemaData = Type.Object({
times: Type.Union([Type.Number(), Type.String()], { default: 1 }),
user_id: Type.Union([Type.Number(), Type.String()])
});
type Payload = FromSchema<typeof SchemaData>;
type Payload = Static<typeof SchemaData>;
export default class SendLike extends OneBotAction<Payload, null> {
actionName = ActionName.SendLike;
@@ -20,7 +16,7 @@ export default class SendLike extends OneBotAction<Payload, null> {
async _handle(payload: Payload): Promise<null> {
const qq = payload.user_id.toString();
const uid: string = await this.core.apis.UserApi.getUidByUinV2(qq) ?? '';
const result = await this.core.apis.UserApi.like(uid, parseInt(payload.times?.toString()) || 1);
const result = await this.core.apis.UserApi.like(uid, +payload.times);
if (result.result !== 0) {
throw new Error(`点赞失败 ${result.errMsg}`);
}