From 641ef8ee852b4f6415e8ccc83ed9a53cd8e53efa Mon Sep 17 00:00:00 2001 From: JonnyBro Date: Wed, 4 Jan 2023 00:22:41 +0500 Subject: [PATCH] v5.3.10 fixed current track to not add twice to previousTracks fixed bot disconnection on member move Thanks PR's from original repo! --- package.json | 2 +- src/Player.ts | 21 +++++++++++++++++++++ src/Structures/Queue.ts | 15 ++++++--------- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/package.json b/package.json index 19186f1..e91dcea 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "discord-player-play-dl", - "version": "5.3.9", + "version": "5.3.10", "description": "Complete framework to facilitate music commands using discord.js and play-dl", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/Player.ts b/src/Player.ts index 25e6098..47b7704 100644 --- a/src/Player.ts +++ b/src/Player.ts @@ -142,6 +142,27 @@ class Player extends EventEmitter { queue._cooldownsTimeout.set(`empty_${oldState.guild.id}`, timeout); } } + + if (queue.connection && oldState.channelId && newState.channelId && oldState.channelId !== newState.channelId && newState.member.id != newState.guild.members.me.id) { + if (newState.channelId !== queue.connection.channel.id) { + if (!Util.isVoiceEmpty(queue.connection.channel)) return; + if (queue._cooldownsTimeout.has(`empty_${oldState.guild.id}`)) 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(true); + this.emit("channelEmpty", queue); + }, queue.options.leaveOnEmptyCooldown || 0).unref(); + queue._cooldownsTimeout.set(`empty_${oldState.guild.id}`, timeout); + } else { + 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}`); + } + } + } } /** diff --git a/src/Structures/Queue.ts b/src/Structures/Queue.ts index cb85fc9..ad0eec0 100644 --- a/src/Structures/Queue.ts +++ b/src/Structures/Queue.ts @@ -192,10 +192,10 @@ class Queue { if (this.options.leaveOnEnd) this.destroy(); this.player.emit("queueEnd", this); } else if (!this.tracks.length && this.repeatMode === QueueRepeatMode.AUTOPLAY) { - this._handleAutoplay(Util.last(this.previousTracks)); + this._handleAutoplay(this.current); } else { - if (this.repeatMode === QueueRepeatMode.TRACK) return void this.play(Util.last(this.previousTracks), { immediate: true }); - if (this.repeatMode === QueueRepeatMode.QUEUE) this.tracks.push(Util.last(this.previousTracks)); + if (this.repeatMode === QueueRepeatMode.TRACK) return void this.play(this.current, { immediate: true }); + if (this.repeatMode === QueueRepeatMode.QUEUE) this.tracks.push(this.current); const nextTrack = this.tracks.shift(); this.play(nextTrack, { immediate: true }); return; @@ -420,7 +420,7 @@ class Queue { */ async back() { if (this.#watchDestroyed()) return; - const prev = this.previousTracks[this.previousTracks.length - 2]; // because last item is the current track + const prev = this.previousTracks[this.previousTracks.length - 1]; if (!prev) throw new PlayerError("Could not find previous track", ErrorStatusCode.TRACK_NOT_FOUND); return await this.play(prev, { immediate: true }); @@ -575,7 +575,7 @@ class Queue { if (this.#watchDestroyed()) return; const length = typeof options.length === "number" ? (options.length <= 0 || options.length === Infinity ? 15 : options.length) : 15; - const index = Math.round((this.streamTime / this.current.durationMS) * length); + const index = Math.floor((this.streamTime / this.current.durationMS) * length); const indicator = typeof options.indicator === "string" && options.indicator.length > 0 ? options.indicator : "🔘"; const line = typeof options.line === "string" && options.line.length > 0 ? options.line : "▬"; @@ -622,10 +622,7 @@ class Queue { this.player.emit("debug", this, "Received play request"); - if (!options.filtersUpdate) { - this.previousTracks = this.previousTracks.filter((x) => x.id !== track.id); - this.previousTracks.push(track); - } + if (!options.filtersUpdate) this.previousTracks = this.previousTracks.filter((x) => x.id !== track.id); let stream = null; const hasCustomDownloader = typeof this.onBeforeCreateStream === "function";