diff --git a/src/bot.ts b/src/bot.ts index a8a7c9cca..8d66081c8 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/args.js'; +import { DISCORD_TOKEN } from './core/node/arguments.js'; import { registerErrorHandlers } from './events/error.js'; import guildMemberAdd from './events/guildMemberAdd.js'; import interactionCreate from './events/interactionCreate/index.js'; diff --git a/src/commands/eligible.ts b/src/commands/eligible.ts index faa00c9e7..30ec318cf 100644 --- a/src/commands/eligible.ts +++ b/src/commands/eligible.ts @@ -12,7 +12,7 @@ import embedNegative from '../core/discord/embedNegative.js'; import embedPositive from '../core/discord/embedPositive.js'; import markdownTable, { TableInput } from '../core/discord/markdownTable.js'; import resolvePlayerFromCommand from '../core/discord/resolvePlayerFromCommand.js'; -import { WARGAMING_APPLICATION_ID } from '../core/node/args.js'; +import { WARGAMING_APPLICATION_ID } from '../core/node/arguments.js'; import { CommandRegistry } from '../events/interactionCreate/index.js'; import { AccountInfo } from '../types/accountInfo.js'; diff --git a/src/commands/inactive.tsx b/src/commands/inactive.tsx index d47a52278..89bcb0d42 100644 --- a/src/commands/inactive.tsx +++ b/src/commands/inactive.tsx @@ -9,7 +9,7 @@ import getBlitzClan from '../core/blitz/getBlitzClan.js'; import getWargamingResponse from '../core/blitz/getWargamingResponse.js'; import addClanChoices from '../core/discord/addClanChoices.js'; import autocompleteClan from '../core/discord/autocompleteClan.js'; -import { WARGAMING_APPLICATION_ID } from '../core/node/args.js'; +import { WARGAMING_APPLICATION_ID } from '../core/node/arguments.js'; import { CommandRegistry } from '../events/interactionCreate/index.js'; import { AccountInfo } from '../types/accountInfo.js'; import { ClanInfo } from '../types/clanInfo.js'; diff --git a/src/commands/ownedtanks.tsx b/src/commands/ownedtanks.tsx index 5dd529765..843eafa08 100644 --- a/src/commands/ownedtanks.tsx +++ b/src/commands/ownedtanks.tsx @@ -15,7 +15,7 @@ import { import addUsernameChoices from '../core/discord/addUsernameChoices.js'; import autocompleteUsername from '../core/discord/autocompleteUsername.js'; import resolvePlayerFromCommand from '../core/discord/resolvePlayerFromCommand.js'; -import { WARGAMING_APPLICATION_ID } from '../core/node/args.js'; +import { WARGAMING_APPLICATION_ID } from '../core/node/arguments.js'; import { CommandRegistry } from '../events/interactionCreate/index.js'; import { AccountInfo } from '../types/accountInfo.js'; import { PlayerClanData } from '../types/playerClanData.js'; diff --git a/src/commands/playerachievements.ts b/src/commands/playerachievements.ts index 0f566c6db..242826988 100644 --- a/src/commands/playerachievements.ts +++ b/src/commands/playerachievements.ts @@ -9,7 +9,7 @@ import markdownTable, { TableInputEntry, } from '../core/discord/markdownTable.js'; import resolvePlayerFromCommand from '../core/discord/resolvePlayerFromCommand.js'; -import { WARGAMING_APPLICATION_ID } from '../core/node/args.js'; +import { WARGAMING_APPLICATION_ID } from '../core/node/arguments.js'; import { CommandRegistry } from '../events/interactionCreate/index.js'; import { AccountAchievements } from '../types/accountAchievements.js'; import { AccountInfo } from '../types/accountInfo.js'; diff --git a/src/commands/playerinfo.ts b/src/commands/playerinfo.ts index 1066e7e60..7e34552e3 100644 --- a/src/commands/playerinfo.ts +++ b/src/commands/playerinfo.ts @@ -6,7 +6,7 @@ import autocompleteUsername from '../core/discord/autocompleteUsername.js'; import embedInfo from '../core/discord/embedInfo.js'; import markdownTable from '../core/discord/markdownTable.js'; import resolvePlayerFromCommand from '../core/discord/resolvePlayerFromCommand.js'; -import { WARGAMING_APPLICATION_ID } from '../core/node/args.js'; +import { WARGAMING_APPLICATION_ID } from '../core/node/arguments.js'; import { CommandRegistry } from '../events/interactionCreate/index.js'; import { AccountInfo } from '../types/accountInfo.js'; diff --git a/src/commands/searchclans.ts b/src/commands/searchclans.ts index d29675fe7..56244b025 100644 --- a/src/commands/searchclans.ts +++ b/src/commands/searchclans.ts @@ -5,7 +5,7 @@ import getWargamingResponse from '../core/blitz/getWargamingResponse.js'; import addClanChoices from '../core/discord/addClanChoices.js'; import addServerChoices from '../core/discord/addServerChoices.js'; import embedInfo from '../core/discord/embedInfo.js'; -import { WARGAMING_APPLICATION_ID } from '../core/node/args.js'; +import { WARGAMING_APPLICATION_ID } from '../core/node/arguments.js'; import { CommandRegistry } from '../events/interactionCreate/index.js'; import { ClanList } from '../types/clanList.js'; diff --git a/src/commands/searchplayers.ts b/src/commands/searchplayers.ts index aad73abc1..fba77980e 100644 --- a/src/commands/searchplayers.ts +++ b/src/commands/searchplayers.ts @@ -6,7 +6,7 @@ import { usernamePattern } from '../core/blitz/listPlayers.js'; import addServerChoices from '../core/discord/addServerChoices.js'; import addUsernameChoices from '../core/discord/addUsernameChoices.js'; import embedInfo from '../core/discord/embedInfo.js'; -import { WARGAMING_APPLICATION_ID } from '../core/node/args.js'; +import { WARGAMING_APPLICATION_ID } from '../core/node/arguments.js'; import { CommandRegistry } from '../events/interactionCreate/index.js'; import { AccountList } from '../types/accountList.js'; diff --git a/src/commands/verify.ts b/src/commands/verify.ts index 8ea2582c9..5a3e3402c 100644 --- a/src/commands/verify.ts +++ b/src/commands/verify.ts @@ -7,7 +7,7 @@ import autocompleteUsername from '../core/discord/autocompleteUsername.js'; import embedNegative from '../core/discord/embedNegative.js'; import embedPositive from '../core/discord/embedPositive.js'; import resolvePlayerFromCommand from '../core/discord/resolvePlayerFromCommand.js'; -import { WARGAMING_APPLICATION_ID } from '../core/node/args.js'; +import { WARGAMING_APPLICATION_ID } from '../core/node/arguments.js'; import { CommandRegistry } from '../events/interactionCreate/index.js'; import { AccountInfo } from '../types/accountInfo.js'; import { PlayerClanData } from '../types/playerClanData.js'; diff --git a/src/core/blitz/getBlitzClan.ts b/src/core/blitz/getBlitzClan.ts index 3e1cf5b62..d8e7c5311 100644 --- a/src/core/blitz/getBlitzClan.ts +++ b/src/core/blitz/getBlitzClan.ts @@ -1,6 +1,6 @@ import { BlitzServer } from '../../constants/servers.js'; import { serverAndIdPattern } from '../discord/resolvePlayerFromCommand.js'; -import errorWithCause from '../node/errorWithCause.js'; +import throwError from '../node/throwError.js'; import listClans from './listClans.js'; export default async function getBlitzClan(clan: string) { @@ -13,7 +13,7 @@ export default async function getBlitzClan(clan: string) { if (accounts[0]) { return { server: accounts[0].server, id: accounts[0].clan_id }; } else { - throw errorWithCause( + throw throwError( 'Could not find clan', `I couldn't find clan \`${clan}\`. Try selecting a username from the search result.`, ); diff --git a/src/core/blitz/getTankStats.ts b/src/core/blitz/getTankStats.ts index 5579400bb..2f75efd05 100644 --- a/src/core/blitz/getTankStats.ts +++ b/src/core/blitz/getTankStats.ts @@ -1,7 +1,7 @@ import { BLITZ_SERVERS, BlitzServer } from '../../constants/servers.js'; import { TanksStats } from '../../types/tanksStats.js'; -import { WARGAMING_APPLICATION_ID } from '../node/args.js'; -import errorWithCause from '../node/errorWithCause.js'; +import { WARGAMING_APPLICATION_ID } from '../node/arguments.js'; +import throwError from '../node/throwError.js'; import getWargamingResponse from './getWargamingResponse.js'; export default async function getTankStats(server: BlitzServer, id: number) { @@ -10,7 +10,7 @@ export default async function getTankStats(server: BlitzServer, id: number) { ); if (tankStats[id] === null) { - throw errorWithCause( + throw throwError( 'No tank stats available', `Wargaming says there is no tank stats for this account. This account may not have any battles/tanks or exist in the ${BLITZ_SERVERS[server]} server.`, ); diff --git a/src/core/blitz/getWargamingResponse.ts b/src/core/blitz/getWargamingResponse.ts index 76b012190..78edbaa93 100644 --- a/src/core/blitz/getWargamingResponse.ts +++ b/src/core/blitz/getWargamingResponse.ts @@ -1,4 +1,4 @@ -import errorWithCause from '../node/errorWithCause.js'; +import throwError from '../node/throwError.js'; export type WargamingResponse = | { @@ -21,7 +21,7 @@ export default async function getWargamingResponse( if (parsed.status === 'ok') { return parsed.data; } else { - throw errorWithCause( + throw throwError( `Wargaming response error status:"${parsed.status}"`, `Message: "${parsed.error.message}"\nURL: "${url}"`, ); diff --git a/src/core/blitz/listClans.ts b/src/core/blitz/listClans.ts index f4b8a3364..d751e2de7 100644 --- a/src/core/blitz/listClans.ts +++ b/src/core/blitz/listClans.ts @@ -1,6 +1,6 @@ import { BlitzServer } from '../../constants/servers.js'; import { Clan, ClanList } from '../../types/clanList.js'; -import { WARGAMING_APPLICATION_ID } from '../node/args.js'; +import { WARGAMING_APPLICATION_ID } from '../node/arguments.js'; import getWargamingResponse from './getWargamingResponse.js'; export type ClanListWithServer = (Clan & { diff --git a/src/core/blitz/listPlayers.ts b/src/core/blitz/listPlayers.ts index 64e4faf28..5820e59d5 100644 --- a/src/core/blitz/listPlayers.ts +++ b/src/core/blitz/listPlayers.ts @@ -1,6 +1,6 @@ import { BlitzServer } from '../../constants/servers.js'; import { Account, AccountList } from '../../types/accountList.js'; -import { WARGAMING_APPLICATION_ID } from '../node/args.js'; +import { WARGAMING_APPLICATION_ID } from '../node/arguments.js'; import getWargamingResponse from './getWargamingResponse.js'; export type AccountListWithServer = (Account & { diff --git a/src/core/blitz/resolveTankId.ts b/src/core/blitz/resolveTankId.ts index 2beee53dd..89da65c1d 100644 --- a/src/core/blitz/resolveTankId.ts +++ b/src/core/blitz/resolveTankId.ts @@ -1,5 +1,5 @@ import { go } from 'fuzzysort'; -import errorWithCause from '../node/errorWithCause.js'; +import throwError from '../node/throwError.js'; import { TANKS, tankopedia } from './tankopedia.js'; export default function resolveTankId(tank: string) { @@ -9,7 +9,7 @@ export default function resolveTankId(tank: string) { const searchResult = go(tank, TANKS, { keys: ['name'], limit: 1 }); if (searchResult.length === 0) { - throw errorWithCause( + throw throwError( 'Tank not found', `Could not find tank by the name "${tank}".`, ); @@ -20,7 +20,7 @@ export default function resolveTankId(tank: string) { if (tankopedia[number]) { return number; } else { - throw errorWithCause( + throw throwError( 'Tank not found', `Could not find tank by the ID "${number}".`, ); diff --git a/src/core/blitzstars/getPlayerHistories.ts b/src/core/blitzstars/getPlayerHistories.ts index aed5d135b..72e6aa852 100644 --- a/src/core/blitzstars/getPlayerHistories.ts +++ b/src/core/blitzstars/getPlayerHistories.ts @@ -7,7 +7,7 @@ import { getHistoriesDefaultOptions, } from '../../types/histories.js'; import getWargamingResponse from '../blitz/getWargamingResponse.js'; -import { WARGAMING_APPLICATION_ID } from '../node/args.js'; +import { WARGAMING_APPLICATION_ID } from '../node/arguments.js'; import { emptyAllStats } from './getTankStatsDiffed.js'; export interface PlayerHistoryRaw { diff --git a/src/core/node/psa.ts b/src/core/discord/psa.ts similarity index 100% rename from src/core/node/psa.ts rename to src/core/discord/psa.ts diff --git a/src/core/discord/resolvePlayerFromCommand.ts b/src/core/discord/resolvePlayerFromCommand.ts index 3ed7cb396..084b8c52f 100644 --- a/src/core/discord/resolvePlayerFromCommand.ts +++ b/src/core/discord/resolvePlayerFromCommand.ts @@ -3,7 +3,7 @@ import { BlitzServer } from '../../constants/servers.js'; import listPlayers, { usernamePatternWithoutPosition, } from '../blitz/listPlayers.js'; -import errorWithCause from '../node/errorWithCause.js'; +import throwError from '../node/throwError.js'; export const serverAndIdPattern = /(com|eu|asia)\/[0-9]+/; @@ -22,7 +22,7 @@ export default async function resolvePlayerFromCommand( const username = commandUsername ?? displayName; if (username === undefined) { - throw errorWithCause( + throw throwError( 'Username could not be inferred', "You didn't provide me a username in the command nor was I able to infer your Blitz username from your Discord nickname. Try providing a username manually in the command or using the `/verify` command to set your Discord nickname to your Blitz username.", ); @@ -43,7 +43,7 @@ export default async function resolvePlayerFromCommand( id: accounts[0].account_id, } satisfies ResolvedPlayer; } else { - throw errorWithCause( + throw throwError( 'Could not find user', `I couldn't find user \`${username}\`. Try providing a valid username manually or using the \`/verify\` command to set your Discord nickname to your Blitz username.`, ); diff --git a/src/core/node/args.ts b/src/core/node/arguments.ts similarity index 100% rename from src/core/node/args.ts rename to src/core/node/arguments.ts diff --git a/src/core/node/errorWithCause.ts b/src/core/node/errorWithCause.ts deleted file mode 100644 index 0e2ef81c7..000000000 --- a/src/core/node/errorWithCause.ts +++ /dev/null @@ -1,3 +0,0 @@ -export default function errorWithCause(message: string, cause: string) { - return new Error(message, { cause }); -} diff --git a/src/core/node/isDev.ts b/src/core/node/isDev.ts index 3a6db04c9..9167667ba 100644 --- a/src/core/node/isDev.ts +++ b/src/core/node/isDev.ts @@ -1,5 +1,3 @@ -const isDevBoolean = process.env.NODE_ENV === 'development'; - export default function isDev() { - return isDevBoolean; + return process.env.NODE_ENV === 'development'; } diff --git a/src/core/node/isNumber.ts b/src/core/node/isNumber.ts index 930922ecb..3ad11be0a 100644 --- a/src/core/node/isNumber.ts +++ b/src/core/node/isNumber.ts @@ -1,3 +1,3 @@ export default function isNumber(value: any) { - return typeof value === 'number' && !isNaN(value) && isFinite(value); + return typeof value === 'number' && isFinite(value); } diff --git a/src/core/node/throwError.ts b/src/core/node/throwError.ts new file mode 100644 index 000000000..2aa57dec7 --- /dev/null +++ b/src/core/node/throwError.ts @@ -0,0 +1,3 @@ +export default function throwError(message: string, cause: string) { + return new Error(message, { cause }); +} diff --git a/src/events/interactionCreate/handlers/chatInputCommand.ts b/src/events/interactionCreate/handlers/chatInputCommand.ts index 090af20b7..3422b8121 100644 --- a/src/events/interactionCreate/handlers/chatInputCommand.ts +++ b/src/events/interactionCreate/handlers/chatInputCommand.ts @@ -8,7 +8,7 @@ import { import embedNegative from '../../../core/discord/embedNegative.js'; import normalizeInteractionReturnable from '../../../core/discord/normalizeInteractionReturnable.js'; import embedWarning from '../../../core/discord/embedWarning.js'; -import { psa } from '../../../core/node/psa.js'; +import { psa } from '../../../core/discord/psa.js'; import { handleError } from '../../error.js'; import { commands } from '../index.js'; diff --git a/src/events/interactionCreate/index.ts b/src/events/interactionCreate/index.ts index 4a7ef7b29..b70b6da24 100644 --- a/src/events/interactionCreate/index.ts +++ b/src/events/interactionCreate/index.ts @@ -28,7 +28,7 @@ import searchtanks from '../../commands/searchtanks.js'; import stats from '../../commands/stats.js'; import today from '../../commands/today.js'; import verify from '../../commands/verify.js'; -import { DISCORD_TOKEN } from '../../core/node/args.js'; +import { DISCORD_TOKEN } from '../../core/node/arguments.js'; import getClientId from '../../core/node/getClientId.js'; import isDev from '../../core/node/isDev.js'; import handleAutocomplete from './handlers/autocomplete.js'; diff --git a/src/renderers/evolution.tsx b/src/renderers/evolution.tsx index 9b1af6974..cff891b6c 100644 --- a/src/renderers/evolution.tsx +++ b/src/renderers/evolution.tsx @@ -11,7 +11,7 @@ import getPlayerHistories from '../core/blitzstars/getPlayerHistories.js'; import getTankHistories from '../core/blitzstars/getTankHistories.js'; import { ResolvedPeriod } from '../core/discord/resolvePeriodFromCommand.js'; import { ResolvedPlayer } from '../core/discord/resolvePlayerFromCommand.js'; -import { WARGAMING_APPLICATION_ID } from '../core/node/args.js'; +import { WARGAMING_APPLICATION_ID } from '../core/node/arguments.js'; import { AccountInfo } from '../types/accountInfo.js'; import { Histories } from '../types/histories.js'; import { PlayerClanData } from '../types/playerClanData.js'; diff --git a/src/renderers/stats.tsx b/src/renderers/stats.tsx index b8257dd38..13e5a6235 100644 --- a/src/renderers/stats.tsx +++ b/src/renderers/stats.tsx @@ -14,7 +14,7 @@ import getTankStatsDiffed from '../core/blitzstars/getTankStatsDiffed.js'; import { tankAverages } from '../core/blitzstars/tankAverages.js'; import { ResolvedPeriod } from '../core/discord/resolvePeriodFromCommand.js'; import { ResolvedPlayer } from '../core/discord/resolvePlayerFromCommand.js'; -import { WARGAMING_APPLICATION_ID } from '../core/node/args.js'; +import { WARGAMING_APPLICATION_ID } from '../core/node/arguments.js'; import { AccountInfo, AllStats, diff --git a/src/renderers/today.tsx b/src/renderers/today.tsx index b224921dc..a880edffc 100644 --- a/src/renderers/today.tsx +++ b/src/renderers/today.tsx @@ -15,7 +15,7 @@ import getTankStatsDiffed from '../core/blitzstars/getTankStatsDiffed.js'; import getTimeDaysAgo from '../core/blitzstars/getTimeDaysAgo.js'; import { tankAverages } from '../core/blitzstars/tankAverages.js'; import { ResolvedPlayer } from '../core/discord/resolvePlayerFromCommand.js'; -import { WARGAMING_APPLICATION_ID } from '../core/node/args.js'; +import { WARGAMING_APPLICATION_ID } from '../core/node/arguments.js'; import { AccountInfo, AllStats } from '../types/accountInfo.js'; import { PlayerClanData } from '../types/playerClanData.js';