Skip to content

Commit

Permalink
feat: check action data 2
Browse files Browse the repository at this point in the history
  • Loading branch information
MliKiowa committed May 18, 2024
1 parent d4e5201 commit 62eee5f
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 43 deletions.
24 changes: 15 additions & 9 deletions src/onebot11/action/go-cqhttp/GetFriendMsgHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,27 @@ import { dbUtil } from '@/core/utils/db';
import { NTQQMsgApi } from '@/core/apis/msg';
import { OB11Constructor } from '../../constructor';
import { logDebug } from '@/common/utils/log';


interface Payload {
user_id: number
message_seq: number,
count: number
}
import { FromSchema, JSONSchema } from 'json-schema-to-ts';

interface Response {
messages: OB11Message[];
messages: OB11Message[];
}

const SchemaData = {
type: 'object',
properties: {
user_id: { type: 'number' },
message_seq: { type: 'number' },
count: { type: 'number' }
},
required: ['user_id', 'message_seq', 'count']
} as const satisfies JSONSchema;

type Payload = FromSchema<typeof SchemaData>;

export default class GetFriendMsgHistory extends BaseAction<Payload, Response> {
actionName = ActionName.GetFriendMsgHistory;
PayloadSchema = SchemaData;
protected async _handle(payload: Payload): Promise<Response> {
const uid = getUidByUin(payload.user_id.toString());
if (!uid) {
Expand Down
28 changes: 15 additions & 13 deletions src/onebot11/action/go-cqhttp/GetGroupHonorInfo.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import { OB11User } from '../../types';
import { OB11Constructor } from '../../constructor';
import { friends } from '@/core/data';

import BaseAction from '../BaseAction';
import { ActionName } from '../types';
import { NTQQUserApi, WebApi, WebHonorType } from '@/core/apis';
interface Payload {
group_id: number,
type?: WebHonorType
}
import { WebApi, WebHonorType } from '@/core/apis';
import { FromSchema, JSONSchema } from 'json-schema-to-ts';
const SchemaData = {
type: 'object',
properties: {
group_id: { type: 'number' },
type: { enum: [WebHonorType.ALL, WebHonorType.EMOTION, WebHonorType.LEGEND, WebHonorType.PERFROMER, WebHonorType.STORONGE_NEWBI, WebHonorType.TALKACTIVE] }
},
required: ['group_id']
} as const satisfies JSONSchema;
// enum是不是有点抽象
type Payload = FromSchema<typeof SchemaData>;

export class GetGroupHonorInfo extends BaseAction<Payload, Array<any>> {
actionName = ActionName.GetGroupHonorInfo;

PayloadSchema = SchemaData;
protected async _handle(payload: Payload) {
// console.log(await NTQQUserApi.getRobotUinRange());
if (!payload.group_id) {
throw '缺少参数group_id';
}
if (!payload.type) {
payload.type = WebHonorType.ALL;
}
Expand Down
23 changes: 14 additions & 9 deletions src/onebot11/action/go-cqhttp/GetGroupMsgHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,26 @@ import { dbUtil } from '@/core/utils/db';
import { NTQQMsgApi } from '@/core/apis/msg';
import { OB11Constructor } from '../../constructor';
import { logDebug } from '@/common/utils/log';


interface Payload {
group_id: number
message_seq: number,
count: number
}

import { FromSchema, JSONSchema } from 'json-schema-to-ts';
interface Response {
messages: OB11Message[];
}

const SchemaData = {
type: 'object',
properties: {
group_id: { type: 'number' },
message_seq: { type: 'number' },
count: { type: 'number' }
},
required: ['group_id', 'message_seq', 'count']
} as const satisfies JSONSchema;

type Payload = FromSchema<typeof SchemaData>;

export default class GoCQHTTPGetGroupMsgHistory extends BaseAction<Payload, Response> {
actionName = ActionName.GoCQHTTP_GetGroupMsgHistory;

PayloadSchema = SchemaData;
protected async _handle(payload: Payload): Promise<Response> {
const group = await getGroup(payload.group_id.toString());
if (!group) {
Expand Down
14 changes: 12 additions & 2 deletions src/onebot11/action/go-cqhttp/GetStrangerInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,22 @@ import { OB11Constructor } from '../../constructor';
import { ActionName } from '../types';
import { NTQQUserApi } from '@/core/apis/user';
import { log, logDebug } from '@/common/utils/log';
import { FromSchema, JSONSchema } from 'json-schema-to-ts';

const SchemaData = {
type: 'object',
properties: {
user_id: { type: 'number' },
},
required: ['user_id']
} as const satisfies JSONSchema;

export default class GoCQHTTPGetStrangerInfo extends BaseAction<{ user_id: number }, OB11User> {
type Payload = FromSchema<typeof SchemaData>;

export default class GoCQHTTPGetStrangerInfo extends BaseAction<Payload, OB11User> {
actionName = ActionName.GoCQHTTP_GetStrangerInfo;

protected async _handle(payload: { user_id: number }): Promise<OB11User> {
protected async _handle(payload: Payload): Promise<OB11User> {
const user_id = payload.user_id.toString();
logDebug('uidMaps', uid2UinMap);
const uid = getUidByUin(user_id);
Expand Down
2 changes: 1 addition & 1 deletion src/onebot11/action/go-cqhttp/SendForwardMsg.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import SendMsg, { normalize } from '../msg/SendMsg';
import { OB11PostSendMsg } from '../../types';
import { ActionName } from '../types';

// 未验证
export class GoCQHTTPSendForwardMsg extends SendMsg {
actionName = ActionName.GoCQHTTP_SendForwardMsg;

Expand Down
25 changes: 16 additions & 9 deletions src/onebot11/action/go-cqhttp/SendGroupNotice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,21 @@ import BaseAction from '../BaseAction';
import { ActionName } from '../types';
import { NTQQGroupApi, WebApi } from '@/core/apis';
import { unlink } from 'node:fs';
interface Payload {
group_id: string;
content: string;
image?: string;
pinned?: number;
confirmRequired?: number;
import { FromSchema, JSONSchema } from 'json-schema-to-ts';
const SchemaData = {
type: 'object',
properties: {
group_id: { type: 'number' },
content: { type: 'string' },
image: { type: 'string' },
pinned: { type: 'number' },
confirmRequired: { type: 'number' }
},
required: ['group_id', 'content']
} as const satisfies JSONSchema;

type Payload = FromSchema<typeof SchemaData>;

}
export class SendGroupNotice extends BaseAction<Payload, null> {
actionName = ActionName.GoCQHTTP_SendGroupNotice;
protected async _handle(payload: Payload) {
Expand All @@ -29,7 +36,7 @@ export class SendGroupNotice extends BaseAction<Payload, null> {
throw `群公告${payload.image}设置失败,获取资源失败`;
}
await checkFileReceived(Image_path, 5000); // 文件不存在QQ会崩溃,需要提前判断
let ImageUploadResult = await NTQQGroupApi.uploadGroupBulletinPic(payload.group_id, Image_path);
let ImageUploadResult = await NTQQGroupApi.uploadGroupBulletinPic(payload.group_id.toString(), Image_path);
if (ImageUploadResult.errCode != 0) {
throw `群公告${payload.image}设置失败,图片上传失败`;
}
Expand All @@ -46,7 +53,7 @@ export class SendGroupNotice extends BaseAction<Payload, null> {
if (!payload.confirmRequired) {
Notice_confirmRequired = 0;
}
let PublishGroupBulletinResult = await NTQQGroupApi.publishGroupBulletin(payload.group_id, payload.content, UploadImage, Notice_Pinned, Notice_confirmRequired);
let PublishGroupBulletinResult = await NTQQGroupApi.publishGroupBulletin(payload.group_id.toString(), payload.content, UploadImage, Notice_Pinned, Notice_confirmRequired);

if (PublishGroupBulletinResult.result! = 0) {
throw `设置群公告失败,错误信息:${PublishGroupBulletinResult.errMsg}`;
Expand Down

0 comments on commit 62eee5f

Please sign in to comment.