buddy

node MVC discord bot
Log | Files | Refs | README

DMChannel.js (2491B)


      1 'use strict';
      2 
      3 const Channel = require('./Channel');
      4 const TextBasedChannel = require('./interfaces/TextBasedChannel');
      5 const MessageManager = require('../managers/MessageManager');
      6 
      7 /**
      8  * Represents a direct message channel between two users.
      9  * @extends {Channel}
     10  * @implements {TextBasedChannel}
     11  */
     12 class DMChannel extends Channel {
     13   /**
     14    * @param {Client} client The instantiating client
     15    * @param {Object} data The data for the DM channel
     16    */
     17   constructor(client, data) {
     18     super(client, data);
     19     // Override the channel type so partials have a known type
     20     this.type = 'dm';
     21     /**
     22      * A manager of the messages belonging to this channel
     23      * @type {MessageManager}
     24      */
     25     this.messages = new MessageManager(this);
     26     this._typing = new Map();
     27   }
     28 
     29   _patch(data) {
     30     super._patch(data);
     31 
     32     if (data.recipients) {
     33       /**
     34        * The recipient on the other end of the DM
     35        * @type {User}
     36        */
     37       this.recipient = this.client.users.add(data.recipients[0]);
     38     }
     39 
     40     /**
     41      * The ID of the last message in the channel, if one was sent
     42      * @type {?Snowflake}
     43      */
     44     this.lastMessageID = data.last_message_id;
     45 
     46     /**
     47      * The timestamp when the last pinned message was pinned, if there was one
     48      * @type {?number}
     49      */
     50     this.lastPinTimestamp = data.last_pin_timestamp ? new Date(data.last_pin_timestamp).getTime() : null;
     51   }
     52 
     53   /**
     54    * Whether this DMChannel is a partial
     55    * @type {boolean}
     56    * @readonly
     57    */
     58   get partial() {
     59     return typeof this.lastMessageID === 'undefined';
     60   }
     61 
     62   /**
     63    * Fetch this DMChannel.
     64    * @returns {Promise<DMChannel>}
     65    */
     66   fetch() {
     67     return this.recipient.createDM();
     68   }
     69 
     70   /**
     71    * When concatenated with a string, this automatically returns the recipient's mention instead of the
     72    * DMChannel object.
     73    * @returns {string}
     74    * @example
     75    * // Logs: Hello from <@123456789012345678>!
     76    * console.log(`Hello from ${channel}!`);
     77    */
     78   toString() {
     79     return this.recipient.toString();
     80   }
     81 
     82   // These are here only for documentation purposes - they are implemented by TextBasedChannel
     83   /* eslint-disable no-empty-function */
     84   get lastMessage() {}
     85   get lastPinAt() {}
     86   send() {}
     87   startTyping() {}
     88   stopTyping() {}
     89   get typing() {}
     90   get typingCount() {}
     91   createMessageCollector() {}
     92   awaitMessages() {}
     93   // Doesn't work on DM channels; bulkDelete() {}
     94 }
     95 
     96 TextBasedChannel.applyToClass(DMChannel, true, ['bulkDelete']);
     97 
     98 module.exports = DMChannel;