buddy

node MVC discord bot
Log | Files | Refs | README

ClientPresence.js (3220B)


      1 'use strict';
      2 
      3 const { Presence } = require('./Presence');
      4 const { TypeError } = require('../errors');
      5 const Collection = require('../util/Collection');
      6 const { ActivityTypes, OPCodes } = require('../util/Constants');
      7 
      8 class ClientPresence extends Presence {
      9   /**
     10    * @param {Client} client The instantiating client
     11    * @param {Object} [data={}] The data for the client presence
     12    */
     13   constructor(client, data = {}) {
     14     super(client, Object.assign(data, { status: 'online', user: { id: null } }));
     15   }
     16 
     17   async set(presence) {
     18     const packet = await this._parse(presence);
     19     this.patch(packet);
     20     if (typeof presence.shardID === 'undefined') {
     21       this.client.ws.broadcast({ op: OPCodes.STATUS_UPDATE, d: packet });
     22     } else if (Array.isArray(presence.shardID)) {
     23       for (const shardID of presence.shardID) {
     24         this.client.ws.shards.get(shardID).send({ op: OPCodes.STATUS_UPDATE, d: packet });
     25       }
     26     } else {
     27       this.client.ws.shards.get(presence.shardID).send({ op: OPCodes.STATUS_UPDATE, d: packet });
     28     }
     29     return this;
     30   }
     31 
     32   async _parse({ status, since, afk, activity }) {
     33     const applicationID = activity && (activity.application ? activity.application.id || activity.application : null);
     34     let assets = new Collection();
     35     if (activity) {
     36       if (typeof activity.name !== 'string') throw new TypeError('INVALID_TYPE', 'name', 'string');
     37       if (!activity.type) activity.type = 0;
     38       if (activity.assets && applicationID) {
     39         try {
     40           const a = await this.client.api.oauth2.applications(applicationID).assets.get();
     41           for (const asset of a) assets.set(asset.name, asset.id);
     42         } catch {} // eslint-disable-line no-empty
     43       }
     44     }
     45 
     46     const packet = {
     47       afk: afk != null ? afk : false, // eslint-disable-line eqeqeq
     48       since: since != null ? since : null, // eslint-disable-line eqeqeq
     49       status: status || this.status,
     50       game: activity
     51         ? {
     52             type: activity.type,
     53             name: activity.name,
     54             url: activity.url,
     55             details: activity.details || undefined,
     56             state: activity.state || undefined,
     57             assets: activity.assets
     58               ? {
     59                   large_text: activity.assets.largeText || undefined,
     60                   small_text: activity.assets.smallText || undefined,
     61                   large_image: assets.get(activity.assets.largeImage) || activity.assets.largeImage,
     62                   small_image: assets.get(activity.assets.smallImage) || activity.assets.smallImage,
     63                 }
     64               : undefined,
     65             timestamps: activity.timestamps || undefined,
     66             party: activity.party || undefined,
     67             application_id: applicationID || undefined,
     68             secrets: activity.secrets || undefined,
     69             instance: activity.instance || undefined,
     70           }
     71         : null,
     72     };
     73 
     74     if ((status || afk || since) && !activity) {
     75       packet.game = this.activities[0] || null;
     76     }
     77 
     78     if (packet.game) {
     79       packet.game.type =
     80         typeof packet.game.type === 'number' ? packet.game.type : ActivityTypes.indexOf(packet.game.type);
     81     }
     82 
     83     return packet;
     84   }
     85 }
     86 
     87 module.exports = ClientPresence;