Store and Model Refactor (#1618)

* UserStore refactor

* Create ChannelStore, remove redundant methods in ClientDataManager

* Create GuildStore

* Emoji stuff

* Use a Base class where possible to reduce code duplication

* Remove unnecessary comments from ChannelStore

* Add Base._clone();

* Remove unused ClientDataManager methods

* Refactor some more stuff

* ESLint

* Move Client#fetchUser to client.users.fetch

* Remove .has checks and just see if .get is truthy

* Fix guild member chunk error

* ESLint

* Fix typo

* Fix channel storing for user bots

* Remove ClientDataManager

* GuildChannelStore

* Reduce use of Util.cloneObject

* and this one too

* update typings

* Fix MessageUpdate handling (#1507)

* Fix role updates (probably fixes #1525)

* fix for eslint

* Address some of appell's comments

* Use debug constant

* start message store crap if it's ugly tell me later k

* fix that

* message store but works™️

* clean up guild stuff

* clean up channel store stuff

* clean up channel event handling

* does this message stuff work? find out soon in the next episode of dIsCoRd.Js

* eslint

* emojis

* emojis and reactions

* hi my name is eslint and im A LIL SHIT

* so i forgot this huh

* user stuff

* Fix @class

* Fix message stuff

* Fix user store docs

* Document all the bases

* fix the super things

* tidy up remove

* fix textbasedchannel

* fix that too

* fix emoji store

* make voice state stuff less ugly

* make voice states even less ugly

* make members less bad

* fix bug

* fix that too

* fix reactions

* how was this broken for so long

* role store

* remove super._patch from UserConnection

* Rename UserProfile#setup to _patch

* remove unnecessary super calls

* update docgen dep (pls fix travis thx)

* doc messagestore

* fix docs

* message store docs

* things

* DOCS PLS

* more things

* Document TextBasedChannel#messages as a MessageStore

* Rebase

* All the stores!
This commit is contained in:
Amish Shah
2017-08-25 21:08:58 +01:00
committed by GitHub
parent 243ff48a67
commit b8315b79c7
80 changed files with 837 additions and 840 deletions

View File

@@ -1,11 +1,12 @@
const path = require('path');
const MessageCollector = require('../MessageCollector');
const Shared = require('../shared');
const Collection = require('../../util/Collection');
const MessageStore = require('../../stores/MessageStore');
const Snowflake = require('../../util/Snowflake');
const Collection = require('../../util/Collection');
const Attachment = require('../../structures/Attachment');
const MessageEmbed = require('../../structures/MessageEmbed');
const { Error, RangeError, TypeError } = require('../../errors');
const { RangeError, TypeError } = require('../../errors');
/**
* Interface for classes that have text-channel-like features.
@@ -15,9 +16,9 @@ class TextBasedChannel {
constructor() {
/**
* A collection containing the messages sent to this channel
* @type {Collection<Snowflake, Message>}
* @type {MessageStore<Snowflake, Message>}
*/
this.messages = new Collection();
this.messages = new MessageStore(this);
/**
* The ID of the last message in the channel, if one was sent
@@ -135,87 +136,6 @@ class TextBasedChannel {
return Shared.sendMessage(this, options);
}
/**
* Gets a single message from this channel, regardless of it being cached or not. Since the single message fetching
* endpoint is reserved for bot accounts, this abstracts the `fetchMessages` method to obtain the single message when
* using a user account.
* @param {Snowflake} messageID ID of the message to get
* @returns {Promise<Message>}
* @example
* // Get message
* channel.fetchMessage('99539446449315840')
* .then(message => console.log(message.content))
* .catch(console.error);
*/
fetchMessage(messageID) {
const Message = require('../Message');
if (!this.client.user.bot) {
return this.fetchMessages({ limit: 1, around: messageID })
.then(messages => {
const msg = messages.get(messageID);
if (!msg) throw new Error('MESSAGE_MISSING');
return msg;
});
}
return this.client.api.channels[this.id].messages[messageID].get()
.then(data => {
const msg = data instanceof Message ? data : new Message(this, data, this.client);
this._cacheMessage(msg);
return msg;
});
}
/**
* The parameters to pass in when requesting previous messages from a channel. `around`, `before` and
* `after` are mutually exclusive. All the parameters are optional.
* @typedef {Object} ChannelLogsQueryOptions
* @property {number} [limit=50] Number of messages to acquire
* @property {Snowflake} [before] ID of a message to get the messages that were posted before it
* @property {Snowflake} [after] ID of a message to get the messages that were posted after it
* @property {Snowflake} [around] ID of a message to get the messages that were posted around it
*/
/**
* Gets the past messages sent in this channel. Resolves with a collection mapping message ID's to Message objects.
* @param {ChannelLogsQueryOptions} [options={}] Query parameters to pass in
* @returns {Promise<Collection<Snowflake, Message>>}
* @example
* // Get messages
* channel.fetchMessages({limit: 10})
* .then(messages => console.log(`Received ${messages.size} messages`))
* .catch(console.error);
*/
fetchMessages(options = {}) {
const Message = require('../Message');
return this.client.api.channels[this.id].messages.get({ query: options })
.then(data => {
const messages = new Collection();
for (const message of data) {
const msg = new Message(this, message, this.client);
messages.set(message.id, msg);
this._cacheMessage(msg);
}
return messages;
});
}
/**
* Fetches the pinned messages of this channel and returns a collection of them.
* @returns {Promise<Collection<Snowflake, Message>>}
*/
fetchPinnedMessages() {
const Message = require('../Message');
return this.client.api.channels[this.id].pins.get().then(data => {
const messages = new Collection();
for (const message of data) {
const msg = new Message(this, message, this.client);
messages.set(message.id, msg);
this._cacheMessage(msg);
}
return messages;
});
}
/**
* Performs a search within the channel.
* <warn>This is only available when using a user account.</warn>
@@ -394,29 +314,17 @@ class TextBasedChannel {
});
}
_cacheMessage(message) {
const maxSize = this.client.options.messageCacheMaxSize;
if (maxSize === 0) return null;
if (this.messages.size >= maxSize && maxSize > 0) this.messages.delete(this.messages.firstKey());
this.messages.set(message.id, message);
return message;
}
static applyToClass(structure, full = false, ignore = []) {
const props = ['send'];
if (full) {
props.push(
'_cacheMessage',
'acknowledge',
'fetchMessages',
'fetchMessage',
'search',
'bulkDelete',
'startTyping',
'stopTyping',
'typing',
'typingCount',
'fetchPinnedMessages',
'createMessageCollector',
'awaitMessages'
);