mirror of
https://github.com/remix-bot/fluxer.git
synced 2026-11-24 06:01:44 -05:00
feat(stats): localize music stats, add shard and queue metrics, remove audio engine line
This commit is contained in:
1 parent
af23db1255
commit
d6c233c311
7 files changed
+124
-29
No files matched your search
+100
-29
@@ -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,
|
||||
|
||||
@@ -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": "🪛 **آخر إعادة تشغيل**",
|
||||
|
||||
@@ -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": "🪛 **دوایین دەستپێکردنەوە**",
|
||||
|
||||
@@ -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**",
|
||||
|
||||
@@ -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**",
|
||||
|
||||
@@ -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**",
|
||||
|
||||
@@ -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**",
|
||||
|
||||
Reference in new issue
Block a user