From 96e26c428d7745b14908d56a96b635226471d699 Mon Sep 17 00:00:00 2001 From: Justin Date: Wed, 25 Aug 2021 04:25:45 +0800 Subject: [PATCH] feat(MessageAttachment): allow files to be marked as spoilers (#6509) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Antonio Román --- src/structures/MessageAttachment.js | 20 +++++++++++++++++++- typings/index.d.ts | 1 + 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/structures/MessageAttachment.js b/src/structures/MessageAttachment.js index 6bac7a70a..1fef93f17 100644 --- a/src/structures/MessageAttachment.js +++ b/src/structures/MessageAttachment.js @@ -43,6 +43,24 @@ class MessageAttachment { return this; } + /** + * Sets whether this attachment is a spoiler + * @param {boolean} [spoiler=true] Whether the attachment should be marked as a spoiler + * @returns {MessageAttachment} This attachment + */ + setSpoiler(spoiler = true) { + if (spoiler === this.spoiler) return this; + + if (!spoiler) { + while (this.spoiler) { + this.name.slice('SPOILER_'.length); + } + return this; + } + this.name = `SPOILER_${this.name}`; + return this; + } + _patch(data) { /** * The attachment's id @@ -93,7 +111,7 @@ class MessageAttachment { * @readonly */ get spoiler() { - return Util.basename(this.url).startsWith('SPOILER_'); + return Util.basename(this.url ?? this.name).startsWith('SPOILER_'); } toJSON() { diff --git a/typings/index.d.ts b/typings/index.d.ts index 0488c8165..724c4901e 100644 --- a/typings/index.d.ts +++ b/typings/index.d.ts @@ -1210,6 +1210,7 @@ export class MessageAttachment { public width: number | null; public setFile(attachment: BufferResolvable | Stream, name?: string): this; public setName(name: string): this; + public setSpoiler(spoiler?: boolean): this; public toJSON(): unknown; }