buddy

node MVC discord bot
Log | Files | Refs | README

ClientApplication.js (3681B)


      1 'use strict';
      2 
      3 const Base = require('./Base');
      4 const Team = require('./Team');
      5 const { ClientApplicationAssetTypes, Endpoints } = require('../util/Constants');
      6 const Snowflake = require('../util/Snowflake');
      7 
      8 const AssetTypes = Object.keys(ClientApplicationAssetTypes);
      9 
     10 /**
     11  * Represents a Client OAuth2 Application.
     12  * @extends {Base}
     13  */
     14 class ClientApplication extends Base {
     15   constructor(client, data) {
     16     super(client);
     17     this._patch(data);
     18   }
     19 
     20   _patch(data) {
     21     /**
     22      * The ID of the app
     23      * @type {Snowflake}
     24      */
     25     this.id = data.id;
     26 
     27     /**
     28      * The name of the app
     29      * @type {string}
     30      */
     31     this.name = data.name;
     32 
     33     /**
     34      * The app's description
     35      * @type {string}
     36      */
     37     this.description = data.description;
     38 
     39     /**
     40      * The app's icon hash
     41      * @type {string}
     42      */
     43     this.icon = data.icon;
     44 
     45     /**
     46      * The app's cover image
     47      * @type {?string}
     48      */
     49     this.cover = data.cover_image || null;
     50 
     51     /**
     52      * The app's RPC origins, if enabled
     53      * @type {string[]}
     54      */
     55     this.rpcOrigins = data.rpc_origins || [];
     56 
     57     /**
     58      * If this app's bot requires a code grant when using the OAuth2 flow
     59      * @type {?boolean}
     60      */
     61     this.botRequireCodeGrant = typeof data.bot_require_code_grant !== 'undefined' ? data.bot_require_code_grant : null;
     62 
     63     /**
     64      * If this app's bot is public
     65      * @type {?boolean}
     66      */
     67     this.botPublic = typeof data.bot_public !== 'undefined' ? data.bot_public : null;
     68 
     69     /**
     70      * The owner of this OAuth application
     71      * @type {?User|Team}
     72      */
     73     this.owner = data.team ? new Team(this.client, data.team) : data.owner ? this.client.users.add(data.owner) : null;
     74   }
     75 
     76   /**
     77    * The timestamp the app was created at
     78    * @type {number}
     79    * @readonly
     80    */
     81   get createdTimestamp() {
     82     return Snowflake.deconstruct(this.id).timestamp;
     83   }
     84 
     85   /**
     86    * The time the app was created at
     87    * @type {Date}
     88    * @readonly
     89    */
     90   get createdAt() {
     91     return new Date(this.createdTimestamp);
     92   }
     93 
     94   /**
     95    * A link to the application's icon.
     96    * @param {ImageURLOptions} [options={}] Options for the Image URL
     97    * @returns {?string} URL to the icon
     98    */
     99   iconURL({ format, size } = {}) {
    100     if (!this.icon) return null;
    101     return this.client.rest.cdn.AppIcon(this.id, this.icon, { format, size });
    102   }
    103 
    104   /**
    105    * A link to this application's cover image.
    106    * @param {ImageURLOptions} [options={}] Options for the Image URL
    107    * @returns {?string} URL to the cover image
    108    */
    109   coverImage({ format, size } = {}) {
    110     if (!this.cover) return null;
    111     return Endpoints.CDN(this.client.options.http.cdn).AppIcon(this.id, this.cover, { format, size });
    112   }
    113 
    114   /**
    115    * Asset data.
    116    * @typedef {Object} ClientAsset
    117    * @property {Snowflake} id The asset ID
    118    * @property {string} name The asset name
    119    * @property {string} type The asset type
    120    */
    121 
    122   /**
    123    * Gets the clients rich presence assets.
    124    * @returns {Promise<Array<ClientAsset>>}
    125    */
    126   fetchAssets() {
    127     return this.client.api.oauth2
    128       .applications(this.id)
    129       .assets.get()
    130       .then(assets =>
    131         assets.map(a => ({
    132           id: a.id,
    133           name: a.name,
    134           type: AssetTypes[a.type - 1],
    135         })),
    136       );
    137   }
    138 
    139   /**
    140    * When concatenated with a string, this automatically returns the application's name instead of the
    141    * ClientApplication object.
    142    * @returns {string}
    143    * @example
    144    * // Logs: Application name: My App
    145    * console.log(`Application name: ${application}`);
    146    */
    147   toString() {
    148     return this.name;
    149   }
    150 
    151   toJSON() {
    152     return super.toJSON({ createdTimestamp: true });
    153   }
    154 }
    155 
    156 module.exports = ClientApplication;