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:
BorgerKing
2020-02-11 14:21:07 -05:00
committed by GitHub
parent fe7df708e4
commit bbdbc4cfa7
87 changed files with 804 additions and 705 deletions

View File

@@ -0,0 +1,66 @@
'use strict';
const Collection = require('../util/Collection');
const BaseManager = require('./BaseManager');
const { Error } = require('../errors');
/**
* Manages API methods for users who reacted to a reaction and stores their cache.
* @extends {BaseManager}
*/
class ReactionUserManager extends BaseManager {
constructor(client, iterable, reaction) {
super(client, iterable, { name: 'User' });
/**
* The reaction that this manager belongs to
* @type {MessageReaction}
*/
this.reaction = reaction;
}
/**
* The cache of this manager
* @property {Collection<Snowflake, User>} cache
* @memberof GuildManager
* @instance
*/
/**
* Fetches all the users that gave this reaction. Resolves with a collection of users, mapped by their IDs.
* @param {Object} [options] Options for fetching the users
* @param {number} [options.limit=100] The maximum amount of users to fetch, defaults to 100
* @param {Snowflake} [options.before] Limit fetching users to those with an id lower than the supplied id
* @param {Snowflake} [options.after] Limit fetching users to those with an id greater than the supplied id
* @returns {Promise<Collection<Snowflake, User>>}
*/
async fetch({ limit = 100, after, before } = {}) {
const message = this.reaction.message;
const data = await this.client.api.channels[message.channel.id].messages[message.id]
.reactions[this.reaction.emoji.identifier]
.get({ query: { limit, before, after } });
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.reaction.message.client.user] The user to remove the reaction of
* @returns {Promise<MessageReaction>}
*/
remove(user = this.reaction.message.client.user) {
const message = this.reaction.message;
const userID = message.client.users.resolveID(user);
if (!userID) return Promise.reject(new Error('REACTION_RESOLVE_USER'));
return message.client.api.channels[message.channel.id].messages[message.id]
.reactions[this.reaction.emoji.identifier][userID === message.client.user.id ? '@me' : userID]
.delete()
.then(() => this.reaction);
}
}
module.exports = ReactionUserManager;