buddy

node MVC discord bot
Log | Files | Refs | README

Team.js (2230B)


      1 'use strict';
      2 
      3 const Base = require('./Base');
      4 const TeamMember = require('./TeamMember');
      5 const Collection = require('../util/Collection');
      6 const Snowflake = require('../util/Snowflake');
      7 
      8 /**
      9  * Represents a Client OAuth2 Application Team.
     10  * @extends {Base}
     11  */
     12 class Team extends Base {
     13   constructor(client, data) {
     14     super(client);
     15     this._patch(data);
     16   }
     17 
     18   _patch(data) {
     19     /**
     20      * The ID of the Team
     21      * @type {Snowflake}
     22      */
     23     this.id = data.id;
     24 
     25     /**
     26      * The name of the Team
     27      * @type {string}
     28      */
     29     this.name = data.name;
     30 
     31     /**
     32      * The Team's icon hash
     33      * @type {?string}
     34      */
     35     this.icon = data.icon || null;
     36 
     37     /**
     38      * The Team's owner id
     39      * @type {?string}
     40      */
     41     this.ownerID = data.owner_user_id || null;
     42 
     43     /**
     44      * The Team's members
     45      * @type {Collection<Snowflake, TeamMember>}
     46      */
     47     this.members = new Collection();
     48 
     49     for (const memberData of data.members) {
     50       const member = new TeamMember(this, memberData);
     51       this.members.set(member.id, member);
     52     }
     53   }
     54 
     55   /**
     56    * The owner of this team
     57    * @type {?TeamMember}
     58    * @readonly
     59    */
     60   get owner() {
     61     return this.members.get(this.ownerID) || null;
     62   }
     63 
     64   /**
     65    * The timestamp the team was created at
     66    * @type {number}
     67    * @readonly
     68    */
     69   get createdTimestamp() {
     70     return Snowflake.deconstruct(this.id).timestamp;
     71   }
     72 
     73   /**
     74    * The time the team was created at
     75    * @type {Date}
     76    * @readonly
     77    */
     78   get createdAt() {
     79     return new Date(this.createdTimestamp);
     80   }
     81 
     82   /**
     83    * A link to the teams's icon.
     84    * @param {ImageURLOptions} [options={}] Options for the Image URL
     85    * @returns {?string} URL to the icon
     86    */
     87   iconURL({ format, size } = {}) {
     88     if (!this.icon) return null;
     89     return this.client.rest.cdn.TeamIcon(this.id, this.icon, { format, size });
     90   }
     91 
     92   /**
     93    * When concatenated with a string, this automatically returns the Team's name instead of the
     94    * Team object.
     95    * @returns {string}
     96    * @example
     97    * // Logs: Team name: My Team
     98    * console.log(`Team name: ${team}`);
     99    */
    100   toString() {
    101     return this.name;
    102   }
    103 
    104   toJSON() {
    105     return super.toJSON({ createdTimestamp: true });
    106   }
    107 }
    108 
    109 module.exports = Team;