mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-10 16:43:31 +01:00
feat: remove datastores and implement Managers (#3696)
* Initial commit: add 5 initial managers - Base manager - GuildChannelManager - MessageManager - PresenceManager - Reaction Manager - Added LimitedCollection * Add GuildEmojiManager, various fixes * Modify some managers and add guildmembermanager * Initial integration * Delete old stores * Integration part two, removed LRUCollection - Most of the integration has been finished - TODO typings - Removed LRUCollection, needless sweeping * Typings + stuff i somehow missed in ChannelManager * LimitedCollection typings/ final changes * Various jsdoc and syntactical fixes, Removed Util.mixin() * tslint fix * Grammatical and logical changes * Delete temporary file placed by mistake * Grammatical changes * Add missing type * Update jsdoc examples * fix: ChannelManager#remove should call cache#delete not cache#remove * fix recursive require * Fix missed cache in util * fix: more missed cache * Remove accidental _fetchMany change from #3645 * fix: use .cache.delete() over .remove() * fix: missing cache in ReactionCollector * fix: missed cache in client * fix: members is a collection not a manager Co-Authored-By: Sugden <28943913+NotSugden@users.noreply.github.com> * fix: various docs and cache fixes * fix: missed cache * fix: missing _roles * Final testing and debugging * LimitedCollection: return the Collection instead of undefined on .set * Add cache to BaseManager in typings * Commit fixes i forgot to stage yesterday * Update invite events * Account for new commit * fix: MessageReactionRemoveAll should call .cache.clear() * fix: add .cache at various places, correct return type * docs: remove mentions of 'store' * Add extra documented properties to typings Co-authored-by: Sugden <28943913+NotSugden@users.noreply.github.com> Co-authored-by: SpaceEEC <spaceeec@yahoo.com>
This commit is contained in:
89
src/managers/ReactionManager.js
Normal file
89
src/managers/ReactionManager.js
Normal file
@@ -0,0 +1,89 @@
|
||||
'use strict';
|
||||
|
||||
const MessageReaction = require('../structures/MessageReaction');
|
||||
const BaseManager = require('./BaseManager');
|
||||
|
||||
/**
|
||||
* Manages API methods for reactions and holds their cache.
|
||||
* @extends {BaseManager}
|
||||
*/
|
||||
class ReactionManager extends BaseManager {
|
||||
constructor(message, iterable) {
|
||||
super(message.client, iterable, MessageReaction);
|
||||
|
||||
/**
|
||||
* The message that this manager belongs to
|
||||
* @type {Message}
|
||||
*/
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
add(data, cache) {
|
||||
return super.add(data, cache, { id: data.emoji.id || data.emoji.name, extras: [this.message] });
|
||||
}
|
||||
|
||||
/**
|
||||
* The reaction cache of this manager
|
||||
* @property {Collection<Snowflake, MessageReaction>} cache
|
||||
* @memberof ReactionManager
|
||||
* @instance
|
||||
*/
|
||||
|
||||
/**
|
||||
* Data that can be resolved to a MessageReaction object. This can be:
|
||||
* * A MessageReaction
|
||||
* * A Snowflake
|
||||
* @typedef {MessageReaction|Snowflake} MessageReactionResolvable
|
||||
*/
|
||||
|
||||
/**
|
||||
* Resolves a MessageReactionResolvable to a MessageReaction object.
|
||||
* @method resolve
|
||||
* @memberof ReactionManager
|
||||
* @instance
|
||||
* @param {MessageReactionResolvable} reaction The MessageReaction to resolve
|
||||
* @returns {?MessageReaction}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Resolves a MessageReactionResolvable to a MessageReaction ID string.
|
||||
* @method resolveID
|
||||
* @memberof ReactionManager
|
||||
* @instance
|
||||
* @param {MessageReactionResolvable} reaction The MessageReaction to resolve
|
||||
* @returns {?Snowflake}
|
||||
*/
|
||||
|
||||
/**
|
||||
* Removes all reactions from a message.
|
||||
* @returns {Promise<Message>}
|
||||
*/
|
||||
removeAll() {
|
||||
return this.client.api.channels(this.message.channel.id).messages(this.message.id).reactions.delete()
|
||||
.then(() => this.message);
|
||||
}
|
||||
|
||||
_partial(emoji) {
|
||||
const id = emoji.id || emoji.name;
|
||||
const existing = this.cache.get(id);
|
||||
return !existing || existing.partial;
|
||||
}
|
||||
|
||||
async _fetchReaction(reactionEmoji, cache) {
|
||||
const id = reactionEmoji.id || reactionEmoji.name;
|
||||
const existing = this.cache.get(id);
|
||||
if (!this._partial(reactionEmoji)) return existing;
|
||||
const data = await this.client.api.channels(this.message.channel.id).messages(this.message.id).get();
|
||||
if (!data.reactions || !data.reactions.some(r => (r.emoji.id || r.emoji.name) === id)) {
|
||||
reactionEmoji.reaction._patch({ count: 0 });
|
||||
this.message.reactions.cache.delete(id);
|
||||
return existing;
|
||||
}
|
||||
for (const reaction of data.reactions) {
|
||||
if (this._partial(reaction.emoji)) this.add(reaction, cache);
|
||||
}
|
||||
return existing;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ReactionManager;
|
||||
Reference in New Issue
Block a user