mirror of
https://github.com/remix-bot/fluxer.git
synced 2026-11-24 06:01:44 -05:00
feat(music): add multi-page pagination to top leaderboard command
- Integrate QueuePaginator into commands/top.mjs for interactive browsing (⬅️ / ➡️) - Support direct page jumping via slash option and text arguments (%top [timeframe] [page]) - Paginate tracks into 10 items per page up to 50 tracks with dynamic rank badges - Increase track query limits in GuildStats.mjs across MySQL and in-memory caches
This commit is contained in:
1 parent
452c080224
commit
ade75a4a79
10 files changed
+113
-49
No files matched your search
+79
-31
@@ -1,13 +1,17 @@
|
||||
/**
|
||||
* @module commands/top
|
||||
* @description Display the most played tracks and top artists for the server or global network.
|
||||
* @description Display the most played tracks and top artists for the server or global network with pagination.
|
||||
*/
|
||||
|
||||
import { CommandBuilder } from "../src/commands/index.mjs";
|
||||
import { EmbedBuilder } from "@fluxerjs/core";
|
||||
import { GuildStats } from "../src/music/GuildStats.mjs";
|
||||
import { cleanId, Utils } from "../src/utils/Utils.mjs";
|
||||
import { getGlobalColor } from "../src/ui/index.mjs";
|
||||
import { getGlobalColor, QueuePaginator } from "../src/ui/index.mjs";
|
||||
|
||||
const PAGE_SIZE = 10;
|
||||
const MAX_TRACKS = 50;
|
||||
const SESSION_MS = 60 * 1000;
|
||||
|
||||
/** @type {CommandBuilder} */
|
||||
export const command = new CommandBuilder()
|
||||
@@ -20,6 +24,12 @@ export const command = new CommandBuilder()
|
||||
.setName("timeframe")
|
||||
.setDescription("Filter timeframe: week, month, all, or global.", "options.top.timeframe")
|
||||
.setRequired(false)
|
||||
)
|
||||
.addNumberOption((o) =>
|
||||
o
|
||||
.setName("page")
|
||||
.setDescription("Page number to jump to.", "options.top.page")
|
||||
.setRequired(false)
|
||||
);
|
||||
|
||||
/**
|
||||
@@ -32,25 +42,41 @@ export async function run(message, data) {
|
||||
const guildId = cleanId(message.message?.guildId ?? message.channel?.guildId);
|
||||
const prefix = this.handler.getPrefix(guildId) || "%";
|
||||
|
||||
const rawArg = (
|
||||
data?.get?.("timeframe")?.value ??
|
||||
(message.message?.content || message.content || "").split(/\s+/)[1] ??
|
||||
""
|
||||
).trim().toLowerCase();
|
||||
let requestedPage = data?.get?.("page")?.value;
|
||||
let rawTimeframe = data?.get?.("timeframe")?.value;
|
||||
|
||||
const isGlobal = rawArg === "global" || rawArg === "network";
|
||||
if (!rawTimeframe || !requestedPage) {
|
||||
const rawTokens = (message.message?.content || message.content || "")
|
||||
.trim()
|
||||
.split(/\s+/)
|
||||
.slice(1);
|
||||
|
||||
for (const token of rawTokens) {
|
||||
const lower = token.toLowerCase();
|
||||
const num = parseInt(lower, 10);
|
||||
if (!isNaN(num) && num > 0 && !requestedPage) {
|
||||
requestedPage = num;
|
||||
} else if (!rawTimeframe) {
|
||||
if (["global", "network", "week", "w", "7d", "all", "alltime", "total", "month", "m", "30d"].includes(lower)) {
|
||||
rawTimeframe = lower;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const isGlobal = rawTimeframe === "global" || rawTimeframe === "network";
|
||||
let timeframe = "month";
|
||||
if (isGlobal) {
|
||||
timeframe = "global";
|
||||
} else if (rawArg === "week" || rawArg === "w" || rawArg === "7d") {
|
||||
} else if (rawTimeframe === "week" || rawTimeframe === "w" || rawTimeframe === "7d") {
|
||||
timeframe = "week";
|
||||
} else if (rawArg === "all" || rawArg === "alltime" || rawArg === "total") {
|
||||
} else if (rawTimeframe === "all" || rawTimeframe === "alltime" || rawTimeframe === "total") {
|
||||
timeframe = "all";
|
||||
}
|
||||
|
||||
const { tracks, topArtist } = isGlobal
|
||||
? await GuildStats.getGlobalTopTracks(10)
|
||||
: await GuildStats.getTopTracks(guildId, timeframe, 10);
|
||||
? await GuildStats.getGlobalTopTracks(MAX_TRACKS)
|
||||
: await GuildStats.getTopTracks(guildId, timeframe, MAX_TRACKS);
|
||||
|
||||
const authorName = isGlobal
|
||||
? `🌐 ${this.t(message, "responses.top.globalAuthor")}`
|
||||
@@ -66,7 +92,7 @@ export async function run(message, data) {
|
||||
? this.t(message, "responses.top.globalTitle")
|
||||
: this.t(message, "responses.top.topTitle", { timeframe: timeframeLabel });
|
||||
|
||||
const footerText = isGlobal
|
||||
const baseFooter = isGlobal
|
||||
? this.t(message, "responses.top.globalFooter", { prefix })
|
||||
: this.t(message, "responses.top.topFooter", { prefix });
|
||||
|
||||
@@ -76,31 +102,53 @@ export async function run(message, data) {
|
||||
.setAuthor({ name: authorName })
|
||||
.setTitle(titleName)
|
||||
.setDescription(this.t(message, "responses.top.noData", { prefix }))
|
||||
.setFooter({ text: footerText });
|
||||
.setFooter({ text: baseFooter });
|
||||
return message.reply({ embeds: [emptyEmbed] });
|
||||
}
|
||||
|
||||
const totalPages = Math.max(1, Math.ceil(tracks.length / PAGE_SIZE));
|
||||
const parsedPage = Number(requestedPage);
|
||||
const startPage = Math.max(1, Math.min((!parsedPage || isNaN(parsedPage)) ? 1 : Math.floor(parsedPage), totalPages));
|
||||
|
||||
const medals = ["🥇", "🥈", "🥉", "4️⃣", "5️⃣", "6️⃣", "7️⃣", "8️⃣", "9️⃣", "🔟"];
|
||||
const playsLabel = this.t(message, "responses.top.plays");
|
||||
|
||||
const lines = tracks.map((t, i) => {
|
||||
const medal = medals[i] || `\`#${i + 1}\``;
|
||||
const cleanTitle = Utils.truncate(t.title, 40);
|
||||
const cleanArtist = Utils.truncate(t.artist, 30);
|
||||
return `${medal} **${cleanTitle}**\n ↳ *${cleanArtist}* • \`${t.count} ${playsLabel}\``;
|
||||
});
|
||||
const buildEmbed = (page) => {
|
||||
const safePage = (isNaN(page) || page < 1) ? 1 : Math.min(Math.floor(page), totalPages);
|
||||
const startIdx = (safePage - 1) * PAGE_SIZE;
|
||||
const pageTracks = tracks.slice(startIdx, startIdx + PAGE_SIZE);
|
||||
|
||||
let desc = lines.join("\n\n");
|
||||
if (topArtist) {
|
||||
desc += `\n\n⭐ **Top Artist:** **${topArtist.name}** (\`${topArtist.count} ${playsLabel}\`)`;
|
||||
const lines = pageTracks.map((t, idx) => {
|
||||
const rank = startIdx + idx + 1;
|
||||
const rankBadge = rank <= 10 ? medals[rank - 1] : `\`#${rank}\``;
|
||||
const cleanTitle = Utils.truncate(t.title, 40);
|
||||
const cleanArtist = Utils.truncate(t.artist, 30);
|
||||
return `${rankBadge} **${cleanTitle}**\n ↳ *${cleanArtist}* • \`${t.count} ${playsLabel}\``;
|
||||
});
|
||||
|
||||
let desc = lines.join("\n\n");
|
||||
if (topArtist && safePage === 1) {
|
||||
desc += `\n\n⭐ **Top Artist:** **${topArtist.name}** (\`${topArtist.count} ${playsLabel}\`)`;
|
||||
}
|
||||
|
||||
const footerText = totalPages > 1
|
||||
? `${this.t(message, "responses.top.pageFooter", { page: safePage, totalPages, timeframe: timeframeLabel })} • ${baseFooter}`
|
||||
: baseFooter;
|
||||
|
||||
return new EmbedBuilder()
|
||||
.setColor(getGlobalColor())
|
||||
.setAuthor({ name: authorName })
|
||||
.setTitle(titleName)
|
||||
.setDescription(desc)
|
||||
.setFooter({ text: footerText });
|
||||
};
|
||||
|
||||
if (totalPages <= 1) {
|
||||
return message.reply({ embeds: [buildEmbed(1)] });
|
||||
}
|
||||
|
||||
const embed = new EmbedBuilder()
|
||||
.setColor(getGlobalColor())
|
||||
.setAuthor({ name: authorName })
|
||||
.setTitle(titleName)
|
||||
.setDescription(desc)
|
||||
.setFooter({ text: footerText });
|
||||
|
||||
return message.reply({ embeds: [embed] });
|
||||
new QueuePaginator(message, this.messages, this.client)
|
||||
.setTimeout(SESSION_MS)
|
||||
.send(buildEmbed, totalPages, startPage)
|
||||
.catch(() => {});
|
||||
}
|
||||
@@ -255,7 +255,7 @@ class GuildStatsManager {
|
||||
* @returns {Promise<{tracks: Array, topArtist: {name: string, count: number}|null}>}
|
||||
*/
|
||||
async getTopTracks(guildId, timeframe = "month", limit = 10) {
|
||||
const lim = Math.max(1, Math.min(25, Number(limit) || 10));
|
||||
const lim = Math.max(1, Math.min(100, Number(limit) || 10));
|
||||
|
||||
if (this._pool) {
|
||||
try {
|
||||
@@ -366,7 +366,7 @@ class GuildStatsManager {
|
||||
* @returns {Promise<{tracks: Array, topArtist: {name: string, count: number}|null}>}
|
||||
*/
|
||||
async getGlobalTopTracks(limit = 10) {
|
||||
const lim = Math.max(1, Math.min(25, Number(limit) || 10));
|
||||
const lim = Math.max(1, Math.min(100, Number(limit) || 10));
|
||||
|
||||
if (this._pool) {
|
||||
try {
|
||||
|
||||
@@ -208,7 +208,8 @@
|
||||
"position": "رقم الأغنية في قائمة الانتظار للانتقال إليها."
|
||||
},
|
||||
"top": {
|
||||
"timeframe": "تصفية الإطار الزمني: week أو month أو all أو global."
|
||||
"timeframe": "تصفية الإطار الزمني: week أو month أو all أو global.",
|
||||
"page": "رقم الصفحة للانتقال إليها."
|
||||
},
|
||||
"wrapped": {
|
||||
"scope": "نطاق الملخص: server أو global."
|
||||
@@ -1403,7 +1404,8 @@
|
||||
"plays": "تشغيلات",
|
||||
"globalAuthor": "لوحة المتصدرين العالمية • كل السيرفرات",
|
||||
"globalTitle": "أكثر الأغاني تشغيلاً عبر كل السيرفرات",
|
||||
"globalFooter": "تصفية بـ: {{prefix}}top week • {{prefix}}top month • {{prefix}}top all • {{prefix}}top global"
|
||||
"globalFooter": "تصفية بـ: {{prefix}}top week • {{prefix}}top month • {{prefix}}top all • {{prefix}}top global",
|
||||
"pageFooter": "الصفحة {{page}}/{{totalPages}} • {{timeframe}}"
|
||||
},
|
||||
"wrapped": {
|
||||
"wrappedAuthor": "ملخص موسيقى المجتمع",
|
||||
|
||||
@@ -208,7 +208,8 @@
|
||||
"position": "ژمارەی گۆرانییەکە لە ڕیزبەندی بۆ پەڕینەوە بۆی."
|
||||
},
|
||||
"top": {
|
||||
"timeframe": "فلتەرکردنی کات: week، month، all، یان global."
|
||||
"timeframe": "فلتەرکردنی کات: week، month، all، یان global.",
|
||||
"page": "ژمارەی پەڕە بۆ بازدان."
|
||||
},
|
||||
"wrapped": {
|
||||
"scope": "مەودای پوختە: server یان global."
|
||||
@@ -1403,7 +1404,8 @@
|
||||
"plays": "لێدان",
|
||||
"globalAuthor": "ڕیزبەندی جیهانی • هەموو سێرڤەرەکان",
|
||||
"globalTitle": "زۆرترین گۆرانی لێدراو لە هەموو سێرڤەرەکاندا",
|
||||
"globalFooter": "فلتەر بکە بە: {{prefix}}top week • {{prefix}}top month • {{prefix}}top all • {{prefix}}top global"
|
||||
"globalFooter": "فلتەر بکە بە: {{prefix}}top week • {{prefix}}top month • {{prefix}}top all • {{prefix}}top global",
|
||||
"pageFooter": "پەڕەی {{page}}/{{totalPages}} • {{timeframe}}"
|
||||
},
|
||||
"wrapped": {
|
||||
"wrappedAuthor": "پوختەی مۆسیقای کۆمەڵگە",
|
||||
|
||||
@@ -208,7 +208,8 @@
|
||||
"position": "Die Nummer des Titels in der Warteschlange, zu dem gesprungen werden soll."
|
||||
},
|
||||
"top": {
|
||||
"timeframe": "Zeitraum filtern: week, month, all oder global."
|
||||
"timeframe": "Zeitraum filtern: week, month, all oder global.",
|
||||
"page": "Seitennummer, zu der gesprungen werden soll."
|
||||
},
|
||||
"wrapped": {
|
||||
"scope": "Rückblick-Bereich: server oder global."
|
||||
@@ -1403,7 +1404,8 @@
|
||||
"plays": "Wiedergaben",
|
||||
"globalAuthor": "Globale Bestenliste • Alle Server",
|
||||
"globalTitle": "Meistgespielte Songs auf allen Servern",
|
||||
"globalFooter": "Filtern mit: {{prefix}}top week • {{prefix}}top month • {{prefix}}top all • {{prefix}}top global"
|
||||
"globalFooter": "Filtern mit: {{prefix}}top week • {{prefix}}top month • {{prefix}}top all • {{prefix}}top global",
|
||||
"pageFooter": "Seite {{page}}/{{totalPages}} • {{timeframe}}"
|
||||
},
|
||||
"wrapped": {
|
||||
"wrappedAuthor": "Community Music Wrapped",
|
||||
|
||||
@@ -208,7 +208,8 @@
|
||||
"position": "The track number in the queue to jump to."
|
||||
},
|
||||
"top": {
|
||||
"timeframe": "Filter timeframe: week, month, all, or global."
|
||||
"timeframe": "Filter timeframe: week, month, all, or global.",
|
||||
"page": "Page number to jump to."
|
||||
},
|
||||
"wrapped": {
|
||||
"scope": "Recap scope: server or global."
|
||||
@@ -1403,7 +1404,8 @@
|
||||
"plays": "plays",
|
||||
"globalAuthor": "Global Leaderboard • All Servers",
|
||||
"globalTitle": "Top Played Tracks Across All Servers",
|
||||
"globalFooter": "Filter with: {{prefix}}top week • {{prefix}}top month • {{prefix}}top all • {{prefix}}top global"
|
||||
"globalFooter": "Filter with: {{prefix}}top week • {{prefix}}top month • {{prefix}}top all • {{prefix}}top global",
|
||||
"pageFooter": "Page {{page}}/{{totalPages}} • {{timeframe}}"
|
||||
},
|
||||
"wrapped": {
|
||||
"wrappedAuthor": "Community Music Wrapped",
|
||||
|
||||
@@ -208,7 +208,8 @@
|
||||
"position": "O número da faixa na fila para onde pular."
|
||||
},
|
||||
"top": {
|
||||
"timeframe": "Filtrar período: week, month, all ou global."
|
||||
"timeframe": "Filtrar período: week, month, all ou global.",
|
||||
"page": "Número da página para pular."
|
||||
},
|
||||
"wrapped": {
|
||||
"scope": "Escopo da retrospectiva: server ou global."
|
||||
@@ -1403,7 +1404,8 @@
|
||||
"plays": "reproduções",
|
||||
"globalAuthor": "Classificação Global • Todos os Servidores",
|
||||
"globalTitle": "Músicas Mais Tocadas em Todos os Servidores",
|
||||
"globalFooter": "Filtrar com: {{prefix}}top week • {{prefix}}top month • {{prefix}}top all • {{prefix}}top global"
|
||||
"globalFooter": "Filtrar com: {{prefix}}top week • {{prefix}}top month • {{prefix}}top all • {{prefix}}top global",
|
||||
"pageFooter": "Página {{page}}/{{totalPages}} • {{timeframe}}"
|
||||
},
|
||||
"wrapped": {
|
||||
"wrappedAuthor": "Retrospectiva Musical da Comunidade",
|
||||
|
||||
@@ -208,7 +208,8 @@
|
||||
"position": "Atlanacak şarkının kuyruktaki sıra numarası."
|
||||
},
|
||||
"top": {
|
||||
"timeframe": "Zaman aralığını filtrele: week, month, all veya global."
|
||||
"timeframe": "Zaman aralığını filtrele: week, month, all veya global.",
|
||||
"page": "Atlanacak sayfa numarası."
|
||||
},
|
||||
"wrapped": {
|
||||
"scope": "Özet kapsamı: server veya global."
|
||||
@@ -1403,7 +1404,8 @@
|
||||
"plays": "çalma",
|
||||
"globalAuthor": "Küresel Lider Tablosu • Tüm Sunucular",
|
||||
"globalTitle": "Tüm Sunucularda En Çok Çalınan Şarkılar",
|
||||
"globalFooter": "Filtrele: {{prefix}}top week • {{prefix}}top month • {{prefix}}top all • {{prefix}}top global"
|
||||
"globalFooter": "Filtrele: {{prefix}}top week • {{prefix}}top month • {{prefix}}top all • {{prefix}}top global",
|
||||
"pageFooter": "Sayfa {{page}}/{{totalPages}} • {{timeframe}}"
|
||||
},
|
||||
"wrapped": {
|
||||
"wrappedAuthor": "Topluluk Müzik Özeti",
|
||||
|
||||
@@ -208,7 +208,8 @@
|
||||
"position": "要跳转到的队列曲目序号。"
|
||||
},
|
||||
"top": {
|
||||
"timeframe": "筛选时间范围:week、month、all 或 global。"
|
||||
"timeframe": "筛选时间范围:week、month、all 或 global。",
|
||||
"page": "要跳转到的页码。"
|
||||
},
|
||||
"wrapped": {
|
||||
"scope": "回顾范围:server 或 global。"
|
||||
@@ -1403,7 +1404,8 @@
|
||||
"plays": "次播放",
|
||||
"globalAuthor": "全球排行榜 • 所有服务器",
|
||||
"globalTitle": "所有服务器中最多播放的曲目",
|
||||
"globalFooter": "筛选方式:{{prefix}}top week • {{prefix}}top month • {{prefix}}top all • {{prefix}}top global"
|
||||
"globalFooter": "筛选方式:{{prefix}}top week • {{prefix}}top month • {{prefix}}top all • {{prefix}}top global",
|
||||
"pageFooter": "第 {{page}}/{{totalPages}} 页 • {{timeframe}}"
|
||||
},
|
||||
"wrapped": {
|
||||
"wrappedAuthor": "社区年度音乐回顾",
|
||||
|
||||
@@ -208,7 +208,8 @@
|
||||
"position": "要跳轉到的佇列曲目序號。"
|
||||
},
|
||||
"top": {
|
||||
"timeframe": "篩選時間範圍:week、month、all 或 global。"
|
||||
"timeframe": "篩選時間範圍:week、month、all 或 global。",
|
||||
"page": "要跳轉到的頁碼。"
|
||||
},
|
||||
"wrapped": {
|
||||
"scope": "回顧範圍:server 或 global。"
|
||||
@@ -1403,7 +1404,8 @@
|
||||
"plays": "次播放",
|
||||
"globalAuthor": "全球排行榜 • 所有伺服器",
|
||||
"globalTitle": "所有伺服器中最多播放的曲目",
|
||||
"globalFooter": "篩選方式:{{prefix}}top week • {{prefix}}top month • {{prefix}}top all • {{prefix}}top global"
|
||||
"globalFooter": "篩選方式:{{prefix}}top week • {{prefix}}top month • {{prefix}}top all • {{prefix}}top global",
|
||||
"pageFooter": "第 {{page}}/{{totalPages}} 頁 • {{timeframe}}"
|
||||
},
|
||||
"wrapped": {
|
||||
"wrappedAuthor": "社區年度音樂回顧",
|
||||
|
||||
Reference in new issue
Block a user