buddy

node MVC discord bot
Log | Files | Refs | README

GuildPreview.js (3770B)


      1 'use strict';
      2 
      3 const Base = require('./Base');
      4 const GuildPreviewEmoji = require('./GuildPreviewEmoji');
      5 const Collection = require('../util/Collection');
      6 
      7 /**
      8  * Represents the data about the guild any bot can preview, connected to the specified public guild.
      9  * @extends {Base}
     10  */
     11 class GuildPreview extends Base {
     12   constructor(client, data) {
     13     super(client);
     14 
     15     if (!data) return;
     16 
     17     this._patch(data);
     18   }
     19 
     20   /**
     21    * Builds the public guild with the provided data.
     22    * @param {*} data The raw data of the public guild
     23    * @private
     24    */
     25   _patch(data) {
     26     /**
     27      * The id of this public guild
     28      * @type {string}
     29      */
     30     this.id = data.id;
     31 
     32     /**
     33      * The name of this public guild
     34      * @type {string}
     35      */
     36     this.name = data.name;
     37 
     38     /**
     39      * The icon of this public guild
     40      * @type {?string}
     41      */
     42     this.icon = data.icon;
     43 
     44     /**
     45      * The splash icon of this public guild
     46      * @type {?string}
     47      */
     48     this.splash = data.splash;
     49 
     50     /**
     51      * The discovery splash icon of this public guild
     52      * @type {?string}
     53      */
     54     this.discoverySplash = data.discovery_splash;
     55 
     56     /**
     57      * An array of enabled guild features
     58      * @type {Features[]}
     59      */
     60     this.features = data.features;
     61 
     62     /**
     63      * The approximate count of members in this public guild
     64      * @type {number}
     65      */
     66     this.approximateMemberCount = data.approximate_member_count;
     67 
     68     /**
     69      * The approximate count of online members in this public guild
     70      * @type {number}
     71      */
     72     this.approximatePresenceCount = data.approximate_presence_count;
     73 
     74     /**
     75      * The description for this public guild
     76      * @type {?string}
     77      */
     78     this.description = data.description;
     79 
     80     if (!this.emojis) {
     81       /**
     82        * Collection of emojis belonging to this public guild
     83        * @type {Collection<Snowflake, GuildPreviewEmoji>}
     84        */
     85       this.emojis = new Collection();
     86     } else {
     87       this.emojis.clear();
     88     }
     89     for (const emoji of data.emojis) {
     90       this.emojis.set(emoji.id, new GuildPreviewEmoji(this.client, emoji, this));
     91     }
     92   }
     93 
     94   /**
     95    * The URL to this public guild's splash.
     96    * @param {ImageURLOptions} [options={}] Options for the Image URL
     97    * @returns {?string}
     98    */
     99   splashURL({ format, size } = {}) {
    100     if (!this.splash) return null;
    101     return this.client.rest.cdn.Splash(this.id, this.splash, format, size);
    102   }
    103 
    104   /**
    105    * The URL to this public guild's discovery splash.
    106    * @param {ImageURLOptions} [options={}] Options for the Image URL
    107    * @returns {?string}
    108    */
    109   discoverySplashURL({ format, size } = {}) {
    110     if (!this.discoverySplash) return null;
    111     return this.client.rest.cdn.DiscoverySplash(this.id, this.discoverySplash, format, size);
    112   }
    113 
    114   /**
    115    * The URL to this public guild's icon.
    116    * @param {ImageURLOptions} [options={}] Options for the Image URL
    117    * @returns {?string}
    118    */
    119   iconURL({ format, size, dynamic } = {}) {
    120     if (!this.icon) return null;
    121     return this.client.rest.cdn.Icon(this.id, this.icon, format, size, dynamic);
    122   }
    123 
    124   /**
    125    * Fetches this public guild.
    126    * @returns {Promise<GuildPreview>}
    127    */
    128   fetch() {
    129     return this.client.api
    130       .guilds(this.id)
    131       .preview.get()
    132       .then(data => {
    133         this._patch(data);
    134         return this;
    135       });
    136   }
    137 
    138   /**
    139    * When concatenated with a string, this automatically returns the guild's name instead of the Guild object.
    140    * @returns {string}
    141    * @example
    142    * // Logs: Hello from My Guild!
    143    * console.log(`Hello from ${previewGuild}!`);
    144    */
    145   toString() {
    146     return this.name;
    147   }
    148 
    149   toJSON() {
    150     const json = super.toJSON();
    151     json.iconURL = this.iconURL();
    152     json.splashURL = this.splashURL();
    153     return json;
    154   }
    155 }
    156 
    157 module.exports = GuildPreview;