From 5e0a7d51fce3b67ba5a0e573fdc00a83693ad008 Mon Sep 17 00:00:00 2001 From: Jake Ward Date: Thu, 11 Nov 2021 19:29:17 +0000 Subject: [PATCH] feat(MessageAttachment): description (alt text) support (#6871) Co-authored-by: D Trombett --- src/rest/APIRequest.js | 4 ++-- src/structures/MessageAttachment.js | 20 +++++++++++++++++++ src/structures/MessagePayload.js | 10 ++++++++++ src/structures/interfaces/TextBasedChannel.js | 3 +++ typings/index.d.ts | 3 +++ 5 files changed, 38 insertions(+), 2 deletions(-) diff --git a/src/rest/APIRequest.js b/src/rest/APIRequest.js index 2ec64baa33db..fae614a3624d 100644 --- a/src/rest/APIRequest.js +++ b/src/rest/APIRequest.js @@ -50,8 +50,8 @@ class APIRequest { let body; if (this.options.files?.length) { body = new FormData(); - for (const file of this.options.files) { - if (file?.file) body.append(file.key ?? file.name, file.file, file.name); + for (const [index, file] of this.options.files.entries()) { + if (file?.file) body.append(file.key ?? `files[${index}]`, file.file, file.name); } if (typeof this.options.data !== 'undefined') { if (this.options.dontUsePayloadJSON) { diff --git a/src/structures/MessageAttachment.js b/src/structures/MessageAttachment.js index 15cef63c5d9e..79e87bf2b521 100644 --- a/src/structures/MessageAttachment.js +++ b/src/structures/MessageAttachment.js @@ -21,6 +21,16 @@ class MessageAttachment { if (data) this._patch(data); } + /** + * Sets the description of this attachment. + * @param {string} description The description of the file + * @returns {MessageAttachment} This attachment + */ + setDescription(description) { + this.description = description; + return this; + } + /** * Sets the file of this attachment. * @param {BufferResolvable|Stream} attachment The file @@ -122,6 +132,16 @@ class MessageAttachment { this.contentType ??= null; } + if ('description' in data) { + /** + * The description (alt text) of this attachment + * @type {?string} + */ + this.description = data.description; + } else { + this.description ??= null; + } + /** * Whether this attachment is ephemeral * @type {boolean} diff --git a/src/structures/MessagePayload.js b/src/structures/MessagePayload.js index db43b8a88622..ba12b16113c1 100644 --- a/src/structures/MessagePayload.js +++ b/src/structures/MessagePayload.js @@ -177,6 +177,16 @@ class MessagePayload { } } + const attachments = this.options.files?.map((file, index) => ({ + id: index.toString(), + description: file.description, + })); + if (Array.isArray(this.options.attachments)) { + this.options.attachments.push(...attachments); + } else { + this.options.attachments = attachments; + } + this.data = { content, tts, diff --git a/src/structures/interfaces/TextBasedChannel.js b/src/structures/interfaces/TextBasedChannel.js index 4dd0a0a74c27..2c6817c3fc10 100644 --- a/src/structures/interfaces/TextBasedChannel.js +++ b/src/structures/interfaces/TextBasedChannel.js @@ -96,6 +96,7 @@ class TextBasedChannel { * @typedef {Object} FileOptions * @property {BufferResolvable} attachment File to attach * @property {string} [name='file.jpg'] Filename of the attachment + * @property {string} description The description of the file */ /** @@ -128,6 +129,7 @@ class TextBasedChannel { * files: [{ * attachment: 'entire/path/to/file.jpg', * name: 'file.jpg' + * description: 'A description of the file' * }] * }) * .then(console.log) @@ -146,6 +148,7 @@ class TextBasedChannel { * files: [{ * attachment: 'entire/path/to/file.jpg', * name: 'file.jpg' + * description: 'A description of the file' * }] * }) * .then(console.log) diff --git a/typings/index.d.ts b/typings/index.d.ts index 2fe9558d8d2a..7f3bfe00e5de 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -1463,6 +1463,7 @@ export class MessageAttachment { public attachment: BufferResolvable | Stream; public contentType: string | null; + public description: string | null; public ephemeral: boolean; public height: number | null; public id: Snowflake; @@ -1472,6 +1473,7 @@ export class MessageAttachment { public readonly spoiler: boolean; public url: string; public width: number | null; + public setDescription(description: string): this; public setFile(attachment: BufferResolvable | Stream, name?: string): this; public setName(name: string): this; public setSpoiler(spoiler?: boolean): this; @@ -4118,6 +4120,7 @@ export interface FetchThreadsOptions { export interface FileOptions { attachment: BufferResolvable | Stream; name?: string; + description?: string; } export interface GuildApplicationCommandPermissionData {