mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-18 20:43:30 +01:00
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:
@@ -1,6 +1,5 @@
|
|||||||
const DataStore = require('./DataStore');
|
const DataStore = require('./DataStore');
|
||||||
const DMChannel = require('../structures/DMChannel');
|
const Channel = require('../structures/Channel');
|
||||||
const GroupDMChannel = require('../structures/GroupDMChannel');
|
|
||||||
const Constants = require('../util/Constants');
|
const Constants = require('../util/Constants');
|
||||||
|
|
||||||
const kLru = Symbol('LRU');
|
const kLru = Symbol('LRU');
|
||||||
@@ -12,8 +11,12 @@ const lruable = ['group', 'dm'];
|
|||||||
* @extends {DataStore}
|
* @extends {DataStore}
|
||||||
*/
|
*/
|
||||||
class ChannelStore extends DataStore {
|
class ChannelStore extends DataStore {
|
||||||
constructor(iterable, options = {}) {
|
constructor(client, iterableOrOptions = {}, options) {
|
||||||
super(iterable);
|
if (!options && typeof iterableOrOptions[Symbol.iterator] !== 'function') {
|
||||||
|
options = iterableOrOptions;
|
||||||
|
iterableOrOptions = undefined;
|
||||||
|
}
|
||||||
|
super(client, iterableOrOptions);
|
||||||
|
|
||||||
if (options.lru) {
|
if (options.lru) {
|
||||||
const lru = this[kLru] = [];
|
const lru = this[kLru] = [];
|
||||||
@@ -52,22 +55,11 @@ class ChannelStore extends DataStore {
|
|||||||
const existing = this.get(data.id);
|
const existing = this.get(data.id);
|
||||||
if (existing) return existing;
|
if (existing) return existing;
|
||||||
|
|
||||||
let channel;
|
const channel = Channel.create(this.client, data, guild);
|
||||||
switch (data.type) {
|
|
||||||
case Constants.ChannelTypes.DM:
|
if (!channel) {
|
||||||
channel = new DMChannel(this.client, data);
|
this.client.emit(Constants.Events.DEBUG, `Failed to find guild for channel ${data.id} ${data.type}`);
|
||||||
break;
|
return null;
|
||||||
case Constants.ChannelTypes.GROUP:
|
|
||||||
channel = new GroupDMChannel(this.client, data);
|
|
||||||
break;
|
|
||||||
default: // eslint-disable-line no-case-declarations
|
|
||||||
guild = guild || this.client.guilds.get(data.guild_id);
|
|
||||||
if (!guild) {
|
|
||||||
this.client.emit(Constants.Events.DEBUG, `Failed to find guild for channel ${data.id} ${data.type}`);
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
channel = guild.channels.create(data, cache);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cache) this.set(channel.id, channel);
|
if (cache) this.set(channel.id, channel);
|
||||||
|
|||||||
@@ -16,15 +16,17 @@ class ClientPresenceStore extends PresenceStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async setClientPresence({ status, since, afk, activity }) { // eslint-disable-line complexity
|
async setClientPresence({ status, since, afk, activity }) { // eslint-disable-line complexity
|
||||||
if (typeof activity.name !== 'string') throw new TypeError('INVALID_TYPE', 'name', 'string');
|
|
||||||
if (!activity.type) activity.type = 0;
|
|
||||||
const applicationID = activity && (activity.application ? activity.application.id || activity.application : null);
|
const applicationID = activity && (activity.application ? activity.application.id || activity.application : null);
|
||||||
let assets = new Collection();
|
let assets = new Collection();
|
||||||
if (activity && activity.assets && applicationID) {
|
if (activity) {
|
||||||
try {
|
if (typeof activity.name !== 'string') throw new TypeError('INVALID_TYPE', 'name', 'string');
|
||||||
const a = await this.client.api.oauth2.applications(applicationID).assets.get();
|
if (!activity.type) activity.type = 0;
|
||||||
for (const asset of a) assets.set(asset.name, asset.id);
|
if (activity.assets && applicationID) {
|
||||||
} catch (err) {} // eslint-disable-line no-empty
|
try {
|
||||||
|
const a = await this.client.api.oauth2.applications(applicationID).assets.get();
|
||||||
|
for (const asset of a) assets.set(asset.name, asset.id);
|
||||||
|
} catch (err) { } // eslint-disable-line no-empty
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const packet = {
|
const packet = {
|
||||||
|
|||||||
@@ -7,8 +7,8 @@ const Collection = require('../util/Collection');
|
|||||||
class DataStore extends Collection {
|
class DataStore extends Collection {
|
||||||
constructor(client, iterable) {
|
constructor(client, iterable) {
|
||||||
super();
|
super();
|
||||||
if (iterable) for (const item of iterable) this.create(item);
|
|
||||||
Object.defineProperty(this, 'client', { value: client });
|
Object.defineProperty(this, 'client', { value: client });
|
||||||
|
if (iterable) for (const item of iterable) this.create(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stubs
|
// Stubs
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
const DataStore = require('./DataStore');
|
const DataStore = require('./DataStore');
|
||||||
const TextChannel = require('../structures/TextChannel');
|
const Channel = require('../structures/Channel');
|
||||||
const VoiceChannel = require('../structures/VoiceChannel');
|
|
||||||
const Constants = require('../util/Constants');
|
|
||||||
/**
|
/**
|
||||||
* Stores guild channels.
|
* Stores guild channels.
|
||||||
* @private
|
* @private
|
||||||
@@ -13,15 +12,11 @@ class GuildChannelStore extends DataStore {
|
|||||||
this.guild = guild;
|
this.guild = guild;
|
||||||
}
|
}
|
||||||
|
|
||||||
create(data, cache = true) {
|
create(data) {
|
||||||
const existing = this.get(data.id);
|
const existing = this.get(data.id);
|
||||||
if (existing) return existing;
|
if (existing) return existing;
|
||||||
|
|
||||||
const ChannelModel = data.type === Constants.ChannelTypes.TEXT ? TextChannel : VoiceChannel;
|
return Channel.create(this.client, data, this.guild);
|
||||||
const channel = new ChannelModel(this.guild, data);
|
|
||||||
if (cache) this.set(channel.id, channel);
|
|
||||||
|
|
||||||
return channel;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,9 +2,10 @@ const DataStore = require('./DataStore');
|
|||||||
const GuildMember = require('../structures/GuildMember');
|
const GuildMember = require('../structures/GuildMember');
|
||||||
const Constants = require('../util/Constants');
|
const Constants = require('../util/Constants');
|
||||||
const Collection = require('../util/Collection');
|
const Collection = require('../util/Collection');
|
||||||
|
const { Error } = require('../errors');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Stores guild members.
|
* Stores guild members.
|
||||||
* @private
|
|
||||||
* @extends {DataStore}
|
* @extends {DataStore}
|
||||||
*/
|
*/
|
||||||
class GuildMemberStore extends DataStore {
|
class GuildMemberStore extends DataStore {
|
||||||
@@ -13,12 +14,12 @@ class GuildMemberStore extends DataStore {
|
|||||||
this.guild = guild;
|
this.guild = guild;
|
||||||
}
|
}
|
||||||
|
|
||||||
create(data) {
|
create(data, cache = true) {
|
||||||
const existing = this.get(data.user.id);
|
const existing = this.get(data.user.id);
|
||||||
if (existing) return existing;
|
if (existing) return existing;
|
||||||
|
|
||||||
const member = new GuildMember(this.guild, data);
|
const member = new GuildMember(this.guild, data);
|
||||||
this.set(member.id, member);
|
if (cache) this.set(member.id, member);
|
||||||
|
|
||||||
return member;
|
return member;
|
||||||
}
|
}
|
||||||
@@ -66,7 +67,7 @@ class GuildMemberStore extends DataStore {
|
|||||||
const user = this.client.resolver.resolveUserID(options);
|
const user = this.client.resolver.resolveUserID(options);
|
||||||
if (user) return this._fetchSingle({ user, cache: true });
|
if (user) return this._fetchSingle({ user, cache: true });
|
||||||
if (options.user) {
|
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);
|
if (options.user) return this._fetchSingle(options);
|
||||||
}
|
}
|
||||||
return this._fetchMany(options);
|
return this._fetchMany(options);
|
||||||
@@ -75,10 +76,7 @@ class GuildMemberStore extends DataStore {
|
|||||||
_fetchSingle({ user, cache }) {
|
_fetchSingle({ user, cache }) {
|
||||||
if (this.has(user)) return Promise.resolve(this.get(user));
|
if (this.has(user)) return Promise.resolve(this.get(user));
|
||||||
return this.client.api.guilds(this.guild.id).members(user).get()
|
return this.client.api.guilds(this.guild.id).members(user).get()
|
||||||
.then(data => {
|
.then(data => this.create(data, cache));
|
||||||
if (cache) return this.create(data);
|
|
||||||
else return new GuildMember(this, data);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_fetchMany({ query = '', limit = 0 } = {}) {
|
_fetchMany({ query = '', limit = 0 } = {}) {
|
||||||
@@ -99,9 +97,11 @@ class GuildMemberStore extends DataStore {
|
|||||||
const handler = (members, guild) => {
|
const handler = (members, guild) => {
|
||||||
if (guild.id !== this.guild.id) return;
|
if (guild.id !== this.guild.id) return;
|
||||||
for (const member of members.values()) {
|
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);
|
this.guild.client.removeListener(Constants.Events.GUILD_MEMBERS_CHUNK, handler);
|
||||||
resolve(query || limit ? fetchedMembers : this);
|
resolve(query || limit ? fetchedMembers : this);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,12 +6,12 @@ const Guild = require('../structures/Guild');
|
|||||||
* @extends {DataStore}
|
* @extends {DataStore}
|
||||||
*/
|
*/
|
||||||
class GuildStore extends DataStore {
|
class GuildStore extends DataStore {
|
||||||
create(data) {
|
create(data, cache = true) {
|
||||||
const existing = this.get(data.id);
|
const existing = this.get(data.id);
|
||||||
if (existing) return existing;
|
if (existing) return existing;
|
||||||
|
|
||||||
const guild = new Guild(this.client, data);
|
const guild = new Guild(this.client, data);
|
||||||
this.set(guild.id, guild);
|
if (cache) this.set(guild.id, guild);
|
||||||
|
|
||||||
return guild;
|
return guild;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,12 +6,12 @@ const User = require('../structures/User');
|
|||||||
* @extends {DataStore}
|
* @extends {DataStore}
|
||||||
*/
|
*/
|
||||||
class UserStore extends DataStore {
|
class UserStore extends DataStore {
|
||||||
create(data) {
|
create(data, cache = true) {
|
||||||
const existing = this.get(data.id);
|
const existing = this.get(data.id);
|
||||||
if (existing) return existing;
|
if (existing) return existing;
|
||||||
|
|
||||||
const user = new User(this.client, data);
|
const user = new User(this.client, data);
|
||||||
this.set(user.id, user);
|
if (cache) this.set(user.id, user);
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,9 +26,7 @@ class UserStore extends DataStore {
|
|||||||
const existing = this.get(id);
|
const existing = this.get(id);
|
||||||
if (existing) return Promise.resolve(existing);
|
if (existing) return Promise.resolve(existing);
|
||||||
|
|
||||||
return this.client.api.users(id).get().then(data =>
|
return this.client.api.users(id).get().then(data => this.create(data, cache));
|
||||||
cache ? this.create(data) : new User(this.client, data)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,8 +5,6 @@ const ClientUserGuildSettings = require('./ClientUserGuildSettings');
|
|||||||
const Constants = require('../util/Constants');
|
const Constants = require('../util/Constants');
|
||||||
const Util = require('../util/Util');
|
const Util = require('../util/Util');
|
||||||
const Guild = require('./Guild');
|
const Guild = require('./Guild');
|
||||||
const Message = require('./Message');
|
|
||||||
const GroupDMChannel = require('./GroupDMChannel');
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents the logged in client's Discord user.
|
* Represents the logged in client's Discord user.
|
||||||
@@ -258,7 +256,7 @@ class ClientUser extends User {
|
|||||||
Util.mergeDefault({ limit: 25, roles: true, everyone: true, guild: null }, options);
|
Util.mergeDefault({ limit: 25, roles: true, everyone: true, guild: null }, options);
|
||||||
|
|
||||||
return this.client.api.users('@me').mentions.get({ query: options })
|
return this.client.api.users('@me').mentions.get({ query: options })
|
||||||
.then(data => data.map(m => new Message(this.client.channels.get(m.channel_id), m, this.client)));
|
.then(data => data.map(m => this.client.channels.get(m.channel_id).messages.create(m, false)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -324,7 +322,7 @@ class ClientUser extends User {
|
|||||||
}, {}),
|
}, {}),
|
||||||
} : { recipients: recipients.map(u => this.client.resolver.resolveUserID(u.user || u.id)) };
|
} : { recipients: recipients.map(u => this.client.resolver.resolveUserID(u.user || u.id)) };
|
||||||
return this.client.api.users('@me').channels.post({ data })
|
return this.client.api.users('@me').channels.post({ data })
|
||||||
.then(res => new GroupDMChannel(this.client, res));
|
.then(res => this.client.channels.create(res));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -13,13 +13,11 @@ class Invite extends Base {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_patch(data) {
|
_patch(data) {
|
||||||
const Guild = require('./Guild');
|
|
||||||
const Channel = require('./Channel');
|
|
||||||
/**
|
/**
|
||||||
* The guild the invite is for
|
* The guild the invite is for
|
||||||
* @type {Guild}
|
* @type {Guild}
|
||||||
*/
|
*/
|
||||||
this.guild = this.client.guilds.get(data.guild.id) || new Guild(this.client, data.guild);
|
this.guild = this.client.guilds.create(data.guild, false);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The code for this invite
|
* The code for this invite
|
||||||
@@ -87,7 +85,7 @@ class Invite extends Base {
|
|||||||
* The channel the invite is for
|
* The channel the invite is for
|
||||||
* @type {GuildChannel}
|
* @type {GuildChannel}
|
||||||
*/
|
*/
|
||||||
this.channel = this.client.channels.get(data.channel.id) || Channel.create(this.client, data.channel, this.guild);
|
this.channel = this.client.channels.create(data.channel, this.guild, false);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The timestamp the invite was created at
|
* The timestamp the invite was created at
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
const Mentions = require('./MessageMentions');
|
const Mentions = require('./MessageMentions');
|
||||||
const Attachment = require('./MessageAttachment');
|
const Attachment = require('./MessageAttachment');
|
||||||
const Embed = require('./MessageEmbed');
|
const Embed = require('./MessageEmbed');
|
||||||
const MessageReaction = require('./MessageReaction');
|
|
||||||
const ReactionCollector = require('./ReactionCollector');
|
const ReactionCollector = require('./ReactionCollector');
|
||||||
const ClientApplication = require('./ClientApplication');
|
const ClientApplication = require('./ClientApplication');
|
||||||
const Util = require('../util/Util');
|
const Util = require('../util/Util');
|
||||||
@@ -118,8 +117,7 @@ class Message extends Base {
|
|||||||
this.reactions = new ReactionStore(this);
|
this.reactions = new ReactionStore(this);
|
||||||
if (data.reactions && data.reactions.length > 0) {
|
if (data.reactions && data.reactions.length > 0) {
|
||||||
for (const reaction of data.reactions) {
|
for (const reaction of data.reactions) {
|
||||||
const id = reaction.emoji.id ? `${reaction.emoji.name}:${reaction.emoji.id}` : reaction.emoji.name;
|
this.reactions.create(reaction);
|
||||||
this.reactions.set(id, new MessageReaction(this, reaction.emoji, reaction.count, reaction.me));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -210,8 +210,7 @@ class Webhook {
|
|||||||
auth: false,
|
auth: false,
|
||||||
}).then(data => {
|
}).then(data => {
|
||||||
if (!this.client.channels) return data;
|
if (!this.client.channels) return data;
|
||||||
const Message = require('./Message');
|
return this.client.channels.get(data.channel_id).messages.create(data, false);
|
||||||
return new Message(this.client.channels.get(data.channel_id), data, this.client);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -239,8 +238,7 @@ class Webhook {
|
|||||||
data: body,
|
data: body,
|
||||||
}).then(data => {
|
}).then(data => {
|
||||||
if (!this.client.channels) return data;
|
if (!this.client.channels) return data;
|
||||||
const Message = require('./Message');
|
return this.client.channels.get(data.channel_id).messages.create(data, false);
|
||||||
return new Message(this.client.channels.get(data.channel_id), data, this.client);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -84,14 +84,13 @@ module.exports = function search(target, options) {
|
|||||||
// Lazy load these because some of them use util
|
// Lazy load these because some of them use util
|
||||||
const Channel = require('../Channel');
|
const Channel = require('../Channel');
|
||||||
const Guild = require('../Guild');
|
const Guild = require('../Guild');
|
||||||
const Message = require('../Message');
|
|
||||||
|
|
||||||
if (!(target instanceof Channel || target instanceof Guild)) throw new TypeError('SEARCH_CHANNEL_TYPE');
|
if (!(target instanceof Channel || target instanceof Guild)) throw new TypeError('SEARCH_CHANNEL_TYPE');
|
||||||
|
|
||||||
let endpoint = target.client.api[target instanceof Channel ? 'channels' : 'guilds'](target.id).messages().search;
|
let endpoint = target.client.api[target instanceof Channel ? 'channels' : 'guilds'](target.id).messages().search;
|
||||||
return endpoint.get({ query: options }).then(body => {
|
return endpoint.get({ query: options }).then(body => {
|
||||||
const results = body.messages.map(x =>
|
const results = body.messages.map(x =>
|
||||||
x.map(m => new Message(target.client.channels.get(m.channel_id), m, target.client))
|
x.map(m => target.client.channels.get(m.channel_id).messages.create(m, false))
|
||||||
);
|
);
|
||||||
return {
|
return {
|
||||||
total: body.total_results,
|
total: body.total_results,
|
||||||
|
|||||||
Reference in New Issue
Block a user