From d415eac6649e047ddd1af11e906364a3ded55542 Mon Sep 17 00:00:00 2001 From: CaoMeiYouRen <996881204@qq.com> Date: Sun, 17 Nov 2024 22:06:41 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=20OneBot=20?= =?UTF-8?q?=E5=92=8C=20Qmsg=20=E7=9A=84=20option=20=E6=A0=A1=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/push/one-bot.ts | 14 ++++++++++++-- src/push/qmsg.ts | 3 +++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/push/one-bot.ts b/src/push/one-bot.ts index a5c9b17..498e56e 100644 --- a/src/push/one-bot.ts +++ b/src/push/one-bot.ts @@ -83,8 +83,9 @@ export const oneBotOptionSchema: OneBotOptionSchema = { message_type: { type: 'select', title: '消息类型', - description: '消息类型', + description: '消息类型,private 或 group,默认为 private', required: true, + default: 'private', options: [ { label: '私聊', @@ -198,7 +199,16 @@ export class OneBot implements Send { */ async send(title: string, desp: string, option: OneBotOption): Promise> { Debugger('title: "%s", desp: "%s", option: "%o"', title, desp, option) - const { message_type, ...args } = option || {} + // !由于 OneBot 的 option 中带有必填项,所以需要校验 + // 根据 optionSchema 验证 option + validate(option, OneBot.optionSchema as OptionSchema) + if (option.message_type === 'private' && !option.user_id) { + throw new Error('OneBot 私聊消息类型必须提供 user_id') + } + if (option.message_type === 'group' && !option.group_id) { + throw new Error('OneBot 群聊消息类型必须提供 group_id') + } + const { message_type = 'private', ...args } = option || {} const message = `${title}${desp ? `\n${desp}` : ''}` return ajax({ baseURL: this.ONE_BOT_BASE_URL, diff --git a/src/push/qmsg.ts b/src/push/qmsg.ts index 2924ba3..87d8451 100644 --- a/src/push/qmsg.ts +++ b/src/push/qmsg.ts @@ -150,6 +150,9 @@ export class Qmsg implements Send { */ async send(title: string, desp: string, option: QmsgOption): Promise> { Debugger('title: "%s", desp: "%s", option: "%o"', title, desp, option) + // !由于 Qmsg 酱的 option 中带有必填项,所以需要校验 + // 根据 optionSchema 验证 option + validate(option, Qmsg.optionSchema) const { qq, type = 'send', bot } = option || {} const msg = `${title}${desp ? `\n${desp}` : ''}` return ajax({