From 4800e7cb7e504dd5b47dbf68e60099db7c0a40d0 Mon Sep 17 00:00:00 2001 From: DooM <68818349+EwoX07@users.noreply.github.com> Date: Sat, 16 Oct 2021 05:09:12 +0700 Subject: [PATCH] Update Player.ts connection.channel rapidly changing in a large server with a busy voicestate, it shud never change. fix: it shud only change the connection.channel when the bot being moved somewhere else. also fixing how it handles voice move so it triggers channelEmpty correctly, on member move from connection.channel and bot move to an empty channel. last thing is limiting the join and leave to the connection.channel only --- src/Player.ts | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/src/Player.ts b/src/Player.ts index 2e3bbfc..3b1cf2a 100644 --- a/src/Player.ts +++ b/src/Player.ts @@ -77,8 +77,17 @@ class Player extends EventEmitter { if (!queue) return; if (oldState.channelId && newState.channelId && oldState.channelId !== newState.channelId) { - if (queue?.connection) queue.connection.channel = newState.channel; - } + if (queue?.connection && newState.member.id === newState.guild.me.id) queue.connection.channel = newState.channel; + if (newState.member.id === newState.guild.me.id || newState.member.id !== newState.guild.me.id && oldState.channelId === queue.connection.channel.id) { + if (!Util.isVoiceEmpty(queue.connection.channel)) return; + const timeout = setTimeout(() => { + if (!Util.isVoiceEmpty(queue.connection.channel)) return; + if (!this.queues.has(queue.guild.id)) return; + if (queue.options.leaveOnEmpty) queue.destroy(); + this.emit("channelEmpty", queue); + }, queue.options.leaveOnEmptyCooldown || 0).unref(); + queue._cooldownsTimeout.set(`empty_${oldState.guild.id}`, timeout); + } if (!oldState.channelId && newState.channelId && newState.member.id === newState.guild.me.id) { if (newState.serverMute || !newState.serverMute) { @@ -109,19 +118,23 @@ class Player extends EventEmitter { const emptyTimeout = queue._cooldownsTimeout.get(`empty_${oldState.guild.id}`); const channelEmpty = Util.isVoiceEmpty(queue.connection.channel); - if (!channelEmpty && emptyTimeout) { - clearTimeout(emptyTimeout); - queue._cooldownsTimeout.delete(`empty_${oldState.guild.id}`); + if (newState.channelId === queue.connection.channel.id) { + if (!channelEmpty && emptyTimeout) { + clearTimeout(emptyTimeout); + queue._cooldownsTimeout.delete(`empty_${oldState.guild.id}`); + } } } else { - if (!Util.isVoiceEmpty(queue.connection.channel)) return; - const timeout = setTimeout(() => { + if (oldState.channelId === queue.connection.channel.id) { if (!Util.isVoiceEmpty(queue.connection.channel)) return; - if (!this.queues.has(queue.guild.id)) return; - if (queue.options.leaveOnEmpty) queue.destroy(); - this.emit("channelEmpty", queue); - }, queue.options.leaveOnEmptyCooldown || 0).unref(); - queue._cooldownsTimeout.set(`empty_${oldState.guild.id}`, timeout); + const timeout = setTimeout(() => { + if (!Util.isVoiceEmpty(queue.connection.channel)) return; + if (!this.queues.has(queue.guild.id)) return; + if (queue.options.leaveOnEmpty) queue.destroy(); + this.emit("channelEmpty", queue); + }, queue.options.leaveOnEmptyCooldown || 0).unref(); + queue._cooldownsTimeout.set(`empty_${oldState.guild.id}`, timeout); + } } }