diff --git a/src/music/player/PlaybackMixin.mjs b/src/music/player/PlaybackMixin.mjs index cd515df..4df6bff 100644 --- a/src/music/player/PlaybackMixin.mjs +++ b/src/music/player/PlaybackMixin.mjs @@ -44,7 +44,6 @@ const PlaybackMixin = { if (!this._paused) { this._lastPlayedTrack = this.queue.getCurrent() ?? songData; - if (!this.queue.songLoop) this.queue.current = null; this._playingNext = false; this.playNext().catch(e => logger.error("[Player] auto-advance playNext error:", e.message)); } @@ -99,13 +98,14 @@ const PlaybackMixin = { * generation counter ensures an older run's cleanup never clears the * in-flight flag of a newer run (which previously allowed double-advances). * @this {import('./Player.mjs').Player} + * @param {object} [opts] - Advancement options forwarded to Queue.next. * @returns {Promise} */ - async playNext() { + async playNext(opts = {}) { if (this._playingNext) return; this._playingNext = true; const generation = (this._playNextGeneration = (this._playNextGeneration ?? 0) + 1); - try { await this._doPlayNext(); } + try { await this._doPlayNext(opts); } finally { if (generation === this._playNextGeneration) this._playingNext = false; } }, @@ -114,13 +114,14 @@ const PlaybackMixin = { * track options, announces, starts playback via the bridge, arms the * track-end timer, and handles the queue-end path. * @this {import('./Player.mjs').Player} + * @param {object} [opts] - Advancement options forwarded to Queue.next. */ - async _doPlayNext() { + async _doPlayNext(opts = {}) { this._bridgeStop(); const currentBeforeNext = this.queue.getCurrent(); if (currentBeforeNext) this._lastPlayedTrack = currentBeforeNext; - const songData = this.queue.next(); + const songData = this.queue.next(opts); if (!songData) { this.emit("stopplay"); this.emit("queueEnd"); @@ -352,24 +353,13 @@ const PlaybackMixin = { this._playingNext = false; this._lastPlayedTrack = this.queue.getCurrent() ?? this._lastPlayedTrack; this.emit("trackSkip", this._lastPlayedTrack); - if (!this.queue.isEmpty() && !this.leaving) { - this.playNext().catch(e => logger.error("[Player] TrackEnd playNext error:", e.message)); + if (!this.leaving) { + this.playNext() + .catch(e => logger.error("[Player] TrackEnd playNext error:", e.message)) + .finally(() => { this._skipping = false; }); } else { - this.queue.current = null; - if (!this._wasRadio && !this._queueEndedSent) { - this._queueEndedSent = true; - this.emit("queueEnd"); - if (!this._autoplay) { - const prefix = this._getPrefix?.(this._guildId) ?? "%"; - this.emit("message", { embeds: [new EmbedBuilder().setColor(getGlobalColor()).setDescription(this._t("responses._common.queueEnded", { prefix }))], system: true }); - } - } - this.emit("stopplay"); - if (!this._is247Enabled()) { - this._startInactivityTimer(); - } + this._skipping = false; } - this._skipping = false; }, /** diff --git a/src/music/player/Player.mjs b/src/music/player/Player.mjs index da76d75..367f2c5 100644 --- a/src/music/player/Player.mjs +++ b/src/music/player/Player.mjs @@ -799,32 +799,20 @@ class Player extends EventEmitter { this._radioAnnounced = false; this._activeTrackOpt = null; this._clearTrackEndTimer(); - this.queue.current = null; this._bridgeStop(); this.emit("trackSkip", this._lastPlayedTrack); - if (this.queue.isEmpty() && !this._wasRadio && !this._queueEndedSent) { - this._queueEndedSent = true; - this.emit("queueEnd"); - if (!this._autoplay) { - const prefix = this._getPrefix?.(this._guildId) ?? "%"; - this.emit("message", { embeds: [new EmbedBuilder().setColor(getGlobalColor()).setDescription(this._t("responses._common.queueEnded", { prefix }))], system: true }); - } - } - this._playingNext = false; - if (!this.queue.isEmpty() && !this.leaving) { - this.playNext().catch(e => logger.error("[Player] playNext error:", e.message)); + if (!this.leaving) { + this.playNext({ ignoreSongLoop: true }) + .catch(e => logger.error("[Player] playNext error:", e.message)) + .finally(() => { this._skipping = false; }); } else { - this.emit("stopplay"); - if (!this._is247Enabled()) { - this._startInactivityTimer(); - } + this._skipping = false; } - this._skipping = false; return ":track_next: Skipped"; } @@ -840,34 +828,29 @@ class Player extends EventEmitter { if (idx < 0 || idx >= this.queue.size()) return `:negative_squared_cross_mark: Position ${position} out of range (queue has ${this.queue.size()} tracks).`; this._lastPlayedTrack = this.queue.getCurrent(); - this.queue.data.splice(0, idx); + const skipped = [this._lastPlayedTrack, ...this.queue.data.splice(0, idx)]; + if (this.queue.loop) { + this.queue.data.push(...skipped); + } this.queue.current = null; this._skipping = true; + this._radioAnnounced = false; + this._activeTrackOpt = null; + this._clearTrackEndTimer(); this._bridgeStop(); this.emit("trackSkip", this._lastPlayedTrack); - if (this.queue.isEmpty() && !this._wasRadio && !this._queueEndedSent) { - this._queueEndedSent = true; - this.emit("queueEnd"); - if (!this._autoplay) { - const prefix = this._getPrefix?.(this._guildId) ?? "%"; - this.emit("message", { embeds: [new EmbedBuilder().setColor(getGlobalColor()).setDescription(this._t("responses._common.queueEnded", { prefix }))], system: true }); - } - } - this._playingNext = false; - if (!this.queue.isEmpty() && !this.leaving) { - this.playNext().catch(e => logger.error("[Player] playNext error:", e.message)); + if (!this.leaving) { + this.playNext({ ignoreSongLoop: true }) + .catch(e => logger.error("[Player] playNext error:", e.message)) + .finally(() => { this._skipping = false; }); } else { - this.emit("stopplay"); - if (!this._is247Enabled()) { - this._startInactivityTimer(); - } + this._skipping = false; } - this._skipping = false; return `:track_next: Skipped to position ${position}`; } diff --git a/src/music/player/Queue.mjs b/src/music/player/Queue.mjs index 7076587..44e6a70 100644 --- a/src/music/player/Queue.mjs +++ b/src/music/player/Queue.mjs @@ -40,12 +40,14 @@ export class Queue extends EventEmitter { /** * Advance to the next track in the queue. If songLoop is active, returns * the current track. If queue loop is active, re-appends the current track. + * @param {object} [options] + * @param {boolean} [options.ignoreSongLoop=false] - Bypass songLoop (used when explicitly skipping). * @returns {object|null} The next track, or null if the queue is empty. */ - next() { + next({ ignoreSongLoop = false } = {}) { const previous = this.current; - if (this.songLoop && this.current) return this.current; + if (!ignoreSongLoop && this.songLoop && this.current) return this.current; if (this.loop && this.current) this.data.push(this.current); if (this.isEmpty()) {