2022-03-19 10:37:45 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const { Collection } = require('@discordjs/collection');
|
2022-04-16 12:01:05 +00:00
|
|
|
const Discord = require('discord.js-selfbot-v13');
|
2022-03-19 10:37:45 +00:00
|
|
|
const CachedManager = require('./CachedManager');
|
|
|
|
const { Error } = require('../errors');
|
|
|
|
/**
|
|
|
|
* Manages API methods for users who reacted to a reaction and stores their cache.
|
|
|
|
* @extends {CachedManager}
|
|
|
|
*/
|
|
|
|
class ReactionUserManager extends CachedManager {
|
|
|
|
constructor(reaction, iterable) {
|
2022-04-11 02:12:35 +00:00
|
|
|
super(reaction.client, Discord.User, iterable);
|
2022-03-19 10:37:45 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The reaction that this manager belongs to
|
|
|
|
* @type {MessageReaction}
|
|
|
|
*/
|
|
|
|
this.reaction = reaction;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The cache of this manager
|
2022-04-11 02:12:35 +00:00
|
|
|
* @type {Collection<Snowflake, Discord.User>}
|
2022-03-19 10:37:45 +00:00
|
|
|
* @name ReactionUserManager#cache
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Options used to fetch users who gave a reaction.
|
|
|
|
* @typedef {Object} FetchReactionUsersOptions
|
|
|
|
* @property {number} [limit=100] The maximum amount of users to fetch, defaults to `100`
|
|
|
|
* @property {Snowflake} [after] Limit fetching users to those with an id greater than the supplied id
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches all the users that gave this reaction. Resolves with a collection of users, mapped by their ids.
|
|
|
|
* @param {FetchReactionUsersOptions} [options] Options for fetching the users
|
2022-04-11 02:12:35 +00:00
|
|
|
* @returns {Promise<Collection<Snowflake, Discord.User>>}
|
2022-03-19 10:37:45 +00:00
|
|
|
*/
|
|
|
|
async fetch({ limit = 100, after } = {}) {
|
|
|
|
const message = this.reaction.message;
|
2022-03-24 10:55:32 +00:00
|
|
|
const data = await this.client.api.channels[message.channelId].messages[message.id].reactions[
|
|
|
|
this.reaction.emoji.identifier
|
|
|
|
].get({ query: { limit, after } });
|
2022-03-19 10:37:45 +00:00
|
|
|
const users = new Collection();
|
|
|
|
for (const rawUser of data) {
|
|
|
|
const user = this.client.users._add(rawUser);
|
|
|
|
this.cache.set(user.id, user);
|
|
|
|
users.set(user.id, user);
|
|
|
|
}
|
|
|
|
return users;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a user from this reaction.
|
|
|
|
* @param {UserResolvable} [user=this.client.user] The user to remove the reaction of
|
|
|
|
* @returns {Promise<MessageReaction>}
|
|
|
|
*/
|
|
|
|
async remove(user = this.client.user) {
|
|
|
|
const userId = this.client.users.resolveId(user);
|
|
|
|
if (!userId) throw new Error('REACTION_RESOLVE_USER');
|
|
|
|
const message = this.reaction.message;
|
2022-03-24 10:55:32 +00:00
|
|
|
await this.client.api.channels[message.channelId].messages[message.id].reactions[this.reaction.emoji.identifier][
|
|
|
|
userId === this.client.user.id ? '@me' : userId
|
|
|
|
].delete();
|
2022-03-19 10:37:45 +00:00
|
|
|
return this.reaction;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ReactionUserManager;
|