import { CommandBuilder } from "../src/CommandHandler.mjs"; import { Utils } from "../src/Utils.mjs"; import { EmbedBuilder } from "@fluxerjs/core"; import { getGlobalColor } from "../src/MessageHandler.mjs"; export const command = new CommandBuilder() .setName("player") .setDescription("Create an interactive player control panel with live progress", "commands.player") .setCategory("music"); // Visual states const STATES = { playing: "๐ŸŽต", paused: "โธ๏ธ", stopped: "๐Ÿ”‡", loading: "โณ" }; // Control layout const CONTROLS = { prev: { emoji: "โฎ๏ธ", action: "previous", desc: "Previous" }, play: { emoji: "โ–ถ๏ธ", action: "resume", desc: "Play" }, pause: { emoji: "โธ๏ธ", action: "pause", desc: "Pause" }, stop: { emoji: "โน๏ธ", action: "stop", desc: "Stop" }, next: { emoji: "โญ๏ธ", action: "skip", desc: "Skip" }, loop: { emoji: "๐Ÿ”", action: "loop", desc: "Loop" }, shuffle: { emoji: "๐Ÿ”€", action: "shuffle", desc: "Shuffle" }, volDown: { emoji: "๐Ÿ”‰", action: "voldown", desc: "Volume Down" }, volUp: { emoji: "๐Ÿ”Š", action: "volup", desc: "Volume Up" }, lyrics: { emoji: "๐Ÿ“œ", action: "lyrics", desc: "Lyrics" }, close: { emoji: "โŒ", action: "close", desc: "Close" } }; // Progress bar characters const PROGRESS = { filled: "โ–ฐ", empty: "โ–ฑ", indicator: "โ—", start: "โ–", end: "โ–•" }; // Auto-remove timer: 1 minute const EMOJI_REMOVE_TIMEOUT = 60000; export async function run(msg) { const player = await this.getPlayer(msg, false, false, false); if (!player) return; const timeout = this.config?.timers?.playerSessionTimeout ?? this.config.playerAFKTimeout ?? 300000; const controlsLayout = [ [CONTROLS.prev, CONTROLS.play, CONTROLS.pause, CONTROLS.stop, CONTROLS.next], [CONTROLS.loop, CONTROLS.shuffle, CONTROLS.volDown, CONTROLS.volUp, CONTROLS.lyrics], [CONTROLS.close] ]; const allControls = controlsLayout.flat(); const controlEmojis = allControls.map(c => c.emoji); const buildEmbed = (state = {}) => { const current = player.queue.getCurrent(); const isPlaying = !player.paused && current; const statusEmoji = isPlaying ? STATES.playing : player.paused ? STATES.paused : STATES.stopped; let progressBar = ""; let timeDisplay = "`0:00 / 0:00`"; if (current?.duration && player.startedPlaying) { const elapsed = Date.now() - player.startedPlaying; const totalMs = typeof current.duration === "object" ? (current.duration.seconds ?? 0) * 1000 : current.duration; const progress = Math.min(elapsed / totalMs, 1); const barLength = 20; const filled = Math.floor(progress * barLength); const position = Math.min(Math.max(filled, 0), barLength - 1); const emptyCount = Math.max(barLength - position - 1, 0); progressBar = PROGRESS.start + PROGRESS.filled.repeat(position) + (isPlaying ? PROGRESS.indicator : PROGRESS.filled) + PROGRESS.empty.repeat(emptyCount) + PROGRESS.end; // Use Utils.prettifyMS instead of local formatTime const elapsedStr = Utils.prettifyMS(elapsed); const totalStr = Utils.prettifyMS(totalMs); timeDisplay = `\`${elapsedStr} / ${totalStr}\``; } else { progressBar = PROGRESS.start + PROGRESS.empty.repeat(20) + PROGRESS.end; } const volPercent = Math.round((player.preferredVolume || 1) * 100); const volBars = Math.ceil(volPercent / 10); const volumeBar = "โ–ˆ".repeat(volBars) + "โ–‘".repeat(10 - volBars); const queueSize = player.queue.size(); const loopStatus = player.queue.songLoop ? "๐Ÿ”‚ Song" : player.queue.loop ? "๐Ÿ” Queue" : "โŒ Off"; const nowPlaying = current // Use Utils.truncate instead of local truncate ? `[${Utils.truncate(current.title, 45)}](${current.spotifyUrl || current.url})` : "*Nothing playing*"; const description = [ `${statusEmoji} **Now Playing**`, `${nowPlaying}`, ``, `${progressBar}`, `${timeDisplay}`, ``, `๐Ÿ”Š Volume: \`${volPercent}%\` ${volumeBar}`, `๐Ÿ“‹ Queue: \`${queueSize}\` tracks | Loop: ${loopStatus}`, ``, state.message ? `๐Ÿ’ฌ *${state.message}*` : "๐Ÿ’ก *React to control playback*", ``, `โฑ๏ธ Session expires in ${Math.ceil(timeout / 60000)}m of inactivity` ].join("\n"); // avatarURL may be a plain property (Fluxer) or a methodโ€” call it if // it's a function, otherwise use it as-is so the footer icon works in both cases. const avatarUrl = typeof msg.author?.avatarURL === "function" ? msg.author.avatarURL() : msg.author?.avatarURL ?? null; const builder = new EmbedBuilder() .setColor(getGlobalColor()) .setTitle("๐ŸŽง Music Player Controls") .setDescription(description) .setFooter({ text: `Requested by ${msg.author?.username || "Unknown"} โ€ข Controls active`, iconURL: avatarUrl }); // setTimestamp() is not verified on @fluxerjs/core's EmbedBuilder โ€” guard it. if (typeof builder.setTimestamp === "function") builder.setTimestamp(); if (current?.thumbnail) builder.setThumbnail(current.thumbnail); return builder.toJSON(); }; const message = await msg.replyEmbed({ embeds: [buildEmbed()] }); if (!message?.message) return; // Add reactions with staggered animation for (const row of controlsLayout) { for (const control of row) { try { await message.message.react(control.emoji); await Utils.sleep(200); // Use Utils.sleep instead of inline Promise } catch (_) {} } } let sessionTimeout; let updateInterval; let emojiRemoveTimeout; let lastState = {}; // Track active lyrics message let activeLyricsMsg = null; let lyricsUnobserve = null; let lyricsEmojiTimeout = null; // Helper: Clear all reactions const clearReactions = async () => { try { await message.message.removeAllReactions(); } catch (e) { for (const emoji of controlEmojis) { try { await message.message.removeReaction(emoji); } catch (_) {} } } }; // Helper: Reset emoji removal timer const resetEmojiTimer = () => { clearTimeout(emojiRemoveTimeout); emojiRemoveTimeout = setTimeout(async () => { await clearReactions(); const disabledEmbed = buildEmbed({ message: "โŒ› Controls disabled (react to re-enable)" }); disabledEmbed.footer = { text: "Controls expired โ€ข React to refresh" }; await message.editEmbed({ embeds: [disabledEmbed] }).catch(() => {}); }, EMOJI_REMOVE_TIMEOUT); }; // Helper: Clear lyrics reactions const clearLyricsReactions = async (lyricsMsg) => { if (!lyricsMsg?.message) return; try { await lyricsMsg.message.removeAllReactions(); } catch (e) { for (const emoji of ["โฌ…๏ธ", "โžก๏ธ", "โŒ"]) { try { await lyricsMsg.message.removeReaction(emoji); } catch (_) {} } } }; const refresh = (extra = {}) => { const embed = buildEmbed(extra); message.editEmbed({ embeds: [embed] }).catch(() => {}); lastState = extra; }; const closeSession = async (reason = "timeout") => { clearTimeout(sessionTimeout); clearTimeout(emojiRemoveTimeout); clearInterval(updateInterval); unobserve?.(); // Remove all player event listeners added by this session player.off("startplay", onStartPlay); player.off("playback", onPlayback); player.off("stopplay", onStopPlay); player.off("queue", onQueue); player.off("volume", onVolume); player.off("autoleave", onAutoLeave); // Cleanup any active lyrics if (lyricsUnobserve) { lyricsUnobserve(); clearTimeout(lyricsEmojiTimeout); } if (activeLyricsMsg) { await clearLyricsReactions(activeLyricsMsg); } await clearReactions(); const closedEmbed = buildEmbed({ message: `Session closed${reason !== "timeout" ? ` โ€ข ${reason}` : " due to inactivity"}` }); // Override title and footer on the plain object returned by toJSON() closedEmbed.color = getGlobalColor(); closedEmbed.footer = { text: "Session ended" }; closedEmbed.title = "๐ŸŽง Music Player (Inactive)"; await message.editEmbed({ embeds: [closedEmbed], content: reason === "user" ? "๐Ÿ‘‹ Player controls closed" : undefined }).catch(() => {}); }; const resetTimeout = () => { clearTimeout(sessionTimeout); sessionTimeout = setTimeout(() => closeSession("timeout"), timeout); }; // Live progress updates updateInterval = setInterval(() => { if (!player.paused && player.queue.getCurrent()) { refresh(lastState); } }, this.config?.timers?.playerUpdateInterval ?? 5000); // Player events โ€” store named handlers so we can remove them on closeSession const onStartPlay = () => refresh({ message: "โ–ถ๏ธ Started playing" }); const onPlayback = (playing) => refresh({ message: playing ? "โ–ถ๏ธ Resumed" : "โธ๏ธ Paused" }); const onStopPlay = () => refresh({ message: "โน๏ธ Stopped" }); const onQueue = (e) => { if (e.type === "shuffle") refresh({ message: "๐Ÿ”€ Queue shuffled" }); if (e.type === "add") refresh({ message: `โž• Added: ${Utils.truncate(e.data.data.title, 30)}` }); }; const onVolume = (v) => refresh({ message: `๐Ÿ”Š Volume: ${Math.round(v * 100)}%` }); const onAutoLeave = () => closeSession("disconnected"); player.on("startplay", onStartPlay); player.on("playback", onPlayback); player.on("stopplay", onStopPlay); player.on("queue", onQueue); player.on("volume", onVolume); player.on("autoleave", onAutoLeave); // Reaction handler const unobserve = message.onReaction(controlEmojis, async (e) => { const control = allControls.find(c => c.emoji === e.emoji_id); if (!control) return; resetEmojiTimer(); resetTimeout(); let reply = ""; let shouldUpdate = true; try { switch (control.action) { case "previous": reply = "โฎ๏ธ Previous track (not implemented)"; break; case "resume": if (player.paused) { reply = player.resume(); } else if (!player.queue.getCurrent() && !player.queue.isEmpty()) { player.playNext(); reply = "โ–ถ๏ธ Starting playback"; } else { reply = "โ–ถ๏ธ Already playing"; } break; case "pause": reply = player.pause(); break; case "stop": await player._stopMediaPlayer(); player.queue.reset(); reply = "โน๏ธ Stopped and cleared"; break; case "skip": reply = player.skip(); break; case "loop": { const currentLoop = player.queue.loop; const currentSongLoop = player.queue.songLoop; if (!currentLoop && !currentSongLoop) { player.queue.setSongLoop(true); reply = "๐Ÿ”‚ Song loop enabled"; } else if (currentSongLoop) { player.queue.setSongLoop(false); player.queue.setLoop(true); reply = "๐Ÿ” Queue loop enabled"; } else { player.queue.setLoop(false); reply = "โŒ Loop disabled"; } break; } case "shuffle": reply = player.shuffle(); break; case "voldown": { const newVolDown = Utils.clamp((player.preferredVolume || 1) - 0.1, 0, 1); reply = player.setVolume(newVolDown); break; } case "volup": { const newVolUp = Utils.clamp((player.preferredVolume || 1) + 0.1, 0, 1); reply = player.setVolume(newVolUp); break; } case "lyrics": reply = "๐Ÿ“œ Fetching lyrics..."; refresh({ message: reply }); try { // Cleanup previous lyrics if (lyricsUnobserve) { lyricsUnobserve(); clearTimeout(lyricsEmojiTimeout); if (activeLyricsMsg) await clearLyricsReactions(activeLyricsMsg); } const lyricsResult = await player.lyrics(); if (!lyricsResult) { reply = "โŒ No lyrics found in NodeLink"; shouldUpdate = true; break; } const syncBadge = lyricsResult.synced ? " โฑ๏ธ Synced" : ""; const lines = lyricsResult.text.split('\n'); const totalLines = lines.length; const LINES_PER_PAGE = 25; const totalPages = Math.ceil(totalLines / LINES_PER_PAGE); // Build pages const pages = []; for (let i = 0; i < totalLines; i += LINES_PER_PAGE) { pages.push(lines.slice(i, i + LINES_PER_PAGE).join('\n')); } let currentPage = 0; const buildLyricsContent = (pageIdx, expired = false, closed = false) => { const title = Utils.truncate( player.queue.getCurrent()?.title?.replace(/\(Official.*?\)/gi, '').trim() ?? '', 50 ); const footerText = closed ? `๐Ÿ‘‹ Lyrics closed โ€ข NodeLink โ€ข ${totalLines} lines` : expired ? `โŒ› Controls expired โ€ข NodeLink โ€ข ${totalLines} lines` : `NodeLink โ€ข ${totalLines} lines total${lyricsResult.synced ? ' โ€ข Synced' : ''}`; const desc = [ `**${title}**${syncBadge} โ€ข Page ${pageIdx + 1}/${totalPages}`, ``, '```', pages[pageIdx], totalPages > 1 && !expired && !closed ? `\n\n๐Ÿ’ก โฌ…๏ธ โžก๏ธ Navigate โ€ข โŒ Close` : '', '```' ].join('\n'); return { embeds: [new EmbedBuilder().setColor(getGlobalColor()).setDescription(desc).setFooter({ text: footerText }).toJSON()] }; }; // Send initial lyrics activeLyricsMsg = await msg.replyEmbed(buildLyricsContent(0)); // Add pagination and close button if needed if (activeLyricsMsg?.message && totalPages > 1) { const navEmojis = ["โฌ…๏ธ", "โžก๏ธ", "โŒ"]; for (const emoji of navEmojis) { await activeLyricsMsg.message.react(emoji).catch(() => {}); } // Reset timer helper const resetLyricsTimer = () => { clearTimeout(lyricsEmojiTimeout); lyricsEmojiTimeout = setTimeout(async () => { await clearLyricsReactions(activeLyricsMsg); await activeLyricsMsg.editEmbed(buildLyricsContent(currentPage, true, false)).catch(() => {}); }, EMOJI_REMOVE_TIMEOUT); }; // Navigation handler lyricsUnobserve = activeLyricsMsg.onReaction(navEmojis, async (e) => { // Close button if (e.emoji_id === "โŒ") { lyricsUnobserve(); clearTimeout(lyricsEmojiTimeout); await clearLyricsReactions(activeLyricsMsg); await activeLyricsMsg.editEmbed(buildLyricsContent(currentPage, false, true)).catch(() => {}); return; } // Reset timer on navigation resetLyricsTimer(); if (e.emoji_id === "โฌ…๏ธ") { currentPage = currentPage > 0 ? currentPage - 1 : totalPages - 1; } else if (e.emoji_id === "โžก๏ธ") { currentPage = currentPage < totalPages - 1 ? currentPage + 1 : 0; } await activeLyricsMsg.editEmbed(buildLyricsContent(currentPage)); }); // Start 1 minute timer resetLyricsTimer(); } else if (activeLyricsMsg?.message) { // Single page - just add close button await activeLyricsMsg.message.react("โŒ").catch(() => {}); const resetLyricsTimer = () => { clearTimeout(lyricsEmojiTimeout); lyricsEmojiTimeout = setTimeout(async () => { await clearLyricsReactions(activeLyricsMsg); await activeLyricsMsg.editEmbed(buildLyricsContent(0, true, false)).catch(() => {}); }, EMOJI_REMOVE_TIMEOUT); }; lyricsUnobserve = activeLyricsMsg.onReaction(["โŒ"], async (e) => { if (e.emoji_id === "โŒ") { lyricsUnobserve(); clearTimeout(lyricsEmojiTimeout); await clearLyricsReactions(activeLyricsMsg); await activeLyricsMsg.editEmbed(buildLyricsContent(0, false, true)).catch(() => {}); } }); resetLyricsTimer(); } reply = `Lyrics displayed (${totalLines} lines, ${totalPages} pages)`; } catch (err) { // Use Utils.truncate instead of local truncate reply = `โš ๏ธ Lyrics error: ${Utils.truncate(err.message, 50)}`; } shouldUpdate = true; break; case "close": await closeSession("user"); return; } } catch (err) { reply = `โš ๏ธ Error: ${err.message}`; } if (shouldUpdate) refresh({ message: reply }); }); // Start timers resetTimeout(); resetEmojiTimer(); }