From ea764afad2cbdc0ee4a82af118b6c7b4182af119 Mon Sep 17 00:00:00 2001 From: Amish Shah Date: Sat, 11 Aug 2018 10:46:51 +0100 Subject: [PATCH] add ClientPresence, remove ClientPresenceStore --- src/client/Client.js | 9 +++--- src/client/ClientManager.js | 4 +-- .../websocket/packets/handlers/Ready.js | 2 +- src/index.js | 2 +- .../ClientPresence.js} | 31 ++++++------------- src/structures/ClientUser.js | 4 +-- src/structures/User.js | 1 - src/util/Structures.js | 1 + 8 files changed, 21 insertions(+), 33 deletions(-) rename src/{stores/ClientPresenceStore.js => structures/ClientPresence.js} (76%) diff --git a/src/client/Client.js b/src/client/Client.js index 5b03c4435..a193587bd 100644 --- a/src/client/Client.js +++ b/src/client/Client.js @@ -14,7 +14,7 @@ const VoiceBroadcast = require('./voice/VoiceBroadcast'); const UserStore = require('../stores/UserStore'); const ChannelStore = require('../stores/ChannelStore'); const GuildStore = require('../stores/GuildStore'); -const ClientPresenceStore = require('../stores/ClientPresenceStore'); +const ClientPresence = require('../structures/ClientPresence'); const GuildEmojiStore = require('../stores/GuildEmojiStore'); const { Events, browser } = require('../util/Constants'); const DataResolver = require('../util/DataResolver'); @@ -97,10 +97,11 @@ class Client extends BaseClient { this.channels = new ChannelStore(this); /** - * Presences that have been received for the client user, mapped by user IDs - * @type {ClientPresenceStore} + * The presence of the Client + * @private + * @type {ClientPresence} */ - this.presences = new ClientPresenceStore(this); + this.presence = new ClientPresence(this); Object.defineProperty(this, 'token', { writable: true }); if (!browser && !this.token && 'CLIENT_TOKEN' in process.env) { diff --git a/src/client/ClientManager.js b/src/client/ClientManager.js index b283fd780..e6d107822 100644 --- a/src/client/ClientManager.js +++ b/src/client/ClientManager.js @@ -41,9 +41,9 @@ class ClientManager { const timeout = this.client.setTimeout(() => reject(new Error('WS_CONNECTION_TIMEOUT')), 1000 * 300); this.client.api.gateway.get().then(async res => { if (this.client.options.presence != null) { // eslint-disable-line eqeqeq - const presence = await this.client.presences._parse(this.client.options.presence); + const presence = await this.client.presence._parse(this.client.options.presence); this.client.options.ws.presence = presence; - this.client.presences.clientPresence.patch(presence); + this.client.presence.patch(presence); } const gateway = `${res.url}/`; this.client.emit(Events.DEBUG, `Using gateway ${gateway}`); diff --git a/src/client/websocket/packets/handlers/Ready.js b/src/client/websocket/packets/handlers/Ready.js index 59b872d90..c1a16df02 100644 --- a/src/client/websocket/packets/handlers/Ready.js +++ b/src/client/websocket/packets/handlers/Ready.js @@ -9,7 +9,7 @@ class ReadyHandler extends AbstractHandler { client.ws.heartbeat(); - client.presences.clientPresence.userID = data.user.id; + client.presence.userID = data.user.id; if (!ClientUser) ClientUser = require('../../../../structures/ClientUser'); const clientUser = new ClientUser(client, data.user); client.user = clientUser; diff --git a/src/index.js b/src/index.js index 8c7f5fb19..5ed3da570 100644 --- a/src/index.js +++ b/src/index.js @@ -25,7 +25,6 @@ module.exports = { // Stores ChannelStore: require('./stores/ChannelStore'), - ClientPresenceStore: require('./stores/ClientPresenceStore'), GuildChannelStore: require('./stores/GuildChannelStore'), GuildEmojiStore: require('./stores/GuildEmojiStore'), GuildEmojiRoleStore: require('./stores/GuildEmojiRoleStore'), @@ -74,6 +73,7 @@ module.exports = { MessageReaction: require('./structures/MessageReaction'), PermissionOverwrites: require('./structures/PermissionOverwrites'), Presence: require('./structures/Presence').Presence, + ClientPresence: require('./structures/ClientPresence'), ReactionCollector: require('./structures/ReactionCollector'), ReactionEmoji: require('./structures/ReactionEmoji'), RichPresenceAssets: require('./structures/Presence').RichPresenceAssets, diff --git a/src/stores/ClientPresenceStore.js b/src/structures/ClientPresence.js similarity index 76% rename from src/stores/ClientPresenceStore.js rename to src/structures/ClientPresence.js index 8e6336d6c..adba78f3e 100644 --- a/src/stores/ClientPresenceStore.js +++ b/src/structures/ClientPresence.js @@ -1,31 +1,18 @@ -const PresenceStore = require('./PresenceStore'); +const { Presence } = require('./Presence'); const Collection = require('../util/Collection'); const { ActivityTypes, OPCodes } = require('../util/Constants'); -const { Presence } = require('../structures/Presence'); const { TypeError } = require('../errors'); -/** - * Stores the client presence and other presences. - * @extends {PresenceStore} - */ -class ClientPresenceStore extends PresenceStore { - constructor(...args) { - super(...args); - this.clientPresence = new Presence(this.client, { - status: 'online', - afk: false, - since: null, - activity: null, - user: { id: null }, - guild_id: null, - }); +class ClientPresence extends Presence { + constructor(client, data = {}) { + super(client, Object.assign(data, { status: 'online', user: { id: null } })); } async setClientPresence(presence) { const packet = await this._parse(presence); - this.clientPresence.patch(packet); + this.patch(packet); this.client.ws.send({ op: OPCodes.STATUS_UPDATE, d: packet }); - return this.clientPresence; + return this; } async _parse({ status, since, afk, activity }) { // eslint-disable-line complexity @@ -45,7 +32,7 @@ class ClientPresenceStore extends PresenceStore { const packet = { afk: afk != null ? afk : false, // eslint-disable-line eqeqeq since: since != null ? since : null, // eslint-disable-line eqeqeq - status: status || this.clientPresence.status, + status: status || this.status, game: activity ? { type: activity.type, name: activity.name, @@ -67,7 +54,7 @@ class ClientPresenceStore extends PresenceStore { }; if ((status || afk || since) && !activity) { - packet.game = this.clientPresence.activity; + packet.game = this.activity; } if (packet.game) { @@ -79,4 +66,4 @@ class ClientPresenceStore extends PresenceStore { } } -module.exports = ClientPresenceStore; +module.exports = ClientPresence; diff --git a/src/structures/ClientUser.js b/src/structures/ClientUser.js index 31d966e6d..8d35765d9 100644 --- a/src/structures/ClientUser.js +++ b/src/structures/ClientUser.js @@ -32,7 +32,7 @@ class ClientUser extends Structures.get('User') { * @type {Presence} */ get presence() { - return this.client.presences.clientPresence; + return this.client.presence; } edit(data) { @@ -97,7 +97,7 @@ class ClientUser extends Structures.get('User') { * .catch(console.error); */ setPresence(data) { - return this.client.presences.setClientPresence(data); + return this.client.presence.setClientPresence(data); } /** diff --git a/src/structures/User.js b/src/structures/User.js index b05ddc241..4098fd95a 100644 --- a/src/structures/User.js +++ b/src/structures/User.js @@ -105,7 +105,6 @@ class User extends Base { * @readonly */ get presence() { - if (this.client.presences.has(this.id)) return this.client.presences.get(this.id); for (const guild of this.client.guilds.values()) { if (guild.presences.has(this.id)) return guild.presences.get(this.id); } diff --git a/src/util/Structures.js b/src/util/Structures.js index c81235033..8b1edd8b5 100644 --- a/src/util/Structures.js +++ b/src/util/Structures.js @@ -73,6 +73,7 @@ const structures = { Message: require('../structures/Message'), MessageReaction: require('../structures/MessageReaction'), Presence: require('../structures/Presence').Presence, + ClientPresence: require('../structures/ClientPresence'), VoiceState: require('../structures/VoiceState'), Role: require('../structures/Role'), User: require('../structures/User'),