From fe8ece0192d8057cac3f02e7fab467238f9022af Mon Sep 17 00:00:00 2001 From: Amish Shah Date: Fri, 10 Aug 2018 15:15:52 +0100 Subject: [PATCH] voice state fixes --- .../websocket/packets/handlers/VoiceStateUpdate.js | 11 +++++------ src/stores/VoiceStateStore.js | 2 +- src/structures/Guild.js | 4 +--- src/structures/VoiceState.js | 1 + test/voice.js | 4 ++++ 5 files changed, 12 insertions(+), 10 deletions(-) diff --git a/src/client/websocket/packets/handlers/VoiceStateUpdate.js b/src/client/websocket/packets/handlers/VoiceStateUpdate.js index f76f51d9b..97b32e816 100644 --- a/src/client/websocket/packets/handlers/VoiceStateUpdate.js +++ b/src/client/websocket/packets/handlers/VoiceStateUpdate.js @@ -10,16 +10,15 @@ class VoiceStateUpdateHandler extends AbstractHandler { const guild = client.guilds.get(data.guild_id); if (guild) { // Update the state - const oldState = guild.voiceStates.get(data.user_id); - if (oldState) oldState._patch(data); - else guild.voiceStates.add(data); - + let oldState = guild.voiceStates.get(data.user_id); + if (oldState) oldState = oldState._clone(); + const newState = guild.voiceStates.add(data); const member = guild.members.get(data.user_id); if (member) { if (member.user.id === client.user.id && data.channel_id) { client.emit('self.voiceStateUpdate', data); } - client.emit(Events.VOICE_STATE_UPDATE, oldState, member.voiceState); + client.emit(Events.VOICE_STATE_UPDATE, oldState, newState); } } } @@ -28,7 +27,7 @@ class VoiceStateUpdateHandler extends AbstractHandler { /** * Emitted whenever a member changes voice state - e.g. joins/leaves a channel, mutes/unmutes. * @event Client#voiceStateUpdate - * @param {VoiceState} oldState The voice state before the update + * @param {?VoiceState} oldState The voice state before the update * @param {VoiceState} newState The voice state after the update */ diff --git a/src/stores/VoiceStateStore.js b/src/stores/VoiceStateStore.js index bc14c8f4b..2de492495 100644 --- a/src/stores/VoiceStateStore.js +++ b/src/stores/VoiceStateStore.js @@ -9,7 +9,7 @@ class VoiceStateStore extends DataStore { add(data, cache = true) { const existing = this.get(data.user_id); - if (existing) return existing; + if (existing) return existing._patch(data); const entry = new VoiceState(this.guild, data); if (cache) this.set(data.user_id, entry); diff --git a/src/structures/Guild.js b/src/structures/Guild.js index 260819251..bfb09d9f5 100644 --- a/src/structures/Guild.js +++ b/src/structures/Guild.js @@ -233,9 +233,7 @@ class Guild extends Base { if (!this.voiceStates) this.voiceStates = new VoiceStateStore(this); if (data.voice_states) { for (const voiceState of data.voice_states) { - const existing = this.voiceStates.get(voiceState.user_id); - if (existing) existing._patch(voiceState); - else this.voiceStates.add(voiceState); + this.voiceStates.add(voiceState); } } diff --git a/src/structures/VoiceState.js b/src/structures/VoiceState.js index faafc62c1..098ec9cff 100644 --- a/src/structures/VoiceState.js +++ b/src/structures/VoiceState.js @@ -50,6 +50,7 @@ class VoiceState extends Base { * @type {Snowflake} */ this.channelID = data.channel_id; + return this; } /** diff --git a/test/voice.js b/test/voice.js index 8494425ca..d516f21a4 100644 --- a/test/voice.js +++ b/test/voice.js @@ -29,6 +29,10 @@ var count = 0; process.on('unhandledRejection', console.log); +client.on('voiceStateUpdate', (a, b) => { + console.log(a ? a.channelID : null, b ? b.channelID : null, b.member.user.username); +}); + client.on('message', m => { if (!m.guild) return; if (m.author.id !== '66564597481480192') return;