Skip to content

Commit 2ad152d

Browse files
committed
refactor(resolveUser): use getOrFetchUser internally; debug counters
1 parent b802e4d commit 2ad152d

File tree

1 file changed

+5
-23
lines changed

1 file changed

+5
-23
lines changed

backend/src/utils.ts

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ import { sendDM } from "./utils/sendDM.js";
4343
import { Brand } from "./utils/typeUtils.js";
4444
import { waitForButtonConfirm } from "./utils/waitForInteraction.js";
4545
import { GenericCommandSource } from "./pluginUtils.js";
46+
import { getOrFetchUser } from "./utils/getOrFetchUser.js";
47+
import { incrementDebugCounter } from "./debugCounters.js";
4648

4749
const fsp = fs.promises;
4850

@@ -1158,9 +1160,7 @@ export function getUser(client: Client, userResolvable: string): User | UnknownU
11581160
* Resolves a User from the passed string. The passed string can be a user id, a user mention, a full username (with discrim), etc.
11591161
* If the user is not found in the cache, it's fetched from the API.
11601162
*/
1161-
export async function resolveUser(bot: Client, value: string): Promise<User | UnknownUser>;
1162-
export async function resolveUser<T>(bot: Client, value: Not<T, string>): Promise<UnknownUser>;
1163-
export async function resolveUser(bot, value) {
1163+
export async function resolveUser(bot: Client, value: unknown, context?: string): Promise<User | UnknownUser> {
11641164
if (typeof value !== "string") {
11651165
return new UnknownUser();
11661166
}
@@ -1170,26 +1170,8 @@ export async function resolveUser(bot, value) {
11701170
return new UnknownUser();
11711171
}
11721172

1173-
// If we have the user cached, return that directly
1174-
if (bot.users.cache.has(userId)) {
1175-
return bot.users.fetch(userId);
1176-
}
1177-
1178-
// We don't want to spam the API by trying to fetch unknown users again and again,
1179-
// so we cache the fact that they're "unknown" for a while
1180-
if (unknownUsers.has(userId)) {
1181-
return new UnknownUser({ id: userId });
1182-
}
1183-
1184-
const freshUser = await bot.users.fetch(userId, true, true).catch(noop);
1185-
if (freshUser) {
1186-
return freshUser;
1187-
}
1188-
1189-
unknownUsers.add(userId);
1190-
setTimeout(() => unknownUsers.delete(userId), 15 * MINUTES);
1191-
1192-
return new UnknownUser({ id: userId });
1173+
incrementDebugCounter(`resolveUser:${context ?? "unknown"}`);
1174+
return (await getOrFetchUser(bot, userId)) ?? new UnknownUser();
11931175
}
11941176

11951177
/**

0 commit comments

Comments
 (0)