idk is that work

This commit is contained in:
NoLogicAlan committed 2026-05-14 15:52:38 +03:00
1 parent 6b4bcf143c
commit 48bebc950d
2 files changed
+57 -13

No files matched your search

+53 -9
View File
@@ -25,10 +25,6 @@ function withTimeout(promise, ms, fallback) {
]); ]);
} }
function computeGuildCount(client) {
return client.guilds?.size ?? 0;
}
function computeUserCount(client) { function computeUserCount(client) {
let total = 0; let total = 0;
for (const [, guild] of client.guilds ?? []) { for (const [, guild] of client.guilds ?? []) {
@@ -37,16 +33,64 @@ function computeUserCount(client) {
return total; return total;
} }
async function fetchAccurateGuildCount(client) {
if (typeof client.fetchTotalGuildCount === "function") {
try {
const count = await withTimeout(client.fetchTotalGuildCount(), 1_200, null);
if (typeof count === "number" && count >= 0) return count;
} catch (_) {}
}
if (typeof client.fetchAllStats === "function") {
try {
const stats = await withTimeout(client.fetchAllStats(), 1_500, null);
if (typeof stats?.guilds === "number" && stats.guilds >= 0) return stats.guilds;
} catch (_) {}
}
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 (_) {}
}
return client.guilds?.size ?? 0;
}
async function fetchAccurateUserCount(client) {
if (typeof client.fetchAllStats === "function") {
try {
const stats = await withTimeout(client.fetchAllStats(), 1_500, null);
if (typeof stats?.users === "number" && stats.users >= 0) return stats.users;
if (typeof stats?.members === "number" && stats.members >= 0) return stats.members;
} catch (_) {}
}
return computeUserCount(client);
}
async function refreshStats(client) { async function refreshStats(client) {
try { try {
cachedStats = { cachedStats = {
guildCount: computeGuildCount(client), guildCount: await fetchAccurateGuildCount(client),
userCount: computeUserCount(client), userCount: await fetchAccurateUserCount(client),
}; };
} catch { } catch {
cachedStats = { cachedStats = {
guildCount: client.guilds?.size ?? 0, guildCount: client.guilds?.size ?? 0,
userCount: 0, userCount: computeUserCount(client),
}; };
} }
@@ -152,7 +196,7 @@ export async function run(message) {
const lastfmSnapshot = getLastfmSnapshot(lastfm); const lastfmSnapshot = getLastfmSnapshot(lastfm);
const shared = { const shared = {
guildCount: computeGuildCount(this.client), guildCount: this.client.guilds?.size ?? 0,
playerCount: getLivePlayerCount(this.players.playerMap), playerCount: getLivePlayerCount(this.players.playerMap),
scrobbleCount: lastfmSnapshot.scrobbleCount, scrobbleCount: lastfmSnapshot.scrobbleCount,
linkedUsers: lastfmSnapshot.linkedUsers, linkedUsers: lastfmSnapshot.linkedUsers,
@@ -172,7 +216,7 @@ export async function run(message) {
const scrobblePromise = lastfmEnabled const scrobblePromise = lastfmEnabled
? withTimeout( ? withTimeout(
lastfm.getStoredTotalScrobbles?.() ?? Promise.resolve(lastfmSnapshot.scrobbleCount), lastfm.getStoredTotalScrobbles?.() ?? Promise.resolve(lastfmSnapshot.scrobbleCount),
FAST_WAIT_MS, 3_000,
lastfmSnapshot.scrobbleCount lastfmSnapshot.scrobbleCount
) )
: Promise.resolve(0); : Promise.resolve(0);
+4 -4
View File
@@ -864,9 +864,9 @@ export class LastFmManager {
try { try {
const pool = await this._getPool(); const pool = await this._getPool();
const [rows] = await pool.execute( const [rows] = await pool.execute(
"SELECT linked_users FROM lastfm_stats WHERE id = 1" "SELECT COUNT(*) AS total FROM lastfm_users"
); );
const count = Number(rows[0]?.linked_users ?? 0); const count = Number(rows[0]?.total ?? 0);
this._linkedUsersCache = count; this._linkedUsersCache = count;
return count; return count;
} catch { } catch {
@@ -879,9 +879,9 @@ export class LastFmManager {
try { try {
const pool = await this._getPool(); const pool = await this._getPool();
const [rows] = await pool.execute( const [rows] = await pool.execute(
"SELECT stored_scrobbles FROM lastfm_stats WHERE id = 1" "SELECT COALESCE(SUM(scrobble_count), 0) AS total FROM lastfm_users"
); );
const total = Number(rows[0]?.stored_scrobbles ?? this._totalScrobblesCache ?? 0); const total = Number(rows[0]?.total ?? this._totalScrobblesCache ?? 0);
this._totalScrobblesCache = total; this._totalScrobblesCache = total;
this._totalScrobblesCacheExpiry = Date.now() + 10 * 60 * 1000; this._totalScrobblesCacheExpiry = Date.now() + 10 * 60 * 1000;
return total; return total;