/** * @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} */ 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)}** ${this.t(msg, "responses.lastfm.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 isArtists = tokenTextTr === "artists"; trendingTitle = isArtists ? this.t(msg, "responses.lastfm.trendingTitleArtists") : this.t(msg, "responses.lastfm.trendingTitleTracks"); const top1 = trendingData[0]; const runners = trendingData.slice(1, 10); const embed = new EmbedBuilder() .setColor(getGlobalColor()) .setAuthor({ name: this.t(msg, "responses.lastfm.trendingAuthor") }) .setTitle(trendingTitle) .setFooter({ text: this.t(msg, "responses.lastfm.trendingFooterPro", { prefix }) }); if (top1) { const link = top1.url ? `[${top1.name}](${top1.url})` : top1.name; const artistStr = top1.artist ? ` by **${top1.artist}**` : ""; const extra = top1.listeners ? ` ยท **${Utils.formatNumber(top1.listeners)}** ${this.t(msg, "responses.lastfm.listeners")}` : (top1.playcount ? ` ยท **${Utils.formatNumber(top1.playcount)}** ${this.t(msg, "responses.lastfm.plays")}` : ""); embed.addFields({ name: `๐Ÿฅ‡ ${this.t(msg, "responses.lastfm.trendingPodium1")}`, value: `**${link}**${artistStr}${extra}`, inline: false }); } if (runners.length > 0) { const MEDALS = ["๐Ÿฅˆ", "๐Ÿฅ‰"]; const lines = runners.map((item, i) => { const badge = i < 2 ? MEDALS[i] : `\`#${i + 2}\``; const link = item.url ? `[${item.name}](${item.url})` : item.name; const artistStr = item.artist ? ` ยท ${item.artist}` : ""; const extra = item.listeners ? ` (${Utils.formatNumber(item.listeners)})` : (item.playcount ? ` (${Utils.formatNumber(item.playcount)})` : ""); return `${badge} **${link}**${artistStr}${extra}`; }); embed.addFields({ name: `๐Ÿ”ฅ ${this.t(msg, "responses.lastfm.trendingRunnersUp")}`, value: lines.join("\n").slice(0, 1024), inline: false }); } return msg.reply({ embeds: [embed] }); } 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 \` or \`tracks \`. 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)] }); } } }