2022-03-19 10:37:45 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const GuildEmoji = require('./GuildEmoji');
|
|
|
|
const ReactionEmoji = require('./ReactionEmoji');
|
|
|
|
const ReactionUserManager = require('../managers/ReactionUserManager');
|
|
|
|
const Util = require('../util/Util');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents a reaction to a message.
|
|
|
|
*/
|
|
|
|
class MessageReaction {
|
|
|
|
constructor(client, data, message) {
|
|
|
|
/**
|
|
|
|
* The client that instantiated this message reaction
|
|
|
|
* @name MessageReaction#client
|
|
|
|
* @type {Client}
|
|
|
|
* @readonly
|
|
|
|
*/
|
|
|
|
Object.defineProperty(this, 'client', { value: client });
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The message that this reaction refers to
|
|
|
|
* @type {Message}
|
|
|
|
*/
|
|
|
|
this.message = message;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the client has given this reaction
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
this.me = data.me;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A manager of the users that have given this reaction
|
|
|
|
* @type {ReactionUserManager}
|
|
|
|
*/
|
|
|
|
this.users = new ReactionUserManager(this, this.me ? [client.user] : []);
|
|
|
|
|
|
|
|
this._emoji = new ReactionEmoji(this, data.emoji);
|
|
|
|
|
|
|
|
this._patch(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
_patch(data) {
|
|
|
|
if ('count' in data) {
|
|
|
|
/**
|
|
|
|
* The number of people that have given the same reaction
|
|
|
|
* @type {?number}
|
|
|
|
*/
|
|
|
|
this.count ??= data.count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes all users from this reaction.
|
|
|
|
* @returns {Promise<MessageReaction>}
|
|
|
|
*/
|
|
|
|
async remove() {
|
2022-03-24 10:55:32 +00:00
|
|
|
await this.client.api
|
|
|
|
.channels(this.message.channelId)
|
|
|
|
.messages(this.message.id)
|
|
|
|
.reactions(this._emoji.identifier)
|
|
|
|
.delete();
|
2022-03-19 10:37:45 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The emoji of this reaction. Either a {@link GuildEmoji} object for known custom emojis, or a {@link ReactionEmoji}
|
|
|
|
* object which has fewer properties. Whatever the prototype of the emoji, it will still have
|
|
|
|
* `name`, `id`, `identifier` and `toString()`
|
|
|
|
* @type {GuildEmoji|ReactionEmoji}
|
|
|
|
* @readonly
|
|
|
|
*/
|
|
|
|
get emoji() {
|
|
|
|
if (this._emoji instanceof GuildEmoji) return this._emoji;
|
|
|
|
// Check to see if the emoji has become known to the client
|
|
|
|
if (this._emoji.id) {
|
|
|
|
const emojis = this.message.client.emojis.cache;
|
|
|
|
if (emojis.has(this._emoji.id)) {
|
|
|
|
const emoji = emojis.get(this._emoji.id);
|
|
|
|
this._emoji = emoji;
|
|
|
|
return emoji;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this._emoji;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether or not this reaction is a partial
|
|
|
|
* @type {boolean}
|
|
|
|
* @readonly
|
|
|
|
*/
|
|
|
|
get partial() {
|
|
|
|
return this.count === null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetch this reaction.
|
|
|
|
* @returns {Promise<MessageReaction>}
|
|
|
|
*/
|
|
|
|
async fetch() {
|
|
|
|
const message = await this.message.fetch();
|
|
|
|
const existing = message.reactions.cache.get(this.emoji.id ?? this.emoji.name);
|
|
|
|
// The reaction won't get set when it has been completely removed
|
|
|
|
this._patch(existing ?? { count: 0 });
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
toJSON() {
|
|
|
|
return Util.flatten(this, { emoji: 'emojiId', message: 'messageId' });
|
|
|
|
}
|
|
|
|
|
|
|
|
_add(user) {
|
|
|
|
if (this.partial) return;
|
|
|
|
this.users.cache.set(user.id, user);
|
|
|
|
if (!this.me || user.id !== this.message.client.user.id || this.count === 0) this.count++;
|
2022-05-14 08:06:15 +00:00
|
|
|
this.me ||= user.id === this.message.client.user.id;
|
2022-03-19 10:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_remove(user) {
|
|
|
|
if (this.partial) return;
|
|
|
|
this.users.cache.delete(user.id);
|
|
|
|
if (!this.me || user.id !== this.message.client.user.id) this.count--;
|
|
|
|
if (user.id === this.message.client.user.id) this.me = false;
|
|
|
|
if (this.count <= 0 && this.users.cache.size === 0) {
|
|
|
|
this.message.reactions.cache.delete(this.emoji.id ?? this.emoji.name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = MessageReaction;
|