buddy

node MVC discord bot
Log | Files | Refs | README

MessageAttachment.js (2132B)


      1 'use strict';
      2 
      3 const Util = require('../util/Util');
      4 
      5 /**
      6  * Represents an attachment in a message.
      7  */
      8 class MessageAttachment {
      9   /**
     10    * @param {BufferResolvable|Stream} attachment The file
     11    * @param {string} [name=null] The name of the file, if any
     12    * @param {Object} [data] Extra data
     13    */
     14   constructor(attachment, name = null, data) {
     15     this.attachment = attachment;
     16     /**
     17      * The name of this attachment
     18      * @type {?string}
     19      */
     20     this.name = name;
     21     if (data) this._patch(data);
     22   }
     23 
     24   /**
     25    * Sets the file of this attachment.
     26    * @param {BufferResolvable|Stream} attachment The file
     27    * @param {string} [name=null] The name of the file, if any
     28    * @returns {MessageAttachment} This attachment
     29    */
     30   setFile(attachment, name = null) {
     31     this.attachment = attachment;
     32     this.name = name;
     33     return this;
     34   }
     35 
     36   /**
     37    * Sets the name of this attachment.
     38    * @param {string} name The name of the file
     39    * @returns {MessageAttachment} This attachment
     40    */
     41   setName(name) {
     42     this.name = name;
     43     return this;
     44   }
     45 
     46   _patch(data) {
     47     /**
     48      * The ID of this attachment
     49      * @type {Snowflake}
     50      */
     51     this.id = data.id;
     52 
     53     /**
     54      * The size of this attachment in bytes
     55      * @type {number}
     56      */
     57     this.size = data.size;
     58 
     59     /**
     60      * The URL to this attachment
     61      * @type {string}
     62      */
     63     this.url = data.url;
     64 
     65     /**
     66      * The Proxy URL to this attachment
     67      * @type {string}
     68      */
     69     this.proxyURL = data.proxy_url;
     70 
     71     /**
     72      * The height of this attachment (if an image or video)
     73      * @type {?number}
     74      */
     75     this.height = typeof data.height !== 'undefined' ? data.height : null;
     76 
     77     /**
     78      * The width of this attachment (if an image or video)
     79      * @type {?number}
     80      */
     81     this.width = typeof data.width !== 'undefined' ? data.width : null;
     82   }
     83 
     84   /**
     85    * Whether or not this attachment has been marked as a spoiler
     86    * @type {boolean}
     87    * @readonly
     88    */
     89   get spoiler() {
     90     return Util.basename(this.url).startsWith('SPOILER_');
     91   }
     92 
     93   toJSON() {
     94     return Util.flatten(this);
     95   }
     96 }
     97 
     98 module.exports = MessageAttachment;