From d6c233c311749b7e2d704958768ce81bc09aaee6 Mon Sep 17 00:00:00 2001 From: NoLogicAlan Date: Thu, 22 Oct 2026 02:59:23 +0300 Subject: [PATCH] feat(stats): localize music stats, add shard and queue metrics, remove audio engine line --- commands/stats.mjs | 129 +++++++++++++++++++++++++-------- storage/locales/bot/ar-SA.json | 4 + storage/locales/bot/ckb.json | 4 + storage/locales/bot/de-DE.json | 4 + storage/locales/bot/en.json | 4 + storage/locales/bot/pt-BR.json | 4 + storage/locales/bot/tr.json | 4 + 7 files changed, 124 insertions(+), 29 deletions(-) diff --git a/commands/stats.mjs b/commands/stats.mjs index 5fdadf7..ef7e312 100644 --- a/commands/stats.mjs +++ b/commands/stats.mjs @@ -10,6 +10,7 @@ import { Utils } from "../src/utils/Utils.mjs"; import { EmbedBuilder } from "@fluxerjs/core"; import { getGlobalColor } from "../src/ui/index.mjs"; import { logger } from "../src/core/Logger.mjs"; +import * as ShardingUtils from "../src/utils/ShardingUtils.mjs"; /** * @type {CommandBuilder} @@ -226,49 +227,94 @@ async function withRateLimitRetry(fn, label = "send") { } /** - * Count players with active voice connections. + * Gather real-time music playback and player statistics. + * Deduplicates active players by instance and computes connected, playing, + * total queued tracks, and 24/7 radio sessions. * @private * @param {Map} playerMap - The player map from PlayerManager. - * @returns {number} Number of live connected players. + * @returns {{totalPlayers: number, playingCount: number, queuedTracks: number, radios247: number}} */ -function getLivePlayerCount(playerMap) { - let live = 0; - - for (const [mapKey, player] of playerMap ?? []) { - if (!player || player._destroyed || player.leaving) continue; - if (player._isJoining) continue; - - const conn = player.connection; - if (conn) { - const room = conn.room; - if (room) { - const isConnected = room.isConnected; - const connectionState = room.connectionState; - if (!isConnected && (connectionState === 0 || connectionState === "disconnected")) continue; +function getMusicStats(playerMap) { + const uniquePlayers = new Set(); + if (playerMap) { + for (const player of playerMap.values()) { + if (!player || player._destroyed || player.leaving || player._isJoining) continue; + const conn = player.connection; + if (conn) { + const room = conn.room; + if (room) { + const isConnected = room.isConnected; + const connectionState = room.connectionState; + if (!isConnected && (connectionState === 0 || connectionState === "disconnected")) continue; + } + } else { + continue; } - } else { - continue; + uniquePlayers.add(player); } - - live++; } - return live; + let totalPlayers = uniquePlayers.size; + let playingCount = 0; + let queuedTracks = 0; + let radios247 = 0; + + for (const player of uniquePlayers) { + const cur = player.queue?.getCurrent?.(); + const isPlaying = !!cur && !player.paused; + if (isPlaying) playingCount++; + + const qSize = (player.queue?.size?.() ?? 0) + (cur ? 1 : 0); + queuedTracks += qSize; + + if (player._is247Enabled?.()) { + radios247++; + } + } + + return { + totalPlayers, + playingCount, + queuedTracks, + radios247, + }; } /** - * Translate with a hardcoded fallback. Locale lookups that miss (custom or - * outdated locale files make translate() return the raw dot-notation key) + * Resolve the current shard assignment string (e.g. "0/2" when sharded, or "0" in single-process mode). + * @private + * @param {object} client - The bot client instance. + * @param {string|null} guildId - The guild ID where the command was invoked. + * @returns {string} Formatted shard info. + */ +function getShardInfo(client, guildId) { + try { + const isSharded = ShardingUtils.isShardedProcess(); + const currentShard = ShardingUtils.shardIdForGuild(client, guildId); + if (isSharded) { + const totalShards = Number(process.env.FLUXER_SHARD_COUNT) || 1; + return totalShards > 1 ? `${currentShard}/${totalShards}` : `${currentShard}`; + } + return `${currentShard}`; + } catch (_) { + return "0"; + } +} + +/** + * Translate with a hardcoded fallback and optional placeholders. Locale lookups + * that miss (custom or outdated locale files make translate() return the raw dot-notation key) * render the readable fallback instead of an ugly key path in the embed. * @private * @param {Function} t - Translation function. * @param {object} msg - The command message wrapper. * @param {string} key - Locale key. * @param {string} fallback - Label used when the key is missing. + * @param {object} [vars={}] - Optional interpolation variables. * @returns {string} */ -function tOr(t, msg, key, fallback) { - const value = t(msg, key); +function tOr(t, msg, key, fallback, vars = {}) { + const value = t(msg, key, vars); return value === key ? fallback : value; } @@ -280,7 +326,11 @@ function tOr(t, msg, key, fallback) { * @param {object} s - Statistics data object. * @param {number} s.guildCount - Number of guilds. * @param {number} s.userCount - Number of users. - * @param {number} s.playerCount - Number of active players. + * @param {number} s.playerCount - Number of active connected players. + * @param {number} s.playingCount - Number of players actively streaming. + * @param {number} s.queuedTracks - Total tracks across all player queues. + * @param {number} s.radios247 - Number of active 24/7 radio sessions. + * @param {string} s.shard - Shard assignment string. * @param {number} s.scrobbleCount - Total Last.fm scrobbles. * @param {number} s.linkedUsers - Number of Last.fm linked users. * @param {number} s.ping - Bot response ping in ms (REST reply round trip). @@ -298,10 +348,19 @@ function buildEmbed(t, msg, s, pending = []) { const num = (v) => Utils.formatNumber(v); const field = (key, value) => `\`${pending.includes(key) ? "..." : value}\``; + const playerVal = s.playingCount > 0 + ? tOr(t, msg, "responses.stats.playersWithPlaying", `${num(s.playerCount)} (${num(s.playingCount)} playing)`, { + total: num(s.playerCount), + playing: num(s.playingCount), + }) + : num(s.playerCount); + const description = [ `${t(msg, "responses.stats.servers")} — ${field("guilds", num(s.guildCount))}`, `${t(msg, "responses.stats.users")} — ${field("users", num(s.userCount))}`, - `${t(msg, "responses.stats.players")} — ${field("players", num(s.playerCount))}`, + `${t(msg, "responses.stats.players")} — ${field("players", playerVal)}`, + `${tOr(t, msg, "responses.stats.queuedTracks", "🎶 **Queued Tracks**")} — ${field("queuedTracks", num(s.queuedTracks))}`, + s.radios247 > 0 ? `${tOr(t, msg, "responses.stats.radios", "📻 **24/7 Radios**")} — ${field("radios", num(s.radios247))}` : null, ]; if (s.lastfmEnabled) { @@ -314,6 +373,7 @@ function buildEmbed(t, msg, s, pending = []) { description.push( `${t(msg, "responses.stats.ping")} — ${field("ping", `${num(s.ping)}ms`)}`, s.gatewayPing >= 0 ? `${tOr(t, msg, "responses.stats.gateway", GATEWAY_FALLBACK_LABEL)} — \`${num(s.gatewayPing)}ms\`` : null, + `${tOr(t, msg, "responses.stats.shard", "🧩 **Shard**")} — \`${s.shard}\``, `${t(msg, "responses.stats.uptime")} — \`${s.uptime}\``, `${t(msg, "responses.stats.build")} — [\`${s.comHash}\`](${s.comLink})`, s.reason ? `${t(msg, "responses.stats.lastRestart")} — \`${s.reason}\`` : null, @@ -349,6 +409,8 @@ export async function run(message) { const t = (...a) => this.t(...a); const totals = getTotals(this.client); + const music = getMusicStats(this.players?.playerMap); + const shard = getShardInfo(this.client, message.guildId ?? message.guild?.id); scheduleBackgroundRefresh(this.client, lastfmEnabled ? lastfm : null); @@ -363,7 +425,11 @@ export async function run(message) { const embedData = { guildCount: totals.guildCount, userCount: totals.userCount, - playerCount: getLivePlayerCount(this.players.playerMap), + playerCount: music.totalPlayers, + playingCount: music.playingCount, + queuedTracks: music.queuedTracks, + radios247: music.radios247, + shard, scrobbleCount: lastfmEnabled ? (cache.scrobbles ?? 0) : 0, linkedUsers: lastfmEnabled ? (cache.linkedUsers ?? 0) : 0, lastfmEnabled, @@ -405,11 +471,16 @@ export async function run(message) { persistCache(); } - const fresh = getTotals(this.client); + const fresh = getTotals(this.client); + const freshMusic = getMusicStats(this.players?.playerMap); const fill = { ...embedData, guildCount: fresh.guildCount, userCount: fresh.userCount, + playerCount: freshMusic.totalPlayers, + playingCount: freshMusic.playingCount, + queuedTracks: freshMusic.queuedTracks, + radios247: freshMusic.radios247, scrobbleCount: lastfmEnabled ? (cache.scrobbles ?? 0) : 0, linkedUsers: lastfmEnabled ? (cache.linkedUsers ?? 0) : 0, ping: lastPing ?? 0, diff --git a/storage/locales/bot/ar-SA.json b/storage/locales/bot/ar-SA.json index eb2fb14..9e4710b 100644 --- a/storage/locales/bot/ar-SA.json +++ b/storage/locales/bot/ar-SA.json @@ -614,10 +614,14 @@ "servers": "📂 **الخوادم**", "users": "👥 **المستخدمون**", "players": "🎧 **المشغّلات**", + "playersWithPlaying": "{{total}} ({{playing}} قيد التشغيل)", + "queuedTracks": "🎶 **المسارات في الانتظار**", + "radios": "📻 **محطات 24/7**", "scrobbles": "🎵 **اسكروبلات**", "linkedUsers": "🔗 **حسابات Last.fm**", "ping": "🏓 **البينغ**", "gateway": "🌐 **البوابة**", + "shard": "🧩 **الشارد (Shard)**", "uptime": "⏱️ **وقت التشغيل**", "build": "🔧 **الإصدار**", "lastRestart": "🪛 **آخر إعادة تشغيل**", diff --git a/storage/locales/bot/ckb.json b/storage/locales/bot/ckb.json index 8042202..92fa3f4 100644 --- a/storage/locales/bot/ckb.json +++ b/storage/locales/bot/ckb.json @@ -614,10 +614,14 @@ "servers": "📂 **سێرڤەرەکان**", "users": "👥 **بەکارهێنەران**", "players": "🎧 **پەخشکەرەکان**", + "playersWithPlaying": "{{total}} ({{playing}} لێدەدرێت)", + "queuedTracks": "🎶 **گۆرانییەکانی ڕیزبەند**", + "radios": "📻 **ڕادیۆکانی 24/7**", "scrobbles": "🎵 **سکرۆبلەکان**", "linkedUsers": "🔗 **هەژمارەکانی Last.fm**", "ping": "🏓 **پینگ**", "gateway": "🌐 **گەیتوەی**", + "shard": "🧩 **شارد (Shard)**", "uptime": "⏱️ **کاتی چالاک**", "build": "🔧 **بەرنامە**", "lastRestart": "🪛 **دوایین دەستپێکردنەوە**", diff --git a/storage/locales/bot/de-DE.json b/storage/locales/bot/de-DE.json index 1cb7731..ca0cfbb 100644 --- a/storage/locales/bot/de-DE.json +++ b/storage/locales/bot/de-DE.json @@ -614,10 +614,14 @@ "servers": "📂 **Server**", "users": "👥 **Benutzer**", "players": "🎧 **Player**", + "playersWithPlaying": "{{total}} ({{playing}} aktiv)", + "queuedTracks": "🎶 **Warteschlangen-Titel**", + "radios": "📻 **24/7-Radios**", "scrobbles": "🎵 **Scrobbles**", "linkedUsers": "🔗 **Verknüpfte Last.fm**", "ping": "🏓 **Ping**", "gateway": "🌐 **Gateway**", + "shard": "🧩 **Shard**", "uptime": "⏱️ **Uptime**", "build": "🔧 **Build**", "lastRestart": "🪛 **Letzter Neustart**", diff --git a/storage/locales/bot/en.json b/storage/locales/bot/en.json index 8279b5e..4d55266 100644 --- a/storage/locales/bot/en.json +++ b/storage/locales/bot/en.json @@ -731,10 +731,14 @@ "servers": "📂 **Servers**", "users": "👥 **Users**", "players": "🎧 **Players**", + "playersWithPlaying": "{{total}} ({{playing}} playing)", + "queuedTracks": "🎶 **Queued Tracks**", + "radios": "📻 **24/7 Radios**", "scrobbles": "🎵 **Scrobbles**", "linkedUsers": "🔗 **Linked Last.fm**", "ping": "🏓 **Ping**", "gateway": "🌐 **Gateway**", + "shard": "🧩 **Shard**", "uptime": "⏱️ **Uptime**", "build": "🔧 **Build**", "lastRestart": "🪛 **Last restart**", diff --git a/storage/locales/bot/pt-BR.json b/storage/locales/bot/pt-BR.json index 1a12ac0..3d65bae 100644 --- a/storage/locales/bot/pt-BR.json +++ b/storage/locales/bot/pt-BR.json @@ -614,10 +614,14 @@ "servers": "📂 **Servidores**", "users": "👥 **Usuários**", "players": "🎧 **Players**", + "playersWithPlaying": "{{total}} ({{playing}} tocando)", + "queuedTracks": "🎶 **Músicas na Fila**", + "radios": "📻 **Rádios 24/7**", "scrobbles": "🎵 **Scrobbles**", "linkedUsers": "🔗 **Last.fm Vinculadas**", "ping": "🏓 **Ping**", "gateway": "🌐 **Gateway**", + "shard": "🧩 **Shard**", "uptime": "⏱️ **Tempo de Atividade**", "build": "🔧 **Build**", "lastRestart": "🪛 **Último Reinício**", diff --git a/storage/locales/bot/tr.json b/storage/locales/bot/tr.json index da725ad..ca79c48 100644 --- a/storage/locales/bot/tr.json +++ b/storage/locales/bot/tr.json @@ -720,10 +720,14 @@ "servers": "📂 **Sunucular**", "users": "👥 **Kullanıcılar**", "players": "🎧 **Oynatıcılar**", + "playersWithPlaying": "{{total}} ({{playing}} çalıyor)", + "queuedTracks": "🎶 **Kuyruktaki Parçalar**", + "radios": "📻 **24/7 Radyolar**", "scrobbles": "🎵 **Scrobble'lar**", "linkedUsers": "🔗 **Bağlı Last.fm**", "ping": "🏓 **Ping**", "gateway": "🌐 **Gateway**", + "shard": "🧩 **Shard**", "uptime": "⏱️ **Çalışma Süresi**", "build": "🔧 **Yapı**", "lastRestart": "🪛 **Son yeniden başlatma**",