From dfcb406c66e11ccd0c41e14cd3536b7d1005eaf7 Mon Sep 17 00:00:00 2001 From: ayntee Date: Mon, 1 Feb 2021 22:57:21 +0400 Subject: [PATCH] feat(rest/request_manager): support multiple attachments (#499) --- src/rest/request_manager.ts | 18 +++++++++++++++--- src/types/channel.ts | 7 ++++++- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/rest/request_manager.ts b/src/rest/request_manager.ts index 4eacb42bf..efce076e7 100644 --- a/src/rest/request_manager.ts +++ b/src/rest/request_manager.ts @@ -1,5 +1,10 @@ import { authorization, eventHandlers } from "../bot.ts"; -import { Errors, HttpResponseCode, RequestMethods } from "../types/mod.ts"; +import { + Errors, + FileContent, + HttpResponseCode, + RequestMethods, +} from "../types/mod.ts"; import { API_VERSION, BASE_URL, @@ -154,9 +159,16 @@ function createRequestBody(body: any, method: RequestMethods) { headers["X-Audit-Log-Reason"] = encodeURIComponent(body.reason); } - if (body?.file) { + if (body.file) { + if (!Array.isArray(body.file)) body.file = [body.file]; + const form = new FormData(); - form.append("file", body.file.blob, body.file.name); + + body.file.map((file: FileContent, index: number) => + // The key of the form data item must be unique; otherwise, Discordeno only considers the first item in the form data with the same names + form.append(`file${index + 1}`, file.blob as Blob, file.name) + ); + form.append("payload_json", JSON.stringify({ ...body, file: undefined })); body.file = form; } else if ( diff --git a/src/types/channel.ts b/src/types/channel.ts index 4b1fcfed0..ad3202a95 100644 --- a/src/types/channel.ts +++ b/src/types/channel.ts @@ -111,7 +111,7 @@ export interface MessageContent { /** Whether this is a TextToSpeech message */ tts?: boolean; /** The contents of the file being sent */ - file?: { blob: unknown; name: string }; + file?: FileContent | FileContent[]; /** Embed object */ embed?: Embed; /** JSON encoded body of any additional request fields. */ @@ -120,6 +120,11 @@ export interface MessageContent { replyMessageID?: string; } +export interface FileContent { + blob: unknown; + name: string; +} + export interface AllowedMentions { /** An array of allowed mention types to parse from the content. */ parse: ("roles" | "users" | "everyone")[];