This commit is contained in:
NoLogicAlan committed 2026-09-15 03:06:23 +03:00
1 parent 54d09d1b64
commit f1c87d410d
15 files changed
+257 -96

No files matched your search

+6 -6
View File
@@ -16,12 +16,12 @@ import { notLinked } from "./shared.mjs";
* @type {Set<string>}
*/
export const ACCOUNT_ACTIONS = new Set([
"link",,
"confirm",,
"unlink",,
"scrobble",,
"np",,
"profile",,
"link",
"confirm",
"unlink",
"scrobble",
"np",
"profile",
"playlists"
]);
+13 -13
View File
@@ -15,17 +15,17 @@ import { notLinked, extractCurrentTrack } from "./shared.mjs";
* @type {Set<string>}
*/
export const DISCOVERY_ACTIONS = new Set([
"friends",,
"fr",,
"weekly",,
"wc",,
"trending",,
"tr",,
"geo",,
"g",,
"artisttracks",,
"atr",,
"search",,
"friends",
"fr",
"weekly",
"wc",
"trending",
"tr",
"geo",
"g",
"artisttracks",
"atr",
"search",
"s"
]);
@@ -119,7 +119,7 @@ export async function runDiscoveryActions(msg, data, lastfm, prefix, userId, tar
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`;
return `\`${num}.\` ${link}${artistStr} — **${Utils.formatNumber(item.playcount)}** ${this.t(msg, "responses.lastfm.plays")}`;
});
const desc = lines.join("\n").slice(0, 4096);
@@ -163,7 +163,7 @@ export async function runDiscoveryActions(msg, data, lastfm, prefix, userId, tar
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` : "";
const extra = item.listeners ? ` — ${Utils.formatNumber(item.listeners)} ${this.t(msg, "responses.lastfm.listeners")}` : item.playcount ? ` — ${Utils.formatNumber(item.playcount)} ${this.t(msg, "responses.lastfm.plays")}` : "";
return `\`${num}.\` ${link}${artistStr}${extra}`;
});
+11 -11
View File
@@ -16,16 +16,16 @@ import { notLinked, extractCurrentTrack, extractPeriod } from "./shared.mjs";
* @type {Set<string>}
*/
export const INFO_ACTIONS = new Set([
"artistinfo",,
"ai",,
"albuminfo",,
"ali",,
"trackinfo",,
"ti",,
"topalbums",,
"toptags",,
"tags",,
"cover",,
"artistinfo",
"ai",
"albuminfo",
"ali",
"trackinfo",
"ti",
"topalbums",
"toptags",
"tags",
"cover",
"art"
]);
@@ -387,7 +387,7 @@ export async function runInfoActions(msg, data, lastfm, prefix, userId, targetUs
let albumTitle = a.name;
if (albumTitle.length > 30) albumTitle = albumTitle.slice(0, 27) + "...";
const link = a.url ? `[${albumTitle}](${a.url})` : albumTitle;
return `\`${num}.\` ${link} — **${a.artist}** (**${a.playcount}** plays)`;
return `\`${num}.\` ${link} — **${a.artist}** (**${a.playcount}** ${this.t(msg, "responses.lastfm.plays")})`;
});
const desc = lines.join("\n").slice(0, 4096);
+54 -30
View File
@@ -10,19 +10,20 @@ import { Utils } from "../../src/utils/Utils.mjs";
import { logger } from "../../src/core/Logger.mjs";
import { ERROR_COLOR, EMOJI_REMOVE_TIMEOUT } from "../../src/utils/UI.mjs";
import { notLinked, extractPeriod } from "./shared.mjs";
import { getGuildLinkedUsers } from "./whoknows.mjs";
/**
* Action names dispatched to this module.
* @type {Set<string>}
*/
export const LISTENING_ACTIONS = new Set([
"loved",,
"top",,
"leaderboard",,
"lb",,
"recent",,
"artists",,
"love",,
"loved",
"top",
"leaderboard",
"lb",
"recent",
"artists",
"love",
"unlove"
]);
@@ -33,17 +34,18 @@ export const LISTENING_ACTIONS = new Set([
* @param {string} username - The Last.fm username.
* @param {string} title - The list title.
* @param {object[]} tracks - Array of track objects with name, artist, url, and optional playcount.
* @param {function(string, object=): string} tr - Translator bound to the invoking message.
* @param {boolean} [showPlaycount=false] - Whether to show play counts.
* @param {string} [prefix="%"] - The command prefix for the footer hint.
* @returns {EmbedBuilder} The constructed embed.
*/
function buildTrackList(username, title, tracks, showPlaycount = false, prefix = "%") {
function buildTrackList(username, title, tracks, tr, showPlaycount = false, prefix = "%") {
const lines = tracks.map((t, i) => {
const num = String(i + 1).padStart(2, " ");
let name = t.name;
if (name.length > 40) name = name.slice(0, 37) + "...";
const link = t.url ? `[${name}](${t.url})` : name;
const extra = showPlaycount && t.playcount ? ` (${t.playcount} plays)` : "";
const extra = showPlaycount && t.playcount ? ` (${t.playcount} ${tr("responses.lastfm.plays")})` : "";
return `\`${num}.\` ${link} — **${t.artist}**${extra}`;
});
@@ -51,38 +53,47 @@ function buildTrackList(username, title, tracks, showPlaycount = false, prefix =
return new EmbedBuilder()
.setColor(getGlobalColor())
.setTitle(`${title} — ${username}`)
.setTitle(title) /* locale titles (lovedTitle/topTitle/recentTitle) already embed {{username}} */
.setDescription(desc)
.setFooter({ text: `💡 Use ${prefix}lastfm play loved to play these!` });
.setFooter({ text: tr("responses.lastfm.trackListFooter", { prefix }) });
}
/**
* Build a scrobble leaderboard embed for a given page.
* @private
* @param {object} lb - Leaderboard data with entries and perPage.
* @param {object} lb - Leaderboard data with entries, perPage and scope ("server"|"global").
* @param {number} pageIdx - The page index (0-based).
* @param {string} prefix - The command prefix for the footer hint.
* @param {function(string, object=): string} tr - Translator bound to the invoking message.
* @param {object} [opts] - Extra options.
* @param {string|null} [opts.requesterId] - Invoking user's ID for the "you" marker.
* @returns {EmbedBuilder} The constructed embed.
*/
function buildLeaderboardEmbed(lb, pageIdx, prefix) {
function buildLeaderboardEmbed(lb, pageIdx, prefix, tr, opts = {}) {
const MEDALS = ["🥇", "🥈", "🥉"];
const startRank = pageIdx * lb.perPage;
const requesterId = opts.requesterId ? String(opts.requesterId) : null;
const lines = lb.entries.map((entry, i) => {
const rank = startRank + i + 1;
const medal = rank <= 3 ? MEDALS[rank - 1] : ` `;
const name = entry.username || entry.userId;
const medal = rank <= 3 ? MEDALS[rank - 1] : "` `";
const who = entry.userId ? `<@${entry.userId}>` : (entry.username || "?");
const lfName = entry.username ? ` — *${entry.username}*` : "";
const marker = requesterId && String(entry.userId) === requesterId ? ` **${tr("responses.lastfm.leaderboardYou")}**` : "";
const count = Utils.formatNumber(entry.scrobbleCount);
return `${medal} ${rank}. **${name}** — ${count} scrobbles`;
return `${medal} **${rank}.** ${who}${lfName} — **${count}** ${tr("responses.lastfm.leaderboardScrobbles")}${marker}`;
});
const desc = lines.join("\n").slice(0, 4096);
const title = lb.scope === "server"
? tr("responses.lastfm.leaderboardTitleServer")
: tr("responses.lastfm.leaderboardTitleGlobal");
return new EmbedBuilder()
.setColor(getGlobalColor())
.setTitle("🎵 Scrobble Leaderboard")
.setTitle(title)
.setDescription(desc)
.setFooter({ text: `💡 View & sync your count: ${prefix}lastfm profile` });
.setFooter({ text: tr("responses.lastfm.leaderboardFooterSync", { prefix }) });
}
/**
@@ -97,6 +108,10 @@ function buildLeaderboardEmbed(lb, pageIdx, prefix) {
* @returns {Promise<*>} Whatever the original switch returned for this action.
*/
export async function runListeningActions(msg, data, lastfm, prefix, userId, targetUserId, action) {
/* Translator bound to this message: every embed builder below renders in
the guild's locale instead of hardcoded English. */
const tr = (key, repl = {}) => this.t(msg, key, repl);
switch (action) {
case "loved": {
const user = await lastfm.getUser(userId);
@@ -117,7 +132,7 @@ export async function runListeningActions(msg, data, lastfm, prefix, userId, tar
});
}
return msg.reply({ embeds: [buildTrackList(user.username, this.t(msg, "responses.lastfm.lovedTitle", { username: user.username }), tracks, false, prefix)] });
return msg.reply({ embeds: [buildTrackList(user.username, tr("responses.lastfm.lovedTitle", { username: user.username }), tracks, tr, false, prefix)] });
}
case "top": {
@@ -142,14 +157,23 @@ export async function runListeningActions(msg, data, lastfm, prefix, userId, tar
}
const periodLabel = period !== "overall" ? ` (${period})` : "";
return msg.reply({ embeds: [buildTrackList(user.username, this.t(msg, "responses.lastfm.topTitle", { username: user.username }) + periodLabel, tracks, true, prefix)] });
return msg.reply({ embeds: [buildTrackList(user.username, tr("responses.lastfm.topTitle", { username: user.username }) + periodLabel, tracks, tr, true, prefix)] });
}
case "leaderboard":
case "lb": {
/* Server scope when the guild's members are resolvable (ranks only
people actually in this server), global fallback for DMs. */
const guild = msg.message?.guild ?? msg.message?.member?.guild ?? null;
const guildIds = guild ? await getGuildLinkedUsers(guild).catch(() => []) : [];
const useServer = guildIds.length > 0;
const fetchLb = (page, perPage = 10) => useServer
? lastfm.getServerLeaderboard(guildIds, page, perPage)
: lastfm.getLeaderboard(page, perPage);
let lb;
try {
lb = await lastfm.getLeaderboard(0, 10);
lb = await fetchLb(0);
} catch (err) {
return msg.reply({
embeds: [new EmbedBuilder().setColor(ERROR_COLOR).setDescription(this.t(msg, "responses.lastfm.fetchFailed", { error: err.message }))]
@@ -165,7 +189,7 @@ export async function runListeningActions(msg, data, lastfm, prefix, userId, tar
}
if (lb.totalPages <= 1) {
const embed = buildLeaderboardEmbed(lb, 0, prefix);
const embed = buildLeaderboardEmbed(lb, 0, prefix, tr, { requesterId: userId });
return msg.reply({ embeds: [embed] });
}
@@ -173,9 +197,9 @@ export async function runListeningActions(msg, data, lastfm, prefix, userId, tar
const buildPage = (pageIdx, expired = false) => {
const footerText = expired
? this.t(msg, "responses.lastfm.leaderboardControlsExpired")
: this.t(msg, "responses.lastfm.leaderboardPageFooter", { current: pageIdx + 1, total: lb.totalPages });
const embed = buildLeaderboardEmbed(lb, pageIdx, prefix);
? tr("responses.lastfm.leaderboardControlsExpired")
: tr("responses.lastfm.leaderboardPageFooter", { current: pageIdx + 1, total: lb.totalPages });
const embed = buildLeaderboardEmbed(lb, pageIdx, prefix, tr, { requesterId: userId });
embed.setFooter({ text: footerText });
return { embeds: [embed] };
};
@@ -225,7 +249,7 @@ export async function runListeningActions(msg, data, lastfm, prefix, userId, tar
}
try {
lb = await lastfm.getLeaderboard(currentPage, 10);
lb = await fetchLb(currentPage);
} catch(e) { logger.warn("[LastFm] Error:", e?.message); }
await replyMsg.edit(buildPage(currentPage)).catch(() => {});
@@ -254,7 +278,7 @@ export async function runListeningActions(msg, data, lastfm, prefix, userId, tar
});
}
return msg.reply({ embeds: [buildTrackList(user.username, this.t(msg, "responses.lastfm.recentTitle", { username: user.username }), tracks, false, prefix)] });
return msg.reply({ embeds: [buildTrackList(user.username, tr("responses.lastfm.recentTitle", { username: user.username }), tracks, tr, false, prefix)] });
}
case "artists": {
@@ -284,7 +308,7 @@ export async function runListeningActions(msg, data, lastfm, prefix, userId, tar
let name = a.name;
if (name.length > 40) name = name.slice(0, 37) + "...";
const link = a.url ? `[${name}](${a.url})` : name;
return `\`${num}.\` ${link} — **${a.playcount}** plays`;
return `\`${num}.\` ${link} — **${a.playcount}** ${tr("responses.lastfm.plays")}`;
});
const desc = lines.join("\n").slice(0, 4096);
@@ -292,9 +316,9 @@ export async function runListeningActions(msg, data, lastfm, prefix, userId, tar
return msg.reply({
embeds: [new EmbedBuilder()
.setColor(getGlobalColor())
.setTitle(this.t(msg, "responses.lastfm.artistsTitle", { username: user.username }) + periodLabel)
.setTitle(tr("responses.lastfm.artistsTitle", { username: user.username }) + periodLabel)
.setDescription(desc)
.setFooter({ text: this.t(msg, "responses.lastfm.artistsFooter", { prefix }) })]
.setFooter({ text: tr("responses.lastfm.artistsFooter", { prefix }) })]
});
}
+10 -10
View File
@@ -16,14 +16,14 @@ import { extractCurrentTrack } from "./shared.mjs";
* @type {Set<string>}
*/
export const TAG_ACTIONS = new Set([
"tag",,
"artisttags",,
"at",,
"albumtags",,
"alt",,
"tracktags",,
"tt",,
"tagalbums",,
"tag",
"artisttags",
"at",
"albumtags",
"alt",
"tracktags",
"tt",
"tagalbums",
"ta"
]);
@@ -85,7 +85,7 @@ export async function runTagActions(msg, data, lastfm, prefix, userId, targetUse
if (topArtists.length) {
const artistStr = topArtists.map((a, i) => {
const link = a.url ? `[${a.name}](${a.url})` : a.name;
return `${i + 1}. ${link} (**${Utils.formatNumber(a.playcount)}** plays)`;
return `${i + 1}. ${link} (**${Utils.formatNumber(a.playcount)}** ${this.t(msg, "responses.lastfm.plays")})`;
}).join("\n");
fields.push({ name: "Top Artists", value: artistStr.slice(0, 1024), inline: false });
}
@@ -316,7 +316,7 @@ export async function runTagActions(msg, data, lastfm, prefix, userId, targetUse
const lines = albums.map((a, i) => {
const num = String(i + 1).padStart(2, " ");
const link = a.url ? `[${a.name}](${a.url})` : a.name;
return `\`${num}.\` ${link} by **${a.artist}**${a.playcount > 0 ? ` (${Utils.formatNumber(a.playcount)} plays)` : ""}`;
return `\`${num}.\` ${link} by **${a.artist}**${a.playcount > 0 ? ` (${Utils.formatNumber(a.playcount)} ${this.t(msg, "responses.lastfm.plays")})` : ""}`;
});
const desc = lines.join("\n").slice(0, 4096);
+20 -20
View File
@@ -16,31 +16,31 @@ import { notLinked, extractCurrentTrack } from "./shared.mjs";
* @type {Set<string>}
*/
export const WHOKNOWS_ACTIONS = new Set([
"whoknows",,
"wk",,
"compare",,
"fmc",,
"refreshmembers",,
"rm",,
"affinity",,
"af",,
"crowns",,
"cr",,
"whoknowstrack",,
"wkt",,
"whoknowsalbum",,
"whoknows",
"wk",
"compare",
"fmc",
"refreshmembers",
"rm",
"affinity",
"af",
"crowns",
"cr",
"whoknowstrack",
"wkt",
"whoknowsalbum",
"wka"
]);
/**
* Get all non-bot user IDs in a guild.
* @private
* @export
* @async
* @param {object} guild - The guild object.
* @returns {Promise<string[]>} Array of user ID strings.
*/
async function getGuildLinkedUsers(guild) {
export async function getGuildLinkedUsers(guild) {
const memberIds = [];
try {
const members = guild?.members;
@@ -127,7 +127,7 @@ export async function runWhoknowsActions(msg, data, lastfm, prefix, userId, targ
const lines = listeners.map((l, i) => {
const medal = i < 3 ? MEDALS[i] : ` `;
return `${medal} ${i + 1}. **${l.username}** — **${Utils.formatNumber(l.playcount)}** plays`;
return `${medal} ${i + 1}. **${l.username}** — **${Utils.formatNumber(l.playcount)}** ${this.t(msg, "responses.lastfm.plays")}`;
});
const desc = lines.join("\n").slice(0, 4096);
@@ -197,7 +197,7 @@ export async function runWhoknowsActions(msg, data, lastfm, prefix, userId, targ
const commonStr = comparison.commonArtists.slice(0, 15).map((a, i) => {
const num = String(i + 1).padStart(2, " ");
const link = a.url ? `[${a.name}](${a.url})` : a.name;
return `\`${num}.\` ${link} (**${a.playcount}** plays)`;
return `\`${num}.\` ${link} (**${a.playcount}** ${this.t(msg, "responses.lastfm.plays")})`;
}).join("\n");
embed.addFields({ name: `Common Artists (${comparison.commonArtists.length})`, value: commonStr.slice(0, 1024), inline: false });
}
@@ -332,7 +332,7 @@ export async function runWhoknowsActions(msg, data, lastfm, prefix, userId, targ
const lines = filteredCrowns.slice(0, 20).map(c => {
const link = c.artistUrl ? `[${c.artist}](${c.artistUrl})` : c.artist;
const nextStr = c.nextBest ? ` (next: **${c.nextBest.username}** with ${Utils.formatNumber(c.nextBest.playcount)})` : "";
return `👑 ${link} — **${Utils.formatNumber(c.userPlaycount)}** plays${nextStr}`;
return `👑 ${link} — **${Utils.formatNumber(c.userPlaycount)}** ${this.t(msg, "responses.lastfm.plays")}${nextStr}`;
});
const desc = lines.join("\n").slice(0, 4096);
@@ -423,7 +423,7 @@ export async function runWhoknowsActions(msg, data, lastfm, prefix, userId, targ
const lines = listeners.map((l, i) => {
const medal = i < 3 ? MEDALS[i] : ` `;
return `${medal} ${i + 1}. **${l.username}** — **${Utils.formatNumber(l.playcount)}** plays`;
return `${medal} ${i + 1}. **${l.username}** — **${Utils.formatNumber(l.playcount)}** ${this.t(msg, "responses.lastfm.plays")}`;
});
const desc = lines.join("\n").slice(0, 4096);
@@ -504,7 +504,7 @@ export async function runWhoknowsActions(msg, data, lastfm, prefix, userId, targ
const lines = listeners.map((l, i) => {
const medal = i < 3 ? MEDALS[i] : ` `;
return `${medal} ${i + 1}. **${l.username}** — **${Utils.formatNumber(l.playcount)}** plays`;
return `${medal} ${i + 1}. **${l.username}** — **${Utils.formatNumber(l.playcount)}** ${this.t(msg, "responses.lastfm.plays")}`;
});
const desc = lines.join("\n").slice(0, 4096);