mirror of
https://github.com/remix-bot/fluxer.git
synced 2026-11-24 06:01:44 -05:00
fixis
This commit is contained in:
1 parent
506d3c4cf8
commit
6b4bcf143c
2 files changed
+54
-89
No files matched your search
+52
-88
@@ -9,12 +9,12 @@ export const command = new CommandBuilder()
|
|||||||
.addAliases("info")
|
.addAliases("info")
|
||||||
.setCategory("util");
|
.setCategory("util");
|
||||||
|
|
||||||
// ── Stats cache ───────────────────────────────────────────────────────────────
|
// ── Stats cache ──────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
const CACHE_TTL_MS = 5 * 60 * 1000; // refresh every 5 minutes
|
const CACHE_TTL_MS = 5 * 60 * 1000;
|
||||||
const FAST_WAIT_MS = 1_500;
|
const FAST_WAIT_MS = 1_500;
|
||||||
|
|
||||||
let cachedStats = null; // { guildCount, userCount } or null
|
let cachedStats = null; // { guildCount, userCount }
|
||||||
let cacheExpiresAt = 0;
|
let cacheExpiresAt = 0;
|
||||||
let inflightPromise = null;
|
let inflightPromise = null;
|
||||||
|
|
||||||
@@ -25,61 +25,13 @@ function withTimeout(promise, ms, fallback) {
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
function computeGuildCount(client) {
|
||||||
* Accurate guild count — same multi-layer strategy as ErinJS:
|
|
||||||
* 1. Try client.fetchTotalGuildCount() (shard-aware, if available)
|
|
||||||
* 2. Try client.fetchAllStats().guilds (shard-aware, if available)
|
|
||||||
* 3. Paginate REST API via /users/@me/guilds?limit=200
|
|
||||||
* 4. Fallback to client.guilds.size (local shard only)
|
|
||||||
*/
|
|
||||||
async function fetchAccurateGuildCount(client) {
|
|
||||||
// Strategy 1: built-in shard-aware method
|
|
||||||
if (typeof client.fetchTotalGuildCount === "function") {
|
|
||||||
try {
|
|
||||||
const count = await client.fetchTotalGuildCount();
|
|
||||||
if (typeof count === "number" && count >= 0) return count;
|
|
||||||
} catch {}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Strategy 2: built-in stats aggregator
|
|
||||||
if (typeof client.fetchAllStats === "function") {
|
|
||||||
try {
|
|
||||||
const stats = await client.fetchAllStats();
|
|
||||||
if (typeof stats?.guilds === "number" && stats.guilds >= 0) return stats.guilds;
|
|
||||||
} catch {}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Strategy 3: REST API pagination — gets all guilds across shards
|
|
||||||
if (client.rest && typeof client.rest.get === "function") {
|
|
||||||
try {
|
|
||||||
let total = 0;
|
|
||||||
let after = undefined;
|
|
||||||
for (let page = 0; page < 25; page++) {
|
|
||||||
const route = `/users/@me/guilds?limit=200${after ? `&after=${after}` : ""}`;
|
|
||||||
const response = await withTimeout(client.rest.get(route), 800, null);
|
|
||||||
if (!response) break;
|
|
||||||
const batch = Array.isArray(response) ? response : (response?.guilds ?? []);
|
|
||||||
total += batch.length;
|
|
||||||
if (batch.length < 200) break;
|
|
||||||
after = batch[batch.length - 1]?.id;
|
|
||||||
if (!after) break;
|
|
||||||
}
|
|
||||||
if (total > 0) return total;
|
|
||||||
} catch {}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Strategy 4: fallback — local shard cache only
|
|
||||||
return client.guilds?.size ?? 0;
|
return client.guilds?.size ?? 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Accurate user count — sum memberCount across all guilds.
|
|
||||||
* Uses guild.memberCount from the GUILD_CREATE payload (O(1), no API calls).
|
|
||||||
* Falls back to guild.members.size if memberCount is missing.
|
|
||||||
*/
|
|
||||||
function computeUserCount(client) {
|
function computeUserCount(client) {
|
||||||
let total = 0;
|
let total = 0;
|
||||||
for (const [, guild] of client.guilds) {
|
for (const [, guild] of client.guilds ?? []) {
|
||||||
total += guild.memberCount ?? guild.members?.size ?? 0;
|
total += guild.memberCount ?? guild.members?.size ?? 0;
|
||||||
}
|
}
|
||||||
return total;
|
return total;
|
||||||
@@ -87,21 +39,17 @@ function computeUserCount(client) {
|
|||||||
|
|
||||||
async function refreshStats(client) {
|
async function refreshStats(client) {
|
||||||
try {
|
try {
|
||||||
const [guildCount, userCount] = await Promise.all([
|
cachedStats = {
|
||||||
fetchAccurateGuildCount(client),
|
guildCount: computeGuildCount(client),
|
||||||
Promise.resolve(computeUserCount(client)),
|
userCount: computeUserCount(client),
|
||||||
]);
|
};
|
||||||
cachedStats = { guildCount, userCount };
|
} catch {
|
||||||
} catch (err) {
|
cachedStats = {
|
||||||
console.error("[stats] refreshStats failed:", err);
|
guildCount: client.guilds?.size ?? 0,
|
||||||
// Keep old cache instead of overwriting with bad data
|
userCount: 0,
|
||||||
if (cachedStats) {
|
};
|
||||||
cacheExpiresAt = Date.now() + CACHE_TTL_MS;
|
|
||||||
inflightPromise = null;
|
|
||||||
return cachedStats;
|
|
||||||
}
|
|
||||||
cachedStats = { guildCount: client.guilds?.size ?? 0, userCount: 0 };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cacheExpiresAt = Date.now() + CACHE_TTL_MS;
|
cacheExpiresAt = Date.now() + CACHE_TTL_MS;
|
||||||
inflightPromise = null;
|
inflightPromise = null;
|
||||||
return cachedStats;
|
return cachedStats;
|
||||||
@@ -145,7 +93,21 @@ function getLivePlayerCount(playerMap) {
|
|||||||
|
|
||||||
// ── Embed builder ─────────────────────────────────────────────────────────────
|
// ── Embed builder ─────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
function buildEmbed(t, msg, { guildCount, userCount, playerCount, scrobbleCount, linkedUsers, ping, uptime, comHash, comLink, reason, footer, loading, lastfmEnabled }) {
|
function buildEmbed(t, msg, {
|
||||||
|
guildCount,
|
||||||
|
userCount,
|
||||||
|
playerCount,
|
||||||
|
scrobbleCount,
|
||||||
|
linkedUsers,
|
||||||
|
ping,
|
||||||
|
uptime,
|
||||||
|
comHash,
|
||||||
|
comLink,
|
||||||
|
reason,
|
||||||
|
footer,
|
||||||
|
loading,
|
||||||
|
lastfmEnabled,
|
||||||
|
}) {
|
||||||
const num = (v) => Utils.formatNumber(v);
|
const num = (v) => Utils.formatNumber(v);
|
||||||
const ld = (v) => loading ? "..." : v;
|
const ld = (v) => loading ? "..." : v;
|
||||||
|
|
||||||
@@ -155,7 +117,6 @@ function buildEmbed(t, msg, { guildCount, userCount, playerCount, scrobbleCount,
|
|||||||
`${t(msg, "responses.stats.players")} — \`${num(playerCount)}\``,
|
`${t(msg, "responses.stats.players")} — \`${num(playerCount)}\``,
|
||||||
];
|
];
|
||||||
|
|
||||||
// Add Last.fm stats if the integration is enabled
|
|
||||||
if (lastfmEnabled) {
|
if (lastfmEnabled) {
|
||||||
description.push(`${t(msg, "responses.stats.scrobbles")} — \`${ld(num(scrobbleCount))}\``);
|
description.push(`${t(msg, "responses.stats.scrobbles")} — \`${ld(num(scrobbleCount))}\``);
|
||||||
description.push(`${t(msg, "responses.stats.linkedUsers")} — \`${ld(num(linkedUsers))}\``);
|
description.push(`${t(msg, "responses.stats.linkedUsers")} — \`${ld(num(linkedUsers))}\``);
|
||||||
@@ -166,12 +127,12 @@ function buildEmbed(t, msg, { guildCount, userCount, playerCount, scrobbleCount,
|
|||||||
`${t(msg, "responses.stats.uptime")} — \`${uptime}\``,
|
`${t(msg, "responses.stats.uptime")} — \`${uptime}\``,
|
||||||
`${t(msg, "responses.stats.build")} — [\`${comHash}\`](${comLink})`,
|
`${t(msg, "responses.stats.build")} — [\`${comHash}\`](${comLink})`,
|
||||||
reason ? `${t(msg, "responses.stats.lastRestart")} — \`${reason}\`` : null,
|
reason ? `${t(msg, "responses.stats.lastRestart")} — \`${reason}\`` : null,
|
||||||
``,
|
"",
|
||||||
t(msg, "responses.stats.supportKofi"),
|
t(msg, "responses.stats.supportKofi"),
|
||||||
t(msg, "responses.stats.community"),
|
t(msg, "responses.stats.community"),
|
||||||
);
|
);
|
||||||
|
|
||||||
const desc = description.filter(l => l !== null).join("\n");
|
const desc = description.filter((line) => line !== null).join("\n");
|
||||||
|
|
||||||
const builder = new EmbedBuilder()
|
const builder = new EmbedBuilder()
|
||||||
.setColor(getGlobalColor())
|
.setColor(getGlobalColor())
|
||||||
@@ -191,7 +152,7 @@ export async function run(message) {
|
|||||||
const lastfmSnapshot = getLastfmSnapshot(lastfm);
|
const lastfmSnapshot = getLastfmSnapshot(lastfm);
|
||||||
|
|
||||||
const shared = {
|
const shared = {
|
||||||
guildCount: this.client.guilds.size,
|
guildCount: computeGuildCount(this.client),
|
||||||
playerCount: getLivePlayerCount(this.players.playerMap),
|
playerCount: getLivePlayerCount(this.players.playerMap),
|
||||||
scrobbleCount: lastfmSnapshot.scrobbleCount,
|
scrobbleCount: lastfmSnapshot.scrobbleCount,
|
||||||
linkedUsers: lastfmSnapshot.linkedUsers,
|
linkedUsers: lastfmSnapshot.linkedUsers,
|
||||||
@@ -203,40 +164,43 @@ export async function run(message) {
|
|||||||
footer: this.config.customStatsFooter || null,
|
footer: this.config.customStatsFooter || null,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Fetch Last.fm stats (non-blocking for initial reply)
|
const statsFallback = cachedStats ?? {
|
||||||
|
guildCount: shared.guildCount,
|
||||||
|
userCount: computeUserCount(this.client),
|
||||||
|
};
|
||||||
|
|
||||||
const scrobblePromise = lastfmEnabled
|
const scrobblePromise = lastfmEnabled
|
||||||
? withTimeout(lastfm.getStoredTotalScrobbles?.() ?? Promise.resolve(lastfmSnapshot.scrobbleCount), FAST_WAIT_MS, lastfmSnapshot.scrobbleCount)
|
? withTimeout(
|
||||||
|
lastfm.getStoredTotalScrobbles?.() ?? Promise.resolve(lastfmSnapshot.scrobbleCount),
|
||||||
|
FAST_WAIT_MS,
|
||||||
|
lastfmSnapshot.scrobbleCount
|
||||||
|
)
|
||||||
: Promise.resolve(0);
|
: Promise.resolve(0);
|
||||||
|
|
||||||
const linkedPromise = lastfmEnabled
|
const linkedPromise = lastfmEnabled
|
||||||
? withTimeout(lastfm.getLinkedUsersCount(), FAST_WAIT_MS, lastfmSnapshot.linkedUsers)
|
? withTimeout(lastfm.getLinkedUsersCount(), FAST_WAIT_MS, lastfmSnapshot.linkedUsers)
|
||||||
: Promise.resolve(0);
|
: Promise.resolve(0);
|
||||||
|
|
||||||
const hasCached = cachedStats !== null;
|
|
||||||
|
|
||||||
// Measure real round-trip latency
|
|
||||||
const start = Date.now();
|
const start = Date.now();
|
||||||
const msg = await message.reply({
|
const msg = await message.reply({
|
||||||
embeds: [buildEmbed((...a) => this.t(...a), message, {
|
embeds: [buildEmbed((...args) => this.t(...args), message, {
|
||||||
...shared,
|
...shared,
|
||||||
guildCount: hasCached ? cachedStats.guildCount : shared.guildCount,
|
guildCount: statsFallback.guildCount,
|
||||||
userCount: hasCached ? cachedStats.userCount : 0,
|
userCount: statsFallback.userCount,
|
||||||
ping: 0,
|
ping: 0,
|
||||||
loading: !hasCached,
|
loading: !cachedStats,
|
||||||
})]
|
})],
|
||||||
});
|
});
|
||||||
const ping = Date.now() - start;
|
const ping = Date.now() - start;
|
||||||
|
|
||||||
const [stats, scrobbleCount, linkedUsers] = await Promise.all([
|
const [stats, scrobbleCount, linkedUsers] = await Promise.all([
|
||||||
withTimeout(getStats(this.client), FAST_WAIT_MS, cachedStats ?? {
|
withTimeout(getStats(this.client), FAST_WAIT_MS, statsFallback),
|
||||||
guildCount: shared.guildCount,
|
|
||||||
userCount: 0,
|
|
||||||
}),
|
|
||||||
scrobblePromise,
|
scrobblePromise,
|
||||||
linkedPromise,
|
linkedPromise,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
await msg.edit({
|
await msg.edit({
|
||||||
embeds: [buildEmbed((...a) => this.t(...a), message, {
|
embeds: [buildEmbed((...args) => this.t(...args), message, {
|
||||||
...shared,
|
...shared,
|
||||||
guildCount: stats.guildCount,
|
guildCount: stats.guildCount,
|
||||||
userCount: stats.userCount,
|
userCount: stats.userCount,
|
||||||
@@ -244,6 +208,6 @@ export async function run(message) {
|
|||||||
linkedUsers,
|
linkedUsers,
|
||||||
ping,
|
ping,
|
||||||
loading: false,
|
loading: false,
|
||||||
})]
|
})],
|
||||||
}).catch((err) => console.error("[stats] editEmbed failed:", err));
|
}).catch((err) => console.error("[stats] editEmbed failed:", err));
|
||||||
}
|
}
|
||||||
@@ -606,6 +606,7 @@ export class Remix {
|
|||||||
*/
|
*/
|
||||||
_attachWsErrorHandlers() {
|
_attachWsErrorHandlers() {
|
||||||
try {
|
try {
|
||||||
|
if (this._wsErrorHandlersAttached) return;
|
||||||
const wsManager = this.client?.ws;
|
const wsManager = this.client?.ws;
|
||||||
if (!wsManager) return;
|
if (!wsManager) return;
|
||||||
|
|
||||||
@@ -657,7 +658,7 @@ export class Remix {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.player("[WS] Proactive error handlers attached to gateway sockets.");
|
this._wsErrorHandlersAttached = true;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
logger.warn("[WS] Failed to attach WS error handlers:", e.message);
|
logger.warn("[WS] Failed to attach WS error handlers:", e.message);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user