diff --git a/src/common/file.ts b/src/common/file.ts index 33276de3..4e239acc 100644 --- a/src/common/file.ts +++ b/src/common/file.ts @@ -210,7 +210,7 @@ export async function checkUriType(Uri: string) { } else { filePath = pathname; } - + return { Uri: filePath, Type: FileUriType.Local }; } if (uri.startsWith('data:')) { @@ -242,7 +242,7 @@ export async function uri2local(dir: string, uri: string, filename: string | und const filenameTemp = tempName + fileExt; const filePath = path.join(dir, filenameTemp); fs.copyFileSync(HandledUri, filePath); - console.log('复制文件到临时文件', HandledUri, filePath); + //console.log('复制文件到临时文件', HandledUri, filePath); return { success: true, errMsg: '', fileName: filename, ext: fileExt, path: filePath }; } //接下来都要有文件名 @@ -250,7 +250,7 @@ export async function uri2local(dir: string, uri: string, filename: string | und if (UriType == FileUriType.Remote) { const pathInfo = path.parse(decodeURIComponent(new URL(HandledUri).pathname)); if (pathInfo.name) { - filename = pathInfo.name; + filename = pathInfo.name.substring(0, 50);//过长截断 if (pathInfo.ext) { filename += pathInfo.ext; } diff --git a/src/onebot/action/go-cqhttp/UploadPrivateFile.ts b/src/onebot/action/go-cqhttp/UploadPrivateFile.ts index f46423ba..ebba2ad6 100644 --- a/src/onebot/action/go-cqhttp/UploadPrivateFile.ts +++ b/src/onebot/action/go-cqhttp/UploadPrivateFile.ts @@ -4,6 +4,8 @@ import { ChatType, Peer, SendFileElement } from '@/core/entities'; import fs from 'fs'; import { uri2local } from '@/common/file'; import { FromSchema, JSONSchema } from 'json-schema-to-ts'; +import { MessageContext } from '@/onebot/api'; +import { ContextMode, createContext } from '../msg/SendMsg'; const SchemaData = { type: 'object', @@ -42,7 +44,15 @@ export default class GoCQHTTPUploadPrivateFile extends BaseAction if (!downloadResult.success) { throw new Error(downloadResult.errMsg); } - const sendFileEle: SendFileElement = await this.core.apis.FileApi.createValidSendFileElement(downloadResult.path, payload.name); + + let msgContext: MessageContext = { + peer: await createContext(this.core, { + user_id: payload.user_id.toString(), + group_id: undefined, + }, ContextMode.Private), + deleteAfterSentFiles: [] + } + const sendFileEle: SendFileElement = await this.core.apis.FileApi.createValidSendFileElement(msgContext, downloadResult.path, payload.name); await this.obContext.apis.MsgApi.sendMsgWithOb11UniqueId(await this.getPeer(payload), [sendFileEle], [], true); return null; } diff --git a/src/onebot/action/msg/SendMsg.ts b/src/onebot/action/msg/SendMsg.ts index c3c80509..a6d99400 100644 --- a/src/onebot/action/msg/SendMsg.ts +++ b/src/onebot/action/msg/SendMsg.ts @@ -3,6 +3,7 @@ import { OB11MessageDataType, OB11MessageMixType, OB11MessageNode, + OB11PostContext, OB11PostSendMsg, } from '@/onebot/types'; import { ActionName, BaseCheckResult } from '@/onebot/action/types'; @@ -30,7 +31,7 @@ export function normalize(message: OB11MessageMixType, autoEscape = false): OB11 ) : Array.isArray(message) ? message : [message]; } -export async function createContext(core: NapCatCore, payload: OB11PostSendMsg, contextMode: ContextMode): Promise { +export async function createContext(core: NapCatCore, payload: OB11PostContext, contextMode: ContextMode): Promise { // This function determines the type of message by the existence of user_id / group_id, // not message_type. // This redundant design of Ob11 here should be blamed. diff --git a/src/onebot/types/message.ts b/src/onebot/types/message.ts index 0e50a95f..bf2f3530 100644 --- a/src/onebot/types/message.ts +++ b/src/onebot/types/message.ts @@ -208,3 +208,8 @@ export interface OB11PostSendMsg { messages?: OB11MessageMixType; // 兼容 go-cqhttp auto_escape?: boolean | string } +export interface OB11PostContext { + message_type?: 'private' | 'group' + user_id?: string, + group_id?: string, +}