fix(stats): fix user count showing 0 and slow fetch

@fluxerjs/core Guild has no memberCount property, so paginate
guild.members.fetch() instead, pool 10 guilds at a time,
and cache the result for 5 minutes so repeated calls are instant.
This commit is contained in:
NoLogicAlan committed 2026-04-18 01:56:36 +03:00
1 parent 003eac39d8
commit 2a2e42efd9
1 file changed
+54 -18
+54 -18
View File
@@ -9,33 +9,68 @@ export const command = new CommandBuilder()
.addAliases("info") .addAliases("info")
.setCategory("util"); .setCategory("util");
// ── User count ──────────────────────────────────────────────────────────────── // ── User count cache ──────────────────────────────────────────────────────────
async function fetchUserCount(client) { const CACHE_TTL_MS = 5 * 60 * 1000; // refresh every 5 minutes
// client.rest is a concept and is not available on @fluxerjs/core.
// Use guild.memberCount (a cached integer Fluxer exposes on every Guild object) let cachedUserCount = null; // null = never fetched, 0 = real zero
// instead of paginating the REST members endpoint. let cacheExpiresAt = 0;
// Falls back to 0 per guild if the property is absent so the stat still renders. let inflightPromise = null;
async function pool(limit, tasks) {
const results = [];
const executing = new Set();
for (const task of tasks) {
const p = Promise.resolve().then(task).then(r => { results.push(r); executing.delete(p); });
executing.add(p);
if (executing.size >= limit) await Promise.race(executing);
}
await Promise.all(executing);
return results;
}
async function fetchGuildMemberCount(guild) {
try { try {
let after = undefined;
let total = 0; let total = 0;
for (const guild of client.guilds.cache.values()) { while (true) {
total += guild.memberCount ?? guild.member_count ?? 0; const batch = await guild.members.fetch({ limit: 1000, after });
total += batch.length;
if (batch.length < 1000) break;
after = batch.reduce((max, m) => (m.id > max ? m.id : max), "0");
} }
return total; return total;
} catch { return 0; } } catch {
return guild.members.size ?? 0;
}
}
async function refreshUserCount(client) {
const guilds = [...client.guilds.cache.values()];
const counts = await pool(10, guilds.map(g => () => fetchGuildMemberCount(g)));
cachedUserCount = counts.reduce((a, b) => a + b, 0);
cacheExpiresAt = Date.now() + CACHE_TTL_MS;
inflightPromise = null;
return cachedUserCount;
}
function getUserCount(client) {
if (cachedUserCount !== null && Date.now() < cacheExpiresAt) return Promise.resolve(cachedUserCount);
if (!inflightPromise) inflightPromise = refreshUserCount(client);
return inflightPromise;
} }
// ── Embed builder ───────────────────────────────────────────────────────────── // ── Embed builder ─────────────────────────────────────────────────────────────
function buildEmbed({ guildCount, userCount, playerCount, ping, uptime, comHash, comLink, reason, footer, loading }) { function buildEmbed({ guildCount, userCount, playerCount, ping, uptime, comHash, comLink, reason, footer, loading }) {
const num = (v) => Number(v).toLocaleString(); const num = (v) => Utils.formatNumber(v);
const ld = (v) => loading ? "..." : v; const ld = (v) => loading ? "..." : v;
const description = [ const description = [
`📂 **Servers** — \`${num(guildCount)}\``, `📂 **Servers** — \`${num(guildCount)}\``,
`👥 **Users** — \`${ld(num(userCount))}\``, `👥 **Users** — \`${ld(num(userCount))}\``,
`🎧 **Players** — \`${num(playerCount)}\``, `🎧 **Players** — \`${num(playerCount)}\``,
`🏓 **Ping** — \`${ld(`${ping}ms`)}\``, `🏓 **Ping** — \`${ld(`${num(ping)}ms`)}\``,
`⏱️ **Uptime** — \`${uptime}\``, `⏱️ **Uptime** — \`${uptime}\``,
`🔧 **Build** — [\`${comHash}\`](${comLink})`, `🔧 **Build** — [\`${comHash}\`](${comLink})`,
reason ? `🪛 **Last restart** — \`${reason}\`` : null, reason ? `🪛 **Last restart** — \`${reason}\`` : null,
@@ -49,8 +84,7 @@ function buildEmbed({ guildCount, userCount, playerCount, ping, uptime, comHash,
.setAuthor({ name: "Remix Music Bot" }) .setAuthor({ name: "Remix Music Bot" })
.setDescription(description) .setDescription(description)
.setFooter({ text: footer || "Remix Music Bot" }); .setFooter({ text: footer || "Remix Music Bot" });
// setTimestamp() is not verified on @fluxerjs/core's EmbedBuilder — call it only
// if the method exists so the command doesn't throw when building the embed.
if (typeof builder.setTimestamp === "function") builder.setTimestamp(); if (typeof builder.setTimestamp === "function") builder.setTimestamp();
return builder.toJSON(); return builder.toJSON();
} }
@@ -68,16 +102,18 @@ export async function run(message) {
footer: this.config.customStatsFooter || null, footer: this.config.customStatsFooter || null,
}; };
const hasCached = cachedUserCount !== null;
// Measure real Discord round-trip latency
const start = Date.now(); const start = Date.now();
const msg = await message.replyEmbed({ const msg = await message.replyEmbed({
embeds: [buildEmbed({ ...shared, userCount: 0, ping: 0, loading: true })] embeds: [buildEmbed({ ...shared, userCount: hasCached ? cachedUserCount : 0, ping: 0, loading: !hasCached })]
}); });
const ping = Date.now() - start;
const ping = Date.now() - start; const users = await getUserCount(this.client);
const users = await fetchUserCount(this.client);
await msg.editEmbed({ await msg.editEmbed({
embeds: [buildEmbed({ ...shared, userCount: users, ping, loading: false })] embeds: [buildEmbed({ ...shared, userCount: users, ping, loading: false })]
}).catch(() => {}); }).catch((err) => console.error("[stats] editEmbed failed:", err));
} }