diff --git a/src/onebot11/action/go-cqhttp/UploadGroupFile.ts b/src/onebot11/action/go-cqhttp/UploadFile.ts similarity index 52% rename from src/onebot11/action/go-cqhttp/UploadGroupFile.ts rename to src/onebot11/action/go-cqhttp/UploadFile.ts index d010090..2dc49c9 100644 --- a/src/onebot11/action/go-cqhttp/UploadGroupFile.ts +++ b/src/onebot11/action/go-cqhttp/UploadFile.ts @@ -1,27 +1,32 @@ import BaseAction from "../BaseAction"; -import {getGroup} from "../../../common/data"; +import {getGroup, getUidByUin} from "../../../common/data"; import {ActionName} from "../types"; import {SendMsgElementConstructor} from "../../../ntqqapi/constructor"; import {ChatType, SendFileElement} from "../../../ntqqapi/types"; import fs from "fs"; -import {NTQQMsgApi} from "../../../ntqqapi/api/msg"; +import {NTQQMsgApi, Peer} from "../../../ntqqapi/api/msg"; import {uri2local} from "../../../common/utils"; interface Payload{ - group_id: number + user_id: number + group_id?: number file: string name: string folder: string } -export default class GoCQHTTPUploadGroupFile extends BaseAction { +class GoCQHTTPUploadFileBase extends BaseAction { actionName = ActionName.GoCQHTTP_UploadGroupFile - protected async _handle(payload: Payload): Promise { - const group = await getGroup(payload.group_id.toString()); - if (!group){ - throw new Error(`群组${payload.group_id}不存在`) + + getPeer(payload: Payload): Peer { + if (payload.user_id){ + return {chatType: ChatType.friend, peerUid: getUidByUin(payload.user_id.toString())} } + return {chatType: ChatType.group, peerUid: payload.group_id.toString()} + } + + protected async _handle(payload: Payload): Promise { let file = payload.file; if (fs.existsSync(file)){ file = `file://${file}` @@ -31,7 +36,16 @@ export default class GoCQHTTPUploadGroupFile extends BaseAction { throw new Error(downloadResult.errMsg) } let sendFileEle: SendFileElement = await SendMsgElementConstructor.file(downloadResult.path, payload.name); - await NTQQMsgApi.sendMsg({chatType: ChatType.group, peerUid: group.groupCode}, [sendFileEle]); + await NTQQMsgApi.sendMsg(this.getPeer(payload), [sendFileEle]); return null } +} + + +export class GoCQHTTPUploadGroupFile extends GoCQHTTPUploadFileBase { + actionName = ActionName.GoCQHTTP_UploadGroupFile +} + +export class GoCQHTTPUploadPrivateFile extends GoCQHTTPUploadFileBase { + actionName = ActionName.GoCQHTTP_UploadPrivateFile } \ No newline at end of file