Files
fluxer/commands/lastfm/discovery.mjs
T
NoLogicAlan 253d11cd57 Remix — the complete rewrite
Upgrades the bot from fluxerjs 2.2 to 3.0 and restructures everything
into a layered architecture (~28K lines):

Dependencies:
- @fluxerjs/core, util, voice: ^2.2.0 -> ^3.0.0 (typed events,
  PartialMessage payloads, camelCase API)
- @fluxerjs/sharding ^3.0.0 added — built-in sharding mode
  (shard.mjs, one process per shard)
- @discordjs/opus dropped — opusscript + prism-media cover encoding

Architecture:
- commands: every command split into submodules (player, lastfm,
  settings, debug) with a shared CommandBuilder/Loader/Handler core
- ui: embeds, paginators, help command, permission checks
- voice: gateway event routing, rejoin/247 boot recovery, state resolver
- music: player split into Queue/Search/Playback/Display mixins,
  stream pipeline + WebM/Opus muxer (no FFmpeg needed)
- dashboard: Redis pub/sub, RPC handlers, serializers
- db: MySQL manager + per-guild settings
- services: LastFm manager split into focused mixins
- utils: sharding, 24/7 helpers, API client

Runtime fixes over v2:
- Redis v5 reconnect: capped numeric backoff (0.5s ramp to 15s),
  never exhausts, auto-recovers when the server comes back
- Redis main/subscriber clients connect in parallel; clean shutdown
- Logger storm guard: identical error/warn lines deduped in a 60s window
- self-deaf fix on voice join
- config example uses redis://127.0.0.1 (avoids IPv6 ENETUNREACH)

Housekeeping: dead code removed (Tuna.mjs + follow-redirects dep),
.gitignore added (config.json, .env, node_modules, runtime state).
2026-09-09 02:04:33 +03:00

373 lines
13 KiB
JavaScript

/**
* @module commands/lastfm/discovery
* @description Charts and discovery actions for the lastfm command (friends, weekly, trending, geo, artisttracks, search). Extracted verbatim from the original single-file command; dispatched by commands/lastfm.mjs.
*/
import { EmbedBuilder } from "@fluxerjs/core";
import { getGlobalColor } from "../../src/ui/index.mjs";
import { Utils } from "../../src/utils/Utils.mjs";
import { ERROR_COLOR } from "../../src/utils/UI.mjs";
import { notLinked, extractCurrentTrack } from "./shared.mjs";
/**
* Action names dispatched to this module.
* @type {Set<string>}
*/
export const DISCOVERY_ACTIONS = new Set([
"friends",,
"fr",,
"weekly",,
"wc",,
"trending",,
"tr",,
"geo",,
"g",,
"artisttracks",,
"atr",,
"search",,
"s"
]);
/**
* @async
* @param {object} msg - The command message wrapper.
* @param {object} data - Parsed command data.
* @param {object} lastfm - The Last.fm manager instance.
* @param {string} prefix - The guild command prefix.
* @param {string} userId - The invoking user's ID.
* @param {string|null} targetUserId - The targeted user's ID, if any.
* @param {string} action - The resolved action name.
* @returns {Promise<*>} Whatever the original switch returned for this action.
*/
export async function runDiscoveryActions(msg, data, lastfm, prefix, userId, targetUserId, action) {
switch (action) {
case "friends":
case "fr": {
const user = await lastfm.getUser(userId);
if (!user) return msg.reply(notLinked(this, msg, prefix));
let friends;
try {
friends = await lastfm.getUserFriends(userId, 20);
} catch (err) {
return msg.reply({
embeds: [new EmbedBuilder().setColor(ERROR_COLOR).setDescription(this.t(msg, "responses.lastfm.fetchFailed", { error: err.message }))]
});
}
if (!friends.length) {
return msg.reply({
embeds: [new EmbedBuilder().setColor(getGlobalColor()).setDescription(`👥 No friends found for **${user.username}**.`)]
});
}
const lines = friends.map((f, i) => {
const num = String(i + 1).padStart(2, " ");
const link = f.url ? `[${f.name}](${f.url})` : f.name;
const details = [];
if (f.realname) details.push(f.realname);
if (f.country) details.push(f.country);
const detailStr = details.length > 0 ? ` (${details.join(", ")})` : "";
return `\`${num}.\` ${link}${detailStr}`;
});
const desc = lines.join("\n").slice(0, 4096);
return msg.reply({
embeds: [new EmbedBuilder()
.setColor(getGlobalColor())
.setTitle(`👥 Friends — ${user.username}`)
.setDescription(desc)]
});
}
case "weekly":
case "wc": {
const user = await lastfm.getUser(userId);
if (!user) return msg.reply(notLinked(this, msg, prefix));
const tokenTextWc = data.get("token")?.value?.toLowerCase().trim() ?? "artists";
let chartData;
let chartTitle;
try {
if (tokenTextWc === "tracks") {
chartData = await lastfm.getUserWeeklyTrackChart(userId);
chartTitle = `📅 Weekly Track Chart — ${user.username}`;
} else if (tokenTextWc === "albums") {
chartData = await lastfm.getUserWeeklyAlbumChart(userId);
chartTitle = `📅 Weekly Album Chart — ${user.username}`;
} else {
chartData = await lastfm.getUserWeeklyArtistChart(userId);
chartTitle = `📅 Weekly Artist Chart — ${user.username}`;
}
} catch (err) {
return msg.reply({
embeds: [new EmbedBuilder().setColor(ERROR_COLOR).setDescription(this.t(msg, "responses.lastfm.fetchFailed", { error: err.message }))]
});
}
if (!chartData.length) {
return msg.reply({
embeds: [new EmbedBuilder().setColor(getGlobalColor()).setDescription(`📅 No weekly chart data found for **${user.username}**.`)]
});
}
const lines = chartData.slice(0, 15).map((item, i) => {
const num = String(i + 1).padStart(2, " ");
const link = item.url ? `[${item.name}](${item.url})` : item.name;
const artistStr = item.artist ? ` by **${item.artist}**` : "";
return `\`${num}.\` ${link}${artistStr} — **${Utils.formatNumber(item.playcount)}** plays`;
});
const desc = lines.join("\n").slice(0, 4096);
return msg.reply({
embeds: [new EmbedBuilder()
.setColor(getGlobalColor())
.setTitle(chartTitle)
.setDescription(desc)
.setFooter({ text: `💡 Use token option: artists, tracks, or albums` })]
});
}
case "trending":
case "tr": {
const tokenTextTr = data.get("token")?.value?.toLowerCase().trim() ?? "tracks";
let trendingData;
let trendingTitle;
try {
if (tokenTextTr === "artists") {
trendingData = await lastfm.getChartTopArtists(15);
trendingTitle = "🔥 Trending Artists on Last.fm";
} else {
trendingData = await lastfm.getChartTopTracks(15);
trendingTitle = "🔥 Trending Tracks on Last.fm";
}
} catch (err) {
return msg.reply({
embeds: [new EmbedBuilder().setColor(ERROR_COLOR).setDescription(this.t(msg, "responses.lastfm.fetchFailed", { error: err.message }))]
});
}
if (!trendingData.length) {
return msg.reply({
embeds: [new EmbedBuilder().setColor(getGlobalColor()).setDescription(`🔥 No trending data found.`)]
});
}
const lines = trendingData.map((item, i) => {
const num = String(i + 1).padStart(2, " ");
const link = item.url ? `[${item.name}](${item.url})` : item.name;
const artistStr = item.artist ? ` by **${item.artist}**` : "";
const extra = item.listeners ? ` — ${Utils.formatNumber(item.listeners)} listeners` : item.playcount ? ` — ${Utils.formatNumber(item.playcount)} plays` : "";
return `\`${num}.\` ${link}${artistStr}${extra}`;
});
const desc = lines.join("\n").slice(0, 4096);
return msg.reply({
embeds: [new EmbedBuilder()
.setColor(getGlobalColor())
.setTitle(trendingTitle)
.setDescription(desc)
.setFooter({ text: `💡 Use token option: tracks or artists` })]
});
}
case "geo":
case "g": {
const tokenTextG = data.get("token")?.value?.trim() ?? "";
if (!tokenTextG) {
return msg.reply({
embeds: [new EmbedBuilder().setColor(ERROR_COLOR).setDescription(`❌ Provide a query like \`artists <country>\` or \`tracks <country>\`. Example: \`${prefix}lastfm geo artists united states\`.`)]
});
}
const lowerToken = tokenTextG.toLowerCase();
let type = "artists";
let country = tokenTextG;
if (lowerToken.startsWith("artists ")) {
type = "artists";
country = tokenTextG.slice(8).trim();
} else if (lowerToken.startsWith("tracks ")) {
type = "tracks";
country = tokenTextG.slice(7).trim();
}
if (!country) {
return msg.reply({
embeds: [new EmbedBuilder().setColor(ERROR_COLOR).setDescription(`❌ Please specify a country name. Example: \`${prefix}lastfm geo artists united states\`.`)]
});
}
let geoData;
let geoTitle;
try {
if (type === "tracks") {
geoData = await lastfm.getGeoTopTracks(country, 15);
geoTitle = `🌍 Top Tracks in ${country}`;
} else {
geoData = await lastfm.getGeoTopArtists(country, 15);
geoTitle = `🌍 Top Artists in ${country}`;
}
} catch (err) {
return msg.reply({
embeds: [new EmbedBuilder().setColor(ERROR_COLOR).setDescription(this.t(msg, "responses.lastfm.fetchFailed", { error: err.message }))]
});
}
if (!geoData.length) {
return msg.reply({
embeds: [new EmbedBuilder().setColor(getGlobalColor()).setDescription(`🌍 No data found for **${country}**.`)]
});
}
const lines = geoData.map((item, i) => {
const num = String(i + 1).padStart(2, " ");
const link = item.url ? `[${item.name}](${item.url})` : item.name;
const artistStr = item.artist ? ` by **${item.artist}**` : "";
const extra = item.listeners ? ` — ${Utils.formatNumber(item.listeners)} listeners` : "";
return `\`${num}.\` ${link}${artistStr}${extra}`;
});
const desc = lines.join("\n").slice(0, 4096);
return msg.reply({
embeds: [new EmbedBuilder()
.setColor(getGlobalColor())
.setTitle(geoTitle)
.setDescription(desc)]
});
}
case "artisttracks":
case "atr": {
const user = await lastfm.getUser(userId);
if (!user) return msg.reply(notLinked(this, msg, prefix));
let artistName = null;
const pAtr = await this.getPlayer(msg, false, false, false);
const current = extractCurrentTrack(pAtr);
if (current?.artist) {
artistName = current.artist;
}
const tokenTextAtr = data.get("token")?.value;
if (tokenTextAtr) {
artistName = tokenTextAtr.trim();
}
if (!artistName) {
return msg.reply({
embeds: [new EmbedBuilder().setColor(ERROR_COLOR).setDescription(this.t(msg, "responses.lastfm.noArtistSpecified"))]
});
}
let recentTracks;
try {
recentTracks = await lastfm.getRecentTracks(userId, 200);
} catch (err) {
return msg.reply({
embeds: [new EmbedBuilder().setColor(ERROR_COLOR).setDescription(this.t(msg, "responses.lastfm.fetchFailed", { error: err.message }))]
});
}
const artistTracks = recentTracks.filter(t =>
t.artist.toLowerCase() === artistName.toLowerCase()
);
if (!artistTracks.length) {
return msg.reply({
embeds: [new EmbedBuilder().setColor(getGlobalColor()).setDescription(`🎵 No scrobbles found for **${artistName}** in your recent history.`)]
});
}
const lines = artistTracks.slice(0, 15).map((t, i) => {
const num = String(i + 1).padStart(2, " ");
const link = t.url ? `[${t.name}](${t.url})` : t.name;
const nowStr = t.now ? " 🎵" : "";
return `\`${num}.\` ${link}${nowStr}`;
});
const desc = lines.join("\n").slice(0, 4096);
return msg.reply({
embeds: [new EmbedBuilder()
.setColor(getGlobalColor())
.setTitle(`🎵 ${artistName} Scrobbles — ${user.username}`)
.setDescription(desc)
.setFooter({ text: `${artistTracks.length} scrobble${artistTracks.length !== 1 ? "s" : ""} found` })]
});
}
case "search":
case "s": {
const query = data.get("token")?.value;
if (!query) {
return msg.reply({
embeds: [new EmbedBuilder().setColor(ERROR_COLOR).setDescription(`❌ Provide a search query via the token option. Example: \`${prefix}lastfm search radiohead\``)]
});
}
let artistResults, albumResults, trackResult;
try {
[artistResults, albumResults, trackResult] = await Promise.all([
lastfm.searchArtist(query, 5),
lastfm.searchAlbum(query, 5),
lastfm.searchTrack(query),
]);
} catch (err) {
return msg.reply({
embeds: [new EmbedBuilder().setColor(ERROR_COLOR).setDescription(this.t(msg, "responses.lastfm.fetchFailed", { error: err.message }))]
});
}
const sections = [];
if (artistResults.length) {
const artistLines = artistResults.map((a, i) => {
const link = a.url ? `[${a.name}](${a.url})` : a.name;
const listeners = a.listeners ? ` — ${Utils.formatNumber(a.listeners)} listeners` : "";
return `${i + 1}. ${link}${listeners}`;
});
sections.push(`**🎤 Artists:**\n${artistLines.join("\n")}`);
}
if (albumResults.length) {
const albumLines = albumResults.map((a, i) => {
const link = a.url ? `[${a.name}](${a.url})` : a.name;
return `${i + 1}. ${link} by **${a.artist}**`;
});
sections.push(`**💿 Albums:**\n${albumLines.join("\n")}`);
}
if (trackResult) {
sections.push(`**🎵 Best Track Match:**\n[${trackResult.name}](${trackResult.url}) by **${trackResult.artist}**`);
}
if (!sections.length) {
return msg.reply({
embeds: [new EmbedBuilder().setColor(getGlobalColor()).setDescription(`🔍 No results found for **${query}**.`)]
});
}
const desc = sections.join("\n\n").slice(0, 4096);
return msg.reply({
embeds: [new EmbedBuilder()
.setColor(getGlobalColor())
.setTitle(`🔍 Search Results for "${query}"`)
.setDescription(desc)]
});
}
}
}