buddy

node MVC discord bot
Log | Files | Refs | README

ReactionUserManager.js (2420B)


      1 'use strict';
      2 
      3 const BaseManager = require('./BaseManager');
      4 const { Error } = require('../errors');
      5 const Collection = require('../util/Collection');
      6 
      7 /**
      8  * Manages API methods for users who reacted to a reaction and stores their cache.
      9  * @extends {BaseManager}
     10  */
     11 class ReactionUserManager extends BaseManager {
     12   constructor(client, iterable, reaction) {
     13     super(client, iterable, { name: 'User' });
     14     /**
     15      * The reaction that this manager belongs to
     16      * @type {MessageReaction}
     17      */
     18     this.reaction = reaction;
     19   }
     20 
     21   /**
     22    * The cache of this manager
     23    * @type {Collection<Snowflake, User>}
     24    * @name ReactionUserManager#cache
     25    */
     26 
     27   /**
     28    * Fetches all the users that gave this reaction. Resolves with a collection of users, mapped by their IDs.
     29    * @param {Object} [options] Options for fetching the users
     30    * @param {number} [options.limit=100] The maximum amount of users to fetch, defaults to 100
     31    * @param {Snowflake} [options.before] Limit fetching users to those with an id lower than the supplied id
     32    * @param {Snowflake} [options.after] Limit fetching users to those with an id greater than the supplied id
     33    * @returns {Promise<Collection<Snowflake, User>>}
     34    */
     35   async fetch({ limit = 100, after, before } = {}) {
     36     const message = this.reaction.message;
     37     const data = await this.client.api.channels[message.channel.id].messages[message.id].reactions[
     38       this.reaction.emoji.identifier
     39     ].get({ query: { limit, before, after } });
     40     const users = new Collection();
     41     for (const rawUser of data) {
     42       const user = this.client.users.add(rawUser);
     43       this.cache.set(user.id, user);
     44       users.set(user.id, user);
     45     }
     46     return users;
     47   }
     48 
     49   /**
     50    * Removes a user from this reaction.
     51    * @param {UserResolvable} [user=this.reaction.message.client.user] The user to remove the reaction of
     52    * @returns {Promise<MessageReaction>}
     53    */
     54   remove(user = this.reaction.message.client.user) {
     55     const message = this.reaction.message;
     56     const userID = message.client.users.resolveID(user);
     57     if (!userID) return Promise.reject(new Error('REACTION_RESOLVE_USER'));
     58     return message.client.api.channels[message.channel.id].messages[message.id].reactions[
     59       this.reaction.emoji.identifier
     60     ][userID === message.client.user.id ? '@me' : userID]
     61       .delete()
     62       .then(() => this.reaction);
     63   }
     64 }
     65 
     66 module.exports = ReactionUserManager;