diff --git a/src/client/websocket/WebSocketManager.js b/src/client/websocket/WebSocketManager.js index 83324576b..a3d7f08ca 100644 --- a/src/client/websocket/WebSocketManager.js +++ b/src/client/websocket/WebSocketManager.js @@ -12,12 +12,12 @@ class WebSocketManager { this.client = client; this.ws = null; this.packetManager = new PacketManager(this); - this.emittedReady = false; this.store = new WebSocketManagerDataStore(); - this.reconnecting = false; + this.status = Constants.Status.IDLE; } connect(gateway) { + this.status = Constants.Status.CONNECTING; this.store.gateway = gateway; gateway += `/?v=${this.client.options.protocol_version}`; this.ws = new WebSocket(gateway); @@ -91,7 +91,7 @@ class WebSocketManager { } checkIfReady() { - if (!this.emittedReady) { + if (this.status !== Constants.Status.READY) { let unavailableCount = 0; for (let guildID in this.client.store.data.guilds) { @@ -99,19 +99,18 @@ class WebSocketManager { } if (unavailableCount === 0) { + this.status = Constants.Status.READY; this.client.emit(Constants.Events.READY); - this.emittedReady = true; this.packetManager.handleQueue(); } } } tryReconnect() { - this.reconnecting = true; + this.status = Constants.Status.RECONNECTING; this.ws.close(); this.packetManager.handleQueue(); this.client.emit(Constants.Events.RECONNECTING); - this.emittedReady = false; this.connect(this.store.gateway); } } diff --git a/src/client/websocket/packets/WebSocketPacketManager.js b/src/client/websocket/packets/WebSocketPacketManager.js index 2f01da2a7..b4e123033 100644 --- a/src/client/websocket/packets/WebSocketPacketManager.js +++ b/src/client/websocket/packets/WebSocketPacketManager.js @@ -82,7 +82,7 @@ class WebSocketPacketManager { this.setSequence(packet.s); - if (!this.ws.emittedReady) { + if (this.ws.status !== Constants.Status.READY) { if (BeforeReadyWhitelist.indexOf(packet.t) === -1) { this.queue.push(packet); return; diff --git a/src/structures/Guild.js b/src/structures/Guild.js index 462edee38..766cc6955 100644 --- a/src/structures/Guild.js +++ b/src/structures/Guild.js @@ -33,7 +33,7 @@ class Guild { guildUser.joined_at = guildUser.joined_at || 0; let member = this.store.add('members', new GuildMember(this, guildUser)); - if (this.client.ws.emittedReady && !noEvent) { + if (this.client.ws.status === Constants.Status.READY && !noEvent) { this.client.emit(Constants.Events.GUILD_MEMBER_ADD, this, member); } @@ -44,14 +44,14 @@ class Guild { let oldRoles = member.roles; member._roles = data.roles; - if (this.client.ws.emittedReady) { + if (this.client.ws.status === Constants.Status.READY) { this.client.emit(Constants.Events.GUILD_MEMBER_ROLES_UPDATE, this, oldRoles, member.roles); } } _removeMember(guildMember) { this.store.remove('members', guildMember); - if (this.client.ws.emittedReady) { + if (this.client.ws.status === Constants.Status.READY) { this.client.emit(Constants.Events.GUILD_MEMBER_REMOVE, this, guildMember); } } diff --git a/src/structures/Message.js b/src/structures/Message.js index 025547e32..b0262e04a 100644 --- a/src/structures/Message.js +++ b/src/structures/Message.js @@ -10,7 +10,7 @@ class Message { } setup(data) { - this.author = this.guild.client.store.get('users', data.author.id); + this.author = this.guild.client.store.NewUser(data.author); this.content = data.content; this.timestamp = new Date(data.timestamp); this.editedTimestamp = data.edited_timestamp ? new Date(data.edited_timestamp) : null; @@ -25,6 +25,9 @@ class Message { let user = this.guild.client.store.get('users', mention.id); if (user) { this.mentions.push(user); + } else { + user = this.guild.client.store.NewUser(mention); + this.mentions.push(user); } } } @@ -53,6 +56,9 @@ class Message { let user = this.guild.client.store.get('users', mention.id); if (user) { this.mentions.push(user); + } else { + user = this.guild.client.store.NewUser(mention); + this.mentions.push(user); } } } diff --git a/src/structures/datastore/ClientDataStore.js b/src/structures/datastore/ClientDataStore.js index 6b9a89488..8cae0c97e 100644 --- a/src/structures/datastore/ClientDataStore.js +++ b/src/structures/datastore/ClientDataStore.js @@ -25,7 +25,7 @@ class ClientDataStore extends AbstractDataStore{ } get pastReady() { - return this.client.ws.emittedReady; + return this.client.ws.status === Constants.Status.READY; } NewGuild(data) { diff --git a/src/util/Constants.js b/src/util/Constants.js index 802b1714c..43ad1819c 100644 --- a/src/util/Constants.js +++ b/src/util/Constants.js @@ -16,6 +16,13 @@ const DefaultOptions = exports.DefaultOptions = { max_message_cache: 200, }; +const Status = exports.Status = { + READY: 0, + CONNECTING: 1, + RECONNECTING: 2, + IDLE: 3, +}; + const Package = exports.Package = require('../../package.json'); const Errors = exports.Errors = {