refactor/fix(DataStores): move instantiating of store related classes into their date stores (#1869)

* refactor/fix(DataStores): move instantiating of store related classes into the relevant create methods and fixed FetchMemberOptions

* Renamed ChannelStore's constructor parameter for claritiy

* define client property before creating items from passed iterable to avoid the client property being undefined

* Fix fetching members with a limit

* add static Channel#create back and moved channel instantiating there

* make ChannelStore's constructor accept an iterable

* make iterable an optional parameter, allow to just pass client and options

* fixed a little typo in docs of <User>.displayAvatarURL() (#1872)

* Rewrite presence a little bit (#1853)

* such presence many good

* Update PresenceStore.js

* Update index.js

* Update ClientPresenceStore.js

* Update Presence.js

* Update ClientPresenceStore.js

* Update ClientUser.js

* Update Presence.js

* add timestamps and party

* Update Presence.js

* Update PresenceStore.js

* Update ClientPresenceStore.js

* Update ClientPresenceStore.js

* fix: made options.type optional in ClientUser#setActivity (#1875)

* fix: made options.type optional in ClientUser#setActivity

* some changes, smol fix

due to too many types being possible, it should just be defaulted to 0. this means streamers will have to set the type manually, though.
also mistake with activity.name check

* docs(Collection): Adjust exists documentation (#1876)

Since the return value of Collection#exists is basically just a boolean of Collection#find's result, the same documentation and arguments can and should be applied.

* refactor(Emoji): remove Guild#deleteEmoji in favour of Emoji#delete (#1877)

* refactor/fix(DataStores): move instantiating of store related classes into the relevant create methods and fixed FetchMemberOptions

* Renamed ChannelStore's constructor parameter for claritiy

* define client property before creating items from passed iterable to avoid the client property being undefined

* Fix fetching members with a limit

* add static Channel#create back and moved channel instantiating there

* make ChannelStore's constructor accept an iterable

* make iterable an optional parameter, allow to just pass client and options

* rebase, this time with saving the file

* address issue with falsy activity throwing errors when setting client's presence
This commit is contained in:
SpaceEEC
2017-09-03 12:57:49 +02:00
committed by Crawl
parent c93c4ad21f
commit c4b5736a16
12 changed files with 49 additions and 71 deletions

View File

@@ -2,9 +2,10 @@ const DataStore = require('./DataStore');
const GuildMember = require('../structures/GuildMember');
const Constants = require('../util/Constants');
const Collection = require('../util/Collection');
const { Error } = require('../errors');
/**
* Stores guild members.
* @private
* @extends {DataStore}
*/
class GuildMemberStore extends DataStore {
@@ -13,12 +14,12 @@ class GuildMemberStore extends DataStore {
this.guild = guild;
}
create(data) {
create(data, cache = true) {
const existing = this.get(data.user.id);
if (existing) return existing;
const member = new GuildMember(this.guild, data);
this.set(member.id, member);
if (cache) this.set(member.id, member);
return member;
}
@@ -66,7 +67,7 @@ class GuildMemberStore extends DataStore {
const user = this.client.resolver.resolveUserID(options);
if (user) return this._fetchSingle({ user, cache: true });
if (options.user) {
options.user = this.client.resolver.resolveUser(options);
options.user = this.client.resolver.resolveUserID(options.user);
if (options.user) return this._fetchSingle(options);
}
return this._fetchMany(options);
@@ -75,10 +76,7 @@ class GuildMemberStore extends DataStore {
_fetchSingle({ user, cache }) {
if (this.has(user)) return Promise.resolve(this.get(user));
return this.client.api.guilds(this.guild.id).members(user).get()
.then(data => {
if (cache) return this.create(data);
else return new GuildMember(this, data);
});
.then(data => this.create(data, cache));
}
_fetchMany({ query = '', limit = 0 } = {}) {
@@ -99,9 +97,11 @@ class GuildMemberStore extends DataStore {
const handler = (members, guild) => {
if (guild.id !== this.guild.id) return;
for (const member of members.values()) {
if (query || limit) fetchedMembers.set(member.user.id, member);
if (query || limit) fetchedMembers.set(member.id, member);
}
if (this.guild.memberCount === this.size || ((query || limit) && members.size < 1000)) {
if (this.guild.memberCount <= this.size ||
((query || limit) && members.size < 1000) ||
(limit && fetchedMembers.size >= limit)) {
this.guild.client.removeListener(Constants.Events.GUILD_MEMBERS_CHUNK, handler);
resolve(query || limit ? fetchedMembers : this);
}