From 9837ef4f36a03f702220365059f044dcca2f69c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=8B=E7=93=9C=E4=B8=80=E5=8D=81=E9=9B=AA?= Date: Mon, 29 Jul 2024 08:59:26 +0800 Subject: [PATCH] fix #125 --- .../action/go-cqhttp/UploadPrivareFile.ts | 51 +++++++++++++++++++ src/onebot11/action/index.ts | 4 +- src/onebot11/action/types.ts | 3 +- 3 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 src/onebot11/action/go-cqhttp/UploadPrivareFile.ts diff --git a/src/onebot11/action/go-cqhttp/UploadPrivareFile.ts b/src/onebot11/action/go-cqhttp/UploadPrivareFile.ts new file mode 100644 index 00000000..10d0b856 --- /dev/null +++ b/src/onebot11/action/go-cqhttp/UploadPrivareFile.ts @@ -0,0 +1,51 @@ +import BaseAction from '../BaseAction'; +import { getGroup } from '@/core/data'; +import { ActionName } from '../types'; +import { SendMsgElementConstructor } from '@/core/entities/constructor'; +import { ChatType, Peer, SendFileElement } from '@/core/entities'; +import fs from 'fs'; +import { SendMsg, sendMsg } from '@/onebot11/action/msg/SendMsg'; +import { uri2local } from '@/common/utils/file'; +import { FromSchema, JSONSchema } from 'json-schema-to-ts'; +import { NTQQFriendApi, NTQQUserApi } from '@/core'; +const SchemaData = { + type: 'object', + properties: { + user_id: { type: ['number', 'string'] }, + file: { type: 'string' }, + name: { type: 'string' } + }, + required: ['user_id', 'file', 'name'] +} as const satisfies JSONSchema; + +type Payload = FromSchema; + +export default class GoCQHTTPUploadPrivateFile extends BaseAction { + actionName = ActionName.GOCQHTTP_UploadPrivateFile; + PayloadSchema = SchemaData; + async getPeer(payload: Payload): Promise { + if (payload.user_id) { + const peerUid = await NTQQUserApi.getUidByUin(payload.user_id.toString()); + if (!peerUid) { + throw `私聊${payload.user_id}不存在`; + } + const isBuddy = await NTQQFriendApi.isBuddy(peerUid); + return { chatType: isBuddy ? ChatType.friend : ChatType.temp, peerUid }; + } + throw '缺少参数 user_id'; + } + protected async _handle(payload: Payload): Promise { + let peer = await this.getPeer(payload); + let file = payload.file; + if (fs.existsSync(file)) { + file = `file://${file}`; + } + const downloadResult = await uri2local(file); + if (downloadResult.errMsg) { + throw new Error(downloadResult.errMsg); + } + const sendFileEle: SendFileElement = await SendMsgElementConstructor.file(downloadResult.path, payload.name); + await sendMsg(peer, [sendFileEle], [], true); + return null; + } +} diff --git a/src/onebot11/action/index.ts b/src/onebot11/action/index.ts index f032a784..bf1689b8 100644 --- a/src/onebot11/action/index.ts +++ b/src/onebot11/action/index.ts @@ -76,6 +76,7 @@ import GetRecentContact from './user/GetRecentContact'; import { GetProfileLike } from './extends/GetProfileLike'; import SetGroupHeader from './extends/SetGroupHeader'; import { FetchCustomFace } from './extends/FetchCustomFace'; +import GoCQHTTPUploadPrivateFile from './go-cqhttp/UploadPrivareFile'; export const actionHandlers = [ new RebootNormal(), @@ -158,7 +159,8 @@ export const actionHandlers = [ new MarkAllMsgAsRead(), new GetProfileLike(), new SetGroupHeader(), - new FetchCustomFace() + new FetchCustomFace(), + new GoCQHTTPUploadPrivateFile() ]; function initActionMap() { diff --git a/src/onebot11/action/types.ts b/src/onebot11/action/types.ts index 2a689d8c..60c2a5c7 100644 --- a/src/onebot11/action/types.ts +++ b/src/onebot11/action/types.ts @@ -100,5 +100,6 @@ export enum ActionName { _MarkAllMsgAsRead = '_mark_all_as_read', GetProfileLike = 'get_profile_like', SetGroupHeader = "set_group_head", - FetchCustomFace = "fetch_custom_face" + FetchCustomFace = "fetch_custom_face", + GOCQHTTP_UploadPrivateFile = 'upload_private_file' }