feat(top): implement fallback to global tracks

This commit is contained in:
NoLogicAlan committed 2026-11-21 00:54:31 +03:00
1 parent bdee46b7f2
commit d78af55f10
10 files changed
+62 -15

No files matched your search

+22 -5
View File
@@ -68,25 +68,35 @@ export async function run(message, data) {
}
}
const { tracks, topArtist } = isGlobal
let { tracks, topArtist } = isGlobal
? await GuildStats.getGlobalTopTracks(MAX_TRACKS)
: await GuildStats.getTopTracks(guildId, timeframe, MAX_TRACKS);
const authorName = isGlobal
let fallbackGlobal = false;
if ((!tracks || tracks.length === 0) && !isGlobal) {
const globalRes = await GuildStats.getGlobalTopTracks(MAX_TRACKS);
if (globalRes?.tracks && globalRes.tracks.length > 0) {
tracks = globalRes.tracks;
topArtist = globalRes.topArtist;
fallbackGlobal = true;
}
}
const authorName = (isGlobal || fallbackGlobal)
? `🌐 ${this.t(message, "responses.top.globalAuthor")}`
: `🏆 ${this.t(message, "responses.top.topAuthor")}`;
const timeframeLabel = isGlobal
const timeframeLabel = (isGlobal || fallbackGlobal)
? "Global"
: (timeframe === "week"
? this.t(message, "responses.top.timeframeWeek")
: (timeframe === "all" ? this.t(message, "responses.top.timeframeAll") : this.t(message, "responses.top.timeframeMonth")));
const titleName = isGlobal
const titleName = (isGlobal || fallbackGlobal)
? this.t(message, "responses.top.globalTitle")
: this.t(message, "responses.top.topTitle", { timeframe: timeframeLabel });
const baseFooter = isGlobal
const baseFooter = (isGlobal || fallbackGlobal)
? this.t(message, "responses.top.globalFooter", { prefix })
: this.t(message, "responses.top.topFooter", { prefix });
@@ -120,6 +130,13 @@ export async function run(message, data) {
});
let desc = lines.join("\n\n");
if (fallbackGlobal && safePage === 1) {
const note = this.t(message, "responses.top.serverFallbackNote");
if (note) {
desc = `${note}\n\n${desc}`;
}
}
if (topArtist && safePage === 1) {
desc += `\n\n⭐ **Top Artist:** **${topArtist.name}** (\`${topArtist.count} ${playsLabel}\`)`;
}
+24 -2
View File
@@ -292,7 +292,19 @@ class GuildStatsManager {
params = [guildId, lim];
}
const [rows] = await this._pool.execute(sql, params);
let [rows] = await this._pool.execute(sql, params);
if ((!rows || rows.length === 0) && (timeframe === "month" || timeframe === "week")) {
const [allRows] = await this._pool.execute(`
SELECT \`title\`, \`artist\`, \`play_count\` as count, \`duration_ms\` as durationMs
FROM \`guild_song_counts\`
WHERE \`guild_id\` = ?
ORDER BY count DESC
LIMIT ?;
`, [guildId, lim]);
if (allRows && allRows.length > 0) {
rows = allRows;
}
}
if (rows && rows.length > 0) {
const artistMap = {};
const tracks = rows.map((r) => {
@@ -347,10 +359,20 @@ class GuildStatsManager {
}
}
const tracks = Object.values(trackMap)
let tracks = Object.values(trackMap)
.sort((a, b) => b.count - a.count)
.slice(0, lim);
if (tracks.length === 0 && Object.keys(data.songs || {}).length > 0) {
trackMap = data.songs;
for (const item of Object.values(trackMap)) {
artistMap[item.artist] = (artistMap[item.artist] || 0) + item.count;
}
tracks = Object.values(trackMap)
.sort((a, b) => b.count - a.count)
.slice(0, lim);
}
let topArtist = null;
const sortedArtists = Object.entries(artistMap).sort((a, b) => b[1] - a[1]);
if (sortedArtists.length > 0 && sortedArtists[0][0] !== "Unknown Artist") {
+2 -1
View File
@@ -1405,7 +1405,8 @@
"globalAuthor": "لوحة المتصدرين العالمية • كل السيرفرات",
"globalTitle": "أكثر الأغاني تشغيلاً عبر كل السيرفرات",
"globalFooter": "تصفية بـ: {{prefix}}top week • {{prefix}}top month • {{prefix}}top all • {{prefix}}top global",
"pageFooter": "الصفحة {{page}}/{{totalPages}} • {{timeframe}}"
"pageFooter": "الصفحة {{page}}/{{totalPages}} • {{timeframe}}",
"serverFallbackNote": "ℹ️ _لم يتم تشغيل أي أغانٍ في هذا السيرفر بعد — عرض مخططات الشبكة العالمية:_"
},
"wrapped": {
"wrappedAuthor": "ملخص موسيقى المجتمع",
+2 -1
View File
@@ -1405,7 +1405,8 @@
"globalAuthor": "ڕیزبەندی جیهانی • هەموو سێرڤەرەکان",
"globalTitle": "زۆرترین گۆرانی لێدراو لە هەموو سێرڤەرەکاندا",
"globalFooter": "فلتەر بکە بە: {{prefix}}top week • {{prefix}}top month • {{prefix}}top all • {{prefix}}top global",
"pageFooter": "پەڕەی {{page}}/{{totalPages}} • {{timeframe}}"
"pageFooter": "پەڕەی {{page}}/{{totalPages}} • {{timeframe}}",
"serverFallbackNote": "ℹ️ _تا ئێستا هیچ گۆرانییەک لەم سێرڤەرە لێنەدراوە — پیشاندانی ڕیزبەندیی تۆڕی جیهانی:_"
},
"wrapped": {
"wrappedAuthor": "پوختەی مۆسیقای کۆمەڵگە",
+2 -1
View File
@@ -1405,7 +1405,8 @@
"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",
"pageFooter": "Seite {{page}}/{{totalPages}} • {{timeframe}}"
"pageFooter": "Seite {{page}}/{{totalPages}} • {{timeframe}}",
"serverFallbackNote": "ℹ️ _Noch keine Tracks auf diesem Server gespielt — globale Charts werden angezeigt:_"
},
"wrapped": {
"wrappedAuthor": "Community Music Wrapped",
+2 -1
View File
@@ -1405,7 +1405,8 @@
"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",
"pageFooter": "Page {{page}}/{{totalPages}} • {{timeframe}}"
"pageFooter": "Page {{page}}/{{totalPages}} • {{timeframe}}",
"serverFallbackNote": "ℹ️ _No tracks played in this server yet — showing Global Network charts:_"
},
"wrapped": {
"wrappedAuthor": "Community Music Wrapped",
+2 -1
View File
@@ -1405,7 +1405,8 @@
"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",
"pageFooter": "Página {{page}}/{{totalPages}} • {{timeframe}}"
"pageFooter": "Página {{page}}/{{totalPages}} • {{timeframe}}",
"serverFallbackNote": "ℹ️ _Nenhuma música tocada neste servidor ainda — exibindo paradas globais da rede:_"
},
"wrapped": {
"wrappedAuthor": "Retrospectiva Musical da Comunidade",
+2 -1
View File
@@ -1405,7 +1405,8 @@
"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",
"pageFooter": "Sayfa {{page}}/{{totalPages}} • {{timeframe}}"
"pageFooter": "Sayfa {{page}}/{{totalPages}} • {{timeframe}}",
"serverFallbackNote": "ℹ️ _Bu sunucuda henüz şarkı çalınmadı — Küresel Ağ listeleri gösteriliyor:_"
},
"wrapped": {
"wrappedAuthor": "Topluluk Müzik Özeti",
+2 -1
View File
@@ -1405,7 +1405,8 @@
"globalAuthor": "全球排行榜 • 所有服务器",
"globalTitle": "所有服务器中最多播放的曲目",
"globalFooter": "筛选方式:{{prefix}}top week • {{prefix}}top month • {{prefix}}top all • {{prefix}}top global",
"pageFooter": "第 {{page}}/{{totalPages}} 页 • {{timeframe}}"
"pageFooter": "第 {{page}}/{{totalPages}} 页 • {{timeframe}}",
"serverFallbackNote": "ℹ️ _此服务器尚未播放任何曲目 — 显示全球网络排行榜:_"
},
"wrapped": {
"wrappedAuthor": "社区年度音乐回顾",
+2 -1
View File
@@ -1405,7 +1405,8 @@
"globalAuthor": "全球排行榜 • 所有伺服器",
"globalTitle": "所有伺服器中最多播放的曲目",
"globalFooter": "篩選方式:{{prefix}}top week • {{prefix}}top month • {{prefix}}top all • {{prefix}}top global",
"pageFooter": "第 {{page}}/{{totalPages}} 頁 • {{timeframe}}"
"pageFooter": "第 {{page}}/{{totalPages}} 頁 • {{timeframe}}",
"serverFallbackNote": "ℹ️ _此伺服器尚未播放任何曲目 — 顯示全球網路排行榜:_"
},
"wrapped": {
"wrappedAuthor": "社區年度音樂回顧",