From 2a7c5f1ac74381e27e8bf00599b88dcb02f4fe28 Mon Sep 17 00:00:00 2001 From: mat Date: Sat, 29 May 2021 16:24:37 -0500 Subject: probably fix a thing --- src/hypixelCached.ts | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'src/hypixelCached.ts') diff --git a/src/hypixelCached.ts b/src/hypixelCached.ts index b3b9c87..ed79e2d 100644 --- a/src/hypixelCached.ts +++ b/src/hypixelCached.ts @@ -13,6 +13,7 @@ import { debug } from '.' // cache usernames for 4 hours /** uuid: username */ + export const usernameCache = new NodeCache({ stdTTL: 60 * 60 * 4, checkperiod: 60, @@ -64,7 +65,7 @@ interface KeyValue { function waitForCacheSet(cache: NodeCache, key?: string, value?: string): Promise { return new Promise((resolve, reject) => { const listener = (setKey, setValue) => { - if (setKey === key || (value && setValue === value)) { + if (((setKey === key) || (value && setValue === value)) && typeof setValue === 'string') { cache.removeListener('set', listener) return resolve({ key: setKey, value: setValue }) } @@ -84,7 +85,7 @@ export async function uuidFromUser(user: string): Promise { if (usernameCache.has(undashUuid(user))) { // check if the uuid is a key - const username: Promise | string | null = usernameCache.get(undashUuid(user)) + const username: Promise | string | null = usernameCache.get>(undashUuid(user)) // sometimes the username will be null, return that if (username === null) return null @@ -92,16 +93,16 @@ export async function uuidFromUser(user: string): Promise { // if it has .then, then that means its a waitForCacheSet promise. This is done to prevent requests made while it is already requesting if ((username as Promise).then) { const { key: uuid, value: _username } = await (username as Promise) - usernameCache.set(uuid, _username) + usernameCache.set>(uuid, _username) return uuid } else return undashUuid(user) } // check if the username is a value - const uuidToUsername: {[ key: string ]: string} = usernameCache.mget(usernameCache.keys()) + const uuidToUsername: { [ key: string ]: string | Promise } = usernameCache.mget(usernameCache.keys()) for (const [ uuid, username ] of Object.entries(uuidToUsername)) { - if (username && username.toLowerCase && user.toLowerCase() === username.toLowerCase()) + if (username && (username).toLowerCase && user.toLowerCase() === (username).toLowerCase()) return uuid } @@ -376,4 +377,4 @@ export async function fetchProfileName(user: string, profile: string): Promise