mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2026-01-07 19:49:01 +08:00
* feat: pnpm new * Refactor build and release workflows, update dependencies Switch build scripts and workflows from npm to pnpm, update build and artifact paths, and simplify release workflow by removing version detection and changelog steps. Add new dependencies (silk-wasm, express, ws, node-pty-prebuilt-multiarch), update exports in package.json files, and add vite config for napcat-framework. Also, rename manifest.json for framework package and fix static asset copying in shell build config.
64 lines
2.5 KiB
TypeScript
64 lines
2.5 KiB
TypeScript
import { OneBotAction } from '@/napcat-onebot/action/OneBotAction';
|
|
import { ActionName } from '@/napcat-onebot/action/router';
|
|
import { ChatType, Peer, SendFileElement, ElementType } from 'napcat-core/types';
|
|
import fs from 'fs';
|
|
import { uriToLocalFile } from 'napcat-common/src/file';
|
|
import { SendMessageContext } from '@/napcat-onebot/api';
|
|
import { ContextMode, createContext } from '@/napcat-onebot/action/msg/SendMsg';
|
|
import { Static, Type } from '@sinclair/typebox';
|
|
|
|
const SchemaData = Type.Object({
|
|
user_id: Type.Union([Type.Number(), Type.String()]),
|
|
file: Type.String(),
|
|
name: Type.String(),
|
|
});
|
|
|
|
type Payload = Static<typeof SchemaData>;
|
|
|
|
interface UploadPrivateFileResponse {
|
|
file_id: string | null;
|
|
}
|
|
|
|
export default class GoCQHTTPUploadPrivateFile extends OneBotAction<Payload, UploadPrivateFileResponse> {
|
|
override actionName = ActionName.GOCQHTTP_UploadPrivateFile;
|
|
override payloadSchema = SchemaData;
|
|
|
|
async getPeer (payload: Payload): Promise<Peer> {
|
|
if (payload.user_id) {
|
|
const peerUid = await this.core.apis.UserApi.getUidByUinV2(payload.user_id.toString());
|
|
if (!peerUid) {
|
|
throw new Error(`私聊${payload.user_id}不存在`);
|
|
}
|
|
const isBuddy = await this.core.apis.FriendApi.isBuddy(peerUid);
|
|
return { chatType: isBuddy ? ChatType.KCHATTYPEC2C : ChatType.KCHATTYPETEMPC2CFROMGROUP, peerUid };
|
|
}
|
|
throw new Error('缺少参数 user_id');
|
|
}
|
|
|
|
async _handle (payload: Payload): Promise<UploadPrivateFileResponse> {
|
|
let file = payload.file;
|
|
if (fs.existsSync(file)) {
|
|
file = `file://${file}`;
|
|
}
|
|
const downloadResult = await uriToLocalFile(this.core.NapCatTempPath, file);
|
|
if (!downloadResult.success) {
|
|
throw new Error(downloadResult.errMsg);
|
|
}
|
|
|
|
const msgContext: SendMessageContext = {
|
|
peer: await createContext(this.core, {
|
|
user_id: payload.user_id.toString(),
|
|
}, ContextMode.Private),
|
|
deleteAfterSentFiles: [],
|
|
};
|
|
const sendFileEle: SendFileElement = await this.core.apis.FileApi.createValidSendFileElement(msgContext, downloadResult.path, payload.name);
|
|
msgContext.deleteAfterSentFiles.push(downloadResult.path);
|
|
const returnMsg = await this.obContext.apis.MsgApi.sendMsgWithOb11UniqueId(await this.getPeer(payload), [sendFileEle], msgContext.deleteAfterSentFiles);
|
|
|
|
const fileElement = returnMsg.elements.find(ele => ele.elementType === ElementType.FILE);
|
|
return {
|
|
file_id: fileElement?.fileElement?.fileUuid || null,
|
|
};
|
|
}
|
|
}
|