buddy

node MVC discord bot
Log | Files | Refs | README

Integration.js (3727B)


      1 'use strict';
      2 
      3 const Base = require('./Base');
      4 
      5 /**
      6  * The information account for an integration
      7  * @typedef {Object} IntegrationAccount
      8  * @property {string} id The id of the account
      9  * @property {string} name The name of the account
     10  */
     11 
     12 /**
     13  *  Represents a guild integration.
     14  */
     15 class Integration extends Base {
     16   constructor(client, data, guild) {
     17     super(client);
     18 
     19     /**
     20      * The guild this integration belongs to
     21      * @type {Guild}
     22      */
     23     this.guild = guild;
     24 
     25     /**
     26      * The integration id
     27      * @type {Snowflake}
     28      */
     29     this.id = data.id;
     30 
     31     /**
     32      * The integration name
     33      * @type {string}
     34      */
     35     this.name = data.name;
     36 
     37     /**
     38      * The integration type (twitch, youtube, etc)
     39      * @type {string}
     40      */
     41     this.type = data.type;
     42 
     43     /**
     44      * Whether this integration is enabled
     45      * @type {boolean}
     46      */
     47     this.enabled = data.enabled;
     48 
     49     /**
     50      * Whether this integration is syncing
     51      * @type {boolean}
     52      */
     53     this.syncing = data.syncing;
     54 
     55     /**
     56      * The role that this integration uses for subscribers
     57      * @type {Role}
     58      */
     59     this.role = this.guild.roles.cache.get(data.role_id);
     60 
     61     /**
     62      * The user for this integration
     63      * @type {User}
     64      */
     65     this.user = this.client.users.add(data.user);
     66 
     67     /**
     68      * The account integration information
     69      * @type {IntegrationAccount}
     70      */
     71     this.account = data.account;
     72 
     73     /**
     74      * The last time this integration was last synced
     75      * @type {number}
     76      */
     77     this.syncedAt = data.synced_at;
     78     this._patch(data);
     79   }
     80 
     81   _patch(data) {
     82     /**
     83      * The behavior of expiring subscribers
     84      * @type {number}
     85      */
     86     this.expireBehavior = data.expire_behavior;
     87 
     88     /**
     89      * The grace period before expiring subscribers
     90      * @type {number}
     91      */
     92     this.expireGracePeriod = data.expire_grace_period;
     93   }
     94 
     95   /**
     96    * Sync this integration
     97    * @returns {Promise<Integration>}
     98    */
     99   sync() {
    100     this.syncing = true;
    101     return this.client.api
    102       .guilds(this.guild.id)
    103       .integrations(this.id)
    104       .post()
    105       .then(() => {
    106         this.syncing = false;
    107         this.syncedAt = Date.now();
    108         return this;
    109       });
    110   }
    111 
    112   /**
    113    * The data for editing an integration.
    114    * @typedef {Object} IntegrationEditData
    115    * @property {number} [expireBehavior] The new behaviour of expiring subscribers
    116    * @property {number} [expireGracePeriod] The new grace period before expiring subscribers
    117    */
    118 
    119   /**
    120    * Edits this integration.
    121    * @param {IntegrationEditData} data The data to edit this integration with
    122    * @param {string} reason Reason for editing this integration
    123    * @returns {Promise<Integration>}
    124    */
    125   edit(data, reason) {
    126     if ('expireBehavior' in data) {
    127       data.expire_behavior = data.expireBehavior;
    128       data.expireBehavior = null;
    129     }
    130     if ('expireGracePeriod' in data) {
    131       data.expire_grace_period = data.expireGracePeriod;
    132       data.expireGracePeriod = null;
    133     }
    134     // The option enable_emoticons is only available for Twitch at this moment
    135     return this.client.api
    136       .guilds(this.guild.id)
    137       .integrations(this.id)
    138       .patch({ data, reason })
    139       .then(() => {
    140         this._patch(data);
    141         return this;
    142       });
    143   }
    144 
    145   /**
    146    * Deletes this integration.
    147    * @returns {Promise<Integration>}
    148    * @param {string} [reason] Reason for deleting this integration
    149    */
    150   delete(reason) {
    151     return this.client.api
    152       .guilds(this.guild.id)
    153       .integrations(this.id)
    154       .delete({ reason })
    155       .then(() => this);
    156   }
    157 
    158   toJSON() {
    159     return super.toJSON({
    160       role: 'roleID',
    161       guild: 'guildID',
    162       user: 'userID',
    163     });
    164   }
    165 }
    166 
    167 module.exports = Integration;