aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/cleaners/player.ts12
-rw-r--r--src/cleaners/rank.ts4
-rw-r--r--src/cleaners/skyblock/collections.ts2
-rw-r--r--src/cleaners/skyblock/member.ts34
-rw-r--r--src/cleaners/skyblock/minions.ts4
-rw-r--r--src/cleaners/skyblock/profile.ts10
-rw-r--r--src/cleaners/skyblock/profiles.ts4
-rw-r--r--src/cleaners/skyblock/skills.ts2
-rw-r--r--src/constants.ts19
-rw-r--r--src/database.ts23
-rw-r--r--src/discord.ts4
-rw-r--r--src/hypixel.ts28
-rw-r--r--src/hypixelApi.ts8
-rw-r--r--src/hypixelCached.ts12
-rw-r--r--src/index.ts10
-rw-r--r--src/mojang.ts16
16 files changed, 109 insertions, 83 deletions
diff --git a/src/cleaners/player.ts b/src/cleaners/player.ts
index b2dfc77..802cdd5 100644
--- a/src/cleaners/player.ts
+++ b/src/cleaners/player.ts
@@ -1,9 +1,9 @@
-import { cleanPlayerSkyblockProfiles } from './skyblock/profiles'
-import { cleanSocialMedia, CleanSocialMedia } from './socialmedia'
-import { CleanBasicProfile } from './skyblock/profile'
-import { cleanRank, CleanRank } from './rank'
-import { HypixelPlayer } from '../hypixelApi'
-import { undashUuid } from '../util'
+import { cleanPlayerSkyblockProfiles } from './skyblock/profiles.js'
+import { cleanSocialMedia, CleanSocialMedia } from './socialmedia.js'
+import { CleanBasicProfile } from './skyblock/profile.js'
+import { cleanRank, CleanRank } from './rank.js'
+import { HypixelPlayer } from '../hypixelApi.js'
+import { undashUuid } from '../util.js'
export interface CleanBasicPlayer {
uuid: string
diff --git a/src/cleaners/rank.ts b/src/cleaners/rank.ts
index 4892bf2..417c8f1 100644
--- a/src/cleaners/rank.ts
+++ b/src/cleaners/rank.ts
@@ -1,5 +1,5 @@
-import { colorCodeFromName, minecraftColorCodes } from '../util'
-import { HypixelPlayer } from '../hypixelApi'
+import { colorCodeFromName, minecraftColorCodes } from '../util.js'
+import { HypixelPlayer } from '../hypixelApi.js'
const rankColors: { [ name: string ]: string } = {
'NONE': '7',
diff --git a/src/cleaners/skyblock/collections.ts b/src/cleaners/skyblock/collections.ts
index 99fdc27..635bfce 100644
--- a/src/cleaners/skyblock/collections.ts
+++ b/src/cleaners/skyblock/collections.ts
@@ -1,4 +1,4 @@
-import { cleanItemId, hypixelItemNames } from "./itemId"
+import { cleanItemId, hypixelItemNames } from './itemId.js'
const COLLECTIONS = {
'farming': [
diff --git a/src/cleaners/skyblock/member.ts b/src/cleaners/skyblock/member.ts
index cee1864..a9a12b8 100644
--- a/src/cleaners/skyblock/member.ts
+++ b/src/cleaners/skyblock/member.ts
@@ -1,20 +1,20 @@
-import { cleanCollections, Collection } from './collections'
-import { cleanInventories, Inventories } from './inventory'
-import { cleanFairySouls, FairySouls } from './fairysouls'
-import { cleanObjectives, Objective } from './objectives'
-import { CleanFullProfileBasicMembers } from './profile'
-import { cleanProfileStats, StatItem } from './stats'
-import { CleanMinion, cleanMinions } from './minions'
-import { AccountCustomization } from '../../database'
-import { cleanSlayers, SlayerData } from './slayers'
-import { cleanVisitedZones, Zone } from './zones'
-import { cleanSkills, Skill } from './skills'
-import * as cached from '../../hypixelCached'
-import * as constants from '../../constants'
-import { Included } from '../../hypixel'
-import { CleanPlayer } from '../player'
-import { CleanRank } from '../rank'
-import { Bank } from './bank'
+import { cleanCollections, Collection } from './collections.js'
+import { cleanInventories, Inventories } from './inventory.js'
+import { cleanFairySouls, FairySouls } from './fairysouls.js'
+import { cleanObjectives, Objective } from './objectives.js'
+import { CleanFullProfileBasicMembers } from './profile.js'
+import { cleanProfileStats, StatItem } from './stats.js'
+import { CleanMinion, cleanMinions } from './minions.js'
+import { cleanSlayers, SlayerData } from './slayers.js'
+import { AccountCustomization } from '../../database.js'
+import { cleanVisitedZones, Zone } from './zones.js'
+import { cleanSkills, Skill } from './skills.js'
+import * as cached from '../../hypixelCached.js'
+import * as constants from '../../constants.js'
+import { Included } from '../../hypixel.js'
+import { CleanPlayer } from '../player.js'
+import { CleanRank } from '../rank.js'
+import { Bank } from './bank.js'
export interface CleanBasicMember {
uuid: string
diff --git a/src/cleaners/skyblock/minions.ts b/src/cleaners/skyblock/minions.ts
index d045a7e..2e51896 100644
--- a/src/cleaners/skyblock/minions.ts
+++ b/src/cleaners/skyblock/minions.ts
@@ -1,5 +1,5 @@
-import { maxMinion } from '../../hypixel'
-import * as constants from '../../constants'
+import { maxMinion } from '../../hypixel.js'
+import * as constants from '../../constants.js'
export interface CleanMinion {
name: string,
diff --git a/src/cleaners/skyblock/profile.ts b/src/cleaners/skyblock/profile.ts
index e4d2a94..34c895a 100644
--- a/src/cleaners/skyblock/profile.ts
+++ b/src/cleaners/skyblock/profile.ts
@@ -1,8 +1,8 @@
-import { CleanBasicMember, CleanMember, cleanSkyBlockProfileMemberResponse, cleanSkyBlockProfileMemberResponseBasic } from './member'
-import { CleanMinion, combineMinionArrays, countUniqueMinions } from './minions'
-import { ApiOptions } from '../../hypixel'
-import { Bank, cleanBank } from './bank'
-import * as constants from '../../constants'
+import { CleanBasicMember, CleanMember, cleanSkyBlockProfileMemberResponse, cleanSkyBlockProfileMemberResponseBasic } from './member.js'
+import { CleanMinion, combineMinionArrays, countUniqueMinions } from './minions.js'
+import * as constants from '../../constants.js'
+import { ApiOptions } from '../../hypixel.js'
+import { Bank, cleanBank } from './bank.js'
export interface CleanProfile extends CleanBasicProfile {
members?: CleanBasicMember[]
diff --git a/src/cleaners/skyblock/profiles.ts b/src/cleaners/skyblock/profiles.ts
index a068dae..ec026de 100644
--- a/src/cleaners/skyblock/profiles.ts
+++ b/src/cleaners/skyblock/profiles.ts
@@ -1,10 +1,10 @@
-import { HypixelPlayerStatsSkyBlockProfiles } from '../../hypixelApi'
+import { HypixelPlayerStatsSkyBlockProfiles } from '../../hypixelApi.js'
import {
CleanBasicProfile,
CleanFullProfile,
CleanProfile,
cleanSkyblockProfileResponse
-} from './profile'
+} from './profile.js'
export function cleanPlayerSkyblockProfiles(rawProfiles: HypixelPlayerStatsSkyBlockProfiles): CleanBasicProfile[] {
let profiles: CleanBasicProfile[] = []
diff --git a/src/cleaners/skyblock/skills.ts b/src/cleaners/skyblock/skills.ts
index 7aa4e2c..886c59c 100644
--- a/src/cleaners/skyblock/skills.ts
+++ b/src/cleaners/skyblock/skills.ts
@@ -1,4 +1,4 @@
-import { fetchSkillXp, fetchSkillXpEasier } from '../../constants'
+import { fetchSkillXp, fetchSkillXpEasier } from '../../constants.js'
export interface Skill {
name: string
diff --git a/src/constants.ts b/src/constants.ts
index dcb9acf..13fc21d 100644
--- a/src/constants.ts
+++ b/src/constants.ts
@@ -3,14 +3,14 @@
*/
// we have to do this so we can mock the function from the tests properly
-import * as constants from './constants'
+import * as constants from './constants.js'
import * as nodeFetch from 'node-fetch'
import NodeCache from 'node-cache'
+import { debug } from './index.js'
import Queue from 'queue-promise'
import fetch from 'node-fetch'
import { Agent } from 'https'
-import { debug } from '.'
const httpsAgent = new Agent({
keepAlive: true
@@ -87,7 +87,7 @@ function fetchFile(path: string): Promise<GithubFile> {
'Accept': 'application/vnd.github.v3+json',
},
)
- const data = await r.json()
+ const data = await r.json() as any
const file = {
path: data.path,
@@ -118,7 +118,7 @@ async function editFile(file: GithubFile, message: string, newContent: string):
branch: 'main'
}
)
- const data = await r.json()
+ const data = await r.json() as any
fileCache.set(file.path, {
path: data.content.path,
content: newContent,
@@ -126,7 +126,7 @@ async function editFile(file: GithubFile, message: string, newContent: string):
})
}
-export async function fetchJSONConstant(filename: string): Promise<any> {
+export let fetchJSONConstant = async function fetchJSONConstant(filename: string): Promise<any> {
const file = await fetchFile(filename)
try {
return JSON.parse(file.content)
@@ -137,7 +137,7 @@ export async function fetchJSONConstant(filename: string): Promise<any> {
}
/** Add stats to skyblock-constants. This has caching so it's fine to call many times */
-export async function addJSONConstants(filename: string, addingValues: string[], unit: string='stat'): Promise<void> {
+export let addJSONConstants = async function addJSONConstants(filename: string, addingValues: string[], unit: string='stat'): Promise<void> {
if (addingValues.length === 0) return // no stats provided, just return
let file: GithubFile = await fetchFile(filename)
@@ -269,4 +269,9 @@ export async function setConstantValues(newValues: constantValues) {
try {
await editFile(file, commitMessage, JSON.stringify(updatedStats, null, 2))
} catch {}
-} \ No newline at end of file
+}
+
+
+// this is necessary for mocking in the tests because es6
+export function mockAddJSONConstants($value) { addJSONConstants = $value }
+export function mockFetchJSONConstant($value) { fetchJSONConstant = $value }
diff --git a/src/database.ts b/src/database.ts
index 0e16d48..5cd10db 100644
--- a/src/database.ts
+++ b/src/database.ts
@@ -2,20 +2,20 @@
* Store data about members for leaderboards
*/
-import { categorizeStat, getStatUnit } from './cleaners/skyblock/stats'
-import { CleanFullProfile } from './cleaners/skyblock/profile'
-import { slayerLevels } from './cleaners/skyblock/slayers'
-import { CleanMember } from './cleaners/skyblock/member'
+import { categorizeStat, getStatUnit } from './cleaners/skyblock/stats.js'
+import { CleanFullProfile } from './cleaners/skyblock/profile.js'
+import { slayerLevels } from './cleaners/skyblock/slayers.js'
+import { CleanMember } from './cleaners/skyblock/member.js'
import { Collection, Db, MongoClient } from 'mongodb'
-import { CleanPlayer } from './cleaners/player'
-import * as cached from './hypixelCached'
-import * as constants from './constants'
-import { shuffle, sleep } from './util'
-import * as discord from './discord'
+import { CleanPlayer } from './cleaners/player.js'
+import * as cached from './hypixelCached.js'
+import * as constants from './constants.js'
+import { shuffle, sleep } from './util.js'
+import * as discord from './discord.js'
import NodeCache from 'node-cache'
import { v4 as uuid4 } from 'uuid'
import Queue from 'queue-promise'
-import { debug } from '.'
+import { debug } from './index.js'
// don't update the user for 3 minutes
const recentlyUpdated = new NodeCache({
@@ -121,7 +121,7 @@ async function connect(): Promise<void> {
return console.warn('Warning: db_uri was not found in .env. Features that utilize the database such as leaderboards won\'t work.')
if (!process.env.db_name)
return console.warn('Warning: db_name was not found in .env. Features that utilize the database such as leaderboards won\'t work.')
- client = await MongoClient.connect(process.env.db_uri, { useNewUrlParser: true, useUnifiedTopology: true })
+ client = await MongoClient.connect(process.env.db_uri)
database = client.db(process.env.db_name)
memberLeaderboardsCollection = database.collection('member-leaderboards')
profileLeaderboardsCollection = database.collection('profile-leaderboards')
@@ -803,6 +803,7 @@ export async function updateAccount(discordId: string, schema: AccountSchema) {
}
// make sure it's not in a test
+console.log('global.isTest', globalThis.isTest)
if (!globalThis.isTest) {
connect().then(() => {
// when it connects, cache the leaderboards and remove bad members
diff --git a/src/discord.ts b/src/discord.ts
index fac5438..067b899 100644
--- a/src/discord.ts
+++ b/src/discord.ts
@@ -50,7 +50,7 @@ export async function exchangeCode(redirectUri: string, code: string): Promise<T
body: new URLSearchParams(data).toString()
}
)
- return await fetchResponse.json()
+ return await fetchResponse.json() as TokenResponse | null
}
@@ -63,5 +63,5 @@ export async function getUser(accessToken: string): Promise<DiscordUser> {
agent: () => httpsAgent,
}
)
- return response.json()
+ return await response.json() as DiscordUser
}
diff --git a/src/hypixel.ts b/src/hypixel.ts
index d6f9661..8811624 100644
--- a/src/hypixel.ts
+++ b/src/hypixel.ts
@@ -2,14 +2,26 @@
* Fetch the clean Hypixel API
*/
-import { cleanSkyblockProfileResponse, CleanProfile, CleanBasicProfile, CleanFullProfile, CleanFullProfileBasicMembers } from './cleaners/skyblock/profile'
-import { AccountCustomization, AccountSchema, fetchAccount, queueUpdateDatabaseMember, queueUpdateDatabaseProfile } from './database'
-import { CleanBasicMember, CleanMemberProfile } from './cleaners/skyblock/member'
-import { chooseApiKey, HypixelResponse, sendApiRequest } from './hypixelApi'
-import { cleanSkyblockProfilesResponse } from './cleaners/skyblock/profiles'
-import { CleanPlayer, cleanPlayerResponse } from './cleaners/player'
-import * as cached from './hypixelCached'
-import { debug } from '.'
+import {
+ cleanSkyblockProfileResponse,
+ CleanProfile,
+ CleanBasicProfile,
+ CleanFullProfile,
+ CleanFullProfileBasicMembers
+} from './cleaners/skyblock/profile.js'
+import {
+ AccountCustomization,
+ AccountSchema,
+ fetchAccount,
+ queueUpdateDatabaseMember,
+ queueUpdateDatabaseProfile
+} from './database.js'
+import { CleanBasicMember, CleanMemberProfile } from './cleaners/skyblock/member.js'
+import { chooseApiKey, HypixelResponse, sendApiRequest } from './hypixelApi.js'
+import { cleanSkyblockProfilesResponse } from './cleaners/skyblock/profiles.js'
+import { CleanPlayer, cleanPlayerResponse } from './cleaners/player.js'
+import * as cached from './hypixelCached.js'
+import { debug } from './index.js'
export type Included = 'profiles' | 'player' | 'stats' | 'inventories' | undefined
diff --git a/src/hypixelApi.ts b/src/hypixelApi.ts
index 1b57d8f..dd5e08c 100644
--- a/src/hypixelApi.ts
+++ b/src/hypixelApi.ts
@@ -3,12 +3,12 @@
*/
import fetch from 'node-fetch'
import * as nodeFetch from 'node-fetch'
-import { jsonToQuery, shuffle } from './util'
+import { jsonToQuery, shuffle } from './util.js'
import { Agent } from 'https'
if (!process.env.hypixel_keys)
// if there's no hypixel keys in env, run dotenv
- require('dotenv').config()
+ (await import('dotenv')).config()
// We need to create an agent to prevent memory leaks and to only do dns lookups once
const httpsAgent = new Agent({
@@ -139,7 +139,7 @@ export interface HypixelPlayer {
}
/** Send an HTTP request to the Hypixel API */
-export async function sendApiRequest({ path, key, args }): Promise<HypixelResponse> {
+export let sendApiRequest = async function sendApiRequest({ path, key, args }): Promise<HypixelResponse> {
// Send a raw http request to api.hypixel.net, and return the parsed json
if (key)
@@ -188,3 +188,5 @@ export async function sendApiRequest({ path, key, args }): Promise<HypixelRespon
return fetchJsonParsed
}
+// this is necessary for mocking in the tests because es6
+export function mockSendApiRequest($value) { sendApiRequest = $value } \ No newline at end of file
diff --git a/src/hypixelCached.ts b/src/hypixelCached.ts
index 51dca5f..73e75aa 100644
--- a/src/hypixelCached.ts
+++ b/src/hypixelCached.ts
@@ -2,14 +2,14 @@
* Fetch the clean and cached Hypixel API
*/
+import { CleanProfile, CleanFullProfile, CleanBasicProfile } from './cleaners/skyblock/profile.js'
+import { CleanPlayer } from './cleaners/player.js'
+import { isUuid, undashUuid } from './util.js'
+import * as hypixel from './hypixel.js'
+import * as mojang from './mojang.js'
import NodeCache from 'node-cache'
+import { debug } from './index.js'
import LRUCache from 'lru-cache'
-import * as mojang from './mojang'
-import * as hypixel from './hypixel'
-import { CleanPlayer } from './cleaners/player'
-import { isUuid, undashUuid } from './util'
-import { CleanProfile, CleanFullProfile, CleanBasicProfile } from './cleaners/skyblock/profile'
-import { debug } from '.'
// cache usernames for 30 minutes
/** uuid: username */
diff --git a/src/index.ts b/src/index.ts
index 647038f..1327771 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -1,10 +1,10 @@
-import { createSession, fetchAccountFromDiscord, fetchAllLeaderboardsCategorized, fetchLeaderboard, fetchMemberLeaderboardSpots, fetchSession, finishedCachingRawLeaderboards, leaderboardUpdateMemberQueue, leaderboardUpdateProfileQueue, updateAccount } from './database'
-import { fetchMemberProfile, fetchUser } from './hypixel'
+import { createSession, fetchAccountFromDiscord, fetchAllLeaderboardsCategorized, fetchLeaderboard, fetchMemberLeaderboardSpots, fetchSession, finishedCachingRawLeaderboards, leaderboardUpdateMemberQueue, leaderboardUpdateProfileQueue, updateAccount } from './database.js'
+import { fetchMemberProfile, fetchUser } from './hypixel.js'
import rateLimit from 'express-rate-limit'
-import * as constants from './constants'
-import * as discord from './discord'
+import * as constants from './constants.js'
+import * as discord from './discord.js'
import express from 'express'
-import { getKeyUsage } from './hypixelApi'
+import { getKeyUsage } from './hypixelApi.js'
const app = express()
diff --git a/src/mojang.ts b/src/mojang.ts
index 84c8c7d..9b224d6 100644
--- a/src/mojang.ts
+++ b/src/mojang.ts
@@ -2,10 +2,10 @@
* Fetch the Mojang username API through api.ashcon.app
*/
-import fetch from 'node-fetch'
+import { isUuid, undashUuid } from './util.js'
import * as nodeFetch from 'node-fetch'
+import fetch from 'node-fetch'
import { Agent } from 'https'
-import { isUuid, undashUuid } from './util'
// We need to create an agent to prevent memory leaks
const httpsAgent = new Agent({
@@ -21,7 +21,7 @@ interface MojangApiResponse {
/**
* Get mojang api data from the session server
*/
-export async function profileFromUuid(uuid: string): Promise<MojangApiResponse> {
+export let profileFromUuid = async function profileFromUuid(uuid: string): Promise<MojangApiResponse> {
let fetchResponse: nodeFetch.Response
try {
@@ -56,7 +56,7 @@ export async function profileFromUuid(uuid: string): Promise<MojangApiResponse>
}
-export async function profileFromUsername(username: string): Promise<MojangApiResponse> {
+export let profileFromUsername = async function profileFromUsername(username: string): Promise<MojangApiResponse> {
// since we don't care about anything other than the uuid, we can use /uuid/ instead of /user/
let fetchResponse: nodeFetch.Response
@@ -118,9 +118,15 @@ export async function profileFromUsernameAlternative(username: string): Promise<
}
}
-export async function profileFromUser(user: string): Promise<MojangApiResponse> {
+export let profileFromUser = async function profileFromUser(user: string): Promise<MojangApiResponse> {
if (isUuid(user)) {
return await profileFromUuid(user)
} else
return await profileFromUsername(user)
}
+
+
+// this is necessary for mocking in the tests because es6
+export function mockProfileFromUuid($value) { profileFromUuid = $value }
+export function mockProfileFromUsername($value) { profileFromUsername = $value }
+export function mockProfileFromUser($value) { profileFromUser = $value }