buddy

node MVC discord bot
Log | Files | Refs | README

MessageManager.js (5166B)


      1 'use strict';
      2 
      3 const BaseManager = require('./BaseManager');
      4 const Message = require('../structures/Message');
      5 const Collection = require('../util/Collection');
      6 const LimitedCollection = require('../util/LimitedCollection');
      7 
      8 /**
      9  * Manages API methods for Messages and holds their cache.
     10  * @extends {BaseManager}
     11  */
     12 class MessageManager extends BaseManager {
     13   constructor(channel, iterable) {
     14     super(channel.client, iterable, Message, LimitedCollection, channel.client.options.messageCacheMaxSize);
     15     /**
     16      * The channel that the messages belong to
     17      * @type {TextBasedChannel}
     18      */
     19     this.channel = channel;
     20   }
     21 
     22   /**
     23    * The cache of Messages
     24    * @type {Collection<Snowflake, Message>}
     25    * @name MessageManager#cache
     26    */
     27 
     28   add(data, cache) {
     29     return super.add(data, cache, { extras: [this.channel] });
     30   }
     31 
     32   /**
     33    * The parameters to pass in when requesting previous messages from a channel. `around`, `before` and
     34    * `after` are mutually exclusive. All the parameters are optional.
     35    * @typedef {Object} ChannelLogsQueryOptions
     36    * @property {number} [limit=50] Number of messages to acquire
     37    * @property {Snowflake} [before] ID of a message to get the messages that were posted before it
     38    * @property {Snowflake} [after] ID of a message to get the messages that were posted after it
     39    * @property {Snowflake} [around] ID of a message to get the messages that were posted around it
     40    */
     41 
     42   /**
     43    * Gets a message, or messages, from this channel.
     44    * <info>The returned Collection does not contain reaction users of the messages if they were not cached.
     45    * Those need to be fetched separately in such a case.</info>
     46    * @param {Snowflake|ChannelLogsQueryOptions} [message] The ID of the message to fetch, or query parameters.
     47    * @param {boolean} [cache=true] Whether to cache the message(s)
     48    * @returns {Promise<Message>|Promise<Collection<Snowflake, Message>>}
     49    * @example
     50    * // Get message
     51    * channel.messages.fetch('99539446449315840')
     52    *   .then(message => console.log(message.content))
     53    *   .catch(console.error);
     54    * @example
     55    * // Get messages
     56    * channel.messages.fetch({ limit: 10 })
     57    *   .then(messages => console.log(`Received ${messages.size} messages`))
     58    *   .catch(console.error);
     59    * @example
     60    * // Get messages and filter by user ID
     61    * channel.messages.fetch()
     62    *   .then(messages => console.log(`${messages.filter(m => m.author.id === '84484653687267328').size} messages`))
     63    *   .catch(console.error);
     64    */
     65   fetch(message, cache = true) {
     66     return typeof message === 'string' ? this._fetchId(message, cache) : this._fetchMany(message, cache);
     67   }
     68 
     69   /**
     70    * Fetches the pinned messages of this channel and returns a collection of them.
     71    * <info>The returned Collection does not contain any reaction data of the messages.
     72    * Those need to be fetched separately.</info>
     73    * @param {boolean} [cache=true] Whether to cache the message(s)
     74    * @returns {Promise<Collection<Snowflake, Message>>}
     75    * @example
     76    * // Get pinned messages
     77    * channel.fetchPinned()
     78    *   .then(messages => console.log(`Received ${messages.size} messages`))
     79    *   .catch(console.error);
     80    */
     81   fetchPinned(cache = true) {
     82     return this.client.api.channels[this.channel.id].pins.get().then(data => {
     83       const messages = new Collection();
     84       for (const message of data) messages.set(message.id, this.add(message, cache));
     85       return messages;
     86     });
     87   }
     88 
     89   /**
     90    * Data that can be resolved to a Message object. This can be:
     91    * * A Message
     92    * * A Snowflake
     93    * @typedef {Message|Snowflake} MessageResolvable
     94    */
     95 
     96   /**
     97    * Resolves a MessageResolvable to a Message object.
     98    * @method resolve
     99    * @memberof MessageManager
    100    * @instance
    101    * @param {MessageResolvable} message The message resolvable to resolve
    102    * @returns {?Message}
    103    */
    104 
    105   /**
    106    * Resolves a MessageResolvable to a Message ID string.
    107    * @method resolveID
    108    * @memberof MessageManager
    109    * @instance
    110    * @param {MessageResolvable} message The message resolvable to resolve
    111    * @returns {?Snowflake}
    112    */
    113 
    114   /**
    115    * Deletes a message, even if it's not cached.
    116    * @param {MessageResolvable} message The message to delete
    117    * @param {string} [reason] Reason for deleting this message, if it does not belong to the client user
    118    * @returns {Promise<void>}
    119    */
    120   async delete(message, reason) {
    121     message = this.resolveID(message);
    122     if (message) {
    123       await this.client.api
    124         .channels(this.channel.id)
    125         .messages(message)
    126         .delete({ reason });
    127     }
    128   }
    129 
    130   async _fetchId(messageID, cache) {
    131     const existing = this.cache.get(messageID);
    132     if (existing && !existing.partial) return existing;
    133     const data = await this.client.api.channels[this.channel.id].messages[messageID].get();
    134     return this.add(data, cache);
    135   }
    136 
    137   async _fetchMany(options = {}, cache) {
    138     const data = await this.client.api.channels[this.channel.id].messages.get({ query: options });
    139     const messages = new Collection();
    140     for (const message of data) messages.set(message.id, this.add(message, cache));
    141     return messages;
    142   }
    143 }
    144 
    145 module.exports = MessageManager;