mirror of
https://github.com/remix-bot/fluxer.git
synced 2026-11-24 06:01:44 -05:00
- Add Locale class (src/constants/Locale.mjs) with per-guild translation
support and settings-manager binding
- Wire locale into CommandHandler, MessageHandler, Player, PlayerManager,
and index.mjs via setLocale() / t() helpers
- Replace all hardcoded English strings (help pages, errors, now-playing,
permission messages, session-closed footers) with locale key lookups
- Fix: %help triggering "Unknown command" — replyHandler intercept now
uses case-insensitive check for "unknown command"
- Fix: TypeError crash in closeSession when embed has no footer — guard
with `if (!footer) footer = { text: "" }` before appending
- Fix: media player teardown now calls destroy() and nulls the instance
instead of relying on _stopMediaPlayer(), preventing stale state
- Refactor: reply() and send() auto-wrap plain strings in embeds,
matching old replyEmbed/sendEmbed behaviour
- Refactor: permission errors and invalid-flag errors use t() lookups
- Add top-level t(message, key, data) helper on the bot instance for
command files to resolve guild locale without manual guildId plumbing
175 lines
6.1 KiB
JavaScript
175 lines
6.1 KiB
JavaScript
import { CommandBuilder } from "../src/CommandHandler.mjs";
|
|
import { logger } from "../src/constants/Logger.mjs";
|
|
import { EmbedBuilder } from "@fluxerjs/core";
|
|
import { getGlobalColor } from "../src/MessageHandler.mjs";
|
|
import { Utils } from "../src/Utils.mjs";
|
|
|
|
export const command = new CommandBuilder()
|
|
.setName("lyrics")
|
|
.setDescription("Display synced lyrics from NodeLink", "commands.lyrics")
|
|
.addAliases("lyric", "ly")
|
|
.setCategory("music");
|
|
|
|
const LOADING_FRAMES = ["◐", "◓", "◑", "◒"];
|
|
const EMOJI_REMOVE_TIMEOUT = 60_000;
|
|
const SESSION_MS = 180_000;
|
|
|
|
export async function run(message) {
|
|
const p = await this.getPlayer(message, false, false, false);
|
|
if (!p) return;
|
|
|
|
const current = p.queue.getCurrent();
|
|
if (!current) {
|
|
const embed = new EmbedBuilder().setColor(getGlobalColor()).setDescription(this.t(message, "responses.lyrics.nothingPlaying"));
|
|
return message.reply({ embeds: [embed] });
|
|
}
|
|
|
|
// Animated loading spinner
|
|
let frame = 0;
|
|
const loadingMsg = await message.reply({ embeds: [
|
|
new EmbedBuilder().setColor(getGlobalColor())
|
|
.setDescription(`${LOADING_FRAMES[0]} ` + this.t(message, "responses.lyrics.searching", { title: Utils.truncate(current.title, 40) }))
|
|
|
|
] });
|
|
|
|
const loadingInterval = setInterval(() => {
|
|
frame = (frame + 1) % LOADING_FRAMES.length;
|
|
loadingMsg.edit({ embeds: [
|
|
new EmbedBuilder().setColor(getGlobalColor())
|
|
.setDescription(`${LOADING_FRAMES[frame]} ` + this.t(message, "responses.lyrics.searching", { title: Utils.truncate(current.title, 40) }))
|
|
|
|
] }).catch(() => {});
|
|
}, 800);
|
|
|
|
try {
|
|
const result = await p.lyrics();
|
|
|
|
clearInterval(loadingInterval);
|
|
|
|
if (!result || !result.text) {
|
|
return loadingMsg.edit({ embeds: [
|
|
new EmbedBuilder().setColor(getGlobalColor())
|
|
.setDescription(
|
|
this.t(message, "responses.lyrics.noLyrics", { title: Utils.truncate(current.title, 40) })
|
|
)
|
|
] });
|
|
}
|
|
|
|
try { await loadingMsg.message.delete(); } catch {}
|
|
|
|
const syncIndicator = result.synced ? this.t(message, "responses.lyrics.syncedBadge") : "";
|
|
const displayTitle = Utils.cleanTitle(current.title);
|
|
const artist = current.artists?.[0]?.name || current.author?.name || "Unknown Artist";
|
|
const lines = result.text.split("\n").filter(l => l.trim());
|
|
const totalLines = lines.length;
|
|
|
|
const LINES_PER_PAGE = 25;
|
|
const totalPages = Math.ceil(totalLines / LINES_PER_PAGE);
|
|
|
|
// Single page — no pagination needed
|
|
if (totalPages === 1) {
|
|
const singleEmbed = new EmbedBuilder()
|
|
.setColor(getGlobalColor())
|
|
.setTitle(`🎵 ${Utils.truncate(displayTitle, 50)}`)
|
|
.setDescription([
|
|
`by *${artist}*${syncIndicator}`,
|
|
``,
|
|
"```",
|
|
lines.join("\n"),
|
|
"```"
|
|
].join("\n"))
|
|
.setFooter({ text: this.t(message, "responses.lyrics.nodeLinkFooter", { lines: totalLines }) });
|
|
if (current.thumbnail) singleEmbed.setThumbnail(current.thumbnail);
|
|
return message.reply({ embeds: [singleEmbed] });
|
|
}
|
|
|
|
// Multi-page
|
|
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;
|
|
let emojiRemoveTimeout;
|
|
|
|
const buildContent = (pageIdx, expired = false) => {
|
|
const b = new EmbedBuilder()
|
|
.setColor(getGlobalColor())
|
|
.setTitle(`🎵 ${Utils.truncate(displayTitle, 50)}`)
|
|
.setDescription([
|
|
`by *${artist}*${syncIndicator} • Page ${pageIdx + 1}/${totalPages}`,
|
|
``,
|
|
"```",
|
|
pages[pageIdx],
|
|
"```"
|
|
].join("\n"))
|
|
.setFooter({ text: expired
|
|
? this.t(message, "responses.lyrics.controlsExpired", { lines: totalLines })
|
|
: this.t(message, "responses.lyrics.nodeLinkFooter", { lines: totalLines }) });
|
|
if (current.thumbnail) b.setThumbnail(current.thumbnail);
|
|
return { embeds: [b] };
|
|
};
|
|
|
|
const msg = await message.reply(buildContent(0));
|
|
if (!msg?.message) return;
|
|
|
|
const emojis = ["⬅️", "➡️"];
|
|
for (const emoji of emojis) {
|
|
await msg.message.react(emoji).catch(() => {});
|
|
}
|
|
|
|
const clearReactions = async () => {
|
|
try {
|
|
await msg.message.removeAllReactions();
|
|
} catch {
|
|
for (const emoji of emojis) {
|
|
await msg.message.removeReaction(emoji).catch(() => {});
|
|
}
|
|
}
|
|
};
|
|
|
|
const resetEmojiTimer = () => {
|
|
clearTimeout(emojiRemoveTimeout);
|
|
emojiRemoveTimeout = setTimeout(async () => {
|
|
await clearReactions();
|
|
msg.edit(buildContent(currentPage, true)).catch(() => {});
|
|
}, EMOJI_REMOVE_TIMEOUT);
|
|
};
|
|
|
|
const unobserve = msg.onReaction(emojis, async (e) => {
|
|
resetEmojiTimer();
|
|
if (e.emoji_id === "⬅️") {
|
|
currentPage = currentPage > 0 ? currentPage - 1 : totalPages - 1;
|
|
} else if (e.emoji_id === "➡️") {
|
|
currentPage = currentPage < totalPages - 1 ? currentPage + 1 : 0;
|
|
}
|
|
msg.edit(buildContent(currentPage)).catch(() => {});
|
|
});
|
|
|
|
resetEmojiTimer();
|
|
|
|
// Full session close after SESSION_MS
|
|
setTimeout(() => {
|
|
clearTimeout(emojiRemoveTimeout);
|
|
unobserve();
|
|
const closedEmbed = new EmbedBuilder()
|
|
.setColor(getGlobalColor())
|
|
.setTitle(`🎵 ${Utils.truncate(displayTitle, 50)}`)
|
|
.setDescription([
|
|
`by *${artist}*${syncIndicator} • Page ${currentPage + 1}/${totalPages}`,
|
|
``,
|
|
"```",
|
|
pages[currentPage],
|
|
"```"
|
|
].join("\n"))
|
|
.setFooter({ text: this.t(message, "responses.lyrics.sessionClosed", { lines: totalLines }) });
|
|
if (current.thumbnail) closedEmbed.setThumbnail(current.thumbnail);
|
|
msg.edit({ embeds: [closedEmbed] }).catch(() => {});
|
|
}, SESSION_MS);
|
|
|
|
} catch (err) {
|
|
clearInterval(loadingInterval);
|
|
logger.error("[Lyrics Command] Error:", err);
|
|
loadingMsg.edit({ embeds: [new EmbedBuilder().setColor(getGlobalColor()).setDescription(this.t(message, "responses.lyrics.error", { error: Utils.truncate(err.message, 100) }))] }).catch(() => {});
|
|
}
|
|
} |