mirror of
https://github.com/remix-bot/fluxer.git
synced 2026-11-24 06:01:44 -05:00
notfix
This commit is contained in:
1 parent
fab2d51681
commit
18d46e6df0
1 file changed
+238
-176
+238
-176
@@ -9,14 +9,23 @@ export const command = new CommandBuilder()
|
|||||||
.addAliases("info")
|
.addAliases("info")
|
||||||
.setCategory("util");
|
.setCategory("util");
|
||||||
|
|
||||||
// ── Stats cache ──────────────────────────────────────────────────────────────
|
|
||||||
|
|
||||||
const CACHE_TTL_MS = 5 * 60 * 1000;
|
const CACHE_TTL_MS = 5 * 60 * 1000;
|
||||||
const FAST_WAIT_MS = 8_000;
|
const GUILD_FETCH_TIMEOUT_MS = 4_000;
|
||||||
|
const REST_PAGE_TIMEOUT_MS = 1_000;
|
||||||
|
const LASTFM_TIMEOUT_MS = 3_000;
|
||||||
|
|
||||||
let cachedStats = null; // { guildCount, userCount }
|
let cachedStats = null;
|
||||||
let cacheExpiresAt = 0;
|
let cacheExpiresAt = 0;
|
||||||
let inflightPromise = null;
|
let inflightStatsPromise = null;
|
||||||
|
|
||||||
|
function isAbortError(err) {
|
||||||
|
const name = String(err?.name ?? "");
|
||||||
|
const code = String(err?.code ?? "");
|
||||||
|
const message = String(err?.message ?? err ?? "");
|
||||||
|
return name === "AbortError" ||
|
||||||
|
code === "ABORT_ERR" ||
|
||||||
|
message.includes("This operation was aborted");
|
||||||
|
}
|
||||||
|
|
||||||
function getMessageGuildId(message) {
|
function getMessageGuildId(message) {
|
||||||
return message?.channel?.channel?.guildId ??
|
return message?.channel?.channel?.guildId ??
|
||||||
@@ -34,35 +43,43 @@ function createTranslator(remix) {
|
|||||||
|
|
||||||
function withTimeout(promise, ms, fallback) {
|
function withTimeout(promise, ms, fallback) {
|
||||||
return Promise.race([
|
return Promise.race([
|
||||||
promise,
|
Promise.resolve(promise),
|
||||||
new Promise((resolve) => setTimeout(() => resolve(fallback), ms)),
|
new Promise((resolve) => setTimeout(() => resolve(fallback), ms)),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
async function pool(limit, tasks) {
|
async function runLimited(tasks, limit = 6) {
|
||||||
const results = [];
|
const results = new Array(tasks.length);
|
||||||
const executing = new Set();
|
let index = 0;
|
||||||
for (const task of tasks) {
|
|
||||||
const p = Promise.resolve()
|
async function worker() {
|
||||||
.then(task)
|
while (index < tasks.length) {
|
||||||
.then((value) => {
|
const current = index++;
|
||||||
results.push(value);
|
try {
|
||||||
executing.delete(p);
|
results[current] = await tasks[current]();
|
||||||
})
|
} catch (_) {
|
||||||
.catch(() => {
|
results[current] = null;
|
||||||
results.push(0);
|
}
|
||||||
executing.delete(p);
|
}
|
||||||
});
|
|
||||||
executing.add(p);
|
|
||||||
if (executing.size >= limit) await Promise.race(executing);
|
|
||||||
}
|
}
|
||||||
await Promise.all(executing);
|
|
||||||
|
const workers = Array.from({ length: Math.min(limit, tasks.length) }, () => worker());
|
||||||
|
await Promise.all(workers);
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
function computeUserCount(client) {
|
function getCachedGuildCount(client) {
|
||||||
|
return Number(client?.guilds?.size ?? 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getFastCachedUserCount() {
|
||||||
|
return Number(cachedStats?.userCount ?? 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getCachedUserCount(client) {
|
||||||
const uniqueUsers = new Set();
|
const uniqueUsers = new Set();
|
||||||
for (const [, guild] of client.guilds ?? []) {
|
|
||||||
|
for (const guild of client?.guilds?.values?.() ?? []) {
|
||||||
const members = guild?.members;
|
const members = guild?.members;
|
||||||
const iterable = typeof members?.values === "function"
|
const iterable = typeof members?.values === "function"
|
||||||
? members.values()
|
? members.values()
|
||||||
@@ -75,58 +92,85 @@ function computeUserCount(client) {
|
|||||||
if (id) uniqueUsers.add(String(id));
|
if (id) uniqueUsers.add(String(id));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return uniqueUsers.size;
|
return uniqueUsers.size;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fetchAccurateGuildCount(client) {
|
async function fetchGuildCount(client) {
|
||||||
if (typeof client?.user?.fetchGuilds === "function") {
|
if (typeof client?.user?.fetchGuilds === "function") {
|
||||||
try {
|
try {
|
||||||
const guilds = await withTimeout(client.user.fetchGuilds(), 4_000, null);
|
const guilds = await withTimeout(client.user.fetchGuilds(), GUILD_FETCH_TIMEOUT_MS, null);
|
||||||
if (Array.isArray(guilds)) return guilds.length;
|
if (Array.isArray(guilds)) return guilds.length;
|
||||||
if (typeof guilds?.size === "number") return guilds.size;
|
if (typeof guilds?.size === "number") return guilds.size;
|
||||||
if (typeof guilds?.values === "function") return [...guilds.values()].length;
|
if (typeof guilds?.values === "function") return [...guilds.values()].length;
|
||||||
} catch (_) {}
|
} catch (_) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof client.fetchTotalGuildCount === "function") {
|
if (typeof client?.fetchTotalGuildCount === "function") {
|
||||||
try {
|
try {
|
||||||
const count = await withTimeout(client.fetchTotalGuildCount(), 4_000, null);
|
const total = await withTimeout(client.fetchTotalGuildCount(), GUILD_FETCH_TIMEOUT_MS, null);
|
||||||
if (typeof count === "number" && count >= 0) return count;
|
if (typeof total === "number" && total >= 0) return total;
|
||||||
} catch (_) {}
|
} catch (_) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (typeof client.fetchAllStats === "function") {
|
if (typeof client?.fetchAllStats === "function") {
|
||||||
try {
|
try {
|
||||||
const stats = await withTimeout(client.fetchAllStats(), 4_000, null);
|
const stats = await withTimeout(client.fetchAllStats(), GUILD_FETCH_TIMEOUT_MS, null);
|
||||||
if (typeof stats?.guilds === "number" && stats.guilds >= 0) return stats.guilds;
|
if (typeof stats?.guilds === "number" && stats.guilds >= 0) return stats.guilds;
|
||||||
} catch (_) {}
|
} catch (_) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (client.rest && typeof client.rest.get === "function") {
|
if (client?.rest && typeof client.rest.get === "function") {
|
||||||
try {
|
try {
|
||||||
let total = 0;
|
let total = 0;
|
||||||
let after = undefined;
|
let after = null;
|
||||||
|
|
||||||
for (let page = 0; page < 25; page++) {
|
for (let page = 0; page < 25; page++) {
|
||||||
const route = `/users/@me/guilds?limit=200${after ? `&after=${after}` : ""}`;
|
const route = `/users/@me/guilds?limit=200${after ? `&after=${after}` : ""}`;
|
||||||
const response = await withTimeout(client.rest.get(route), 800, null);
|
const response = await withTimeout(client.rest.get(route), REST_PAGE_TIMEOUT_MS, null);
|
||||||
if (!response) break;
|
if (!response) break;
|
||||||
|
|
||||||
const batch = Array.isArray(response) ? response : (response?.guilds ?? []);
|
const batch = Array.isArray(response) ? response : (response?.guilds ?? []);
|
||||||
total += batch.length;
|
total += batch.length;
|
||||||
if (batch.length < 200) break;
|
if (batch.length < 200) break;
|
||||||
after = batch[batch.length - 1]?.id;
|
|
||||||
|
after = batch[batch.length - 1]?.id ?? null;
|
||||||
if (!after) break;
|
if (!after) break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (total > 0) return total;
|
if (total > 0) return total;
|
||||||
} catch (_) {}
|
} catch (_) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
return client.guilds?.size ?? 0;
|
return getCachedGuildCount(client);
|
||||||
|
}
|
||||||
|
|
||||||
|
function collectIdsFromMembers(guild) {
|
||||||
|
const ids = new Set();
|
||||||
|
const members = guild?.members;
|
||||||
|
const iterable = typeof members?.values === "function"
|
||||||
|
? members.values()
|
||||||
|
: Array.isArray(members)
|
||||||
|
? members
|
||||||
|
: Object.values(members ?? {});
|
||||||
|
|
||||||
|
for (const member of iterable) {
|
||||||
|
const id = member?.id ?? member?.user?.id ?? null;
|
||||||
|
if (id) ids.add(String(id));
|
||||||
|
}
|
||||||
|
|
||||||
|
return ids;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fetchGuildMemberIds(guild) {
|
async function fetchGuildMemberIds(guild) {
|
||||||
|
if (typeof guild?.members?.fetch !== "function") {
|
||||||
|
return collectIdsFromMembers(guild);
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let after = undefined;
|
|
||||||
const ids = new Set();
|
const ids = new Set();
|
||||||
|
let after = undefined;
|
||||||
|
|
||||||
for (let page = 0; page < 1000; page++) {
|
for (let page = 0; page < 1000; page++) {
|
||||||
const raw = await guild.members.fetch({ limit: 1000, after });
|
const raw = await guild.members.fetch({ limit: 1000, after });
|
||||||
const batch = Array.isArray(raw)
|
const batch = Array.isArray(raw)
|
||||||
@@ -134,68 +178,75 @@ async function fetchGuildMemberIds(guild) {
|
|||||||
: typeof raw?.values === "function"
|
: typeof raw?.values === "function"
|
||||||
? [...raw.values()]
|
? [...raw.values()]
|
||||||
: Object.values(raw ?? {});
|
: Object.values(raw ?? {});
|
||||||
|
|
||||||
for (const member of batch) {
|
for (const member of batch) {
|
||||||
const id = member?.id ?? member?.user?.id ?? null;
|
const id = member?.id ?? member?.user?.id ?? null;
|
||||||
if (id) ids.add(String(id));
|
if (id) ids.add(String(id));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (batch.length < 1000) break;
|
if (batch.length < 1000) break;
|
||||||
|
|
||||||
after = batch.reduce((max, member) => {
|
after = batch.reduce((max, member) => {
|
||||||
const id = member.id ?? member.user?.id ?? "";
|
const id = String(member?.id ?? member?.user?.id ?? "");
|
||||||
return id > max ? id : max;
|
return id > max ? id : max;
|
||||||
}, "0");
|
}, "0");
|
||||||
|
|
||||||
if (!after) break;
|
if (!after) break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return ids;
|
return ids;
|
||||||
} catch {
|
} catch (_) {
|
||||||
const ids = new Set();
|
return collectIdsFromMembers(guild);
|
||||||
const members = guild?.members;
|
|
||||||
const iterable = typeof members?.values === "function"
|
|
||||||
? members.values()
|
|
||||||
: Array.isArray(members)
|
|
||||||
? members
|
|
||||||
: Object.values(members ?? {});
|
|
||||||
for (const member of iterable) {
|
|
||||||
const id = member?.id ?? member?.user?.id ?? null;
|
|
||||||
if (id) ids.add(String(id));
|
|
||||||
}
|
|
||||||
return ids;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function fetchAccurateUserCount(client) {
|
async function fetchUserCount(client) {
|
||||||
const guilds = [...(client.guilds?.values?.() ?? [])];
|
const guilds = [...(client?.guilds?.values?.() ?? [])];
|
||||||
if (guilds.length === 0) return 0;
|
if (guilds.length === 0) return 0;
|
||||||
const idSets = await pool(8, guilds.map((guild) => () => fetchGuildMemberIds(guild)));
|
|
||||||
|
const idSets = await runLimited(
|
||||||
|
guilds.map((guild) => () => fetchGuildMemberIds(guild)),
|
||||||
|
8
|
||||||
|
);
|
||||||
|
|
||||||
const uniqueUsers = new Set();
|
const uniqueUsers = new Set();
|
||||||
for (const idSet of idSets) {
|
for (const idSet of idSets) {
|
||||||
if (!idSet || typeof idSet[Symbol.iterator] !== "function") continue;
|
if (!idSet || typeof idSet[Symbol.iterator] !== "function") continue;
|
||||||
for (const id of idSet) uniqueUsers.add(String(id));
|
for (const id of idSet) uniqueUsers.add(String(id));
|
||||||
}
|
}
|
||||||
|
|
||||||
return uniqueUsers.size;
|
return uniqueUsers.size;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function refreshStats(client) {
|
async function refreshStats(client) {
|
||||||
try {
|
try {
|
||||||
cachedStats = {
|
cachedStats = {
|
||||||
guildCount: await fetchAccurateGuildCount(client),
|
guildCount: await fetchGuildCount(client),
|
||||||
userCount: await fetchAccurateUserCount(client),
|
userCount: await fetchUserCount(client),
|
||||||
};
|
};
|
||||||
} catch {
|
} catch (_) {
|
||||||
cachedStats = {
|
cachedStats = {
|
||||||
guildCount: client.guilds?.size ?? 0,
|
guildCount: getCachedGuildCount(client),
|
||||||
userCount: computeUserCount(client),
|
userCount: getCachedUserCount(client),
|
||||||
};
|
};
|
||||||
|
} finally {
|
||||||
|
cacheExpiresAt = Date.now() + CACHE_TTL_MS;
|
||||||
|
inflightStatsPromise = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
cacheExpiresAt = Date.now() + CACHE_TTL_MS;
|
|
||||||
inflightPromise = null;
|
|
||||||
return cachedStats;
|
return cachedStats;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getStats(client) {
|
function getStats(client) {
|
||||||
if (cachedStats && Date.now() < cacheExpiresAt) return Promise.resolve(cachedStats);
|
if (cachedStats && Date.now() < cacheExpiresAt) {
|
||||||
if (!inflightPromise) inflightPromise = refreshStats(client);
|
return Promise.resolve(cachedStats);
|
||||||
return inflightPromise;
|
}
|
||||||
|
|
||||||
|
if (!inflightStatsPromise) {
|
||||||
|
inflightStatsPromise = refreshStats(client);
|
||||||
|
}
|
||||||
|
|
||||||
|
return inflightStatsPromise;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getLastfmSnapshot(lastfm) {
|
function getLastfmSnapshot(lastfm) {
|
||||||
@@ -206,88 +257,71 @@ function getLastfmSnapshot(lastfm) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function getLivePlayerCount(playerMap) {
|
function getLivePlayerCount(playerMap) {
|
||||||
let live = 0;
|
let count = 0;
|
||||||
|
|
||||||
for (const [, player] of playerMap ?? []) {
|
for (const player of playerMap?.values?.() ?? []) {
|
||||||
if (!player || player._destroyed || player.leaving) continue;
|
if (!player || player._destroyed || player.leaving || player._isJoining) continue;
|
||||||
if (player._isJoining) continue;
|
|
||||||
|
|
||||||
const conn = player.connection;
|
const room = player?.connection?.room;
|
||||||
if (!conn) continue;
|
if (room && !room.isConnected && (room.connectionState === 0 || room.connectionState === "disconnected")) {
|
||||||
|
continue;
|
||||||
const room = conn.room;
|
|
||||||
if (room) {
|
|
||||||
const isConnected = room.isConnected;
|
|
||||||
const connectionState = room.connectionState;
|
|
||||||
if (!isConnected && (connectionState === 0 || connectionState === "disconnected")) continue;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
live++;
|
if (!player.connection) continue;
|
||||||
|
count++;
|
||||||
}
|
}
|
||||||
|
|
||||||
return live;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
// ── Embed builder ─────────────────────────────────────────────────────────────
|
function buildEmbed(t, message, stats) {
|
||||||
|
const num = (value) => Utils.formatNumber(Number(value ?? 0));
|
||||||
|
const maybeLoading = (value, loading) => loading ? "..." : value;
|
||||||
|
|
||||||
function buildEmbed(t, msg, {
|
const lines = [
|
||||||
guildCount,
|
`${t(message, "responses.stats.servers")} — \`${num(stats.guildCount)}\``,
|
||||||
userCount,
|
`${t(message, "responses.stats.users")} — \`${maybeLoading(num(stats.userCount), stats.loading)}\``,
|
||||||
playerCount,
|
`${t(message, "responses.stats.players")} — \`${num(stats.playerCount)}\``,
|
||||||
scrobbleCount,
|
|
||||||
linkedUsers,
|
|
||||||
ping,
|
|
||||||
uptime,
|
|
||||||
comHash,
|
|
||||||
comLink,
|
|
||||||
reason,
|
|
||||||
footer,
|
|
||||||
loading,
|
|
||||||
lastfmEnabled,
|
|
||||||
}) {
|
|
||||||
const num = (v) => Utils.formatNumber(v);
|
|
||||||
const ld = (v) => loading ? "..." : v;
|
|
||||||
|
|
||||||
const description = [
|
|
||||||
`${t(msg, "responses.stats.servers")} — \`${num(guildCount)}\``,
|
|
||||||
`${t(msg, "responses.stats.users")} — \`${ld(num(userCount))}\``,
|
|
||||||
`${t(msg, "responses.stats.players")} — \`${num(playerCount)}\``,
|
|
||||||
];
|
];
|
||||||
|
|
||||||
if (lastfmEnabled) {
|
if (stats.lastfmEnabled) {
|
||||||
description.push(`${t(msg, "responses.stats.scrobbles")} — \`${ld(num(scrobbleCount))}\``);
|
lines.push(`${t(message, "responses.stats.scrobbles")} — \`${maybeLoading(num(stats.scrobbleCount), stats.loading)}\``);
|
||||||
description.push(`${t(msg, "responses.stats.linkedUsers")} — \`${ld(num(linkedUsers))}\``);
|
lines.push(`${t(message, "responses.stats.linkedUsers")} — \`${maybeLoading(num(stats.linkedUsers), stats.loading)}\``);
|
||||||
}
|
}
|
||||||
|
|
||||||
description.push(
|
lines.push(
|
||||||
`${t(msg, "responses.stats.ping")} — \`${ld(`${num(ping)}ms`)}\``,
|
`${t(message, "responses.stats.ping")} — \`${maybeLoading(`${num(stats.ping)}ms`, stats.loading)}\``,
|
||||||
`${t(msg, "responses.stats.uptime")} — \`${uptime}\``,
|
`${t(message, "responses.stats.uptime")} — \`${stats.uptime}\``,
|
||||||
`${t(msg, "responses.stats.build")} — [\`${comHash}\`](${comLink})`,
|
`${t(message, "responses.stats.build")} — [\`${stats.comHash}\`](${stats.comLink})`
|
||||||
reason ? `${t(msg, "responses.stats.lastRestart")} — \`${reason}\`` : null,
|
|
||||||
"",
|
|
||||||
t(msg, "responses.stats.supportKofi"),
|
|
||||||
t(msg, "responses.stats.community"),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
const desc = description.filter((line) => line !== null).join("\n");
|
if (stats.reason) {
|
||||||
|
lines.push(`${t(message, "responses.stats.lastRestart")} — \`${stats.reason}\``);
|
||||||
|
}
|
||||||
|
|
||||||
const builder = new EmbedBuilder()
|
lines.push("", t(message, "responses.stats.supportKofi"), t(message, "responses.stats.community"));
|
||||||
|
|
||||||
|
const embed = new EmbedBuilder()
|
||||||
.setColor(getGlobalColor())
|
.setColor(getGlobalColor())
|
||||||
.setAuthor({ name: t(msg, "responses.stats.title") })
|
.setAuthor({ name: t(message, "responses.stats.title") })
|
||||||
.setDescription(desc)
|
.setDescription(lines.join("\n"))
|
||||||
.setFooter({ text: footer || t(msg, "responses.stats.title") });
|
.setFooter({ text: stats.footer || t(message, "responses.stats.title") });
|
||||||
|
|
||||||
if (typeof builder.setTimestamp === "function") builder.setTimestamp();
|
if (typeof embed.setTimestamp === "function") {
|
||||||
return builder;
|
embed.setTimestamp();
|
||||||
|
}
|
||||||
|
|
||||||
|
return embed;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function safeEditStatsMessage(messageRef, payload, fallbackMessage) {
|
async function editOrReply(messageRef, payload, fallbackMessage) {
|
||||||
try {
|
try {
|
||||||
if (messageRef && typeof messageRef.edit === "function") {
|
if (typeof messageRef?.edit === "function") {
|
||||||
await messageRef.edit(payload);
|
await messageRef.edit(payload);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (messageRef?.message && typeof messageRef.message.edit === "function") {
|
|
||||||
|
if (typeof messageRef?.message?.edit === "function") {
|
||||||
await messageRef.message.edit(payload);
|
await messageRef.message.edit(payload);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -299,84 +333,112 @@ async function safeEditStatsMessage(messageRef, payload, fallbackMessage) {
|
|||||||
await fallbackMessage.reply(payload);
|
await fallbackMessage.reply(payload);
|
||||||
return false;
|
return false;
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error("[stats] fallback reply failed:", err);
|
if (!isAbortError(err)) {
|
||||||
|
console.error("[stats] fallback reply failed:", err);
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ── Runner ────────────────────────────────────────────────────────────────────
|
async function safeReply(message, payload) {
|
||||||
|
try {
|
||||||
|
return await message.reply(payload);
|
||||||
|
} catch (err) {
|
||||||
|
if (!isAbortError(err)) {
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.warn("[stats] reply aborted by Fluxer timeout");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export async function run(message) {
|
export async function run(message) {
|
||||||
|
const t = createTranslator(this);
|
||||||
|
const lastfm = this.lastfm;
|
||||||
|
const lastfmEnabled = lastfm?.enabled ?? false;
|
||||||
|
const lastfmSnapshot = getLastfmSnapshot(lastfm);
|
||||||
|
|
||||||
|
const baseStats = {
|
||||||
|
guildCount: getCachedGuildCount(this.client),
|
||||||
|
userCount: getFastCachedUserCount(),
|
||||||
|
playerCount: getLivePlayerCount(this.players.playerMap),
|
||||||
|
scrobbleCount: lastfmSnapshot.scrobbleCount,
|
||||||
|
linkedUsers: lastfmSnapshot.linkedUsers,
|
||||||
|
lastfmEnabled,
|
||||||
|
uptime: Utils.prettifyMS(Math.round(process.uptime()) * 1000),
|
||||||
|
comHash: this.comHash,
|
||||||
|
comLink: this.comLink,
|
||||||
|
reason: this.config.restart ?? null,
|
||||||
|
footer: this.config.customStatsFooter || null,
|
||||||
|
ping: 0,
|
||||||
|
loading: !cachedStats,
|
||||||
|
};
|
||||||
|
|
||||||
|
const cachedOrFallbackStats = cachedStats ?? {
|
||||||
|
guildCount: baseStats.guildCount,
|
||||||
|
userCount: getCachedUserCount(this.client),
|
||||||
|
};
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const t = createTranslator(this);
|
const start = Date.now();
|
||||||
const lastfm = this.lastfm;
|
const reply = await safeReply(message, {
|
||||||
const lastfmEnabled = lastfm?.enabled ?? false;
|
embeds: [
|
||||||
const lastfmSnapshot = getLastfmSnapshot(lastfm);
|
buildEmbed(t, message, {
|
||||||
|
...baseStats,
|
||||||
|
guildCount: cachedOrFallbackStats.guildCount,
|
||||||
|
userCount: cachedOrFallbackStats.userCount,
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
});
|
||||||
|
if (!reply) return;
|
||||||
|
|
||||||
const shared = {
|
const ping = Date.now() - start;
|
||||||
guildCount: this.client.guilds?.size ?? 0,
|
|
||||||
playerCount: getLivePlayerCount(this.players.playerMap),
|
|
||||||
scrobbleCount: lastfmSnapshot.scrobbleCount,
|
|
||||||
linkedUsers: lastfmSnapshot.linkedUsers,
|
|
||||||
lastfmEnabled,
|
|
||||||
uptime: Utils.prettifyMS(Math.round(process.uptime()) * 1000),
|
|
||||||
comHash: this.comHash,
|
|
||||||
comLink: this.comLink,
|
|
||||||
reason: this.config.restart ?? null,
|
|
||||||
footer: this.config.customStatsFooter || null,
|
|
||||||
};
|
|
||||||
|
|
||||||
const statsFallback = cachedStats ?? {
|
|
||||||
guildCount: shared.guildCount,
|
|
||||||
userCount: computeUserCount(this.client),
|
|
||||||
};
|
|
||||||
|
|
||||||
|
const statsPromise = getStats(this.client).catch(() => cachedOrFallbackStats);
|
||||||
const scrobblePromise = lastfmEnabled
|
const scrobblePromise = lastfmEnabled
|
||||||
? withTimeout(
|
? withTimeout(
|
||||||
lastfm.getStoredTotalScrobbles?.() ?? Promise.resolve(lastfmSnapshot.scrobbleCount),
|
lastfm.getStoredTotalScrobbles?.() ?? Promise.resolve(lastfmSnapshot.scrobbleCount),
|
||||||
3_000,
|
LASTFM_TIMEOUT_MS,
|
||||||
lastfmSnapshot.scrobbleCount
|
lastfmSnapshot.scrobbleCount
|
||||||
).catch(() => lastfmSnapshot.scrobbleCount)
|
).catch(() => lastfmSnapshot.scrobbleCount)
|
||||||
: Promise.resolve(0);
|
: Promise.resolve(0);
|
||||||
|
|
||||||
const linkedPromise = lastfmEnabled
|
const linkedPromise = lastfmEnabled
|
||||||
? withTimeout(lastfm.getLinkedUsersCount(), FAST_WAIT_MS, lastfmSnapshot.linkedUsers).catch(() => lastfmSnapshot.linkedUsers)
|
? withTimeout(
|
||||||
|
lastfm.getLinkedUsersCount?.() ?? Promise.resolve(lastfmSnapshot.linkedUsers),
|
||||||
|
LASTFM_TIMEOUT_MS,
|
||||||
|
lastfmSnapshot.linkedUsers
|
||||||
|
).catch(() => lastfmSnapshot.linkedUsers)
|
||||||
: Promise.resolve(0);
|
: Promise.resolve(0);
|
||||||
|
|
||||||
const start = Date.now();
|
|
||||||
const msg = await message.reply({
|
|
||||||
embeds: [buildEmbed(t, message, {
|
|
||||||
...shared,
|
|
||||||
guildCount: statsFallback.guildCount,
|
|
||||||
userCount: statsFallback.userCount,
|
|
||||||
ping: 0,
|
|
||||||
loading: !cachedStats,
|
|
||||||
})],
|
|
||||||
});
|
|
||||||
const ping = Date.now() - start;
|
|
||||||
|
|
||||||
const [stats, scrobbleCount, linkedUsers] = await Promise.all([
|
const [stats, scrobbleCount, linkedUsers] = await Promise.all([
|
||||||
getStats(this.client).catch(() => statsFallback),
|
statsPromise,
|
||||||
scrobblePromise,
|
scrobblePromise,
|
||||||
linkedPromise,
|
linkedPromise,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
await safeEditStatsMessage(msg, {
|
await editOrReply(reply, {
|
||||||
embeds: [buildEmbed(t, message, {
|
embeds: [
|
||||||
...shared,
|
buildEmbed(t, message, {
|
||||||
guildCount: stats?.guildCount ?? statsFallback.guildCount,
|
...baseStats,
|
||||||
userCount: stats?.userCount ?? statsFallback.userCount,
|
guildCount: stats?.guildCount ?? cachedOrFallbackStats.guildCount,
|
||||||
scrobbleCount,
|
userCount: stats?.userCount ?? cachedOrFallbackStats.userCount,
|
||||||
linkedUsers,
|
scrobbleCount,
|
||||||
ping,
|
linkedUsers,
|
||||||
loading: false,
|
ping,
|
||||||
})],
|
loading: false,
|
||||||
|
}),
|
||||||
|
],
|
||||||
}, message);
|
}, message);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error("[stats] run failed:", err);
|
if (!isAbortError(err)) {
|
||||||
|
console.error("[stats] run failed:", err);
|
||||||
|
}
|
||||||
|
|
||||||
const fallback = new EmbedBuilder()
|
const fallback = new EmbedBuilder()
|
||||||
.setColor(getGlobalColor())
|
.setColor(getGlobalColor())
|
||||||
.setDescription("Failed to load full stats. Please try again in a moment.");
|
.setDescription("Failed to load full stats. Please try again in a moment.");
|
||||||
await message.reply({ embeds: [fallback] }).catch(() => {});
|
|
||||||
|
await safeReply(message, { embeds: [fallback] }).catch(() => {});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user