From eca7cbbbebee0429ea9e1855851350caf21db3c9 Mon Sep 17 00:00:00 2001 From: tresabhi Date: Sun, 16 Jul 2023 18:26:33 +0000 Subject: [PATCH] feat: added more mature secrets handling --- src/bot.ts | 4 ++-- src/commands/eligible.ts | 4 ++-- src/commands/evolution.tsx | 6 +++--- src/commands/fullStats.tsx | 6 +++--- src/commands/inactive.tsx | 10 +++++----- src/commands/ownedTanks.tsx | 6 +++--- src/commands/playerAchievements.ts | 6 +++--- src/commands/playerInfo.ts | 4 ++-- src/commands/ratings.tsx | 4 ++-- src/commands/searchClans.ts | 4 ++-- src/commands/searchPlayers.ts | 8 ++++---- src/commands/stats.tsx | 6 +++--- src/commands/today.tsx | 4 ++-- src/commands/verify.ts | 6 +++--- src/core/blitz/getTankStats.ts | 4 ++-- src/core/blitz/listClans.ts | 8 ++++---- src/core/blitz/listPlayers.ts | 8 ++++---- src/core/blitzstars/getPlayerHistories.ts | 4 ++-- src/core/node/arguments.ts | 9 --------- src/core/node/secrets.ts | 15 +++++++++++++++ src/events/interactionCreate/index.ts | 4 ++-- src/renderers/evolution.tsx | 6 +++--- src/renderers/fullStats.tsx | 6 +++--- src/renderers/stats.tsx | 6 +++--- src/renderers/today.tsx | 6 +++--- src/routes/stats.tsx | 2 +- 26 files changed, 81 insertions(+), 75 deletions(-) delete mode 100644 src/core/node/arguments.ts create mode 100644 src/core/node/secrets.ts diff --git a/src/bot.ts b/src/bot.ts index 8297d739e..cbf0561b9 100644 --- a/src/bot.ts +++ b/src/bot.ts @@ -1,5 +1,5 @@ import { Client, GatewayIntentBits } from 'discord.js'; -import { DISCORD_TOKEN } from './core/node/arguments'; +import { secrets } from './core/node/secrets'; import { registerErrorHandlers } from './events/error'; import guildMemberAdd from './events/guildMemberAdd'; import interactionCreate from './events/interactionCreate'; @@ -13,4 +13,4 @@ export const client = new Client({ .on('interactionCreate', interactionCreate); registerErrorHandlers(); -client.login(DISCORD_TOKEN); +client.login(secrets.DISCORD_TOKEN); diff --git a/src/commands/eligible.ts b/src/commands/eligible.ts index 2ded85c56..06ad066fd 100644 --- a/src/commands/eligible.ts +++ b/src/commands/eligible.ts @@ -12,7 +12,7 @@ import embedNegative from '../core/discord/embedNegative'; import embedPositive from '../core/discord/embedPositive'; import markdownTable, { TableInput } from '../core/discord/markdownTable'; import resolvePlayerFromCommand from '../core/discord/resolvePlayerFromCommand'; -import { WARGAMING_APPLICATION_ID } from '../core/node/arguments'; +import { secrets } from '../core/node/secrets'; import { CommandRegistry } from '../events/interactionCreate'; import { AccountInfo } from '../types/accountInfo'; import { PossiblyPromise } from '../types/possiblyPromise'; @@ -51,7 +51,7 @@ export const eligibleCommand: CommandRegistry = { const clan = interaction.options.getString('clan') as SkilledClan; const { id, region: server } = await resolvePlayerFromCommand(interaction); const accountInfo = await getWargamingResponse( - `https://api.wotblitz.${server}/wotb/account/info/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}`, + `https://api.wotblitz.${server}/wotb/account/info/?application_id=${secrets.WARGAMING_APPLICATION_ID}&account_id=${id}`, ); const problems: TableInput = []; const title = `${accountInfo[id].nickname}'s eligibility for ${SKILLED_CLANS[clan]}`; diff --git a/src/commands/evolution.tsx b/src/commands/evolution.tsx index 4754f635a..9354f2689 100644 --- a/src/commands/evolution.tsx +++ b/src/commands/evolution.tsx @@ -12,7 +12,7 @@ import resolvePeriodFromButton from '../core/discord/resolvePeriodFromButton'; import resolvePeriodFromCommand from '../core/discord/resolvePeriodFromCommand'; import resolvePlayerFromButton from '../core/discord/resolvePlayerFromButton'; import resolvePlayerFromCommand from '../core/discord/resolvePlayerFromCommand'; -import { WARGAMING_APPLICATION_ID } from '../core/node/arguments'; +import { secrets } from '../core/node/secrets'; import { CommandRegistry } from '../events/interactionCreate'; import evolution from '../renderers/evolution'; import { StatType } from '../renderers/fullStats'; @@ -42,7 +42,7 @@ export const evolutionCommand: CommandRegistry = { const end = interaction.options.getInteger('end'); const { nickname } = ( await getWargamingResponse( - `https://api.wotblitz.${player.region}/wotb/account/info/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${player.id}`, + `https://api.wotblitz.${player.region}/wotb/account/info/?application_id=${secrets.WARGAMING_APPLICATION_ID}&account_id=${player.id}`, ) )[player.id]; const path = interactionToURL(interaction, { @@ -75,7 +75,7 @@ export const evolutionCommand: CommandRegistry = { const path = url.pathname.split('/').filter(Boolean); const commandGroup = path[1] as StatType; const player = await resolvePlayerFromButton(interaction); - const period = resolvePeriodFromButton(player.server, interaction); + const period = resolvePeriodFromButton(player.region, interaction); return await evolution( commandGroup, diff --git a/src/commands/fullStats.tsx b/src/commands/fullStats.tsx index d5c9a7e67..8b30f58d9 100644 --- a/src/commands/fullStats.tsx +++ b/src/commands/fullStats.tsx @@ -12,7 +12,7 @@ import resolvePeriodFromButton from '../core/discord/resolvePeriodFromButton'; import resolvePeriodFromCommand from '../core/discord/resolvePeriodFromCommand'; import resolvePlayerFromButton from '../core/discord/resolvePlayerFromButton'; import resolvePlayerFromCommand from '../core/discord/resolvePlayerFromCommand'; -import { WARGAMING_APPLICATION_ID } from '../core/node/arguments'; +import { secrets } from '../core/node/secrets'; import { CommandRegistry } from '../events/interactionCreate'; import fullStats, { StatType } from '../renderers/fullStats'; import { AccountInfo } from '../types/accountInfo'; @@ -47,7 +47,7 @@ export const fullStatsCommand: CommandRegistry = { }); const { nickname } = ( await getWargamingResponse( - `https://api.wotblitz.${player.region}/wotb/account/info/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${player.id}`, + `https://api.wotblitz.${player.region}/wotb/account/info/?application_id=${secrets.WARGAMING_APPLICATION_ID}&account_id=${player.id}`, ) )[player.id]; @@ -72,7 +72,7 @@ export const fullStatsCommand: CommandRegistry = { const path = url.pathname.split('/').filter(Boolean); const commandGroup = path[1] as StatType; const player = await resolvePlayerFromButton(interaction); - const period = resolvePeriodFromButton(player.server, interaction); + const period = resolvePeriodFromButton(player.region, interaction); return await fullStats( commandGroup, diff --git a/src/commands/inactive.tsx b/src/commands/inactive.tsx index e059125fe..8784e03d7 100644 --- a/src/commands/inactive.tsx +++ b/src/commands/inactive.tsx @@ -9,7 +9,7 @@ import getBlitzClan from '../core/blitz/getBlitzClan'; import getWargamingResponse from '../core/blitz/getWargamingResponse'; import addClanChoices from '../core/discord/addClanChoices'; import autocompleteClan from '../core/discord/autocompleteClan'; -import { WARGAMING_APPLICATION_ID } from '../core/node/arguments'; +import { secrets } from '../core/node/secrets'; import { CommandRegistry } from '../events/interactionCreate'; import { AccountInfo } from '../types/accountInfo'; import { ClanInfo } from '../types/clanInfo'; @@ -42,14 +42,14 @@ export const inactiveCommand: CommandRegistry = { const time = new Date().getTime() / 1000; const clanData = ( await getWargamingResponse( - `https://api.wotblitz.${server}/wotb/clans/info/?application_id=${WARGAMING_APPLICATION_ID}&clan_id=${id}`, + `https://api.wotblitz.${server}/wotb/clans/info/?application_id=${secrets.WARGAMING_APPLICATION_ID}&clan_id=${id}`, ) )[id]; const memberIds = clanData.members_ids; const accounts = await getWargamingResponse( - `https://api.wotblitz.${server}/wotb/account/info/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${memberIds.join( - ',', - )}`, + `https://api.wotblitz.${server}/wotb/account/info/?application_id=${ + secrets.WARGAMING_APPLICATION_ID + }&account_id=${memberIds.join(',')}`, ); const inactive = memberIds .map((memberId) => { diff --git a/src/commands/ownedTanks.tsx b/src/commands/ownedTanks.tsx index 637739d6b..f5bddbb84 100644 --- a/src/commands/ownedTanks.tsx +++ b/src/commands/ownedTanks.tsx @@ -17,7 +17,7 @@ import { import addUsernameChoices from '../core/discord/addUsernameChoices'; import autocompleteUsername from '../core/discord/autocompleteUsername'; import resolvePlayerFromCommand from '../core/discord/resolvePlayerFromCommand'; -import { WARGAMING_APPLICATION_ID } from '../core/node/arguments'; +import { secrets } from '../core/node/secrets'; import { CommandRegistry } from '../events/interactionCreate'; import { AccountInfo } from '../types/accountInfo'; import { PlayerClanData } from '../types/playerClanData'; @@ -76,7 +76,7 @@ export const ownedTanksCommand: CommandRegistry = { const account = await resolvePlayerFromCommand(interaction); const { id, region: server } = account; const accountInfo = await getWargamingResponse( - `https://api.wotblitz.${server}/wotb/account/info/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}`, + `https://api.wotblitz.${server}/wotb/account/info/?application_id=${secrets.WARGAMING_APPLICATION_ID}&account_id=${id}`, ); const tankStats = await getTankStats(server, id); const tanks = ( @@ -88,7 +88,7 @@ export const ownedTanksCommand: CommandRegistry = { ) ).filter((tank) => tank?.tier === tier); const clanData = await getWargamingResponse( - `https://api.wotblitz.${server}/wotb/clans/accountinfo/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}&extra=clan`, + `https://api.wotblitz.${server}/wotb/clans/accountinfo/?application_id=${secrets.WARGAMING_APPLICATION_ID}&account_id=${id}&extra=clan`, ); const leftColumnSize = Math.ceil(tanks.length / 2); const leftColumn = tanks.slice(0, leftColumnSize); diff --git a/src/commands/playerAchievements.ts b/src/commands/playerAchievements.ts index 505cf18de..904b8534a 100644 --- a/src/commands/playerAchievements.ts +++ b/src/commands/playerAchievements.ts @@ -7,7 +7,7 @@ import autocompleteUsername from '../core/discord/autocompleteUsername'; import embedInfo from '../core/discord/embedInfo'; import markdownTable, { TableInputEntry } from '../core/discord/markdownTable'; import resolvePlayerFromCommand from '../core/discord/resolvePlayerFromCommand'; -import { WARGAMING_APPLICATION_ID } from '../core/node/arguments'; +import { secrets } from '../core/node/secrets'; import { CommandRegistry } from '../events/interactionCreate'; import { AccountAchievements } from '../types/accountAchievements'; import { AccountInfo } from '../types/accountInfo'; @@ -38,11 +38,11 @@ export const playerAchievementsCommand: CommandRegistry = { const account = await resolvePlayerFromCommand(interaction); const { id, region: server } = account; const accounts = await getWargamingResponse( - `https://api.wotblitz.${server}/wotb/account/info/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}`, + `https://api.wotblitz.${server}/wotb/account/info/?application_id=${secrets.WARGAMING_APPLICATION_ID}&account_id=${id}`, ); const accountsAchievements = await getWargamingResponse( - `https://api.wotblitz.${server}/wotb/account/achievements/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}`, + `https://api.wotblitz.${server}/wotb/account/achievements/?application_id=${secrets.WARGAMING_APPLICATION_ID}&account_id=${id}`, ); const accountAchievements = accountsAchievements[id]; const compound = { diff --git a/src/commands/playerInfo.ts b/src/commands/playerInfo.ts index ad7876c66..e11a66ebe 100644 --- a/src/commands/playerInfo.ts +++ b/src/commands/playerInfo.ts @@ -6,7 +6,7 @@ import autocompleteUsername from '../core/discord/autocompleteUsername'; import embedInfo from '../core/discord/embedInfo'; import markdownTable from '../core/discord/markdownTable'; import resolvePlayerFromCommand from '../core/discord/resolvePlayerFromCommand'; -import { WARGAMING_APPLICATION_ID } from '../core/node/arguments'; +import { secrets } from '../core/node/secrets'; import { CommandRegistry } from '../events/interactionCreate'; import { AccountInfo } from '../types/accountInfo'; @@ -24,7 +24,7 @@ export const playerInfoCommand: CommandRegistry = { const account = await resolvePlayerFromCommand(interaction); const { id, region: server } = account; const accounts = await getWargamingResponse( - `https://api.wotblitz.${server}/wotb/account/info/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}`, + `https://api.wotblitz.${server}/wotb/account/info/?application_id=${secrets.WARGAMING_APPLICATION_ID}&account_id=${id}`, ); const accountInfo = accounts[id]; diff --git a/src/commands/ratings.tsx b/src/commands/ratings.tsx index 1b9959c63..db20006c0 100644 --- a/src/commands/ratings.tsx +++ b/src/commands/ratings.tsx @@ -8,7 +8,7 @@ import regionToRegionSubdomain from '../core/blitz/regionToRegionSubdomain'; import addUsernameChoices from '../core/discord/addUsernameChoices'; import autocompleteUsername from '../core/discord/autocompleteUsername'; import resolvePlayerFromCommand from '../core/discord/resolvePlayerFromCommand'; -import { WARGAMING_APPLICATION_ID } from '../core/node/arguments'; +import { secrets } from '../core/node/secrets'; import { CommandRegistry } from '../events/interactionCreate'; import { AccountInfo } from '../types/accountInfo'; @@ -181,7 +181,7 @@ export const ratingsCommand: CommandRegistry = { `https://${regionSubdomain}.wotblitz.com/en/api/rating-leaderboards/league/${leagueIndex}/top/`, ).then((response) => response.json() as Promise); const { nickname } = await getWargamingResponse( - `https://api.wotblitz.${player.region}/wotb/account/info/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${player.id}`, + `https://api.wotblitz.${player.region}/wotb/account/info/?application_id=${secrets.WARGAMING_APPLICATION_ID}&account_id=${player.id}`, ).then((accounts) => accounts[player.id]); const awaitedInfo = await ratingsInfo; const leagueInfo = awaitedInfo.leagues[leagueIndex]; diff --git a/src/commands/searchClans.ts b/src/commands/searchClans.ts index a483d4510..15d44a919 100644 --- a/src/commands/searchClans.ts +++ b/src/commands/searchClans.ts @@ -5,7 +5,7 @@ import getWargamingResponse from '../core/blitz/getWargamingResponse'; import addClanChoices from '../core/discord/addClanChoices'; import addServerChoices from '../core/discord/addServerChoices'; import embedInfo from '../core/discord/embedInfo'; -import { WARGAMING_APPLICATION_ID } from '../core/node/arguments'; +import { secrets } from '../core/node/secrets'; import { CommandRegistry } from '../events/interactionCreate'; import { ClanList } from '../types/clanList'; @@ -32,7 +32,7 @@ export const searchClansCommand: CommandRegistry = { const clan = interaction.options.getString('clan')!; const limit = interaction.options.getInteger('limit') ?? 25; const clanList = await getWargamingResponse( - `https://api.wotblitz.${server}/wotb/clans/list/?application_id=${WARGAMING_APPLICATION_ID}&search=${clan}&limit=${limit}`, + `https://api.wotblitz.${server}/wotb/clans/list/?application_id=${secrets.WARGAMING_APPLICATION_ID}&search=${clan}&limit=${limit}`, ); return embedInfo( diff --git a/src/commands/searchPlayers.ts b/src/commands/searchPlayers.ts index 7de26bc69..e01e01f25 100644 --- a/src/commands/searchPlayers.ts +++ b/src/commands/searchPlayers.ts @@ -6,7 +6,7 @@ import { usernamePattern } from '../core/blitz/listPlayers'; import addServerChoices from '../core/discord/addServerChoices'; import addUsernameChoices from '../core/discord/addUsernameChoices'; import embedInfo from '../core/discord/embedInfo'; -import { WARGAMING_APPLICATION_ID } from '../core/node/arguments'; +import { secrets } from '../core/node/secrets'; import { CommandRegistry } from '../events/interactionCreate'; import { AccountList } from '../types/accountList'; @@ -37,9 +37,9 @@ export const searchPlayersCommand: CommandRegistry = { const trimmedSearch = name.trim(); const players = usernamePattern.test(trimmedSearch) ? await getWargamingResponse( - `https://api.wotblitz.${server}/wotb/account/list/?application_id=${WARGAMING_APPLICATION_ID}&search=${encodeURIComponent( - trimmedSearch, - )}&limit=${limit}`, + `https://api.wotblitz.${server}/wotb/account/list/?application_id=${ + secrets.WARGAMING_APPLICATION_ID + }&search=${encodeURIComponent(trimmedSearch)}&limit=${limit}`, ) : []; diff --git a/src/commands/stats.tsx b/src/commands/stats.tsx index 2b392fb7f..a366eeb93 100644 --- a/src/commands/stats.tsx +++ b/src/commands/stats.tsx @@ -12,7 +12,7 @@ import resolvePeriodFromButton from '../core/discord/resolvePeriodFromButton'; import resolvePeriodFromCommand from '../core/discord/resolvePeriodFromCommand'; import resolvePlayerFromButton from '../core/discord/resolvePlayerFromButton'; import resolvePlayerFromCommand from '../core/discord/resolvePlayerFromCommand'; -import { WARGAMING_APPLICATION_ID } from '../core/node/arguments'; +import { secrets } from '../core/node/secrets'; import { CommandRegistry } from '../events/interactionCreate'; import { StatType } from '../renderers/fullStats'; import stats from '../renderers/stats'; @@ -48,7 +48,7 @@ export const statsCommand: CommandRegistry = { }); const { nickname } = ( await getWargamingResponse( - `https://api.wotblitz.${player.region}/wotb/account/info/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${player.id}`, + `https://api.wotblitz.${player.region}/wotb/account/info/?application_id=${secrets.WARGAMING_APPLICATION_ID}&account_id=${player.id}`, ) )[player.id]; @@ -73,7 +73,7 @@ export const statsCommand: CommandRegistry = { const path = url.pathname.split('/').filter(Boolean); const commandGroup = path[1] as StatType; const player = await resolvePlayerFromButton(interaction); - const period = resolvePeriodFromButton(player.server, interaction); + const period = resolvePeriodFromButton(player.region, interaction); return await stats( commandGroup, diff --git a/src/commands/today.tsx b/src/commands/today.tsx index 48c01c68c..9a5a7a037 100644 --- a/src/commands/today.tsx +++ b/src/commands/today.tsx @@ -8,7 +8,7 @@ import linkButton from '../core/discord/linkButton'; import primaryButton from '../core/discord/primaryButton'; import resolvePlayerFromButton from '../core/discord/resolvePlayerFromButton'; import resolvePlayerFromCommand from '../core/discord/resolvePlayerFromCommand'; -import { WARGAMING_APPLICATION_ID } from '../core/node/arguments'; +import { secrets } from '../core/node/secrets'; import { CommandRegistry } from '../events/interactionCreate'; import today from '../renderers/today'; import { AccountInfo } from '../types/accountInfo'; @@ -37,7 +37,7 @@ export const todayCommand: CommandRegistry = { const limit = interaction.options.getInteger('limit') ?? Infinity; const { nickname } = ( await getWargamingResponse( - `https://api.wotblitz.${player.region}/wotb/account/info/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${player.id}`, + `https://api.wotblitz.${player.region}/wotb/account/info/?application_id=${secrets.WARGAMING_APPLICATION_ID}&account_id=${player.id}`, ) )[player.id]; const path = interactionToURL(interaction, player); diff --git a/src/commands/verify.ts b/src/commands/verify.ts index de53dc2a7..1dea0918f 100644 --- a/src/commands/verify.ts +++ b/src/commands/verify.ts @@ -7,7 +7,7 @@ import autocompleteUsername from '../core/discord/autocompleteUsername'; import embedNegative from '../core/discord/embedNegative'; import embedPositive from '../core/discord/embedPositive'; import resolvePlayerFromCommand from '../core/discord/resolvePlayerFromCommand'; -import { WARGAMING_APPLICATION_ID } from '../core/node/arguments'; +import { secrets } from '../core/node/secrets'; import { CommandRegistry } from '../events/interactionCreate'; import { AccountInfo } from '../types/accountInfo'; import { PlayerClanData } from '../types/playerClanData'; @@ -26,10 +26,10 @@ export const verifyCommand: CommandRegistry = { const blitzAccount = await resolvePlayerFromCommand(interaction); const { id, region: server } = blitzAccount; const accountInfo = await getWargamingResponse( - `https://api.wotblitz.${server}/wotb/account/info/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}`, + `https://api.wotblitz.${server}/wotb/account/info/?application_id=${secrets.WARGAMING_APPLICATION_ID}&account_id=${id}`, ); const clanData = await getWargamingResponse( - `https://api.wotblitz.${server}/wotb/clans/accountinfo/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}&extra=clan`, + `https://api.wotblitz.${server}/wotb/clans/accountinfo/?application_id=${secrets.WARGAMING_APPLICATION_ID}&account_id=${id}&extra=clan`, ); const clan = clanData[id]?.clan; const clanTag = clan ? ` [${clan!.tag}]` : ''; diff --git a/src/core/blitz/getTankStats.ts b/src/core/blitz/getTankStats.ts index d12be6448..444b544fa 100644 --- a/src/core/blitz/getTankStats.ts +++ b/src/core/blitz/getTankStats.ts @@ -1,12 +1,12 @@ import { REGION_NAMES, Region } from '../../constants/regions'; import { TanksStats } from '../../types/tanksStats'; -import { WARGAMING_APPLICATION_ID } from '../node/arguments'; +import { secrets } from '../node/secrets'; import throwError from '../node/throwError'; import getWargamingResponse from './getWargamingResponse'; export default async function getTankStats(server: Region, id: number) { const tankStats = await getWargamingResponse( - `https://api.wotblitz.${server}/wotb/tanks/stats/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}`, + `https://api.wotblitz.${server}/wotb/tanks/stats/?application_id=${secrets.WARGAMING_APPLICATION_ID}&account_id=${id}`, ); if (tankStats[id] === null) { diff --git a/src/core/blitz/listClans.ts b/src/core/blitz/listClans.ts index 69e84ec29..3c7e3fab8 100644 --- a/src/core/blitz/listClans.ts +++ b/src/core/blitz/listClans.ts @@ -1,6 +1,6 @@ import { Region } from '../../constants/regions'; import { Clan, ClanList } from '../../types/clanList'; -import { WARGAMING_APPLICATION_ID } from '../node/arguments'; +import { secrets } from '../node/secrets'; import getWargamingResponse from './getWargamingResponse'; export type ClanListWithServer = (Clan & { @@ -15,7 +15,7 @@ export default async function listClans(search: string, limit = 9) { return ( await Promise.all([ getWargamingResponse( - `https://api.wotblitz.com/wotb/clans/list/?application_id=${WARGAMING_APPLICATION_ID}&search=${search}&limit=${normalizedLimit}`, + `https://api.wotblitz.com/wotb/clans/list/?application_id=${secrets.WARGAMING_APPLICATION_ID}&search=${search}&limit=${normalizedLimit}`, ).then( (value) => value && @@ -25,7 +25,7 @@ export default async function listClans(search: string, limit = 9) { })), ), getWargamingResponse( - `https://api.wotblitz.eu/wotb/clans/list/?application_id=${WARGAMING_APPLICATION_ID}&search=${search}&limit=${normalizedLimit}`, + `https://api.wotblitz.eu/wotb/clans/list/?application_id=${secrets.WARGAMING_APPLICATION_ID}&search=${search}&limit=${normalizedLimit}`, ).then( (value) => value && @@ -35,7 +35,7 @@ export default async function listClans(search: string, limit = 9) { })), ), getWargamingResponse( - `https://api.wotblitz.asia/wotb/clans/list/?application_id=${WARGAMING_APPLICATION_ID}&search=${search}&limit=${normalizedLimit}`, + `https://api.wotblitz.asia/wotb/clans/list/?application_id=${secrets.WARGAMING_APPLICATION_ID}&search=${search}&limit=${normalizedLimit}`, ).then( (value) => value && diff --git a/src/core/blitz/listPlayers.ts b/src/core/blitz/listPlayers.ts index 6ecc9a3fb..15df2cb1e 100644 --- a/src/core/blitz/listPlayers.ts +++ b/src/core/blitz/listPlayers.ts @@ -1,6 +1,6 @@ import { Region } from '../../constants/regions'; import { Account, AccountList } from '../../types/accountList'; -import { WARGAMING_APPLICATION_ID } from '../node/arguments'; +import { secrets } from '../node/secrets'; import getWargamingResponse from './getWargamingResponse'; export type AccountListWithServer = (Account & { @@ -19,7 +19,7 @@ export default async function listPlayers(search: string, limit = 9) { return ( await Promise.all([ getWargamingResponse( - `https://api.wotblitz.com/wotb/account/list/?application_id=${WARGAMING_APPLICATION_ID}&search=${encodedSearch}&limit=${normalizedLimit}`, + `https://api.wotblitz.com/wotb/account/list/?application_id=${secrets.WARGAMING_APPLICATION_ID}&search=${encodedSearch}&limit=${normalizedLimit}`, ).then( (value) => value && @@ -29,7 +29,7 @@ export default async function listPlayers(search: string, limit = 9) { })), ), getWargamingResponse( - `https://api.wotblitz.eu/wotb/account/list/?application_id=${WARGAMING_APPLICATION_ID}&search=${encodedSearch}&limit=${normalizedLimit}`, + `https://api.wotblitz.eu/wotb/account/list/?application_id=${secrets.WARGAMING_APPLICATION_ID}&search=${encodedSearch}&limit=${normalizedLimit}`, ).then( (value) => value && @@ -39,7 +39,7 @@ export default async function listPlayers(search: string, limit = 9) { })), ), getWargamingResponse( - `https://api.wotblitz.asia/wotb/account/list/?application_id=${WARGAMING_APPLICATION_ID}&search=${encodedSearch}&limit=${normalizedLimit}`, + `https://api.wotblitz.asia/wotb/account/list/?application_id=${secrets.WARGAMING_APPLICATION_ID}&search=${encodedSearch}&limit=${normalizedLimit}`, ).then( (value) => value && diff --git a/src/core/blitzstars/getPlayerHistories.ts b/src/core/blitzstars/getPlayerHistories.ts index ad9cf6e46..0c04357c0 100644 --- a/src/core/blitzstars/getPlayerHistories.ts +++ b/src/core/blitzstars/getPlayerHistories.ts @@ -7,7 +7,7 @@ import { getHistoriesDefaultOptions, } from '../../types/histories'; import getWargamingResponse from '../blitz/getWargamingResponse'; -import { WARGAMING_APPLICATION_ID } from '../node/arguments'; +import { secrets } from '../node/secrets'; import { emptyAllStats } from './getDiffedTankStats'; export interface PlayerHistoryRaw { @@ -67,7 +67,7 @@ export default async function getPlayerHistories( statistics: { all }, } = ( await getWargamingResponse( - `https://api.wotblitz.${server}/wotb/account/info/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}`, + `https://api.wotblitz.${server}/wotb/account/info/?application_id=${secrets.WARGAMING_APPLICATION_ID}&account_id=${id}`, ) )[id]; diff --git a/src/core/node/arguments.ts b/src/core/node/arguments.ts deleted file mode 100644 index dc8797b8c..000000000 --- a/src/core/node/arguments.ts +++ /dev/null @@ -1,9 +0,0 @@ -export const DISCORD_TOKEN = process.env.DISCORD_TOKEN!; -export const WARGAMING_APPLICATION_ID = process.env.WARGAMING_APPLICATION_ID!; - -if (!DISCORD_TOKEN) { - throw new Error('No Discord token provided'); -} -if (!WARGAMING_APPLICATION_ID) { - throw new Error('No Wargaming application id provided}'); -} diff --git a/src/core/node/secrets.ts b/src/core/node/secrets.ts new file mode 100644 index 000000000..096b5d382 --- /dev/null +++ b/src/core/node/secrets.ts @@ -0,0 +1,15 @@ +const SECRET_KEYS = ['DISCORD_TOKEN', 'WARGAMING_APPLICATION_ID'] as const; + +type SECRET_KEY = typeof SECRET_KEYS extends readonly (infer T)[] ? T : never; + +export const secrets = SECRET_KEYS.reduce>>( + (accumulator, key) => ({ + ...accumulator, + + get [key](): string { + if (!(key in process.env)) throw new Error(`Key ${key} not provided`); + return process.env[key]!; + }, + }), + {}, +) as Record; diff --git a/src/events/interactionCreate/index.ts b/src/events/interactionCreate/index.ts index ef951d1d9..b1d0ab354 100644 --- a/src/events/interactionCreate/index.ts +++ b/src/events/interactionCreate/index.ts @@ -30,9 +30,9 @@ import { searchTanksCommand } from '../../commands/searchTanks'; import { statsCommand } from '../../commands/stats'; import { todayCommand } from '../../commands/today'; import { verifyCommand } from '../../commands/verify'; -import { DISCORD_TOKEN } from '../../core/node/arguments'; import getClientId from '../../core/node/getClientId'; import isDev from '../../core/node/isDev'; +import { secrets } from '../../core/node/secrets'; import handleAutocomplete from './handlers/autocomplete'; import handleButton from './handlers/button'; import handleChatInputCommand from './handlers/chatInputCommand'; @@ -71,7 +71,7 @@ export interface CommandRegistry button?: (interaction: ButtonInteraction) => InteractionReturnable; } -const rest = new REST().setToken(DISCORD_TOKEN); +const rest = new REST().setToken(secrets.DISCORD_TOKEN); export const COMMANDS_RAW: CommandRegistry[] = [ debugCommand, diff --git a/src/renderers/evolution.tsx b/src/renderers/evolution.tsx index c6996cbcb..1267cf92c 100644 --- a/src/renderers/evolution.tsx +++ b/src/renderers/evolution.tsx @@ -11,7 +11,7 @@ import getPlayerHistories from '../core/blitzstars/getPlayerHistories'; import getTankHistories from '../core/blitzstars/getTankHistories'; import { ResolvedPeriod } from '../core/discord/resolvePeriodFromCommand'; import { ResolvedPlayer } from '../core/discord/resolvePlayerFromCommand'; -import { WARGAMING_APPLICATION_ID } from '../core/node/arguments'; +import { secrets } from '../core/node/secrets'; import { AccountInfo } from '../types/accountInfo'; import { Histories } from '../types/histories'; import { PlayerClanData } from '../types/playerClanData'; @@ -29,7 +29,7 @@ export default async function evolution( if (type === 'player') { const clan = ( await getWargamingResponse( - `https://api.wotblitz.${server}/wotb/clans/accountinfo/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}&extra=clan`, + `https://api.wotblitz.${server}/wotb/clans/accountinfo/?application_id=${secrets.WARGAMING_APPLICATION_ID}&account_id=${id}&extra=clan`, ) )[id]?.clan; @@ -79,7 +79,7 @@ export default async function evolution( const minX = Math.min(...xs); const accountInfo = await getWargamingResponse( - `https://api.wotblitz.${server}/wotb/account/info/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}`, + `https://api.wotblitz.${server}/wotb/account/info/?application_id=${secrets.WARGAMING_APPLICATION_ID}&account_id=${id}`, ); return ( diff --git a/src/renderers/fullStats.tsx b/src/renderers/fullStats.tsx index 21e798190..c6fc3a6d2 100644 --- a/src/renderers/fullStats.tsx +++ b/src/renderers/fullStats.tsx @@ -14,7 +14,7 @@ import getDiffedTankStats from '../core/blitzstars/getDiffedTankStats'; import { tankAverages } from '../core/blitzstars/tankAverages'; import { ResolvedPeriod } from '../core/discord/resolvePeriodFromCommand'; import { ResolvedPlayer } from '../core/discord/resolvePlayerFromCommand'; -import { WARGAMING_APPLICATION_ID } from '../core/node/arguments'; +import { secrets } from '../core/node/secrets'; import { AccountInfo, AllStats, @@ -37,7 +37,7 @@ export default async function fullStats( if (type === 'player') { const clan = ( await getWargamingResponse( - `https://api.wotblitz.${server}/wotb/clans/accountinfo/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}&extra=clan`, + `https://api.wotblitz.${server}/wotb/clans/accountinfo/?application_id=${secrets.WARGAMING_APPLICATION_ID}&account_id=${id}&extra=clan`, ) )[id]?.clan; @@ -133,7 +133,7 @@ export default async function fullStats( } const accountInfo = await getWargamingResponse( - `https://api.wotblitz.${server}/wotb/account/info/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}`, + `https://api.wotblitz.${server}/wotb/account/info/?application_id=${secrets.WARGAMING_APPLICATION_ID}&account_id=${id}`, ); return ( diff --git a/src/renderers/stats.tsx b/src/renderers/stats.tsx index b95db2528..8c0287cc4 100644 --- a/src/renderers/stats.tsx +++ b/src/renderers/stats.tsx @@ -14,7 +14,7 @@ import getDiffedTankStats from '../core/blitzstars/getDiffedTankStats'; import { tankAverages } from '../core/blitzstars/tankAverages'; import { ResolvedPeriod } from '../core/discord/resolvePeriodFromCommand'; import { ResolvedPlayer } from '../core/discord/resolvePlayerFromCommand'; -import { WARGAMING_APPLICATION_ID } from '../core/node/arguments'; +import { secrets } from '../core/node/secrets'; import { theme } from '../stitches.config'; import { AccountInfo, @@ -39,7 +39,7 @@ export default async function stats( if (type === 'player') { const clan = ( await getWargamingResponse( - `https://api.wotblitz.${server}/wotb/clans/accountinfo/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}&extra=clan`, + `https://api.wotblitz.${server}/wotb/clans/accountinfo/?application_id=${secrets.WARGAMING_APPLICATION_ID}&account_id=${id}&extra=clan`, ) )[id]?.clan; @@ -137,7 +137,7 @@ export default async function stats( } const accountInfo = await getWargamingResponse( - `https://api.wotblitz.${server}/wotb/account/info/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}`, + `https://api.wotblitz.${server}/wotb/account/info/?application_id=${secrets.WARGAMING_APPLICATION_ID}&account_id=${id}`, ); const overview = ( diff --git a/src/renderers/today.tsx b/src/renderers/today.tsx index dd0716979..d3afd0ba1 100644 --- a/src/renderers/today.tsx +++ b/src/renderers/today.tsx @@ -15,7 +15,7 @@ import getPeriodNow from '../core/blitzstars/getPeriodNow'; import getTimeDaysAgo from '../core/blitzstars/getTimeDaysAgo'; import { tankAverages } from '../core/blitzstars/tankAverages'; import { ResolvedPlayer } from '../core/discord/resolvePlayerFromCommand'; -import { WARGAMING_APPLICATION_ID } from '../core/node/arguments'; +import { secrets } from '../core/node/secrets'; import { AccountInfo, AllStats } from '../types/accountInfo'; import { PlayerClanData } from '../types/playerClanData'; import { PossiblyPromise } from '../types/possiblyPromise'; @@ -32,10 +32,10 @@ export default async function today( getPeriodNow(), ); const accountInfo = await getWargamingResponse( - `https://api.wotblitz.${server}/wotb/account/info/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}`, + `https://api.wotblitz.${server}/wotb/account/info/?application_id=${secrets.WARGAMING_APPLICATION_ID}&account_id=${id}`, ); const clanData = await getWargamingResponse( - `https://api.wotblitz.${server}/wotb/clans/accountinfo/?application_id=${WARGAMING_APPLICATION_ID}&account_id=${id}&extra=clan`, + `https://api.wotblitz.${server}/wotb/clans/accountinfo/?application_id=${secrets.WARGAMING_APPLICATION_ID}&account_id=${id}&extra=clan`, ); const careerTankStatsRaw = await getTankStats(server, id); const careerStats: Record = { diff --git a/src/routes/stats.tsx b/src/routes/stats.tsx index b9f79752a..b06a87a7a 100644 --- a/src/routes/stats.tsx +++ b/src/routes/stats.tsx @@ -11,7 +11,7 @@ export const statsRoute: RouteRegistry = { async handler(req) { const type = req.params.type as StatType; const player = resolvePlayerFromRequest(req); - const period = resolvePeriodFromRequest(player.server, req); + const period = resolvePeriodFromRequest(player.region, req); return await stats( type,