/** * @module src/music/player/Player * @description Per-channel music player: voice connection lifecycle (join / * leave / destroy / rejoin), playback controls (pause, resume, skip, seek, * volume, filters), queue operations, 24/7 mode helpers and the inactivity * timer. Playback advancement, search and display rendering live in the * playback/search/display mixin modules applied at the bottom of this file. */ import { getVoiceManager } from "@fluxerjs/voice"; import { EventEmitter } from "node:events"; import http from "node:http"; import https from "node:https"; import { PermissionFlags } from "@fluxerjs/core"; import { Utils, cleanId } from "../../utils/Utils.mjs"; import { logger } from "../../core/Logger.mjs"; import { get247ChannelMode, detachPlayerFromManager } from "../../utils/Helpers247.mjs"; import { hasHumansInChannel } from "../../voice/VoiceStateResolver.mjs"; import { FluxerAudioBridge } from "../audio/FluxerAudioBridge.mjs"; import { getEffectiveVolume } from "../audio/VolumeNormalizer.mjs"; import { applyMixins } from "../../utils/mixins.mjs"; import { Queue } from "./Queue.mjs"; import PlaybackMixin from "./PlaybackMixin.mjs"; import SearchMixin from "./SearchMixin.mjs"; import DisplayMixin from "./DisplayMixin.mjs"; /** @type {number} Timestamp (ms) of the last logged player-state persistence failure. */ let _lastPersistErrorLog = 0; /** @type {number} Minimum spacing between persistence-error log lines, to avoid log floods when MySQL is down/unreachable. */ const PERSIST_ERROR_LOG_COOLDOWN_MS = 60_000; /** * Log a player-state persistence failure (saveSnapshot / updatePosition), * rate-limited so a prolonged DB outage doesn't spam the log for every * player's debounce/heartbeat tick. Persistence is best-effort — playback * must never be interrupted by it — but a silent, permanently-swallowed * failure previously left operators with zero visibility when it broke. * @param {string} where - Short tag identifying the call site. * @param {Error} e - The error thrown/rejected by the store call. */ function logPersistError(where, e) { const now = Date.now(); if (now - _lastPersistErrorLog < PERSIST_ERROR_LOG_COOLDOWN_MS) return; _lastPersistErrorLog = now; logger.warn(`[Player] State persistence (${where}) failed — resume/queue restore may be stale until this clears (will not re-log for 60s):`, e?.message ?? e); } /** * @class Player * @description Main music player class. Manages voice connections, playback, * queue, filters, and search. * @extends {EventEmitter} * * Events: * - `"queue"` — queue mutation (proxied from the Queue) * - `"message"` — announcements `{ embeds: [...] }` (or a plain string) * - `"startplay"` / `"stopplay"` / `"queueEnd"` / `"trackSkip"` * - `"playback"` (paused state boolean), `"volume"`, `"filter"` * - `"autoleave"`, `"leave"`, `"roomfetched"` */ class Player extends EventEmitter { /** @private @type {object|null} The LiveKit voice connection. */ _voiceConn = null; /** @private @type {FluxerAudioBridge|null} */ _audioBridge = null; lavalinkPlayer = null; /** @type {object|null} Alias of _voiceConn (kept for dashboard/debug compat). */ connection = null; /** @private @type {string|null} */ _guildId = null; /** @private @type {string|null} */ _channelId = null; /** @private @type {number|null} */ _lastConnectedAt = null; /** @private @type {string|null} The home channel of a 24/7 spawn. */ _home247Channel = null; /** @private @type {Set} User IDs who have voted to skip the current track. Reset whenever a new track starts. */ _skipVotes = new Set(); /** @type {Queue} */ queue = null; client = null; settings = null; config = {}; /** @type {import('../LavalinkManager.mjs').LavalinkManager|null} */ _lavalink = null; leaving = false; _paused = false; _pausedAt = null; _playingNext = false; startedPlaying = null; /** @type {Map} Search sessions by key. */ searches = new Map(); _searchMaxSize = 50; _maxQueueSize = 10_000; resultLimit = 5; preferredVolume = 1; _skipping = false; _seeking = false; _wasRadio = false; _radioAnnounced = false; _queueEndedSent = false; _lastPlayedTrack = null; _autoplay = false; _autoplayHandler = null; activeFilter = null; activeFilterPayload = null; /** @type {number} Default inactivity timeout before auto-leave. */ static INACTIVITY_DEFAULT_MS = 3 * 60 * 1000; /** @type {number} Track is considered finished past this ratio. */ static TRACK_MOSTLY_FINISHED_RATIO = 0.85; /** @type {number} Or when less than this much time remains. */ static TRACK_MOSTLY_FINISHED_FLOOR_MS = 15_000; /** @type {number} Hard radio safety timeout. */ static RADIO_SAFETY_TIMEOUT_MS = 20 * 60 * 1000; /** @private @type {Timeout|null} */ _inactivityTimer = null; /** @private @type {number} */ _inactivityLimit = Player.INACTIVITY_DEFAULT_MS; /** @private @type {boolean} */ _pendingInactivityCheck = false; /** @private @type {boolean} */ _isJoining = false; /** @private @type {boolean} */ _destroyed = false; /** * @param {string} token - Bot authentication token (used internally). * @param {object} [opts={}] - Player configuration options. * @param {import('@fluxerjs/core').Client} [opts.client] * @param {object} [opts.config] * @param {import('../LavalinkManager.mjs').LavalinkManager} [opts.lavalink] * @param {import('../../db/Settings.mjs').RemoteSettingsManager} [opts.settingsMgr] * @param {Function} [opts.getPrefix] * @param {import('../../voice/VoiceStateCache.mjs').VoiceStateCache} [opts.observedVoiceUsers] * @param {import('../../voice/VoiceStateCache.mjs').VoiceStateCache} [opts.voiceCache] * @param {import('../../core/Locale.mjs').Locale} [opts.locale] * @param {import('../../services/TrackOptionsManager.mjs').TrackOptionsManager} [opts.trackOptions] */ constructor(token, opts = {}) { super(); this.queue = new Queue(); this.queue.on("queue", (...args) => this.emit("queue", ...args)); this.client = opts.client; this.config = opts.config ?? {}; this.settings = opts.settings ?? null; this.settingsMgr = opts.settingsMgr ?? null; this._getPrefix = opts.getPrefix ?? null; this._observedVoiceUsers = opts.observedVoiceUsers ?? null; this._voiceCache = opts.voiceCache ?? null; this.locale = opts.locale ?? null; this.trackOptions = opts.trackOptions ?? null; this._activeTrackOpt = null; this._lavalink = opts.lavalink ?? null; this._audioBridge = new FluxerAudioBridge(opts.lavalink ?? null); this._audioBridge.on("error", (err) => { logger.error(`[Player] Audio bridge error (guild ${this._guildId}): ${err.message}`); }); const inactivityMs = this.config?.timers?.inactivityTimeout ?? this.config?.inactivityTimeout; if (inactivityMs !== undefined) { const parsed = Number(inactivityMs); if (!Number.isNaN(parsed) && parsed >= 0) { this._inactivityLimit = parsed; } } this._stayInVoice = this.config?.timers?.stayInVoice ?? this.config?.stayInVoice ?? true; this._snapSaveTimer = null; this._scheduleStateSave = () => { if (this._snapSaveTimer || this._destroyed || this.leaving) return; this._snapSaveTimer = setTimeout(() => { this._snapSaveTimer = null; try { this.client?._remix?.playerState?.saveSnapshot(this)?.catch?.(e => logPersistError("saveSnapshot", e)); } catch (e) { logPersistError("saveSnapshot/sync", e); } }, 3_000); }; for (const evt of ["queue", "startplay", "stopplay", "playback"]) { try { this.on(evt, this._scheduleStateSave); } catch (_) {} } this._stateHeartbeat = setInterval(() => { try { const current = this.queue?.getCurrent?.(); if (!current || this._destroyed || this.leaving || this._paused) return; if (!this.startedPlaying) return; const pos = Math.max(0, Date.now() - this.startedPlaying); this.client?._remix?.playerState?.updatePosition?.(this._guildId, pos)?.catch?.(e => logPersistError("updatePosition", e)); } catch (e) { logPersistError("updatePosition/sync", e); } }, 15_000); this._stateHeartbeat.unref?.(); if (this._lavalink) { this._onLavalinkPlayerDisconnect = (lavaPlayer) => { if (!lavaPlayer || String(lavaPlayer.guildId) !== String(this._guildId)) return; logger.lavalink("[Player] lavalink-client player disconnect (guild: " + this._guildId + ") — ignored (voice via LiveKit)"); }; this._lavalink.on("playerDisconnected", this._onLavalinkPlayerDisconnect); } } /** * Check whether 24/7 mode is active for this player's current channel. * @returns {boolean} True if 24/7 is enabled. */ _is247Enabled() { return this._get247Mode() !== "off"; } /** * Resolve the 24/7 mode for this player's current or home channel by * consulting the guild's stay_247 setting. * @returns {"on"|"off"} The 24/7 mode. Only "on" or "off" — no "auto". * @private */ _get247Mode() { if (!this._guildId) return "off"; const serverSettings = this.settingsMgr?.getServer?.(this._guildId) ?? this.settings ?? this.client?.settings?.getServer?.(this._guildId); if (!serverSettings?.get) return "off"; const channelId = cleanId(this._home247Channel ?? this._channelId ?? ""); if (!channelId) return "off"; const raw = serverSettings.get("stay_247"); if (raw && raw !== "none") { const channels = Array.isArray(raw) ? raw.map(id => cleanId(id)).filter(Boolean) : [cleanId(raw)].filter(Boolean); if (!channels.includes(channelId)) return "off"; } else { return "off"; } return get247ChannelMode(serverSettings, channelId); } /** @private @returns {string|null} The cleaned guild ID, or null if unresolvable. */ _resolveGuildId() { const cleanGuild = cleanId(this._guildId ?? ""); if (cleanGuild) return cleanGuild; try { const channelId = this._channelId ?? this._home247Channel; if (channelId) { const ch = this.client?.channels?.get?.(channelId); const fromChannel = ch?.guildId ?? ch?.guild?.id ?? null; if (fromChannel) return cleanId(fromChannel); } } catch(e) { logger.warn("[Player] Guild resolution failed:", e?.message); } return null; } /** @private Restore saved volume from guild settings. */ _restoreVolume() { if (!this._guildId) return; let savedVol = null; if (this.settings?.get) { savedVol = this.settings.get("volume"); } else if (this.settingsMgr?.getServer) { const set = this.settingsMgr.getServer(this._guildId); savedVol = set?.get?.("volume"); } else if (this.client?.settings?.getServer) { savedVol = this.client.settings.getServer(this._guildId)?.get?.("volume"); } else if (this.client?.settings?.get) { const s = this.client.settings.get(this._guildId); savedVol = s?.get?.("volume") ?? s?.volume; } if (savedVol !== undefined && savedVol !== null) { const parsed = Number(savedVol); if (!Number.isNaN(parsed) && parsed > 0) { this.preferredVolume = Utils.clamp(parsed / 100, 0, 2); logger.player(`[Player] Restored volume ${savedVol}% for guild ${this._guildId}`); if (this._voiceConn) { try { this._voiceConn.setVolume(this.preferredVolume * 100); } catch (_) {} } } } } /** @private @returns {boolean} Whether there are non-bot users in the channel. */ _hasHumansInChannel() { return hasHumansInChannel({ guildId: cleanId(this._guildId ?? ""), channelId: cleanId(this._channelId ?? ""), client: this.client, voiceCache: this._voiceCache, observedVoiceUsers: this._observedVoiceUsers, botId: this.client?.user?.id, }); } /** * Start the inactivity timer. Skipped when 24/7 is active, the queue has * songs, or humans are present. The timer callback re-checks all * conditions before emitting autoleave. */ _startInactivityTimer() { this._stopInactivityTimer(); if (this._inactivityLimit <= 0) return; if (this._stayInVoice) { logger.inactivity(`[Player] stay-in-voice active for guild ${this._guildId} — skipping inactivity auto-leave`); return; } const mode = this._get247Mode(); logger.inactivity(`[Player] Checking 24/7 mode for guild ${this._guildId}: ${mode}`); if (mode === "on") { logger.inactivity(`[Player] 24/7 mode active for guild ${this._guildId}, skipping inactivity timer`); return; } if (this.queue?.getCurrent() || !this.queue?.isEmpty()) { logger.inactivity(`[Player] Queue has songs for guild ${this._guildId}, skipping inactivity timer`); return; } if (this._hasHumansInChannel()) { logger.inactivity(`[Player] Humans present in channel ${this._channelId}, skipping inactivity timer`); return; } logger.inactivity(`[Player] Starting inactivity timer for guild ${this._guildId} (${this._inactivityLimit / 1000}s)`); this._inactivityTimer = setTimeout(() => { const currentMode = this._get247Mode(); if (currentMode === "on") { logger.inactivity(`[Player] 24/7 mode enabled during inactivity wait, aborting leave`); return; } if (this.queue?.getCurrent() || !this.queue?.isEmpty()) { logger.inactivity("[Player] Song in queue during inactivity wait, aborting leave"); return; } if (this._hasHumansInChannel()) { logger.inactivity("[Player] Human joined during inactivity wait, aborting leave"); return; } logger.inactivity(`[Player] Guild ${this._guildId} inactive too long. Leaving.`); this.emit("autoleave"); }, this._inactivityLimit); } /** Clear the inactivity timer if running. */ _stopInactivityTimer() { this._pendingInactivityCheck = false; if (this._inactivityTimer) { logger.inactivity(`[Player] Stopping inactivity timer for guild ${this._guildId}`); clearTimeout(this._inactivityTimer); this._inactivityTimer = null; } } /** * 24/7 serverLeave recovery: remove this (now connection-less) player * from every manager index, schedule a BOT-LEVEL rejoin, notify listeners, * and destroy the player. * * Why bot-level: the old design kept the rejoin timer on the dying player * (`this._rejoinTimer`), so `destroy()` — which cleared that timer — and * the rejoin scheduler fought each other; and when the player survived, * `_rejoinChannel` skipped the rejoin because the zombie was still in the * playerMap. Either way the channel never came back. The rejoin now lives * on the Remix context (`ctx.schedule247Rejoin`), which survives this * instance's destruction. * * @param {string} channelId - The 24/7 channel to rejoin. * @param {string} guildId - The guild ID. * @private */ _detachAndSchedule247Rejoin(channelId, guildId) { const ctx = this.client?._remix; if (typeof ctx?.schedule247Rejoin !== "function") { logger.warn(`[Player] No bot-level rejoin scheduler for ${channelId} — falling back to autoleave`); this.emit("autoleave"); return; } detachPlayerFromManager(ctx, this, channelId); ctx.schedule247Rejoin(channelId, guildId); this.emit("autoleave"); this.destroy(); } /** * @private Low-level HTTP(S) request helper with redirects and JSON parsing. * Used for Lavalink REST calls (pre-resolve, loadtracks). * @param {string} url * @param {object} [options={}] * @param {boolean} [returnStream=false] * @returns {Promise} */ async _request(url, options = {}, returnStream = false) { return new Promise((resolve, reject) => { const fetchUrl = (target, _redirects = 0) => { const urlObj = new URL(target); const client = urlObj.protocol === "https:" ? https : http; const req = client.request({ protocol: urlObj.protocol, host: urlObj.hostname, port: urlObj.port, path: urlObj.pathname + urlObj.search, method: options.method || "GET", headers: { "User-Agent": "Mozilla/5.0 (compatible; Bot/1.0)", "Accept": "*/*", ...options.headers, }, }, (res) => { if (returnStream) req.setTimeout(0); if ([301, 302, 307, 308].includes(res.statusCode)) { let loc = res.headers.location; if (!loc) return reject(new Error("Redirect without location")); if (loc.startsWith("/")) loc = `${urlObj.protocol}//${urlObj.host}${loc}`; if (_redirects >= 5) return reject(new Error("Too many redirects")); const redirectUrl = new URL(loc); if (redirectUrl.host !== urlObj.host) { if (options.headers) delete options.headers.Authorization; } return fetchUrl(loc, _redirects + 1); } if (![200, 204, 206].includes(res.statusCode)) { res.resume(); return reject(new Error(`HTTP ${res.statusCode}`)); } if (returnStream) return resolve(res); if (res.statusCode === 204) { res.resume(); return resolve(null); } const chunks = []; res.on("data", d => chunks.push(d)); res.on("end", () => { const raw = Buffer.concat(chunks).toString().trim(); if (!raw) return resolve(null); try { resolve(JSON.parse(raw)); } catch (e) { reject(new Error(`JSON parse error from ${target}`)); } }); }); req.on("error", reject); req.setTimeout(options.timeout || 60_000, () => { req.destroy(); reject(new Error("Request timeout")); }); if (options.body) req.write(options.body); req.end(); }; fetchUrl(url); }); } /** * @async Join a voice channel via LiveKit. Sets up voice connection, event * listeners, and resumes playback if the queue has tracks. * @param {string} channelId - The voice channel ID to join. * @returns {Promise} * @throws {Error} If the channel is not found, VoiceManager is unavailable, or connection fails. */ async join(channelId) { if (this._destroyed) return; if (this._isJoining) { logger.player(`[Player] Busy joining. Ignoring: ${channelId}`); return; } const isConnActive = this._voiceConn && (typeof this._voiceConn.isConnected !== "function" || this._voiceConn.isConnected()); if (isConnActive && this._channelId === channelId) { logger.player(`[Player] Already in channel: ${channelId}`); return; } if (this._voiceConn) { logger.player("[Player] Cleaning up existing voice connection before join"); this._bridgeStop(); try { await this._voiceConn.disconnect(); } catch(e) { logger.warn("[Player] Existing voiceConn disconnect error:", e?.message); } this._voiceConn = null; this.connection = null; await Utils.sleep(500); } this._isJoining = true; try { const channel = this.client?.channels?.get?.(channelId); if (!channel) throw new Error(`Channel not found: ${channelId}`); if (this._lavalink) { logger.player(`[Player] Waiting for Lavalink node...`); await this._lavalink.waitForNode({ timeoutMs: 15_000 }); } this._channelId = channelId; this._guildId = cleanId(channel.guildId); this._lastConnectedAt = Date.now(); this.leaving = false; try { const me = channel.guild?.members?.me ?? (this.client?.user?.id ? channel.guild?.members?.get?.(this.client.user.id) : null); if (me) { const perms = channel.permissionsFor?.(me) ?? me.permissionsIn?.(channel); if (perms && !perms.has?.(PermissionFlags?.Administrator)) { if (PermissionFlags?.ViewChannel && !perms.has(PermissionFlags.ViewChannel)) { throw new Error(`Bot lacks ViewChannel permission in channel ${channelId}`); } if (PermissionFlags?.Connect && !perms.has(PermissionFlags.Connect)) { throw new Error(`Bot lacks Connect permission in channel ${channelId}`); } } } } catch (permErr) { if (permErr?.message?.includes?.("Bot lacks")) throw permErr; } logger.player(`[Player] Joining channel ${channelId} via vm.join() (LiveKit)...`); const vm = getVoiceManager(this.client); if (!vm) { throw new Error("VoiceManager not available — call getVoiceManager(client) before login"); } let voiceConn; try { voiceConn = await vm.join(channel); } catch (e) { throw new Error(`vm.join() failed for channel ${channelId}: ${e.message}`); } if (!voiceConn) { throw new Error(`vm.join() returned null for channel ${channelId}`); } this._voiceConn = voiceConn; this.connection = voiceConn; if (typeof voiceConn.isConnected === "function" && !voiceConn.isConnected()) { logger.player("[Player] Waiting for LiveKit room to connect..."); await new Promise((resolve, reject) => { const timeout = setTimeout(() => reject(new Error("LiveKit connection timeout (15s)")), 15_000); const check = () => { try { if (voiceConn.isConnected()) { clearTimeout(timeout); return resolve(); } if (voiceConn.connectionState === 0) { clearTimeout(timeout); return resolve(); } } catch (_) { clearTimeout(timeout); return resolve(); } setTimeout(check, 200); }; check(); }); await Utils.sleep(500); } logger.player(`[Player] LiveKit voice connected (guild: ${this._guildId}, channel: ${channelId})` + ` isConnected=${voiceConn.isConnected ? voiceConn.isConnected() : 'unknown'}`); this._assertSelfDeaf(vm, channelId); if (typeof voiceConn.on === "function") { voiceConn.on("requestVoiceStateSync", () => { if (this.leaving || this._destroyed) return; this._assertSelfDeaf(vm, channelId); }); voiceConn.on("serverLeave", () => { if (this.leaving || this._destroyed) return; logger.player(`[Player] serverLeave received — Fluxer/LiveKit terminated session`); this._voiceConn = null; this.connection = null; this._paused = true; this._stopInactivityTimer(); const mode = this._get247Mode(); const cId = cleanId(this._channelId ?? this._home247Channel ?? ""); const gId = cleanId(this._guildId ?? ""); if (mode === "on" && cId && gId) { logger.player("[Player] serverLeave in 24/7 mode — detaching and scheduling rejoin"); this._detachAndSchedule247Rejoin(cId, gId); } else { logger.player("[Player] Unexpected serverLeave"); this.emit("autoleave"); } }); voiceConn.on("disconnect", () => { if (this.leaving || this._destroyed) return; logger.player(`[Player] Voice connection disconnected`); this._voiceConn = null; this.connection = null; const cId = cleanId(this._channelId ?? this._home247Channel ?? ""); if (cId && (this._get247Mode() === "on" || this.queue?.getCurrent() || !(this.queue?.isEmpty?.() ?? true))) { logger.player(`[Player] Voice disconnected with active session — scheduling reconnect to channel ${cId}`); this.join(cId).catch(err => logger.warn(`[Player] Voice reconnect after disconnect failed: ${err?.message}`)); } }); } this._restoreVolume(); this.emit("roomfetched"); logger.player(`[Player] Voice connected to ${channel.name || channelId}`); if (!this.queue.isEmpty() && !this.queue.getCurrent()) { this.playNext().catch(e => logger.error("[Player] playNext error:", e.message)); } else if (this.queue.isEmpty()) { this._pendingInactivityCheck = true; setTimeout(() => { if (!this._pendingInactivityCheck) return; this._pendingInactivityCheck = false; if (this.queue.isEmpty() && !this.queue.getCurrent()) { this._startInactivityTimer(); } }, 3000); } } catch (e) { const causeStr = e.cause ? ` (Cause: ${e.cause})` : ""; logger.player("[Player] Join failed:", e.message, causeStr); if (this._voiceConn) { try { await this._voiceConn.disconnect(); } catch(err) { logger.warn("[Player] voiceConn disconnect on join failure:", err?.message); } this._voiceConn = null; this.connection = null; } throw e; } finally { this._isJoining = false; } } /** * @async Leave the current voice channel. Stops playback, resets state, and * cleans up the voice connection. * @returns {Promise} True if leave was successful, false otherwise. */ async leave() { if (!this._voiceConn && !this.connection) return false; try { this.leaving = true; this._stopInactivityTimer(); this._clearTrackEndTimer(); this._activeTrackOpt = null; const channelId = this._channelId; this._bridgeStop(); try { const vm = getVoiceManager(this.client); if (channelId) { vm.leaveChannel(channelId); logger.player("[Player] Left channel " + channelId + " via vm.leaveChannel()"); } } catch(e) { logger.warn("[Player] Gateway leave error:", e?.message); } if (this._voiceConn) { try { await this._voiceConn.disconnect(); } catch(e) { logger.warn("[Player] voiceConn disconnect error:", e?.message); } } if (this._audioBridge) { this._audioBridge.stop(); } this.queue.reset(); this._voiceConn = null; this.lavalinkPlayer = null; this.connection = null; this._paused = false; this._pausedAt = null; this._playingNext = false; this._autoplay = false; if (this._autoplayHandler) { this.removeListener("queueEnd", this._autoplayHandler); this._autoplayHandler = null; } try { this.client?._remix?.playerState?.clearSnapshot?.(this._guildId)?.catch?.(() => {}); } catch (_) { /* best effort */ } } catch (e) { logger.error("[Player] leave error:", e.message); this.leaving = false; return false; } this.leaving = false; this.emit("leave"); return true; } /** * Fully destroy this player instance: clean up timers, listeners, * connections, and resources. */ destroy() { if (this._destroyed) return; this._destroyed = true; this._scheduleStateSave = null; if (this._snapSaveTimer) { clearTimeout(this._snapSaveTimer); this._snapSaveTimer = null; } if (this._stateHeartbeat) { clearInterval(this._stateHeartbeat); this._stateHeartbeat = null; } this._clearTrackEndTimer(); this._activeTrackOpt = null; try { if (this._lavalink) { if (this._onLavalinkPlayerDisconnect) { try { this._lavalink.off("playerDisconnected", this._onLavalinkPlayerDisconnect); } catch(e) { /* best effort */ } this._onLavalinkPlayerDisconnect = null; } } this.queue?.reset(); this.leaving = true; this._stopInactivityTimer(); this._autoplay = false; if (this._autoplayHandler) { this.removeListener("queueEnd", this._autoplayHandler); this._autoplayHandler = null; } this.searches.clear(); this._bridgeStop(); if (this._audioBridge) { this._audioBridge.destroy(); this._audioBridge = null; } if (this._voiceConn) { try { this._voiceConn.disconnect(); } catch(e) { logger.warn("[Player] voiceConn disconnect in destroy:", e?.message); } this._voiceConn = null; } this.lavalinkPlayer = null; this.connection = null; if (this._channelId) { try { const vm = getVoiceManager(this.client); vm.leaveChannel(this._channelId); } catch(e) { /* best effort */ } } this.removeAllListeners(); } catch (e) { logger.error("[Player] destroy error:", e.message); } } /** @type {boolean} Whether the player is currently paused. */ get paused() { return this._paused; } /** * Pause current playback. * @returns {string} Status message. */ pause() { if (!this._voiceConn || !this.queue.getCurrent()) return ":negative_squared_cross_mark: There's nothing playing at the moment!"; if (this._paused) return ":negative_squared_cross_mark: Already paused!"; this._bridgeStop(); this._paused = true; this._pausedAt = Date.now(); this._pauseTrackEndTimer(); this.emit("playback", false); this._stopInactivityTimer(); return ":pause_button: Paused"; } /** * Resume paused playback. * @returns {string} Status message. */ resume() { if (!this._voiceConn || !this.queue.getCurrent()) return ":negative_squared_cross_mark: There's nothing playing at the moment!"; if (!this._paused) return ":negative_squared_cross_mark: Not paused!"; if (this._pausedAt) { this.startedPlaying += (Date.now() - this._pausedAt); } this._paused = false; this._pausedAt = null; this._resumeTrackEndTimer(); this.emit("playback", true); this._stopInactivityTimer(); const current = this.queue.getCurrent(); if (current?.url) { const elapsedMs = Date.now() - this.startedPlaying; this._playTrackViaBridge(current, { seekSeconds: elapsedMs / 1000 }).catch(e => logger.error("[Player] Resume playback error:", e.message) ); } return ":arrow_forward: Resumed"; } /** * Compute the current track playback position in milliseconds. * @returns {number} Elapsed playback position in ms. */ computePosition() { if (!this.startedPlaying) return 0; if (this._paused && this._pausedAt) { return Math.max(0, this._pausedAt - this.startedPlaying); } return Math.max(0, Date.now() - this.startedPlaying); } /** * Seamlessly resume playback after a NodeLink/Lavalink node reconnects. * Re-requests the stream at the last computed position so listeners experience * minimal interruption. * @returns {Promise} */ async resumePlaybackAfterNodeReconnect() { if (this._destroyed || this.leaving || !this._voiceConn) return false; const current = this.queue?.getCurrent(); if (!current || this._paused) return false; const posMs = Math.max(0, this.computePosition()); logger.player(`[Player:${this._guildId}] Node reconnected — resuming "${current.title}" at ${Math.round(posMs / 1000)}s`); try { await this._playTrackViaBridge(current, { seekSeconds: posMs / 1000 }); return true; } catch (err) { logger.warn(`[Player:${this._guildId}] Failed to auto-resume on node reconnect:`, err?.message); return false; } } /** * Skip the current track and advance to the next. Emits "trackSkip" with * the skipped track so autoplay (when enabled) can replenish the queue. * @returns {string} Status message. */ skip() { if (!this._voiceConn || !this.queue.getCurrent()) return ":negative_squared_cross_mark: There's nothing playing at the moment!"; this._lastPlayedTrack = this.queue.getCurrent(); this._skipping = true; this._radioAnnounced = false; this._activeTrackOpt = null; this._clearTrackEndTimer(); this._bridgeStop(); this.emit("trackSkip", this._lastPlayedTrack); this._playingNext = false; if (!this.leaving) { this.playNext({ ignoreSongLoop: true }) .catch(e => logger.error("[Player] playNext error:", e.message)) .finally(() => { this._skipping = false; }); } else { this._skipping = false; } return ":track_next: Skipped"; } /** * Skip to a specific position in the queue. * @param {number} position - 1-based queue position. * @returns {string} Status message. */ skipTo(position) { if (!this._voiceConn || !this.queue.getCurrent()) return ":negative_squared_cross_mark: There's nothing playing at the moment!"; const idx = position - 1; 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(); 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); this._playingNext = false; if (!this.leaving) { this.playNext({ ignoreSongLoop: true }) .catch(e => logger.error("[Player] playNext error:", e.message)) .finally(() => { this._skipping = false; }); } else { this._skipping = false; } return `:track_next: Skipped to position ${position}`; } /** * Get the effective playback volume percentage (0-200), applying dynamic * volume normalization (ReplayGain / EBU R128) if enabled. * @param {object|null} [track=null] * @returns {number} Effective volume percentage (0-200). */ getEffectiveVolume(track = null) { const cur = track || this.queue?.getCurrent?.(); const isNorm = this.volumeNormalization !== false; return getEffectiveVolume(this.preferredVolume, cur, isNorm); } /** * Set the playback volume. * @param {number} v - Volume as a decimal (0–2, where 1 = 100%). * @returns {string} Status message. */ setVolume(v) { this.preferredVolume = Utils.clamp(v, 0, 2); this.emit("volume", this.preferredVolume); if (this._voiceConn) { try { this._voiceConn.setVolume(this.getEffectiveVolume()); } catch (_) {} } if (!this._voiceConn) return `Volume set to \`${Math.round(this.preferredVolume * 100)}%\` — will apply when connected.`; return `Volume changed to \`${Math.round(this.preferredVolume * 100)}%\`.`; } /** @returns {boolean} Whether the queue is empty. */ isEmpty() { return this.queue.isEmpty(); } /** * Add a track to the queue with optional top-insert. * @param {object} d - Track data. * @param {boolean} [t=false] - Insert at the top of the queue. */ addToQueue(d, t) { if (this.queue.data.length >= this._maxQueueSize) { logger.warn(`[Player] Queue size cap reached (${this._maxQueueSize}) — dropping oldest track`); this.queue.data.shift(); } this.queue.add(d, t); this.emit("update", "queue"); this._stopInactivityTimer(); } /** Clear the queue and start the inactivity timer if nothing is playing. */ clear() { this.queue.clear(); this.emit("update", "queue"); if (!this.queue.getCurrent()) { this._startInactivityTimer(); } } /** * Add multiple tracks to the queue. * @param {Array} t - Track data array. * @param {boolean} [top=false] - Insert at the top. * @returns {number} Number of tracks added. */ addManyToQueue(t, top = false) { if (!Array.isArray(t)) return 0; const overflow = (this.queue.data.length + t.length) - this._maxQueueSize; if (overflow > 0) { logger.warn(`[Player] Queue size cap (${this._maxQueueSize}) — dropping ${overflow} oldest tracks`); this.queue.data.splice(0, overflow); } const added = this.queue.addMany(t, top); this.emit("update", "queue"); this._stopInactivityTimer(); return added; } /** * Shuffle the queue. * @returns {string} Status message. */ shuffle() { if (this.isEmpty()) return "There is nothing to shuffle in the queue."; this.queue.shuffle(); this.emit("update", "queue"); return ":twisted_rightwards_arrows: Shuffled queue"; } /** * Move a track from one position to another (1-based indices). * @param {number} from * @param {number} to * @returns {string} Result message. */ move(from, to) { if (this.queue.size() === 0) return "The queue is empty."; return this.queue.move(from - 1, to - 1); } /** * Toggle a loop mode. * @param {"song"|"queue"} choice * @returns {string} */ loop(choice) { if (!["song", "queue"].includes(choice)) return `'${choice}' is not valid. Use \`song\` or \`queue\``; const state = this.queue.toggleLoop(choice); const name = choice.charAt(0).toUpperCase() + choice.slice(1); return state ? `:repeat: ${name} loop activated` : `:arrow_right: ${name} loop disabled`; } /** * Remove a track from the queue by index. * @param {number} index - 0-based. * @returns {string} * @throws {Error} If index is empty. */ remove(index) { if (index === undefined || index === null) throw new Error("Index can't be empty"); const oldSize = this.queue.size(); const msg = this.queue.remove(index); if (oldSize !== this.queue.size()) this.emit("update", "queue"); if (this.isEmpty() && !this.queue.getCurrent()) { this._startInactivityTimer(); } return msg; } /** * Remove a slice or range of tracks from the queue (0-based indices, inclusive). * @param {number} from - Starting 0-based index. * @param {number} to - Ending 0-based index. * @returns {string} Result message. */ removeRange(from, to) { if (this.isEmpty()) return "The queue is empty."; const oldSize = this.queue.size(); const result = this.queue.removeRange(from, to); if (typeof result === "string") return result; if (oldSize !== this.queue.size()) this.emit("update", "queue"); if (this.isEmpty() && !this.queue.getCurrent()) { this._startInactivityTimer(); } return `:wastebasket: Successfully removed **${result.count}** track${result.count === 1 ? "" : "s"} from the queue.`; } /** @private Stop the audio bridge if currently playing. */ _bridgeStop() { if (this._audioBridge) { try { this._audioBridge.stop(); } catch (_) {} } } /** * @private Send the self-deafen voice state update. Best effort: the * VoiceManager silently no-ops when the connection has no connection_id * yet (missing from VoiceServerUpdate) — re-sent after every * requestVoiceStateSync so later attempts still land once it exists. * @param {object} vm - VoiceManager instance. * @param {string} channelId - Voice channel ID. */ _assertSelfDeaf(vm, channelId) { try { vm.updateVoiceState(channelId, { self_deaf: true, self_mute: false }); } catch (e) { logger.warn("[Player] Self-deafen failed:", e.message); } } /** * @private Translate a locale key for this player's guild. * @param {string} key * @param {object} [replacements={}] * @returns {string} */ _t(key, replacements = {}) { if (!this.locale) return key; return this.locale.translate(this._guildId, key, replacements); } } applyMixins(Player, PlaybackMixin, SearchMixin, DisplayMixin); export { Player as default, Player, Queue };