feat(MessageAttachment): description (alt text) support (#6871)

Co-authored-by: D Trombett <maxtromb.dt@gmail.com>
This commit is contained in:
Jake Ward
2021-11-11 19:29:17 +00:00
committed by GitHub
parent 7630158f59
commit 5e0a7d51fc
5 changed files with 38 additions and 2 deletions

View File

@@ -50,8 +50,8 @@ class APIRequest {
let body; let body;
if (this.options.files?.length) { if (this.options.files?.length) {
body = new FormData(); body = new FormData();
for (const file of this.options.files) { for (const [index, file] of this.options.files.entries()) {
if (file?.file) body.append(file.key ?? file.name, file.file, file.name); if (file?.file) body.append(file.key ?? `files[${index}]`, file.file, file.name);
} }
if (typeof this.options.data !== 'undefined') { if (typeof this.options.data !== 'undefined') {
if (this.options.dontUsePayloadJSON) { if (this.options.dontUsePayloadJSON) {

View File

@@ -21,6 +21,16 @@ class MessageAttachment {
if (data) this._patch(data); 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. * Sets the file of this attachment.
* @param {BufferResolvable|Stream} attachment The file * @param {BufferResolvable|Stream} attachment The file
@@ -122,6 +132,16 @@ class MessageAttachment {
this.contentType ??= null; 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 * Whether this attachment is ephemeral
* @type {boolean} * @type {boolean}

View File

@@ -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 = { this.data = {
content, content,
tts, tts,

View File

@@ -96,6 +96,7 @@ class TextBasedChannel {
* @typedef {Object} FileOptions * @typedef {Object} FileOptions
* @property {BufferResolvable} attachment File to attach * @property {BufferResolvable} attachment File to attach
* @property {string} [name='file.jpg'] Filename of the attachment * @property {string} [name='file.jpg'] Filename of the attachment
* @property {string} description The description of the file
*/ */
/** /**
@@ -128,6 +129,7 @@ class TextBasedChannel {
* files: [{ * files: [{
* attachment: 'entire/path/to/file.jpg', * attachment: 'entire/path/to/file.jpg',
* name: 'file.jpg' * name: 'file.jpg'
* description: 'A description of the file'
* }] * }]
* }) * })
* .then(console.log) * .then(console.log)
@@ -146,6 +148,7 @@ class TextBasedChannel {
* files: [{ * files: [{
* attachment: 'entire/path/to/file.jpg', * attachment: 'entire/path/to/file.jpg',
* name: 'file.jpg' * name: 'file.jpg'
* description: 'A description of the file'
* }] * }]
* }) * })
* .then(console.log) * .then(console.log)

3
typings/index.d.ts vendored
View File

@@ -1463,6 +1463,7 @@ export class MessageAttachment {
public attachment: BufferResolvable | Stream; public attachment: BufferResolvable | Stream;
public contentType: string | null; public contentType: string | null;
public description: string | null;
public ephemeral: boolean; public ephemeral: boolean;
public height: number | null; public height: number | null;
public id: Snowflake; public id: Snowflake;
@@ -1472,6 +1473,7 @@ export class MessageAttachment {
public readonly spoiler: boolean; public readonly spoiler: boolean;
public url: string; public url: string;
public width: number | null; public width: number | null;
public setDescription(description: string): this;
public setFile(attachment: BufferResolvable | Stream, name?: string): this; public setFile(attachment: BufferResolvable | Stream, name?: string): this;
public setName(name: string): this; public setName(name: string): this;
public setSpoiler(spoiler?: boolean): this; public setSpoiler(spoiler?: boolean): this;
@@ -4118,6 +4120,7 @@ export interface FetchThreadsOptions {
export interface FileOptions { export interface FileOptions {
attachment: BufferResolvable | Stream; attachment: BufferResolvable | Stream;
name?: string; name?: string;
description?: string;
} }
export interface GuildApplicationCommandPermissionData { export interface GuildApplicationCommandPermissionData {