From 91dc45c8809da991a76cf2b522677a91ada4bdcf Mon Sep 17 00:00:00 2001 From: tresabhi Date: Fri, 4 Aug 2023 18:24:20 +0000 Subject: [PATCH] feat: added more mature periods --- src/commands/debug.ts | 2 +- src/commands/eligible.ts | 2 +- src/commands/evolution.tsx | 2 +- src/commands/fullStats.tsx | 2 +- src/commands/help.ts | 9 ++++--- src/commands/inactive.tsx | 2 +- src/commands/ownedTanks.tsx | 2 +- src/commands/ping.ts | 4 +-- src/commands/playerAchievements.ts | 2 +- src/commands/playerInfo.ts | 2 +- src/commands/ratings.tsx | 2 +- src/commands/searchClans.ts | 2 +- src/commands/searchPlayers.ts | 2 +- src/commands/searchTanks.ts | 2 +- src/commands/stats.tsx | 9 +++++-- src/commands/today.tsx | 2 +- src/commands/verify.ts | 2 +- src/core/blitzstars/getPeriodStart.ts | 4 +-- src/core/discord/addPeriodSubCommands.ts | 27 +++++++++---------- .../discord/addStatTypeSubCommandGroups.ts | 1 + src/core/discord/resolvePeriodFromCommand.ts | 25 +++++++---------- src/core/express/resolvePeriodFromURL.ts | 16 +++++------ .../handlers/autocomplete.ts | 4 +-- .../interactionCreate/handlers/button.tsx | 2 +- .../handlers/chatInputCommand.ts | 2 +- src/routes/stats.tsx | 2 +- src/routes/today.tsx | 2 +- 27 files changed, 68 insertions(+), 67 deletions(-) diff --git a/src/commands/debug.ts b/src/commands/debug.ts index 76bd7c807..fa7240883 100644 --- a/src/commands/debug.ts +++ b/src/commands/debug.ts @@ -12,7 +12,7 @@ const executionStart = new Date().getTime(); export const debugCommand: CommandRegistry = { inProduction: true, - inDevelopment: false, + inDevelopment: true, inPublic: true, command: new SlashCommandBuilder() diff --git a/src/commands/eligible.ts b/src/commands/eligible.ts index 06ad066fd..eadb83c0b 100644 --- a/src/commands/eligible.ts +++ b/src/commands/eligible.ts @@ -26,7 +26,7 @@ export const SKILLED_CLANS: Record = { export const eligibleCommand: CommandRegistry = { inProduction: true, - inDevelopment: false, + inDevelopment: true, inPublic: false, command: new SlashCommandBuilder() diff --git a/src/commands/evolution.tsx b/src/commands/evolution.tsx index 5da0c83b5..f2b1c0e50 100644 --- a/src/commands/evolution.tsx +++ b/src/commands/evolution.tsx @@ -28,7 +28,7 @@ export const evolutionCommand = new Promise( resolve({ inProduction: true, - inDevelopment: false, + inDevelopment: true, inPublic: true, command, diff --git a/src/commands/fullStats.tsx b/src/commands/fullStats.tsx index 689d958ba..7b3162f9e 100644 --- a/src/commands/fullStats.tsx +++ b/src/commands/fullStats.tsx @@ -27,7 +27,7 @@ export const fullStatsCommand = new Promise( resolve({ inProduction: true, - inDevelopment: false, + inDevelopment: true, inPublic: true, command, diff --git a/src/commands/help.ts b/src/commands/help.ts index 2052fd12a..a85d83319 100644 --- a/src/commands/help.ts +++ b/src/commands/help.ts @@ -30,7 +30,7 @@ function addDocsSubcommands(option: SlashCommandSubcommandsOnlyBuilder) { export const helpCommand: CommandRegistry = { inProduction: true, - inDevelopment: false, + inDevelopment: true, inPublic: true, command: addDocsSubcommands( @@ -44,13 +44,14 @@ export const helpCommand: CommandRegistry = { ), ), - handler(interaction) { + async handler(interaction) { const subcommand = interaction.options.getSubcommand(); if (subcommand === 'commands') { - return `# Commands\n\nBlitzkrieg offers the following commands:\n\n${COMMANDS_RAW.filter( - (registry) => registry.inPublic && registry.inProduction, + return `# Commands\n\nBlitzkrieg offers the following commands:\n\n${( + await Promise.all(COMMANDS_RAW) ) + .filter((registry) => registry.inPublic && registry.inProduction) .map( (registry) => `- \`/${registry.command.name}\`: ${registry.command.description}`, diff --git a/src/commands/inactive.tsx b/src/commands/inactive.tsx index 8784e03d7..1e8e4b947 100644 --- a/src/commands/inactive.tsx +++ b/src/commands/inactive.tsx @@ -18,7 +18,7 @@ const DEFAULT_THRESHOLD = 7; export const inactiveCommand: CommandRegistry = { inProduction: true, - inDevelopment: false, + inDevelopment: true, inPublic: true, command: new SlashCommandBuilder() diff --git a/src/commands/ownedTanks.tsx b/src/commands/ownedTanks.tsx index 82e338d6d..33e3031e4 100644 --- a/src/commands/ownedTanks.tsx +++ b/src/commands/ownedTanks.tsx @@ -45,7 +45,7 @@ const COMP_TANKS = [ export const ownedTanksCommand: CommandRegistry = { inProduction: true, - inDevelopment: false, + inDevelopment: true, inPublic: true, command: new SlashCommandBuilder() diff --git a/src/commands/ping.ts b/src/commands/ping.ts index 2005400f0..a9fcd5146 100644 --- a/src/commands/ping.ts +++ b/src/commands/ping.ts @@ -1,9 +1,9 @@ import { SlashCommandBuilder } from 'discord.js'; import { CommandRegistry } from '../events/interactionCreate'; -export const pingCommand: CommandRegistry = { +export const pingCommand: CommandRegistry = { inProduction: true, - inDevelopment: false, + inDevelopment: true, inPublic: true, handlesInteraction: true, diff --git a/src/commands/playerAchievements.ts b/src/commands/playerAchievements.ts index 60cdf86fa..2a81a2d2d 100644 --- a/src/commands/playerAchievements.ts +++ b/src/commands/playerAchievements.ts @@ -16,7 +16,7 @@ type SortBy = 'name' | 'count'; export const playerAchievementsCommand: CommandRegistry = { inProduction: true, - inDevelopment: false, + inDevelopment: true, inPublic: true, command: new SlashCommandBuilder() diff --git a/src/commands/playerInfo.ts b/src/commands/playerInfo.ts index 704daf3b0..ce26234c5 100644 --- a/src/commands/playerInfo.ts +++ b/src/commands/playerInfo.ts @@ -12,7 +12,7 @@ import { AccountInfo } from '../types/accountInfo'; export const playerInfoCommand: CommandRegistry = { inProduction: true, - inDevelopment: false, + inDevelopment: true, inPublic: true, command: new SlashCommandBuilder() diff --git a/src/commands/ratings.tsx b/src/commands/ratings.tsx index 98eac5b01..9c129dbed 100644 --- a/src/commands/ratings.tsx +++ b/src/commands/ratings.tsx @@ -121,7 +121,7 @@ export const DATABASE_REPO = { owner: 'tresabhi', repo: 'blitzkrieg-db' }; export const ratingsCommand: CommandRegistry = { inProduction: true, - inDevelopment: false, + inDevelopment: true, inPublic: true, command: new SlashCommandBuilder() diff --git a/src/commands/searchClans.ts b/src/commands/searchClans.ts index 01ca6061d..a7ca4e6f8 100644 --- a/src/commands/searchClans.ts +++ b/src/commands/searchClans.ts @@ -11,7 +11,7 @@ import { ClanList } from '../types/clanList'; export const searchClansCommand: CommandRegistry = { inProduction: true, - inDevelopment: false, + inDevelopment: true, inPublic: true, command: new SlashCommandBuilder() diff --git a/src/commands/searchPlayers.ts b/src/commands/searchPlayers.ts index 536d5fb18..b6c62b6d2 100644 --- a/src/commands/searchPlayers.ts +++ b/src/commands/searchPlayers.ts @@ -12,7 +12,7 @@ import { AccountList } from '../types/accountList'; export const searchPlayersCommand: CommandRegistry = { inProduction: true, - inDevelopment: false, + inDevelopment: true, inPublic: true, command: new SlashCommandBuilder() diff --git a/src/commands/searchTanks.ts b/src/commands/searchTanks.ts index f02b95c05..f2330aad5 100644 --- a/src/commands/searchTanks.ts +++ b/src/commands/searchTanks.ts @@ -8,7 +8,7 @@ import { CommandRegistry } from '../events/interactionCreate'; export const searchTanksCommand: CommandRegistry = { inProduction: true, - inDevelopment: false, + inDevelopment: true, inPublic: true, command: new SlashCommandBuilder() diff --git a/src/commands/stats.tsx b/src/commands/stats.tsx index cf6ca65f5..c4de71558 100644 --- a/src/commands/stats.tsx +++ b/src/commands/stats.tsx @@ -37,7 +37,11 @@ export const statsCommand = new Promise(async (resolve) => { true, ) as StatType; const player = await resolvePlayerFromCommand(interaction); - const period = resolvePeriodFromCommand(player.region, interaction); + const resolvedPeriod = resolvePeriodFromCommand( + player.region, + interaction, + ); + const period = interaction.options.getString('period'); const tankIdRaw = interaction.options.getString('tank')!; const tankId = commandGroup === 'tank' ? await resolveTankId(tankIdRaw) : null; @@ -48,6 +52,7 @@ export const statsCommand = new Promise(async (resolve) => { tankId, start, end, + period, }); const { nickname } = ( await getWargamingResponse( @@ -56,7 +61,7 @@ export const statsCommand = new Promise(async (resolve) => { )[player.id]; return [ - await stats(commandGroup, period, player, tankId), + await stats(commandGroup, resolvedPeriod, player, tankId), primaryButton(path, 'Refresh'), linkButton(`${CYCLIC_API}/${path}`, 'Embed'), linkButton( diff --git a/src/commands/today.tsx b/src/commands/today.tsx index 9a5a7a037..f8505c682 100644 --- a/src/commands/today.tsx +++ b/src/commands/today.tsx @@ -15,7 +15,7 @@ import { AccountInfo } from '../types/accountInfo'; export const todayCommand: CommandRegistry = { inProduction: true, - inDevelopment: false, + inDevelopment: true, inPublic: true, command: new SlashCommandBuilder() diff --git a/src/commands/verify.ts b/src/commands/verify.ts index 1dea0918f..c6682c1cf 100644 --- a/src/commands/verify.ts +++ b/src/commands/verify.ts @@ -14,7 +14,7 @@ import { PlayerClanData } from '../types/playerClanData'; export const verifyCommand: CommandRegistry = { inProduction: true, - inDevelopment: false, + inDevelopment: true, inPublic: true, command: new SlashCommandBuilder() diff --git a/src/core/blitzstars/getPeriodStart.ts b/src/core/blitzstars/getPeriodStart.ts index 330997209..118084a35 100644 --- a/src/core/blitzstars/getPeriodStart.ts +++ b/src/core/blitzstars/getPeriodStart.ts @@ -1,7 +1,7 @@ import { Region } from '../../constants/regions'; -import { PeriodOption } from '../discord/addPeriodSubCommands'; +import { PeriodSize } from '../discord/addPeriodSubCommands'; import getTimeDaysAgo from './getTimeDaysAgo'; -export default function getPeriodStart(region: Region, period: PeriodOption) { +export default function getPeriodStart(region: Region, period: PeriodSize) { return period === 'career' ? 0 : getTimeDaysAgo(region, parseInt(period)); } diff --git a/src/core/discord/addPeriodSubCommands.ts b/src/core/discord/addPeriodSubCommands.ts index f4261acc4..cd28a4c4b 100644 --- a/src/core/discord/addPeriodSubCommands.ts +++ b/src/core/discord/addPeriodSubCommands.ts @@ -2,10 +2,9 @@ import { SlashCommandSubcommandBuilder, SlashCommandSubcommandGroupBuilder, } from 'discord.js'; -import { PERIOD_NAMES } from '../discord/resolvePeriodFromCommand'; -export type PeriodSubcommand = 'period' | 'custom'; -export type PeriodOption = `${number}` | 'career'; +export type PeriodType = 'period' | 'custom'; +export type PeriodSize = `${number}` | 'career'; export default function addPeriodSubCommands( option: SlashCommandSubcommandGroupBuilder, @@ -17,18 +16,18 @@ export default function addPeriodSubCommands( .addSubcommand((option) => extra( option - .setName('period' satisfies PeriodSubcommand) - .setDescription(PERIOD_NAMES.period) + .setName('period' satisfies PeriodType) + .setDescription("Preset period's statistics") .addStringOption((option) => option .setName('period') .setDescription('A preset period') .setChoices( - { name: 'Today', value: '1' satisfies PeriodOption }, - { name: '30 days', value: '30' satisfies PeriodOption }, - { name: '60 days', value: '60' satisfies PeriodOption }, - { name: '90 days', value: '90' satisfies PeriodOption }, - { name: 'Career', value: 'career' satisfies PeriodOption }, + { name: 'Today', value: '1' satisfies PeriodSize }, + { name: '30 days', value: '30' satisfies PeriodSize }, + { name: '60 days', value: '60' satisfies PeriodSize }, + { name: '90 days', value: '90' satisfies PeriodSize }, + { name: 'Career', value: 'career' satisfies PeriodSize }, ) .setRequired(true), ), @@ -37,19 +36,19 @@ export default function addPeriodSubCommands( .addSubcommand((option) => extra( option - .setName('custom' satisfies PeriodSubcommand) - .setDescription(PERIOD_NAMES.custom) + .setName('custom' satisfies PeriodType) + .setDescription("Custom period's statistics") .addIntegerOption((option) => option .setName('start') - .setDescription('Start of period in days ago from today') + .setDescription('Days ago from today') .setMinValue(0) .setRequired(true), ) .addIntegerOption((option) => option .setName('end') - .setDescription('End of period in days ago from today') + .setDescription('Days ago from today') .setMinValue(0) .setRequired(true), ), diff --git a/src/core/discord/addStatTypeSubCommandGroups.ts b/src/core/discord/addStatTypeSubCommandGroups.ts index 86c91d0b8..e9e5dbb01 100644 --- a/src/core/discord/addStatTypeSubCommandGroups.ts +++ b/src/core/discord/addStatTypeSubCommandGroups.ts @@ -29,6 +29,7 @@ export default async function addStatTypeSubCommandGroups( .addSubcommandGroup((option) => addPeriodSubCommands(option, (option) => option + .addStringOption(addUsernameChoices) .addStringOption((option) => { option .setName('nation') diff --git a/src/core/discord/resolvePeriodFromCommand.ts b/src/core/discord/resolvePeriodFromCommand.ts index db4fb1c7f..dc172292b 100644 --- a/src/core/discord/resolvePeriodFromCommand.ts +++ b/src/core/discord/resolvePeriodFromCommand.ts @@ -3,17 +3,11 @@ import { Region } from '../../constants/regions'; import getPeriodNow from '../blitzstars/getPeriodNow'; import getPeriodStart from '../blitzstars/getPeriodStart'; import getTimeDaysAgo from '../blitzstars/getTimeDaysAgo'; -import { PeriodSubcommand } from '../discord/addPeriodSubCommands'; +import { PeriodSize, PeriodType } from '../discord/addPeriodSubCommands'; -export const PERIOD_NAMES: Record = { - period: "Preset period's statistics", - custom: "Custom period's statistics", -}; - -export const EVOLUTION_PERIOD_NAMES: Record = { - period: "Preset period's evolution", - custom: "Custom period's evolution", -}; +export function getPeriodOptionName(period: PeriodSize) { + return period === 'career' ? 'Career' : `${period}-day`; +} export interface ResolvedPeriod { statsName: string; @@ -30,9 +24,10 @@ export default function resolvePeriodFromCommand( let evolutionName: string; let start: number; let end: number; - const period = interaction.options.getSubcommand() as PeriodSubcommand; + const periodSubcommand = interaction.options.getSubcommand() as PeriodType; + const periodOption = interaction.options.getString('period') as PeriodSize; - if (period === 'custom') { + if (periodSubcommand === 'custom') { const startRaw = interaction.options.getInteger('start')!; const endRaw = interaction.options.getInteger('end')!; const startMin = Math.min(startRaw, endRaw); @@ -43,9 +38,9 @@ export default function resolvePeriodFromCommand( start = getTimeDaysAgo(server, startMin); end = getTimeDaysAgo(server, endMax); } else { - statsName = PERIOD_NAMES[period]; - evolutionName = EVOLUTION_PERIOD_NAMES[period]; - start = getPeriodStart(server, period); + statsName = `${getPeriodOptionName(periodOption)} Statistics`; + evolutionName = `${getPeriodOptionName(periodOption)} Evolution`; + start = getPeriodStart(server, periodOption); end = getPeriodNow(); } diff --git a/src/core/express/resolvePeriodFromURL.ts b/src/core/express/resolvePeriodFromURL.ts index a4bee04fe..d9cfc564e 100644 --- a/src/core/express/resolvePeriodFromURL.ts +++ b/src/core/express/resolvePeriodFromURL.ts @@ -2,11 +2,10 @@ import { Region } from '../../constants/regions'; import getPeriodNow from '../blitzstars/getPeriodNow'; import getPeriodStart from '../blitzstars/getPeriodStart'; import getTimeDaysAgo from '../blitzstars/getTimeDaysAgo'; -import { PeriodSubcommand } from '../discord/addPeriodSubCommands'; +import { PeriodSize, PeriodType } from '../discord/addPeriodSubCommands'; import { - EVOLUTION_PERIOD_NAMES, - PERIOD_NAMES, ResolvedPeriod, + getPeriodOptionName, } from '../discord/resolvePeriodFromCommand'; export default function resolvePeriodFromURL( @@ -19,9 +18,10 @@ export default function resolvePeriodFromURL( let end: number; const url = new URL(urlString); const path = url.pathname.split('/').filter(Boolean); - const period = path[path.length - 1] as PeriodSubcommand; + const periodSubcommand = path[path.length - 1] as PeriodType; + const periodOption = url.searchParams.get('period') as PeriodSize; - if (period === 'custom') { + if (periodSubcommand === 'custom') { const startRaw = parseInt(url!.searchParams.get('start')!); const endRaw = parseInt(url!.searchParams.get('end')!); const startMin = Math.min(startRaw, endRaw); @@ -32,9 +32,9 @@ export default function resolvePeriodFromURL( start = getTimeDaysAgo(server, startMin); end = getTimeDaysAgo(server, endMax); } else { - statsName = PERIOD_NAMES[period]; - evolutionName = EVOLUTION_PERIOD_NAMES[period]; - start = getPeriodStart(server, period); + statsName = `${getPeriodOptionName(periodOption)} Statistics`; + evolutionName = `${getPeriodOptionName(periodOption)} Evolution`; + start = getPeriodStart(server, periodOption); end = getPeriodNow(); } diff --git a/src/events/interactionCreate/handlers/autocomplete.ts b/src/events/interactionCreate/handlers/autocomplete.ts index af4b81bed..c688cd491 100644 --- a/src/events/interactionCreate/handlers/autocomplete.ts +++ b/src/events/interactionCreate/handlers/autocomplete.ts @@ -1,10 +1,10 @@ import { AutocompleteInteraction, CacheType } from 'discord.js'; import { commands } from '..'; -export default function handleAutocomplete( +export default async function handleAutocomplete( interaction: AutocompleteInteraction, ) { - const command = commands[interaction.commandName]; + const command = (await commands)[interaction.commandName]; if (!command) { console.error(`No command matching ${interaction.commandName} was found.`); diff --git a/src/events/interactionCreate/handlers/button.tsx b/src/events/interactionCreate/handlers/button.tsx index 7fc55ae21..cac6fddb5 100644 --- a/src/events/interactionCreate/handlers/button.tsx +++ b/src/events/interactionCreate/handlers/button.tsx @@ -11,7 +11,7 @@ export default async function handleButton( const url = new URL(`${CYCLIC_API}/${interaction.customId}`); const commandName = url.pathname.split('/')[1]; - const button = commands[commandName]?.button; + const button = (await commands)[commandName]?.button; if (!button) { throwError(`Button handler not found for "${commandName}"`); diff --git a/src/events/interactionCreate/handlers/chatInputCommand.ts b/src/events/interactionCreate/handlers/chatInputCommand.ts index 18b81dcb6..e603b4048 100644 --- a/src/events/interactionCreate/handlers/chatInputCommand.ts +++ b/src/events/interactionCreate/handlers/chatInputCommand.ts @@ -16,7 +16,7 @@ import { handleError } from '../../error'; export default async function handleChatInputCommand( interaction: ChatInputCommandInteraction, ) { - const registry = commands[interaction.commandName]; + const registry = (await commands)[interaction.commandName]; console.log(interaction.toString()); await interaction.deferReply(); diff --git a/src/routes/stats.tsx b/src/routes/stats.tsx index b06a87a7a..a096d4181 100644 --- a/src/routes/stats.tsx +++ b/src/routes/stats.tsx @@ -6,7 +6,7 @@ import { RouteRegistry } from '../server'; export const statsRoute: RouteRegistry = { route: '/stats/:type/:period', inProduction: true, - inDevelopment: false, + inDevelopment: true, async handler(req) { const type = req.params.type as StatType; diff --git a/src/routes/today.tsx b/src/routes/today.tsx index 8a5f193ed..82f2631a7 100644 --- a/src/routes/today.tsx +++ b/src/routes/today.tsx @@ -5,7 +5,7 @@ import { RouteRegistry } from '../server'; export const todayRoute: RouteRegistry = { route: '/today', inProduction: true, - inDevelopment: false, + inDevelopment: true, async handler(req) { const player = resolvePlayerFromRequest(req);