diff --git a/packages/bot/src/commands/ownedTanks.tsx b/packages/bot/src/commands/ownedTanks.tsx index 9b6663b28..4f98f9781 100644 --- a/packages/bot/src/commands/ownedTanks.tsx +++ b/packages/bot/src/commands/ownedTanks.tsx @@ -6,7 +6,6 @@ import { getAccountInfo, getTankStats, } from '@blitzkit/core'; -import { chunk } from 'lodash-es'; import markdownEscape from 'markdown-escape'; import { gameDefinitions, @@ -15,13 +14,12 @@ import { import { addTierChoices } from '../core/discord/addTierChoices'; import { addUsernameChoices } from '../core/discord/addUsernameChoices'; import { autocompleteUsername } from '../core/discord/autocompleteUsername'; +import { chunkLines } from '../core/discord/chunkLines'; import { createLocalizedCommand } from '../core/discord/createLocalizedCommand'; import { resolvePlayerFromCommand } from '../core/discord/resolvePlayerFromCommand'; import { translator } from '../core/localization/translator'; import { CommandRegistry } from '../events/interactionCreate'; -const TANKS_PER_MESSAGE = 64; - export const ownedTanksCommand = new Promise((resolve) => { resolve({ command: createLocalizedCommand('owned-tanks') @@ -61,7 +59,11 @@ export const ownedTanksCommand = new Promise((resolve) => { const awaitedTankDefinitions = await tankDefinitions; const awaitedGameDefinitions = await gameDefinitions; - const lines = tankStats + const title = translate('bot.commands.owned_tanks.body.title', [ + markdownEscape(accountInfo.nickname), + TIER_ROMAN_NUMERALS[tier], + ]); + const tankList = tankStats .map(({ tank_id }) => awaitedTankDefinitions.tanks[tank_id]) .filter((tank) => tank.tier === tier) .sort( @@ -73,16 +75,13 @@ export const ownedTanksCommand = new Promise((resolve) => { awaitedGameDefinitions.nations.indexOf(a.nation) - awaitedGameDefinitions.nations.indexOf(b.nation), ) - .map((tank) => `${flags[tank.nation]} ${markdownEscape(tank.name)}`); + .map( + (tank) => + `[${flags[tank.nation]} ${markdownEscape(tank.name)}]()`, + ); + const lines = [title, ...tankList]; - return chunk(lines, TANKS_PER_MESSAGE).map((lines, index) => - index === 0 - ? `${translate('bot.commands.owned_tanks.body.title', [ - markdownEscape(accountInfo.nickname), - TIER_ROMAN_NUMERALS[tier], - ])}${lines.join('\n')}` - : lines.join('\n'), - ); + return chunkLines(lines); }, autocomplete: autocompleteUsername, diff --git a/packages/bot/src/core/discord/chunkLines.ts b/packages/bot/src/core/discord/chunkLines.ts new file mode 100644 index 000000000..dfcdef6d0 --- /dev/null +++ b/packages/bot/src/core/discord/chunkLines.ts @@ -0,0 +1,17 @@ +export function chunkLines(lines: string[], joiner = '\n', maxSize = 2000) { + const chunks = ['']; + + lines.map((line) => { + if (chunks.at(-1)!.length + line.length > maxSize) { + chunks.push(''); + } + + if (chunks.at(-1)!.length === 0) { + chunks[chunks.length - 1] += line; + } else { + chunks[chunks.length - 1] += joiner + line; + } + }); + + return chunks; +}