fix: go-cqhttp 上传接口返回 file_id (UploadGroupFile, UploadPrivateFile)
This commit is contained in:
2 files changed
+26
-10
No files matched your search
@@ -1,6 +1,6 @@
|
|||||||
import { OneBotAction } from '@/onebot/action/OneBotAction';
|
import { OneBotAction } from '@/onebot/action/OneBotAction';
|
||||||
import { ActionName } from '@/onebot/action/router';
|
import { ActionName } from '@/onebot/action/router';
|
||||||
import { ChatType, Peer } from '@/core/types';
|
import { ChatType, Peer, ElementType } from '@/core/types';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import { uriToLocalFile } from '@/common/file';
|
import { uriToLocalFile } from '@/common/file';
|
||||||
import { SendMessageContext } from '@/onebot/api';
|
import { SendMessageContext } from '@/onebot/api';
|
||||||
@@ -16,11 +16,15 @@ const SchemaData = Type.Object({
|
|||||||
|
|
||||||
type Payload = Static<typeof SchemaData>;
|
type Payload = Static<typeof SchemaData>;
|
||||||
|
|
||||||
export default class GoCQHTTPUploadGroupFile extends OneBotAction<Payload, null> {
|
interface UploadGroupFileResponse {
|
||||||
|
file_id: string | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default class GoCQHTTPUploadGroupFile extends OneBotAction<Payload, UploadGroupFileResponse> {
|
||||||
override actionName = ActionName.GoCQHTTP_UploadGroupFile;
|
override actionName = ActionName.GoCQHTTP_UploadGroupFile;
|
||||||
override payloadSchema = SchemaData;
|
override payloadSchema = SchemaData;
|
||||||
|
|
||||||
async _handle(payload: Payload): Promise<null> {
|
async _handle(payload: Payload): Promise<UploadGroupFileResponse> {
|
||||||
let file = payload.file;
|
let file = payload.file;
|
||||||
if (fs.existsSync(file)) {
|
if (fs.existsSync(file)) {
|
||||||
file = `file://${file}`;
|
file = `file://${file}`;
|
||||||
@@ -39,7 +43,11 @@ export default class GoCQHTTPUploadGroupFile extends OneBotAction<Payload, null>
|
|||||||
};
|
};
|
||||||
const sendFileEle = await this.core.apis.FileApi.createValidSendFileElement(msgContext, downloadResult.path, payload.name, payload.folder ?? payload.folder_id);
|
const sendFileEle = await this.core.apis.FileApi.createValidSendFileElement(msgContext, downloadResult.path, payload.name, payload.folder ?? payload.folder_id);
|
||||||
msgContext.deleteAfterSentFiles.push(downloadResult.path);
|
msgContext.deleteAfterSentFiles.push(downloadResult.path);
|
||||||
await this.obContext.apis.MsgApi.sendMsgWithOb11UniqueId(peer, [sendFileEle], msgContext.deleteAfterSentFiles);
|
const returnMsg = await this.obContext.apis.MsgApi.sendMsgWithOb11UniqueId(peer, [sendFileEle], msgContext.deleteAfterSentFiles);
|
||||||
return null;
|
|
||||||
|
const fileElement = returnMsg.elements.find(ele => ele.elementType === ElementType.FILE);
|
||||||
|
return {
|
||||||
|
file_id: fileElement?.fileElement?.fileUuid || null
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
import { OneBotAction } from '@/onebot/action/OneBotAction';
|
import { OneBotAction } from '@/onebot/action/OneBotAction';
|
||||||
import { ActionName } from '@/onebot/action/router';
|
import { ActionName } from '@/onebot/action/router';
|
||||||
import { ChatType, Peer, SendFileElement } from '@/core/types';
|
import { ChatType, Peer, SendFileElement, ElementType } from '@/core/types';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import { uriToLocalFile } from '@/common/file';
|
import { uriToLocalFile } from '@/common/file';
|
||||||
import { SendMessageContext } from '@/onebot/api';
|
import { SendMessageContext } from '@/onebot/api';
|
||||||
@@ -15,7 +15,11 @@ const SchemaData = Type.Object({
|
|||||||
|
|
||||||
type Payload = Static<typeof SchemaData>;
|
type Payload = Static<typeof SchemaData>;
|
||||||
|
|
||||||
export default class GoCQHTTPUploadPrivateFile extends OneBotAction<Payload, null> {
|
interface UploadPrivateFileResponse {
|
||||||
|
file_id: string | null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default class GoCQHTTPUploadPrivateFile extends OneBotAction<Payload, UploadPrivateFileResponse> {
|
||||||
override actionName = ActionName.GOCQHTTP_UploadPrivateFile;
|
override actionName = ActionName.GOCQHTTP_UploadPrivateFile;
|
||||||
override payloadSchema = SchemaData;
|
override payloadSchema = SchemaData;
|
||||||
|
|
||||||
@@ -31,7 +35,7 @@ export default class GoCQHTTPUploadPrivateFile extends OneBotAction<Payload, nul
|
|||||||
throw new Error('缺少参数 user_id');
|
throw new Error('缺少参数 user_id');
|
||||||
}
|
}
|
||||||
|
|
||||||
async _handle(payload: Payload): Promise<null> {
|
async _handle(payload: Payload): Promise<UploadPrivateFileResponse> {
|
||||||
let file = payload.file;
|
let file = payload.file;
|
||||||
if (fs.existsSync(file)) {
|
if (fs.existsSync(file)) {
|
||||||
file = `file://${file}`;
|
file = `file://${file}`;
|
||||||
@@ -49,7 +53,11 @@ export default class GoCQHTTPUploadPrivateFile extends OneBotAction<Payload, nul
|
|||||||
};
|
};
|
||||||
const sendFileEle: SendFileElement = await this.core.apis.FileApi.createValidSendFileElement(msgContext, downloadResult.path, payload.name);
|
const sendFileEle: SendFileElement = await this.core.apis.FileApi.createValidSendFileElement(msgContext, downloadResult.path, payload.name);
|
||||||
msgContext.deleteAfterSentFiles.push(downloadResult.path);
|
msgContext.deleteAfterSentFiles.push(downloadResult.path);
|
||||||
await this.obContext.apis.MsgApi.sendMsgWithOb11UniqueId(await this.getPeer(payload), [sendFileEle], msgContext.deleteAfterSentFiles);
|
const returnMsg = await this.obContext.apis.MsgApi.sendMsgWithOb11UniqueId(await this.getPeer(payload), [sendFileEle], msgContext.deleteAfterSentFiles);
|
||||||
return null;
|
|
||||||
|
const fileElement = returnMsg.elements.find(ele => ele.elementType === ElementType.FILE);
|
||||||
|
return {
|
||||||
|
file_id: fileElement?.fileElement?.fileUuid || null
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user