summaryrefslogtreecommitdiff
path: root/node_modules/discord.js/src/structures/MessageReaction.js
diff options
context:
space:
mode:
authorsowgro <tpoke.ferrari@gmail.com>2023-09-02 19:12:47 -0400
committersowgro <tpoke.ferrari@gmail.com>2023-09-02 19:12:47 -0400
commite4450c8417624b71d779cb4f41692538f9165e10 (patch)
treeb70826542223ecdf8a7a259f61b0a1abb8a217d8 /node_modules/discord.js/src/structures/MessageReaction.js
downloadsowbot3-e4450c8417624b71d779cb4f41692538f9165e10.tar.gz
sowbot3-e4450c8417624b71d779cb4f41692538f9165e10.tar.bz2
sowbot3-e4450c8417624b71d779cb4f41692538f9165e10.zip
first commit
Diffstat (limited to 'node_modules/discord.js/src/structures/MessageReaction.js')
-rw-r--r--node_modules/discord.js/src/structures/MessageReaction.js142
1 files changed, 142 insertions, 0 deletions
diff --git a/node_modules/discord.js/src/structures/MessageReaction.js b/node_modules/discord.js/src/structures/MessageReaction.js
new file mode 100644
index 0000000..43f05e3
--- /dev/null
+++ b/node_modules/discord.js/src/structures/MessageReaction.js
@@ -0,0 +1,142 @@
+'use strict';
+
+const { Routes } = require('discord-api-types/v10');
+const GuildEmoji = require('./GuildEmoji');
+const ReactionEmoji = require('./ReactionEmoji');
+const ReactionUserManager = require('../managers/ReactionUserManager');
+const { flatten } = 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;
+ }
+ }
+
+ /**
+ * Makes the client user react with this reaction
+ * @returns {Promise<MessageReaction>}
+ */
+ react() {
+ return this.message.react(this.emoji);
+ }
+
+ /**
+ * Removes all users from this reaction.
+ * @returns {Promise<MessageReaction>}
+ */
+ async remove() {
+ await this.client.rest.delete(
+ Routes.channelMessageReaction(this.message.channelId, this.message.id, this._emoji.identifier),
+ );
+ 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 flatten(this, { emoji: 'emojiId', message: 'messageId' });
+ }
+
+ valueOf() {
+ return this._emoji.id ?? this._emoji.name;
+ }
+
+ _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++;
+ this.me ||= user.id === this.message.client.user.id;
+ }
+
+ _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;