mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-23 19:49:50 +02:00
feat: /owned-tanks links to tankopedia and chunks better
This commit is contained in:
1 parent
5b5e204cce
commit
d9f6acea2d
2 files changed
+29
-13
No files matched your search
@@ -6,7 +6,6 @@ import {
|
|||||||
getAccountInfo,
|
getAccountInfo,
|
||||||
getTankStats,
|
getTankStats,
|
||||||
} from '@blitzkit/core';
|
} from '@blitzkit/core';
|
||||||
import { chunk } from 'lodash-es';
|
|
||||||
import markdownEscape from 'markdown-escape';
|
import markdownEscape from 'markdown-escape';
|
||||||
import {
|
import {
|
||||||
gameDefinitions,
|
gameDefinitions,
|
||||||
@@ -15,13 +14,12 @@ import {
|
|||||||
import { addTierChoices } from '../core/discord/addTierChoices';
|
import { addTierChoices } from '../core/discord/addTierChoices';
|
||||||
import { addUsernameChoices } from '../core/discord/addUsernameChoices';
|
import { addUsernameChoices } from '../core/discord/addUsernameChoices';
|
||||||
import { autocompleteUsername } from '../core/discord/autocompleteUsername';
|
import { autocompleteUsername } from '../core/discord/autocompleteUsername';
|
||||||
|
import { chunkLines } from '../core/discord/chunkLines';
|
||||||
import { createLocalizedCommand } from '../core/discord/createLocalizedCommand';
|
import { createLocalizedCommand } from '../core/discord/createLocalizedCommand';
|
||||||
import { resolvePlayerFromCommand } from '../core/discord/resolvePlayerFromCommand';
|
import { resolvePlayerFromCommand } from '../core/discord/resolvePlayerFromCommand';
|
||||||
import { translator } from '../core/localization/translator';
|
import { translator } from '../core/localization/translator';
|
||||||
import { CommandRegistry } from '../events/interactionCreate';
|
import { CommandRegistry } from '../events/interactionCreate';
|
||||||
|
|
||||||
const TANKS_PER_MESSAGE = 64;
|
|
||||||
|
|
||||||
export const ownedTanksCommand = new Promise<CommandRegistry>((resolve) => {
|
export const ownedTanksCommand = new Promise<CommandRegistry>((resolve) => {
|
||||||
resolve({
|
resolve({
|
||||||
command: createLocalizedCommand('owned-tanks')
|
command: createLocalizedCommand('owned-tanks')
|
||||||
@@ -61,7 +59,11 @@ export const ownedTanksCommand = new Promise<CommandRegistry>((resolve) => {
|
|||||||
|
|
||||||
const awaitedTankDefinitions = await tankDefinitions;
|
const awaitedTankDefinitions = await tankDefinitions;
|
||||||
const awaitedGameDefinitions = await gameDefinitions;
|
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])
|
.map(({ tank_id }) => awaitedTankDefinitions.tanks[tank_id])
|
||||||
.filter((tank) => tank.tier === tier)
|
.filter((tank) => tank.tier === tier)
|
||||||
.sort(
|
.sort(
|
||||||
@@ -73,16 +75,13 @@ export const ownedTanksCommand = new Promise<CommandRegistry>((resolve) => {
|
|||||||
awaitedGameDefinitions.nations.indexOf(a.nation) -
|
awaitedGameDefinitions.nations.indexOf(a.nation) -
|
||||||
awaitedGameDefinitions.nations.indexOf(b.nation),
|
awaitedGameDefinitions.nations.indexOf(b.nation),
|
||||||
)
|
)
|
||||||
.map((tank) => `${flags[tank.nation]} ${markdownEscape(tank.name)}`);
|
.map(
|
||||||
|
(tank) =>
|
||||||
|
`[${flags[tank.nation]} ${markdownEscape(tank.name)}](<https://blitzkit.app/tools/tankopedia/${tank.id}>)`,
|
||||||
|
);
|
||||||
|
const lines = [title, ...tankList];
|
||||||
|
|
||||||
return chunk(lines, TANKS_PER_MESSAGE).map((lines, index) =>
|
return chunkLines(lines);
|
||||||
index === 0
|
|
||||||
? `${translate('bot.commands.owned_tanks.body.title', [
|
|
||||||
markdownEscape(accountInfo.nickname),
|
|
||||||
TIER_ROMAN_NUMERALS[tier],
|
|
||||||
])}${lines.join('\n')}`
|
|
||||||
: lines.join('\n'),
|
|
||||||
);
|
|
||||||
},
|
},
|
||||||
|
|
||||||
autocomplete: autocompleteUsername,
|
autocomplete: autocompleteUsername,
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
Reference in new issue
Block a user