diff options
Diffstat (limited to 'src/constants.ts')
-rw-r--r-- | src/constants.ts | 52 |
1 files changed, 35 insertions, 17 deletions
diff --git a/src/constants.ts b/src/constants.ts index 760dcb1..5f20147 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -30,17 +30,23 @@ const queue = new Queue({ * @param json The JSON body, only applicable for some types of methods */ async function fetchGithubApi(method: string, route: string, headers?: any, json?: any): Promise<nodeFetch.Response> { - return await fetch( - githubApiBase + route, - { - agent: () => httpsAgent, - body: json ? JSON.stringify(json) : null, - method, - headers: Object.assign({ - 'Authorization': `token ${process.env.github_token}` - }, headers), - } - ) + try { + return await fetch( + githubApiBase + route, + { + agent: () => httpsAgent, + body: json ? JSON.stringify(json) : null, + method, + headers: Object.assign({ + 'Authorization': `token ${process.env.github_token}` + }, headers), + } + ) + } catch { + // if there's an error, wait a second and try again + await new Promise((resolve) => setTimeout(resolve, 1000)) + return await fetchGithubApi(method, route, headers, json) + } } interface GithubFile { @@ -73,6 +79,7 @@ async function fetchFile(path: string): Promise<GithubFile> { }, ) const data = await r.json() + const file = { path: data.path, content: Buffer.from(data.content, data.encoding).toString(), @@ -119,7 +126,7 @@ async function fetchJSONConstant(filename: string): Promise<string[]> { } /** Add stats to skyblock-constants. This has caching so it's fine to call many times */ -export async function addJSONConstants(filename: string, addingValues: string[], units: string='stats'): Promise<void> { +export async function addJSONConstants(filename: string, addingValues: string[], unit: string='stat'): Promise<void> { if (addingValues.length === 0) return // no stats provided, just return queue.enqueue(async() => { @@ -143,7 +150,7 @@ export async function addJSONConstants(filename: string, addingValues: string[], // there's not actually any new stats, just return if (newStats.length === 0) return - const commitMessage = newStats.length >= 2 ? `Add ${newStats.length} new ${units}` : `Add '${newStats[0]}'` + const commitMessage = newStats.length >= 2 ? `Add ${newStats.length} new ${unit}s` : `Add '${newStats[0]}' ${unit}` await editFile(file, commitMessage, JSON.stringify(updatedStats, null, 2)) }) } @@ -156,7 +163,7 @@ export async function fetchStats(): Promise<string[]> { /** Add stats to skyblock-constants. This has caching so it's fine to call many times */ export async function addStats(addingStats: string[]): Promise<void> { - await addJSONConstants('stats.json', addingStats, 'stats') + await addJSONConstants('stats.json', addingStats, 'stat') } /** Fetch all the known SkyBlock collections as an array of strings */ @@ -166,7 +173,7 @@ export async function fetchCollections(): Promise<string[]> { /** Add collections to skyblock-constants. This has caching so it's fine to call many times */ export async function addCollections(addingCollections: string[]): Promise<void> { - await addJSONConstants('collections.json', addingCollections, 'collections') + await addJSONConstants('collections.json', addingCollections, 'collection') } /** Fetch all the known SkyBlock collections as an array of strings */ @@ -176,7 +183,7 @@ export async function fetchSkills(): Promise<string[]> { /** Add skills to skyblock-constants. This has caching so it's fine to call many times */ export async function addSkills(addingSkills: string[]): Promise<void> { - await addJSONConstants('skills.json', addingSkills, 'skills') + await addJSONConstants('skills.json', addingSkills, 'skill') } /** Fetch all the known SkyBlock collections as an array of strings */ @@ -186,5 +193,16 @@ export async function fetchZones(): Promise<string[]> { /** Add skills to skyblock-constants. This has caching so it's fine to call many times */ export async function addZones(addingZones: string[]): Promise<void> { - await addJSONConstants('zones.json', addingZones, 'zones') + await addJSONConstants('zones.json', addingZones, 'zone') +} + + +/** Fetch all the known SkyBlock slayer names as an array of strings */ +export async function fetchSlayers(): Promise<string[]> { + return await fetchJSONConstant('slayers.json') +} + +/** Add skills to skyblock-constants. This has caching so it's fine to call many times */ +export async function addSlayers(addingSlayers: string[]): Promise<void> { + await addJSONConstants('slayers.json', addingSlayers, 'slayer') } |