buddy

node MVC discord bot
Log | Files | Refs | README

Invite.js (4813B)


      1 'use strict';
      2 
      3 const Base = require('./Base');
      4 const { Endpoints } = require('../util/Constants');
      5 const Permissions = require('../util/Permissions');
      6 
      7 /**
      8  * Represents an invitation to a guild channel.
      9  * <warn>The only guaranteed properties are `code`, `channel`, and `url`. Other properties can be missing.</warn>
     10  * @extends {Base}
     11  */
     12 class Invite extends Base {
     13   constructor(client, data) {
     14     super(client);
     15     this._patch(data);
     16   }
     17 
     18   _patch(data) {
     19     /**
     20      * The guild the invite is for
     21      * @type {?Guild}
     22      */
     23     this.guild = data.guild ? this.client.guilds.add(data.guild, false) : null;
     24 
     25     /**
     26      * The code for this invite
     27      * @type {string}
     28      */
     29     this.code = data.code;
     30 
     31     /**
     32      * The approximate number of online members of the guild this invite is for
     33      * @type {?number}
     34      */
     35     this.presenceCount = 'approximate_presence_count' in data ? data.approximate_presence_count : null;
     36 
     37     /**
     38      * The approximate total number of members of the guild this invite is for
     39      * @type {?number}
     40      */
     41     this.memberCount = 'approximate_member_count' in data ? data.approximate_member_count : null;
     42 
     43     /**
     44      * Whether or not this invite is temporary
     45      * @type {?boolean}
     46      */
     47     this.temporary = 'temporary' in data ? data.temporary : null;
     48 
     49     /**
     50      * The maximum age of the invite, in seconds, 0 if never expires
     51      * @type {?number}
     52      */
     53     this.maxAge = 'max_age' in data ? data.max_age : null;
     54 
     55     /**
     56      * How many times this invite has been used
     57      * @type {?number}
     58      */
     59     this.uses = 'uses' in data ? data.uses : null;
     60 
     61     /**
     62      * The maximum uses of this invite
     63      * @type {?number}
     64      */
     65     this.maxUses = 'max_uses' in data ? data.max_uses : null;
     66 
     67     /**
     68      * The user who created this invite
     69      * @type {?User}
     70      */
     71     this.inviter = data.inviter ? this.client.users.add(data.inviter) : null;
     72 
     73     /**
     74      * The target user for this invite
     75      * @type {?User}
     76      */
     77     this.targetUser = data.target_user ? this.client.users.add(data.target_user) : null;
     78 
     79     /**
     80      * The type of the target user:
     81      * * 1: STREAM
     82      * @typedef {number} TargetUser
     83      */
     84 
     85     /**
     86      * The target user type
     87      * @type {?TargetUser}
     88      */
     89     this.targetUserType = typeof data.target_user_type === 'number' ? data.target_user_type : null;
     90 
     91     /**
     92      * The channel the invite is for
     93      * @type {Channel}
     94      */
     95     this.channel = this.client.channels.add(data.channel, this.guild, false);
     96 
     97     /**
     98      * The timestamp the invite was created at
     99      * @type {?number}
    100      */
    101     this.createdTimestamp = 'created_at' in data ? new Date(data.created_at).getTime() : null;
    102   }
    103 
    104   /**
    105    * The time the invite was created at
    106    * @type {?Date}
    107    * @readonly
    108    */
    109   get createdAt() {
    110     return this.createdTimestamp ? new Date(this.createdTimestamp) : null;
    111   }
    112 
    113   /**
    114    * Whether the invite is deletable by the client user
    115    * @type {boolean}
    116    * @readonly
    117    */
    118   get deletable() {
    119     const guild = this.guild;
    120     if (!guild || !this.client.guilds.cache.has(guild.id)) return false;
    121     if (!guild.me) throw new Error('GUILD_UNCACHED_ME');
    122     return (
    123       this.channel.permissionsFor(this.client.user).has(Permissions.FLAGS.MANAGE_CHANNELS, false) ||
    124       guild.me.permissions.has(Permissions.FLAGS.MANAGE_GUILD)
    125     );
    126   }
    127 
    128   /**
    129    * The timestamp the invite will expire at
    130    * @type {?number}
    131    * @readonly
    132    */
    133   get expiresTimestamp() {
    134     return this.createdTimestamp && this.maxAge ? this.createdTimestamp + this.maxAge * 1000 : null;
    135   }
    136 
    137   /**
    138    * The time the invite will expire at
    139    * @type {?Date}
    140    * @readonly
    141    */
    142   get expiresAt() {
    143     const { expiresTimestamp } = this;
    144     return expiresTimestamp ? new Date(expiresTimestamp) : null;
    145   }
    146 
    147   /**
    148    * The URL to the invite
    149    * @type {string}
    150    * @readonly
    151    */
    152   get url() {
    153     return Endpoints.invite(this.client.options.http.invite, this.code);
    154   }
    155 
    156   /**
    157    * Deletes this invite.
    158    * @param {string} [reason] Reason for deleting this invite
    159    * @returns {Promise<Invite>}
    160    */
    161   delete(reason) {
    162     return this.client.api.invites[this.code].delete({ reason }).then(() => this);
    163   }
    164 
    165   /**
    166    * When concatenated with a string, this automatically concatenates the invite's URL instead of the object.
    167    * @returns {string}
    168    * @example
    169    * // Logs: Invite: https://discord.gg/A1b2C3
    170    * console.log(`Invite: ${invite}`);
    171    */
    172   toString() {
    173     return this.url;
    174   }
    175 
    176   toJSON() {
    177     return super.toJSON({
    178       url: true,
    179       expiresTimestamp: true,
    180       presenceCount: false,
    181       memberCount: false,
    182       uses: false,
    183       channel: 'channelID',
    184       inviter: 'inviterID',
    185       guild: 'guildID',
    186     });
    187   }
    188 
    189   valueOf() {
    190     return this.code;
    191   }
    192 }
    193 
    194 module.exports = Invite;