aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/cleaners/player.ts4
-rw-r--r--src/cleaners/skyblock/collections.ts2
-rw-r--r--src/cleaners/skyblock/inventory.ts4
-rw-r--r--src/cleaners/skyblock/member.ts13
-rw-r--r--src/cleaners/skyblock/minions.ts2
-rw-r--r--src/cleaners/skyblock/profile.ts8
-rw-r--r--src/cleaners/skyblock/profiles.ts11
-rw-r--r--src/cleaners/skyblock/stats.ts11
-rw-r--r--src/constants.ts4
-rw-r--r--src/database.ts62
-rw-r--r--src/discord.ts6
-rw-r--r--src/hypixel.ts55
-rw-r--r--src/hypixelApi.ts12
-rw-r--r--src/hypixelCached.ts77
-rw-r--r--src/index.ts9
-rw-r--r--src/mojang.ts7
-rw-r--r--src/util.ts2
17 files changed, 171 insertions, 118 deletions
diff --git a/src/cleaners/player.ts b/src/cleaners/player.ts
index 6ac1382..b2dfc77 100644
--- a/src/cleaners/player.ts
+++ b/src/cleaners/player.ts
@@ -17,10 +17,10 @@ export interface CleanPlayer extends CleanBasicPlayer {
// first_join?: number
}
-export async function cleanPlayerResponse(data: HypixelPlayer): Promise<CleanPlayer> {
+export async function cleanPlayerResponse(data: HypixelPlayer): Promise<CleanPlayer | null> {
// Cleans up a 'player' api response
if (!data)
- return // bruh
+ return null // bruh
return {
uuid: undashUuid(data.uuid),
username: data.displayname,
diff --git a/src/cleaners/skyblock/collections.ts b/src/cleaners/skyblock/collections.ts
index 708822b..99fdc27 100644
--- a/src/cleaners/skyblock/collections.ts
+++ b/src/cleaners/skyblock/collections.ts
@@ -84,7 +84,7 @@ export interface Collection {
}
// get a category name (farming) from a collection name (wheat)
-function getCategory(collectionName): CollectionCategory {
+function getCategory(collectionName): CollectionCategory | undefined {
for (const categoryName in COLLECTIONS) {
const categoryItems = COLLECTIONS[categoryName]
if (categoryItems.includes(collectionName))
diff --git a/src/cleaners/skyblock/inventory.ts b/src/cleaners/skyblock/inventory.ts
index 890937a..5ad6617 100644
--- a/src/cleaners/skyblock/inventory.ts
+++ b/src/cleaners/skyblock/inventory.ts
@@ -26,7 +26,7 @@ interface Item {
export type Inventory = Item[]
-function cleanItem(rawItem): Item {
+function cleanItem(rawItem): Item | null {
// if the item doesn't have an id, it isn't an item
if (rawItem.id === undefined) return null
@@ -35,7 +35,7 @@ function cleanItem(rawItem): Item {
const damageValue = rawItem.Damage
const itemTag = rawItem.tag
const extraAttributes = itemTag?.ExtraAttributes ?? {}
- let headId: string
+ let headId: string | undefined
if (vanillaId === 397) {
const headDataBase64 = itemTag?.SkullOwner?.Properties?.textures?.[0]?.Value
diff --git a/src/cleaners/skyblock/member.ts b/src/cleaners/skyblock/member.ts
index 4a2f8cc..cee1864 100644
--- a/src/cleaners/skyblock/member.ts
+++ b/src/cleaners/skyblock/member.ts
@@ -30,7 +30,7 @@ export interface CleanMember extends CleanBasicMember {
rawHypixelStats?: { [ key: string ]: number }
minions: CleanMinion[]
fairy_souls: FairySouls
- inventories: Inventories
+ inventories?: Inventories
objectives: Objective[]
skills: Skill[]
visited_zones: Zone[]
@@ -38,8 +38,9 @@ export interface CleanMember extends CleanBasicMember {
slayers: SlayerData
}
-export async function cleanSkyBlockProfileMemberResponseBasic(member: any, included: Included[] = null): Promise<CleanBasicMember> {
+export async function cleanSkyBlockProfileMemberResponseBasic(member: any): Promise<CleanBasicMember | null> {
const player = await cached.fetchPlayer(member.uuid)
+ if (!player) return null
return {
uuid: member.uuid,
username: player.username,
@@ -50,11 +51,11 @@ export async function cleanSkyBlockProfileMemberResponseBasic(member: any, inclu
}
/** Cleans up a member (from skyblock/profile) */
-export async function cleanSkyBlockProfileMemberResponse(member, included: Included[] = null): Promise<CleanMember> {
+export async function cleanSkyBlockProfileMemberResponse(member, included: Included[] | undefined = undefined): Promise<CleanMember | null> {
// profiles.members[]
- const inventoriesIncluded = included === null || included.includes('inventories')
+ const inventoriesIncluded = included === undefined || included.includes('inventories')
const player = await cached.fetchPlayer(member.uuid)
- if (!player) return
+ if (!player) return null
const fairySouls = cleanFairySouls(member)
const { max_fairy_souls: maxFairySouls } = await constants.fetchConstantValues()
@@ -109,5 +110,5 @@ export interface CleanMemberProfilePlayer extends CleanPlayer {
export interface CleanMemberProfile {
member: CleanMemberProfilePlayer
profile: CleanFullProfileBasicMembers
- customization: AccountCustomization
+ customization?: AccountCustomization
}
diff --git a/src/cleaners/skyblock/minions.ts b/src/cleaners/skyblock/minions.ts
index 21f7b66..d045a7e 100644
--- a/src/cleaners/skyblock/minions.ts
+++ b/src/cleaners/skyblock/minions.ts
@@ -80,7 +80,7 @@ export function combineMinionArrays(minions: CleanMinion[][]): CleanMinion[] {
// This should never happen, but in case the length of `minion.levels` is longer than
// `matchingMinionReference.levels`, then it should be extended to be equal length
while (matchingMinionReference.levels.length < minion.levels.length)
- matchingMinionReference.levels.push(null)
+ matchingMinionReference.levels.push(false)
for (let i = 0; i < minion.levels.length; i++) {
if (minion.levels[i])
diff --git a/src/cleaners/skyblock/profile.ts b/src/cleaners/skyblock/profile.ts
index 0453ff3..5bcea4b 100644
--- a/src/cleaners/skyblock/profile.ts
+++ b/src/cleaners/skyblock/profile.ts
@@ -25,7 +25,7 @@ export interface CleanFullProfileBasicMembers extends CleanProfile {
/** Return a `CleanProfile` instead of a `CleanFullProfile`, useful when we need to get members but don't want to waste much ram */
export async function cleanSkyblockProfileResponseLighter(data): Promise<CleanProfile> {
// We use Promise.all so it can fetch all the usernames at once instead of waiting for the previous promise to complete
- const promises: Promise<CleanBasicMember>[] = []
+ const promises: Promise<CleanBasicMember | null>[] = []
for (const memberUUID in data.members) {
const memberRaw = data.members[memberUUID]
@@ -34,7 +34,7 @@ export async function cleanSkyblockProfileResponseLighter(data): Promise<CleanPr
promises.push(cleanSkyBlockProfileMemberResponseBasic(memberRaw))
}
- const cleanedMembers: CleanBasicMember[] = await Promise.all(promises)
+ const cleanedMembers: CleanBasicMember[] = (await Promise.all(promises)).filter(m => m) as CleanBasicMember[]
return {
uuid: data.profile_id,
@@ -48,7 +48,7 @@ export async function cleanSkyblockProfileResponseLighter(data): Promise<CleanPr
*/
export async function cleanSkyblockProfileResponse(data: any, options?: ApiOptions): Promise<CleanFullProfile|CleanProfile> {
// We use Promise.all so it can fetch all the users at once instead of waiting for the previous promise to complete
- const promises: Promise<CleanMember>[] = []
+ const promises: Promise<CleanMember | null>[] = []
for (const memberUUID in data.members) {
const memberRaw = data.members[memberUUID]
@@ -62,7 +62,7 @@ export async function cleanSkyblockProfileResponse(data: any, options?: ApiOptio
))
}
- const cleanedMembers: CleanMember[] = (await Promise.all(promises)).filter(m => m !== null && m !== undefined)
+ const cleanedMembers: CleanMember[] = (await Promise.all(promises)).filter(m => m !== null && m !== undefined) as CleanMember[]
if (options?.basic) {
return {
diff --git a/src/cleaners/skyblock/profiles.ts b/src/cleaners/skyblock/profiles.ts
index f12b5f6..a44fe48 100644
--- a/src/cleaners/skyblock/profiles.ts
+++ b/src/cleaners/skyblock/profiles.ts
@@ -1,5 +1,10 @@
-import { HypixelPlayerStatsSkyBlockProfiles } from "../../hypixelApi"
-import { CleanBasicProfile, CleanProfile, cleanSkyblockProfileResponse, cleanSkyblockProfileResponseLighter } from "./profile"
+import { HypixelPlayerStatsSkyBlockProfiles } from '../../hypixelApi'
+import {
+ CleanBasicProfile,
+ CleanFullProfile,
+ CleanProfile,
+ cleanSkyblockProfileResponse
+} from './profile'
export function cleanPlayerSkyblockProfiles(rawProfiles: HypixelPlayerStatsSkyBlockProfiles): CleanBasicProfile[] {
let profiles: CleanBasicProfile[] = []
@@ -14,7 +19,7 @@ export function cleanPlayerSkyblockProfiles(rawProfiles: HypixelPlayerStatsSkyBl
/** Convert an array of raw profiles into clean profiles */
export async function cleanSkyblockProfilesResponse(data: any[]): Promise<CleanProfile[]> {
- const promises = []
+ const promises: Promise<CleanProfile | CleanFullProfile>[] = []
for (const profile of data ?? []) {
// let cleanedProfile = await cleanSkyblockProfileResponseLighter(profile)
promises.push(cleanSkyblockProfileResponse(profile))
diff --git a/src/cleaners/skyblock/stats.ts b/src/cleaners/skyblock/stats.ts
index 31a894c..a91ca74 100644
--- a/src/cleaners/skyblock/stats.ts
+++ b/src/cleaners/skyblock/stats.ts
@@ -14,8 +14,8 @@ const statCategories: { [ key: string ]: string[] | null } = { // sorted in orde
}
export interface StatCategory {
- category: string,
- name: string
+ category: string | null
+ name: string | null
}
export function categorizeStat(statNameRaw: string): StatCategory {
@@ -71,11 +71,11 @@ export interface StatItem {
rawName: string
value: number
categorizedName: string
- category: string
- unit: string
+ category: string | null
+ unit: string | null
}
-export function getStatUnit(name: string): string {
+export function getStatUnit(name: string): string | null {
for (const [ unitName, statMatchers ] of Object.entries(statUnits)) {
for (const statMatch of statMatchers) {
let trailingEnd = statMatch[0] === '_'
@@ -88,6 +88,7 @@ export function getStatUnit(name: string): string {
return unitName
}
}
+ return null
}
diff --git a/src/constants.ts b/src/constants.ts
index 9699e57..dcb9acf 100644
--- a/src/constants.ts
+++ b/src/constants.ts
@@ -40,7 +40,7 @@ async function fetchGithubApi(method: string, route: string, headers?: any, json
githubApiBase + route,
{
agent: () => httpsAgent,
- body: json ? JSON.stringify(json) : null,
+ body: json ? JSON.stringify(json) : undefined,
method,
headers: Object.assign({
'Authorization': `token ${process.env.github_token}`
@@ -78,7 +78,7 @@ function fetchFile(path: string): Promise<GithubFile> {
return new Promise(resolve => {
queue.enqueue(async() => {
if (fileCache.has(path))
- return resolve(fileCache.get(path))
+ return resolve(fileCache.get(path)!)
const r = await fetchGithubApi(
'GET',
diff --git a/src/database.ts b/src/database.ts
index 60fbc7e..5d15954 100644
--- a/src/database.ts
+++ b/src/database.ts
@@ -59,7 +59,7 @@ interface profileRawLeaderboardItem {
}
interface MemberLeaderboardItem {
- player: CleanPlayer
+ player: CleanPlayer | null
profileUuid: string
value: number
}
@@ -205,9 +205,11 @@ export async function fetchAllLeaderboardsCategorized(): Promise<{ [ category: s
const categorizedLeaderboards: { [ category: string ]: string[] } = {}
for (const leaderboard of [...memberLeaderboardAttributes, ...profileLeaderboardAttributes]) {
const { category } = categorizeStat(leaderboard)
- if (!categorizedLeaderboards[category])
- categorizedLeaderboards[category] = []
- categorizedLeaderboards[category].push(leaderboard)
+ if (category) {
+ if (!categorizedLeaderboards[category])
+ categorizedLeaderboards[category] = []
+ categorizedLeaderboards[category].push(leaderboard)
+ }
}
// move misc to end by removing and readding it
@@ -366,14 +368,14 @@ async function fetchProfileLeaderboardRaw(name: string): Promise<profileRawLeade
interface MemberLeaderboard {
name: string
- unit?: string
+ unit: string | null
list: MemberLeaderboardItem[]
info?: string
}
interface ProfileLeaderboard {
name: string
- unit?: string
+ unit: string | null
list: ProfileLeaderboardItem[]
info?: string
}
@@ -384,8 +386,9 @@ export async function fetchMemberLeaderboard(name: string): Promise<MemberLeader
const leaderboardRaw = await fetchMemberLeaderboardRaw(name)
const fetchLeaderboardPlayer = async(i: memberRawLeaderboardItem): Promise<MemberLeaderboardItem> => {
+ const player = await cached.fetchBasicPlayer(i.uuid)
return {
- player: await cached.fetchBasicPlayer(i.uuid),
+ player,
profileUuid: i.profile,
value: i.value
}
@@ -408,9 +411,12 @@ export async function fetchProfileLeaderboard(name: string): Promise<ProfileLead
const leaderboardRaw = await fetchProfileLeaderboardRaw(name)
const fetchLeaderboardProfile = async(i: profileRawLeaderboardItem): Promise<ProfileLeaderboardItem> => {
- const players = []
- for (const playerUuid of i.players)
- players.push(await cached.fetchBasicPlayer(playerUuid))
+ const players: CleanPlayer[] = []
+ for (const playerUuid of i.players) {
+ const player = await cached.fetchBasicPlayer(playerUuid)
+ if (player)
+ players.push(player)
+ }
return {
players: players,
profileUuid: i.uuid,
@@ -445,17 +451,26 @@ export async function fetchLeaderboard(name: string): Promise<MemberLeaderboard|
return leaderboard
}
+interface LeaderboardSpot {
+ name: string
+ positionIndex: number
+ value: number
+ unit: string | null
+}
+
/** Get the leaderboard positions a member is on. This may take a while depending on whether stuff is cached */
-export async function fetchMemberLeaderboardSpots(player: string, profile: string) {
+export async function fetchMemberLeaderboardSpots(player: string, profile: string): Promise<LeaderboardSpot[] | null> {
const fullProfile = await cached.fetchProfile(player, profile)
+ if (!fullProfile) return null
const fullMember = fullProfile.members.find(m => m.username.toLowerCase() === player.toLowerCase() || m.uuid === player)
+ if (!fullMember) return null
// update the leaderboard positions for the member
await updateDatabaseMember(fullMember, fullProfile)
const applicableAttributes = await getApplicableMemberLeaderboardAttributes(fullMember)
- const memberLeaderboardSpots = []
+ const memberLeaderboardSpots: LeaderboardSpot[] = []
for (const leaderboardName in applicableAttributes) {
const leaderboard = await fetchMemberLeaderboardRaw(leaderboardName)
@@ -472,7 +487,7 @@ export async function fetchMemberLeaderboardSpots(player: string, profile: strin
return memberLeaderboardSpots
}
-async function getLeaderboardRequirement(name: string, leaderboardType: 'member' | 'profile'): Promise<number> {
+async function getLeaderboardRequirement(name: string, leaderboardType: 'member' | 'profile'): Promise<number | null> {
let leaderboard: memberRawLeaderboardItem[] | profileRawLeaderboardItem[]
if (leaderboardType === 'member')
leaderboard = await fetchMemberLeaderboardRaw(name)
@@ -480,8 +495,8 @@ async function getLeaderboardRequirement(name: string, leaderboardType: 'member'
leaderboard = await fetchProfileLeaderboardRaw(name)
// if there's more than 100 items, return the 100th. if there's less, return null
- if (leaderboard.length >= leaderboardMax)
- return leaderboard[leaderboardMax - 1].value
+ if (leaderboard!.length >= leaderboardMax)
+ return leaderboard![leaderboardMax - 1].value
else
return null
}
@@ -504,7 +519,7 @@ async function getApplicableMemberLeaderboardAttributes(member: CleanMember): Pr
let leaderboardsCount: number = Object.keys(applicableAttributes).length
- const leaderboardsCountRequirement: number = await getLeaderboardRequirement('leaderboards_count', 'member')
+ const leaderboardsCountRequirement: number | null = await getLeaderboardRequirement('leaderboards_count', 'member')
if (
(leaderboardsCountRequirement === null)
@@ -534,11 +549,11 @@ async function getApplicableProfileLeaderboardAttributes(profile: CleanFullProfi
let leaderboardsCount: number = Object.keys(applicableAttributes).length
- const leaderboardsCountRequirement: number = await getLeaderboardRequirement('leaderboards_count', 'member')
+ const leaderboardsCountRequirement: number | null = await getLeaderboardRequirement('leaderboards_count', 'member')
if (
- (leaderboardsCountRequirement === null)
- || (leaderboardsCount > leaderboardsCountRequirement)
+ leaderboardsCountRequirement === null
+ || leaderboardsCount > leaderboardsCountRequirement
) {
applicableAttributes['leaderboards_count'] = leaderboardsCount
}
@@ -557,7 +572,8 @@ export async function updateDatabaseMember(member: CleanMember, profile: CleanFu
if (debug) console.debug('adding member to leaderboards', member.username)
- await constants.addStats(Object.keys(member.rawHypixelStats))
+ if (member.rawHypixelStats)
+ await constants.addStats(Object.keys(member.rawHypixelStats))
await constants.addCollections(member.collections.map(coll => coll.name))
await constants.addSkills(member.skills.map(skill => skill.name))
await constants.addZones(member.visited_zones.map(zone => zone.name))
@@ -738,15 +754,15 @@ export async function createSession(refreshToken: string, userData: discord.Disc
return sessionId
}
-export async function fetchSession(sessionId: string): Promise<SessionSchema> {
+export async function fetchSession(sessionId: string): Promise<SessionSchema | null> {
return await sessionsCollection?.findOne({ _id: sessionId })
}
-export async function fetchAccount(minecraftUuid: string): Promise<AccountSchema> {
+export async function fetchAccount(minecraftUuid: string): Promise<AccountSchema | null> {
return await accountsCollection?.findOne({ minecraftUuid })
}
-export async function fetchAccountFromDiscord(discordId: string): Promise<AccountSchema> {
+export async function fetchAccountFromDiscord(discordId: string): Promise<AccountSchema | null> {
return await accountsCollection?.findOne({ discordId })
}
diff --git a/src/discord.ts b/src/discord.ts
index 48146b5..fac5438 100644
--- a/src/discord.ts
+++ b/src/discord.ts
@@ -26,9 +26,13 @@ export interface DiscordUser {
mfa_enabled: boolean
}
-export async function exchangeCode(redirectUri: string, code: string): Promise<TokenResponse> {
+export async function exchangeCode(redirectUri: string, code: string): Promise<TokenResponse | null> {
const API_ENDPOINT = 'https://discord.com/api/v6'
const CLIENT_SECRET = process.env.discord_client_secret
+ if (!CLIENT_SECRET) {
+ console.error('discord_client_secret isn\'t in env, couldn\'t login with discord')
+ return null
+ }
const data = {
'client_id': DISCORD_CLIENT_ID,
'client_secret': CLIENT_SECRET,
diff --git a/src/hypixel.ts b/src/hypixel.ts
index eb66d48..d6f9661 100644
--- a/src/hypixel.ts
+++ b/src/hypixel.ts
@@ -11,7 +11,7 @@ import { CleanPlayer, cleanPlayerResponse } from './cleaners/player'
import * as cached from './hypixelCached'
import { debug } from '.'
-export type Included = 'profiles' | 'player' | 'stats' | 'inventories'
+export type Included = 'profiles' | 'player' | 'stats' | 'inventories' | undefined
// the interval at which the "last_save" parameter updates in the hypixel api, this is 3 minutes
export const saveInterval = 60 * 3 * 1000
@@ -62,7 +62,7 @@ export interface UserAny {
}
export interface CleanUser {
- player: CleanPlayer
+ player: CleanPlayer | null
profiles?: CleanProfile[]
activeProfile?: string
online?: boolean
@@ -76,24 +76,25 @@ export interface CleanUser {
* @param included lets you choose what is returned, so there's less processing required on the backend
* used inclusions: player, profiles
*/
-export async function fetchUser({ user, uuid, username }: UserAny, included: Included[]=['player'], customization?: boolean): Promise<CleanUser> {
+export async function fetchUser({ user, uuid, username }: UserAny, included: Included[]=['player'], customization?: boolean): Promise<CleanUser | null> {
if (!uuid) {
// If the uuid isn't provided, get it
- uuid = await cached.uuidFromUser(user || username)
+ if (!username && !user) return null
+ uuid = await cached.uuidFromUser((user ?? username)!)
}
- const websiteAccountPromise = customization ? fetchAccount(uuid) : null
if (!uuid) {
// the user doesn't exist.
if (debug) console.debug('error:', user, 'doesnt exist')
return null
}
+ const websiteAccountPromise = customization ? fetchAccount(uuid) : null
const includePlayers = included.includes('player')
const includeProfiles = included.includes('profiles')
- let profilesData: CleanProfile[]
- let basicProfilesData: CleanBasicProfile[]
- let playerData: CleanPlayer
+ let profilesData: CleanProfile[] | undefined
+ let basicProfilesData: CleanBasicProfile[] | undefined
+ let playerData: CleanPlayer | null = null
if (includePlayers) {
playerData = await cached.fetchPlayer(uuid)
@@ -103,31 +104,30 @@ export async function fetchUser({ user, uuid, username }: UserAny, included: Inc
if (playerData)
delete playerData.profiles
}
- if (includeProfiles) {
+ if (includeProfiles)
profilesData = await cached.fetchSkyblockProfiles(uuid)
- }
- let activeProfile: CleanProfile = null
+ let activeProfile: CleanProfile
let lastOnline: number = 0
if (includeProfiles) {
- for (const profile of profilesData) {
- const member = profile.members.find(member => member.uuid === uuid)
- if (member.last_save > lastOnline) {
+ for (const profile of profilesData!) {
+ const member = profile.members?.find(member => member.uuid === uuid)
+ if (member && member.last_save > lastOnline) {
lastOnline = member.last_save
activeProfile = profile
}
}
}
- let websiteAccount: AccountSchema = undefined
+ let websiteAccount: AccountSchema | null = null
- if (websiteAccountPromise) {
+ if (websiteAccountPromise)
websiteAccount = await websiteAccountPromise
- }
+
return {
- player: playerData ?? null,
+ player: playerData,
profiles: profilesData ?? basicProfilesData,
- activeProfile: includeProfiles ? activeProfile?.uuid : undefined,
+ activeProfile: includeProfiles ? activeProfile!?.uuid : undefined,
online: includeProfiles ? lastOnline > (Date.now() - saveInterval): undefined,
customization: websiteAccount?.customization
}
@@ -140,9 +140,9 @@ export async function fetchUser({ user, uuid, username }: UserAny, included: Inc
* @param profile A profile name or profile uuid
* @param customization Whether stuff like the user's custom background will be returned
*/
-export async function fetchMemberProfile(user: string, profile: string, customization: boolean): Promise<CleanMemberProfile> {
+export async function fetchMemberProfile(user: string, profile: string, customization: boolean): Promise<CleanMemberProfile | null> {
const playerUuid = await cached.uuidFromUser(user)
- if (!playerUuid) return
+ if (!playerUuid) return null
// we don't await the promise immediately so it can load while we do other stuff
const websiteAccountPromise = customization ? fetchAccount(playerUuid) : null
const profileUuid = await cached.fetchProfileUuid(user, profile)
@@ -153,9 +153,12 @@ export async function fetchMemberProfile(user: string, profile: string, customiz
const player = await cached.fetchPlayer(playerUuid)
+ if (!player) return null // this should never happen, but if it does just return null
+
const cleanProfile = await cached.fetchProfile(playerUuid, profileUuid) as CleanFullProfileBasicMembers
const member = cleanProfile.members.find(m => m.uuid === playerUuid)
+ if (!member) return null // this should never happen, but if it does just return null
// remove unnecessary member data
const simpleMembers: CleanBasicMember[] = cleanProfile.members.map(m => {
@@ -170,7 +173,7 @@ export async function fetchMemberProfile(user: string, profile: string, customiz
cleanProfile.members = simpleMembers
- let websiteAccount: AccountSchema = undefined
+ let websiteAccount: AccountSchema | null = null
if (websiteAccountPromise)
websiteAccount = await websiteAccountPromise
@@ -178,7 +181,7 @@ export async function fetchMemberProfile(user: string, profile: string, customiz
return {
member: {
// the profile name is in member rather than profile since they sometimes differ for each member
- profileName: cleanProfile.name,
+ profileName: cleanProfile.name!,
// add all the member data
...member,
// add all other data relating to the hypixel player, such as username, rank, etc
@@ -200,7 +203,7 @@ export async function fetchMemberProfile(user: string, profile: string, customiz
path: 'skyblock/profile',
args: { profile: profileUuid }
},
- null,
+ undefined,
{ mainMemberUuid: playerUuid }
)
@@ -223,7 +226,7 @@ export async function fetchMemberProfile(user: string, profile: string, customiz
path: 'skyblock/profile',
args: { profile: profileUuid }
},
- null,
+ undefined,
{ basic: true }
)
@@ -237,7 +240,7 @@ export async function fetchMemberProfilesUncached(playerUuid: string): Promise<C
args: {
uuid: playerUuid
}},
- null,
+ undefined,
{
// only the inventories for the main player are generated, this is for optimization purposes
mainMemberUuid: playerUuid
diff --git a/src/hypixelApi.ts b/src/hypixelApi.ts
index b3a1cae..1b57d8f 100644
--- a/src/hypixelApi.ts
+++ b/src/hypixelApi.ts
@@ -32,10 +32,10 @@ const baseHypixelAPI = 'https://api.hypixel.net'
/** Choose the best current API key */
-export function chooseApiKey(): string {
+export function chooseApiKey(): string | null {
// find the api key with the lowest amount of uses
- let bestKeyUsage: KeyUsage = null
- let bestKey: string = null
+ let bestKeyUsage: KeyUsage | null = null
+ let bestKey: string | null = null
for (let key of shuffle(apiKeys.slice())) {
const keyUsage = apiKeyUsage[key]
@@ -173,9 +173,9 @@ export async function sendApiRequest({ path, key, args }): Promise<HypixelRespon
if (fetchResponse.headers.get('ratelimit-limit'))
// remember how many uses it has
apiKeyUsage[key] = {
- remaining: parseInt(fetchResponse.headers.get('ratelimit-remaining')),
- limit: parseInt(fetchResponse.headers.get('ratelimit-limit')),
- reset: Date.now() + parseInt(fetchResponse.headers.get('ratelimit-reset')) * 1000
+ remaining: parseInt(fetchResponse.headers.get('ratelimit-remaining') ?? '0'),
+ limit: parseInt(fetchResponse.headers.get('ratelimit-limit') ?? '0'),
+ reset: Date.now() + parseInt(fetchResponse.headers.get('ratelimit-reset') ?? '0') * 1000
}
if (fetchJsonParsed.throttle) {
diff --git a/src/hypixelCached.ts b/src/hypixelCached.ts
index 92a7801..4aa96ef 100644
--- a/src/hypixelCached.ts
+++ b/src/hypixelCached.ts
@@ -81,17 +81,17 @@ function waitForCacheSet(cache: NodeCache, key?: string, value?: string): Promis
* Fetch the uuid from a user
* @param user A user can be either a uuid or a username
*/
-export async function uuidFromUser(user: string): Promise<string> {
+export async function uuidFromUser(user: string): Promise<string | undefined> {
// if the user is 32 characters long, it has to be a uuid
if (isUuid(user))
return undashUuid(user)
if (usernameCache.has(undashUuid(user))) {
// check if the uuid is a key
- const username: Promise<KeyValue> | string | null = usernameCache.get<string | Promise<KeyValue>>(undashUuid(user))
+ const username: Promise<KeyValue> | string | undefined = usernameCache.get<string | Promise<KeyValue>>(undashUuid(user))
// sometimes the username will be null, return that
- if (username === null) return null
+ if (username === null) return undefined
// 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<KeyValue>).then) {
@@ -136,15 +136,16 @@ export async function uuidFromUser(user: string): Promise<string> {
* Fetch the username from a user
* @param user A user can be either a uuid or a username
*/
-export async function usernameFromUser(user: string): Promise<string> {
+export async function usernameFromUser(user: string): Promise<string | null> {
if (usernameCache.has(undashUuid(user))) {
if (debug) console.debug('Cache hit! usernameFromUser', user)
- return usernameCache.get(undashUuid(user))
+ return usernameCache.get(undashUuid(user)) ?? null
}
if (debug) console.debug('Cache miss: usernameFromUser', user)
let { uuid, username } = await mojang.profileFromUser(user)
+ if (!uuid) return null
uuid = undashUuid(uuid)
usernameCache.set(uuid, username)
return username
@@ -152,12 +153,12 @@ export async function usernameFromUser(user: string): Promise<string> {
let fetchingPlayers: Set<string> = new Set()
-export async function fetchPlayer(user: string): Promise<CleanPlayer> {
+export async function fetchPlayer(user: string): Promise<CleanPlayer | null> {
const playerUuid = await uuidFromUser(user)
-
+ if (!playerUuid) return null
if (playerCache.has(playerUuid))
- return playerCache.get(playerUuid)
+ return playerCache.get(playerUuid)!
// if it's already in the process of fetching, check every 100ms until it's not fetching the player anymore and fetch it again, since it'll be cached now
if (fetchingPlayers.has(playerUuid)) {
@@ -176,7 +177,7 @@ export async function fetchPlayer(user: string): Promise<CleanPlayer> {
fetchingPlayers.delete(playerUuid)
- if (!cleanPlayer) return
+ if (!cleanPlayer) return null
// clone in case it gets modified somehow later
playerCache.set(playerUuid, cleanPlayer)
@@ -190,14 +191,19 @@ export async function fetchPlayer(user: string): Promise<CleanPlayer> {
}
/** Fetch a player without their profiles. This is heavily cached. */
-export async function fetchBasicPlayer(user: string): Promise<CleanPlayer> {
+export async function fetchBasicPlayer(user: string): Promise<CleanPlayer | null> {
const playerUuid = await uuidFromUser(user)
+ if (!playerUuid) return null
+
if (basicPlayerCache.has(playerUuid))
- return basicPlayerCache.get(playerUuid)
+ return basicPlayerCache.get(playerUuid)!
const player = await fetchPlayer(playerUuid)
- if (!player) console.debug('no player? this should never happen', user, playerUuid)
+ if (!player) {
+ console.debug('no player? this should never happen', user, playerUuid)
+ return null
+ }
delete player.profiles
return player
@@ -206,7 +212,7 @@ export async function fetchBasicPlayer(user: string): Promise<CleanPlayer> {
export async function fetchSkyblockProfiles(playerUuid: string): Promise<CleanProfile[]> {
if (profilesCache.has(playerUuid)) {
if (debug) console.debug('Cache hit! fetchSkyblockProfiles', playerUuid)
- return profilesCache.get(playerUuid)
+ return profilesCache.get(playerUuid)!
}
if (debug) console.debug('Cache miss: fetchSkyblockProfiles', playerUuid)
@@ -220,7 +226,7 @@ export async function fetchSkyblockProfiles(playerUuid: string): Promise<CleanPr
const basicProfile: CleanProfile = {
name: profile.name,
uuid: profile.uuid,
- members: profile.members.map(m => {
+ members: profile.members?.map(m => {
return {
uuid: m.uuid,
username: m.username,
@@ -240,14 +246,14 @@ export async function fetchSkyblockProfiles(playerUuid: string): Promise<CleanPr
}
/** Fetch an array of `BasicProfile`s */
-async function fetchBasicProfiles(user: string): Promise<CleanBasicProfile[]> {
+async function fetchBasicProfiles(user: string): Promise<CleanBasicProfile[] | null> {
const playerUuid = await uuidFromUser(user)
- if (!playerUuid) return // invalid player, just return
+ if (!playerUuid) return null // invalid player, just return
if (basicProfilesCache.has(playerUuid)) {
if (debug) console.debug('Cache hit! fetchBasicProfiles', playerUuid)
- return basicProfilesCache.get(playerUuid)
+ return basicProfilesCache.get(playerUuid)!
}
if (debug) console.debug('Cache miss: fetchBasicProfiles', user)
@@ -259,6 +265,7 @@ async function fetchBasicProfiles(user: string): Promise<CleanBasicProfile[]> {
}
const profiles = player.profiles
basicProfilesCache.set(playerUuid, profiles)
+ if (!profiles) return null
// cache the profile names and uuids to profileNameCache because we can
for (const profile of profiles)
@@ -272,7 +279,7 @@ async function fetchBasicProfiles(user: string): Promise<CleanBasicProfile[]> {
* @param user A username or uuid
* @param profile A profile name or profile uuid
*/
-export async function fetchProfileUuid(user: string, profile: string): Promise<string> {
+export async function fetchProfileUuid(user: string, profile: string): Promise<string | null> {
// if a profile wasn't provided, return
if (!profile) {
if (debug) console.debug('no profile provided?', user, profile)
@@ -282,12 +289,12 @@ export async function fetchProfileUuid(user: string, profile: string): Promise<s
if (debug) console.debug('Cache miss: fetchProfileUuid', user, profile)
const profiles = await fetchBasicProfiles(user)
- if (!profiles) return // user probably doesnt exist
+ if (!profiles) return null // user probably doesnt exist
const profileUuid = undashUuid(profile)
for (const p of profiles) {
- if (p.name.toLowerCase() === profileUuid.toLowerCase())
+ if (p.name?.toLowerCase() === profileUuid.toLowerCase())
return undashUuid(p.uuid)
else if (undashUuid(p.uuid) === undashUuid(profileUuid))
return undashUuid(p.uuid)
@@ -300,8 +307,9 @@ export async function fetchProfileUuid(user: string, profile: string): Promise<s
* @param user A username or uuid
* @param profile A profile name or profile uuid
*/
-export async function fetchProfile(user: string, profile: string): Promise<CleanFullProfile> {
+export async function fetchProfile(user: string, profile: string): Promise<CleanFullProfile | null> {
const playerUuid = await uuidFromUser(user)
+ if (!playerUuid) return null
const profileUuid = await fetchProfileUuid(playerUuid, profile)
if (!profileUuid) return null
@@ -309,13 +317,15 @@ export async function fetchProfile(user: string, profile: string): Promise<Clean
if (profileCache.has(profileUuid)) {
// we have the profile cached, return it :)
if (debug) console.debug('Cache hit! fetchProfile', profileUuid)
- return profileCache.get(profileUuid)
+ return profileCache.get(profileUuid)!
}
if (debug) console.debug('Cache miss: fetchProfile', user, profile)
const profileName = await fetchProfileName(user, profile)
+ if (!profileName) return null // uhh this should never happen but if it does just return null
+
const cleanProfile: CleanFullProfile = await hypixel.fetchMemberProfileUncached(playerUuid, profileUuid)
// we know the name from fetchProfileName, so set it here
@@ -330,11 +340,12 @@ export async function fetchProfile(user: string, profile: string): Promise<Clean
* Fetch a CleanProfile from the uuid
* @param profileUuid A profile name or profile uuid
*/
-export async function fetchBasicProfileFromUuid(profileUuid: string): Promise<CleanProfile> {
+export async function fetchBasicProfileFromUuid(profileUuid: string): Promise<CleanProfile | undefined> {
if (profileCache.has(profileUuid)) {
// we have the profile cached, return it :)
if (debug) console.debug('Cache hit! fetchBasicProfileFromUuid', profileUuid)
- const profile: CleanFullProfile = profileCache.get(profileUuid)
+ const profile: CleanFullProfile | undefined = profileCache.get(profileUuid)
+ if (!profile) return undefined
return {
uuid: profile.uuid,
members: profile.members.map(m => ({
@@ -357,26 +368,32 @@ export async function fetchBasicProfileFromUuid(profileUuid: string): Promise<Cl
* @param user A player uuid or username
* @param profile A profile uuid or name
*/
-export async function fetchProfileName(user: string, profile: string): Promise<string> {
+export async function fetchProfileName(user: string, profile: string): Promise<string | null> {
// we're fetching the profile and player uuid again in case we were given a name, but it's cached so it's not much of a problem
const profileUuid = await fetchProfileUuid(user, profile)
- if (!profileUuid)
- return null
+ if (!profileUuid) return null
+
const playerUuid = await uuidFromUser(user)
+ if (!playerUuid) return null
+
if (profileNameCache.has(`${playerUuid}.${profileUuid}`)) {
// Return the profile name if it's cached
if (debug) console.debug('Cache hit! fetchProfileName', profileUuid)
- return profileNameCache.get(`${playerUuid}.${profileUuid}`)
+ return profileNameCache.get!(`${playerUuid}.${profileUuid}`) ?? null
}
if (debug) console.debug('Cache miss: fetchProfileName', user, profile)
const basicProfiles = await fetchBasicProfiles(playerUuid)
- let profileName
+
+ if (!basicProfiles) return null
+
+ let profileName: string | null = null
+
for (const basicProfile of basicProfiles)
if (basicProfile.uuid === playerUuid)
- profileName = basicProfile.name
+ profileName = basicProfile.name ?? null
profileNameCache.set(`${playerUuid}.${profileUuid}`, profileName)
return profileName
diff --git a/src/index.ts b/src/index.ts
index 4f4ad00..647038f 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -132,7 +132,12 @@ app.get('/constants', async(req, res) => {
app.post('/accounts/createsession', async(req, res) => {
try {
const { code } = req.body
- const { access_token: accessToken, refresh_token: refreshToken } = await discord.exchangeCode(`${mainSiteUrl}/loggedin`, code)
+ const codeExchange = await discord.exchangeCode(`${mainSiteUrl}/loggedin`, code)
+ if (!codeExchange) {
+ res.json({ ok: false, error: 'discord_client_secret isn\'t in env' })
+ return
+ }
+ const { access_token: accessToken, refresh_token: refreshToken } = codeExchange
if (!accessToken)
// access token is invalid :(
return res.json({ ok: false })
@@ -148,6 +153,8 @@ app.post('/accounts/session', async(req, res) => {
try {
const { uuid } = req.body
const session = await fetchSession(uuid)
+ if (!session)
+ return res.json({ ok: false })
const account = await fetchAccountFromDiscord(session.discord_user.id)
res.json({ session, account })
} catch (err) {
diff --git a/src/mojang.ts b/src/mojang.ts
index e22c8d8..84c8c7d 100644
--- a/src/mojang.ts
+++ b/src/mojang.ts
@@ -14,9 +14,8 @@ const httpsAgent = new Agent({
interface MojangApiResponse {
/** These uuids are already undashed */
- uuid: string
-
- username: string
+ uuid: string | null
+ username: string | null
}
/**
@@ -73,7 +72,7 @@ export async function profileFromUsername(username: string): Promise<MojangApiRe
return await profileFromUsername(username)
}
- let data = null
+ let data: any = null
const rawData = await fetchResponse.text()
try {
data = JSON.parse(rawData)
diff --git a/src/util.ts b/src/util.ts
index 67cf4b5..45839cc 100644
--- a/src/util.ts
+++ b/src/util.ts
@@ -61,7 +61,7 @@ export const minecraftColorCodes: { [ key: string ]: string } = {
* For example: blue -> 9
* @param colorName The name of the color (blue, red, aqua, etc)
*/
-export function colorCodeFromName(colorName: string): string {
+export function colorCodeFromName(colorName: string): string | undefined {
const hexColor = minecraftColorCodes[colorName.toLowerCase()]
for (const key in minecraftColorCodes) {
const value = minecraftColorCodes[key]