mirror of
https://github.com/remix-bot/fluxer.git
synced 2026-11-24 06:01:44 -05:00
Compare commits
2
Commits
3e30cdc6a3
...
c1dbda2068
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c1dbda2068 | ||
|
|
1e02478fb6 |
No files matched your search
+26
-56
@@ -11,6 +11,7 @@ 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";
|
||||
import { TrackStats } from "../src/music/TrackStats.mjs";
|
||||
|
||||
/**
|
||||
* @type {CommandBuilder}
|
||||
@@ -355,76 +356,52 @@ function buildEmbed(t, msg, s, pending = []) {
|
||||
})
|
||||
: num(s.playerCount);
|
||||
|
||||
const fields = [];
|
||||
|
||||
const overviewParts = [
|
||||
const sections = [
|
||||
`### ${tOr(t, msg, "responses.stats.categoryOverview", "📊 Overview")}`,
|
||||
`${t(msg, "responses.stats.servers")} — ${field("guilds", num(s.guildCount))}`,
|
||||
`${t(msg, "responses.stats.users")} — ${field("users", num(s.userCount))}`,
|
||||
`${tOr(t, msg, "responses.stats.shard", "🧩 **Shard**")} — \`${s.shard}\``,
|
||||
``,
|
||||
];
|
||||
fields.push({
|
||||
name: tOr(t, msg, "responses.stats.categoryOverview", "📊 Overview"),
|
||||
value: overviewParts.join(" • "),
|
||||
inline: false,
|
||||
});
|
||||
|
||||
const musicLines = [
|
||||
`### ${tOr(t, msg, "responses.stats.categoryMusic", "🎧 Music Activity")}`,
|
||||
`${t(msg, "responses.stats.players")} — ${field("players", playerVal)}`,
|
||||
`${tOr(t, msg, "responses.stats.tracksPlayed", "▶️ **Tracks Played**")} — ${field("tracksPlayed", num(s.tracksPlayed))}`,
|
||||
];
|
||||
const musicExtra = [];
|
||||
if (s.queuedTracks > 0) {
|
||||
musicExtra.push(`${tOr(t, msg, "responses.stats.queuedTracks", "🎶 **Queued Tracks**")} — ${field("queuedTracks", num(s.queuedTracks))}`);
|
||||
musicLines.push(`${tOr(t, msg, "responses.stats.queuedTracks", "🎶 **Queued Tracks**")} — ${field("queuedTracks", num(s.queuedTracks))}`);
|
||||
}
|
||||
if (s.radios247 > 0) {
|
||||
musicExtra.push(`${tOr(t, msg, "responses.stats.radios", "📻 **24/7 Radios**")} — ${field("radios", num(s.radios247))}`);
|
||||
musicLines.push(`${tOr(t, msg, "responses.stats.radios", "📻 **24/7 Radios**")} — ${field("radios", num(s.radios247))}`);
|
||||
}
|
||||
if (musicExtra.length > 0) {
|
||||
musicLines.push(musicExtra.join(" • "));
|
||||
}
|
||||
fields.push({
|
||||
name: tOr(t, msg, "responses.stats.categoryMusic", "🎧 Music Activity"),
|
||||
value: musicLines.join("\n"),
|
||||
inline: false,
|
||||
});
|
||||
musicLines.push(``);
|
||||
sections.push(...musicLines);
|
||||
|
||||
if (s.lastfmEnabled) {
|
||||
const lastfmParts = [
|
||||
sections.push(
|
||||
`### ${tOr(t, msg, "responses.stats.categoryLastfm", "🎵 Last.fm")}`,
|
||||
`${t(msg, "responses.stats.scrobbles")} — ${field("scrobbles", num(s.scrobbleCount))}`,
|
||||
`${t(msg, "responses.stats.linkedUsers")} — ${field("linkedUsers", num(s.linkedUsers))}`,
|
||||
];
|
||||
fields.push({
|
||||
name: tOr(t, msg, "responses.stats.categoryLastfm", "🎵 Last.fm"),
|
||||
value: lastfmParts.join(" • "),
|
||||
inline: false,
|
||||
});
|
||||
``,
|
||||
);
|
||||
}
|
||||
|
||||
const latencyParts = [
|
||||
const perfLines = [
|
||||
`### ${tOr(t, msg, "responses.stats.categoryPerformance", "⚡ System & Network")}`,
|
||||
`${t(msg, "responses.stats.ping")} — ${field("ping", `${num(s.ping)}ms`)}`,
|
||||
];
|
||||
if (s.gatewayPing >= 0) {
|
||||
latencyParts.push(`${tOr(t, msg, "responses.stats.gateway", GATEWAY_FALLBACK_LABEL)} — \`${num(s.gatewayPing)}ms\``);
|
||||
perfLines.push(`${tOr(t, msg, "responses.stats.gateway", GATEWAY_FALLBACK_LABEL)} — \`${num(s.gatewayPing)}ms\``);
|
||||
}
|
||||
const perfLines = [
|
||||
latencyParts.join(" • "),
|
||||
`${t(msg, "responses.stats.uptime")} — \`${s.uptime}\``,
|
||||
];
|
||||
fields.push({
|
||||
name: tOr(t, msg, "responses.stats.categoryPerformance", "⚡ System & Network"),
|
||||
value: perfLines.join("\n"),
|
||||
inline: false,
|
||||
});
|
||||
perfLines.push(`${t(msg, "responses.stats.uptime")} — \`${s.uptime}\``, ``);
|
||||
sections.push(...perfLines);
|
||||
|
||||
const bottomLines = [
|
||||
sections.push(
|
||||
`${t(msg, "responses.stats.build")} — [\`${s.comHash}\`](${s.comLink})` +
|
||||
(s.reason ? ` • ${t(msg, "responses.stats.lastRestart")} — \`${s.reason}\`` : ""),
|
||||
`${t(msg, "responses.stats.supportKofi")} • ${t(msg, "responses.stats.community")}`,
|
||||
];
|
||||
fields.push({
|
||||
name: "\u200B",
|
||||
value: bottomLines.join("\n"),
|
||||
inline: false,
|
||||
});
|
||||
);
|
||||
|
||||
const authorOptions = { name: t(msg, "responses.stats.title") };
|
||||
if (typeof s.botIcon === "string" && s.botIcon.startsWith("http")) {
|
||||
@@ -434,20 +411,9 @@ function buildEmbed(t, msg, s, pending = []) {
|
||||
const builder = new EmbedBuilder()
|
||||
.setColor(getGlobalColor())
|
||||
.setAuthor(authorOptions)
|
||||
.setDescription(sections.join("\n"))
|
||||
.setFooter({ text: s.footer || t(msg, "responses.stats.title") });
|
||||
|
||||
if (fields.length > 0) {
|
||||
try {
|
||||
builder.addFields(...fields);
|
||||
} catch (_) {
|
||||
if (typeof builder.setFields === "function") {
|
||||
try { builder.setFields(fields); } catch (__) { builder.data.fields = fields; }
|
||||
} else {
|
||||
builder.data.fields = fields;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof s.botIcon === "string" && s.botIcon.startsWith("http")) {
|
||||
try { builder.setThumbnail(s.botIcon); } catch (_) {}
|
||||
}
|
||||
@@ -473,6 +439,7 @@ export async function run(message) {
|
||||
const totals = getTotals(this.client);
|
||||
const music = getMusicStats(this.players?.playerMap);
|
||||
const shard = getShardInfo(this.client, message.guildId ?? message.guild?.id);
|
||||
const tracksPlayed = await TrackStats.getTracksPlayed();
|
||||
let botIcon = null;
|
||||
try {
|
||||
const raw = this.client?.user?.displayAvatarURL?.() || this.client?.user?.avatarURL?.();
|
||||
@@ -496,6 +463,7 @@ export async function run(message) {
|
||||
playingCount: music.playingCount,
|
||||
queuedTracks: music.queuedTracks,
|
||||
radios247: music.radios247,
|
||||
tracksPlayed,
|
||||
shard,
|
||||
botIcon,
|
||||
scrobbleCount: lastfmEnabled ? (cache.scrobbles ?? 0) : 0,
|
||||
@@ -541,6 +509,7 @@ export async function run(message) {
|
||||
|
||||
const fresh = getTotals(this.client);
|
||||
const freshMusic = getMusicStats(this.players?.playerMap);
|
||||
const freshTracks = await TrackStats.getTracksPlayed();
|
||||
const fill = {
|
||||
...embedData,
|
||||
guildCount: fresh.guildCount,
|
||||
@@ -549,6 +518,7 @@ export async function run(message) {
|
||||
playingCount: freshMusic.playingCount,
|
||||
queuedTracks: freshMusic.queuedTracks,
|
||||
radios247: freshMusic.radios247,
|
||||
tracksPlayed: freshTracks,
|
||||
scrobbleCount: lastfmEnabled ? (cache.scrobbles ?? 0) : 0,
|
||||
linkedUsers: lastfmEnabled ? (cache.linkedUsers ?? 0) : 0,
|
||||
ping: lastPing ?? 0,
|
||||
|
||||
@@ -30,6 +30,7 @@ import { GatewayHandler } from "../voice/gateway/GatewayHandler.mjs";
|
||||
import { LastFmManager } from "../services/lastfm/LastFmManager.mjs";
|
||||
import { FluxerListManager } from "../services/FluxerListManager.mjs";
|
||||
import { TrackOptionsManager } from "../services/TrackOptionsManager.mjs";
|
||||
import { TrackStats } from "../music/TrackStats.mjs";
|
||||
import { configureBilibili } from "../music/bilibili/index.mjs";
|
||||
import { configureAudio } from "../music/audio/AudioSettings.mjs";
|
||||
import { applyMixins } from "../utils/mixins.mjs";
|
||||
@@ -145,6 +146,9 @@ class Remix {
|
||||
redis: config.dashboard?.redis,
|
||||
mysql: sharedPromisePool,
|
||||
});
|
||||
if (this.dashboard?.redis) {
|
||||
TrackStats.setRedis(this.dashboard.redis);
|
||||
}
|
||||
|
||||
const presenceContents = config.presenceContents ?? [];
|
||||
const presenceInterval = config.presenceInterval ?? 30_000;
|
||||
@@ -686,6 +690,10 @@ class Remix {
|
||||
logger.warn("[Shutdown] Lavalink cleanup error:", e?.message);
|
||||
}
|
||||
|
||||
try {
|
||||
TrackStats.save();
|
||||
} catch (_) {}
|
||||
|
||||
// 4. Close Redis if dashboard enabled
|
||||
try {
|
||||
if (this.dashboard?.redis?.destroy) {
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
import { cleanId } from "../utils/Utils.mjs";
|
||||
import { getMessageGuildId } from "../ui/index.mjs";
|
||||
import { Dashboard } from "../dashboard/Dashboard.mjs";
|
||||
import { TrackStats } from "./TrackStats.mjs";
|
||||
|
||||
/** @private @param {Player} player @param {object|null} [fallbackChannel=null] @returns {string} Cleaned guild ID. */
|
||||
function getPlayerGuildId(player, fallbackChannel = null) {
|
||||
@@ -95,6 +96,10 @@ const PlayerEventsMixin = {
|
||||
emit("streamStartPlay", Date.now());
|
||||
this.dashboard.playerUpdate({ type: "startplay" }, player);
|
||||
|
||||
if (!player._radioReconnects) {
|
||||
TrackStats.increment();
|
||||
}
|
||||
|
||||
if (this._lastfm?.enabled && song) {
|
||||
this._handleLastFmStartPlay(player, song);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,116 @@
|
||||
/**
|
||||
* @module src/music/TrackStats
|
||||
* @description Persistent playback counter tracking total songs and tracks played across shards.
|
||||
* Backed by Redis with atomic INCR and debounced local storage in storage/stats.json.
|
||||
*/
|
||||
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
import { logger } from "../core/Logger.mjs";
|
||||
|
||||
const STATS_FILE = path.join(process.cwd(), "storage", "stats.json");
|
||||
const REDIS_KEY = "fluxer:stats:tracks_played";
|
||||
|
||||
class TrackStatsManager {
|
||||
constructor() {
|
||||
this._tracksPlayed = 0;
|
||||
this._extraData = {};
|
||||
this._redis = null;
|
||||
this._saveTimer = null;
|
||||
this._loadFromFile();
|
||||
}
|
||||
|
||||
_loadFromFile() {
|
||||
try {
|
||||
if (fs.existsSync(STATS_FILE)) {
|
||||
const raw = JSON.parse(fs.readFileSync(STATS_FILE, "utf8"));
|
||||
if (raw && typeof raw === "object") {
|
||||
this._extraData = { ...raw };
|
||||
const count = typeof raw.songs === "number"
|
||||
? raw.songs
|
||||
: (typeof raw.tracksPlayed === "number" ? raw.tracksPlayed : 0);
|
||||
this._tracksPlayed = Math.max(0, count);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
logger.warn("[TrackStats] Failed to read stats.json:", e?.message);
|
||||
}
|
||||
}
|
||||
|
||||
setRedis(redisHandler) {
|
||||
this._redis = redisHandler;
|
||||
this._syncRedis();
|
||||
}
|
||||
|
||||
async _syncRedis() {
|
||||
const client = this._redis?.client;
|
||||
if (!client || !client.isOpen) return;
|
||||
|
||||
try {
|
||||
await client.set(REDIS_KEY, String(this._tracksPlayed), { NX: true });
|
||||
const val = await client.get(REDIS_KEY);
|
||||
if (val != null) {
|
||||
const parsed = parseInt(val, 10);
|
||||
if (Number.isFinite(parsed)) {
|
||||
this._tracksPlayed = Math.max(this._tracksPlayed, parsed);
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
logger.warn("[TrackStats] Redis sync error:", e?.message);
|
||||
}
|
||||
}
|
||||
|
||||
increment(delta = 1) {
|
||||
this._tracksPlayed += delta;
|
||||
|
||||
const client = this._redis?.client;
|
||||
if (client && client.isOpen) {
|
||||
client.incrBy(REDIS_KEY, delta).catch((e) => {
|
||||
logger.warn("[TrackStats] Redis incr error:", e?.message);
|
||||
});
|
||||
}
|
||||
|
||||
this._scheduleSave();
|
||||
}
|
||||
|
||||
_scheduleSave() {
|
||||
if (this._saveTimer) return;
|
||||
this._saveTimer = setTimeout(() => {
|
||||
this._saveTimer = null;
|
||||
this.save();
|
||||
}, 5000);
|
||||
this._saveTimer.unref?.();
|
||||
}
|
||||
|
||||
save() {
|
||||
try {
|
||||
fs.mkdirSync(path.dirname(STATS_FILE), { recursive: true });
|
||||
const payload = {
|
||||
...this._extraData,
|
||||
songs: this._tracksPlayed,
|
||||
};
|
||||
fs.writeFileSync(STATS_FILE, JSON.stringify(payload, null, 2));
|
||||
} catch (e) {
|
||||
logger.warn("[TrackStats] Failed to save stats.json:", e?.message);
|
||||
}
|
||||
}
|
||||
|
||||
async getTracksPlayed() {
|
||||
const client = this._redis?.client;
|
||||
if (client && client.isOpen) {
|
||||
try {
|
||||
const val = await client.get(REDIS_KEY);
|
||||
if (val != null) {
|
||||
const parsed = parseInt(val, 10);
|
||||
if (Number.isFinite(parsed)) {
|
||||
this._tracksPlayed = Math.max(this._tracksPlayed, parsed);
|
||||
return this._tracksPlayed;
|
||||
}
|
||||
}
|
||||
} catch (_) {}
|
||||
}
|
||||
return this._tracksPlayed;
|
||||
}
|
||||
}
|
||||
|
||||
export const TrackStats = new TrackStatsManager();
|
||||
@@ -630,7 +630,8 @@
|
||||
"build": "🔧 **الإصدار**",
|
||||
"lastRestart": "🪛 **آخر إعادة تشغيل**",
|
||||
"supportKofi": "☕ [ادعمنا على Ko-fi](https://ko-fi.com/remixbot)",
|
||||
"community": "💬 [المجتمع](https://fluxer.gg/Remix)"
|
||||
"community": "💬 [المجتمع](https://fluxer.gg/Remix)",
|
||||
"tracksPlayed": "▶️ **المسارات المشغلة**"
|
||||
},
|
||||
"support": {
|
||||
"title": "💬 الدعم",
|
||||
|
||||
@@ -630,7 +630,8 @@
|
||||
"build": "🔧 **بەرنامە**",
|
||||
"lastRestart": "🪛 **دوایین دەستپێکردنەوە**",
|
||||
"supportKofi": "☕ [پشتگیری ئێمە لە Ko-fi](https://ko-fi.com/remixbot)",
|
||||
"community": "💬 [کۆمەڵگە](https://fluxer.gg/Remix)"
|
||||
"community": "💬 [کۆمەڵگە](https://fluxer.gg/Remix)",
|
||||
"tracksPlayed": "▶️ **گۆرانییە لێدراوەکان**"
|
||||
},
|
||||
"support": {
|
||||
"title": "💬 پشتگیری",
|
||||
|
||||
@@ -630,7 +630,8 @@
|
||||
"build": "🔧 **Build**",
|
||||
"lastRestart": "🪛 **Letzter Neustart**",
|
||||
"supportKofi": "☕ [Unterstütze uns auf Ko-fi](https://ko-fi.com/remixbot)",
|
||||
"community": "💬 [Community](https://fluxer.gg/Remix)"
|
||||
"community": "💬 [Community](https://fluxer.gg/Remix)",
|
||||
"tracksPlayed": "▶️ **Gespielte Titel**"
|
||||
},
|
||||
"support": {
|
||||
"title": "💬 Support",
|
||||
|
||||
@@ -747,7 +747,8 @@
|
||||
"build": "🔧 **Build**",
|
||||
"lastRestart": "🪛 **Last restart**",
|
||||
"supportKofi": "☕ [Support us on Ko-fi](https://ko-fi.com/remixbot)",
|
||||
"community": "💬 [Community](https://fluxer.gg/Remix)"
|
||||
"community": "💬 [Community](https://fluxer.gg/Remix)",
|
||||
"tracksPlayed": "▶️ **Tracks Played**"
|
||||
},
|
||||
"support": {
|
||||
"title": "💬 Support",
|
||||
|
||||
@@ -630,7 +630,8 @@
|
||||
"build": "🔧 **Build**",
|
||||
"lastRestart": "🪛 **Último Reinício**",
|
||||
"supportKofi": "☕ [Apoie-nos no Ko-fi](https://ko-fi.com/remixbot)",
|
||||
"community": "💬 [Comunidade](https://fluxer.gg/Remix)"
|
||||
"community": "💬 [Comunidade](https://fluxer.gg/Remix)",
|
||||
"tracksPlayed": "▶️ **Faixas Tocadas**"
|
||||
},
|
||||
"support": {
|
||||
"title": "💬 Suporte",
|
||||
|
||||
@@ -736,7 +736,8 @@
|
||||
"build": "🔧 **Yapı**",
|
||||
"lastRestart": "🪛 **Son yeniden başlatma**",
|
||||
"supportKofi": "☕ [Bizi Ko-fi'de destekleyin](https://ko-fi.com/remixbot)",
|
||||
"community": "💬 [Topluluk](https://fluxer.gg/Remix)"
|
||||
"community": "💬 [Topluluk](https://fluxer.gg/Remix)",
|
||||
"tracksPlayed": "▶️ **Oynatılan Parçalar**"
|
||||
},
|
||||
"support": {
|
||||
"title": "💬 Destek",
|
||||
|
||||
Reference in new issue
Block a user