mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-24 03:59:50 +02:00
feat: simplified owned tanks command
This commit is contained in:
1 parent
d39ab43e74
commit
56321d9e33
6 files changed
+74
-78
No files matched your search
+27
-70
@@ -1,14 +1,13 @@
|
|||||||
import CommandWrapper from '../components/CommandWrapper';
|
import { chunk } from 'lodash';
|
||||||
import NoData from '../components/NoData';
|
import markdownEscape from 'markdown-escape';
|
||||||
import * as Tanks from '../components/Tanks';
|
import { TANK_CLASSES } from '../components/Tanks/components/Item/constants';
|
||||||
import TitleBar from '../components/TitleBar';
|
|
||||||
import { getAccountInfo } from '../core/blitz/getAccountInfo';
|
import { getAccountInfo } from '../core/blitz/getAccountInfo';
|
||||||
import { getClanAccountInfo } from '../core/blitz/getClanAccountInfo';
|
|
||||||
import getTankStats from '../core/blitz/getTankStats';
|
import getTankStats from '../core/blitz/getTankStats';
|
||||||
import { emblemIdToURL } from '../core/blitzkrieg/emblemIdToURL';
|
|
||||||
import {
|
import {
|
||||||
|
NATIONS,
|
||||||
TankDefinition,
|
TankDefinition,
|
||||||
Tier,
|
Tier,
|
||||||
|
flags,
|
||||||
tankDefinitions,
|
tankDefinitions,
|
||||||
} from '../core/blitzkrieg/tankDefinitions';
|
} from '../core/blitzkrieg/tankDefinitions';
|
||||||
import { TIER_ROMAN_NUMERALS } from '../core/blitzkrieg/tankDefinitions/constants';
|
import { TIER_ROMAN_NUMERALS } from '../core/blitzkrieg/tankDefinitions/constants';
|
||||||
@@ -20,6 +19,8 @@ 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({
|
||||||
inProduction: true,
|
inProduction: true,
|
||||||
@@ -43,7 +44,6 @@ export const ownedTanksCommand = new Promise<CommandRegistry>((resolve) => {
|
|||||||
})),
|
})),
|
||||||
)
|
)
|
||||||
).filter((tank) => tank.tankDefinitions?.tier === tier);
|
).filter((tank) => tank.tankDefinitions?.tier === tier);
|
||||||
const clanAccountInfo = await getClanAccountInfo(region, id, ['clan']);
|
|
||||||
const groupedTanks: Record<string, TankDefinition[]> = {};
|
const groupedTanks: Record<string, TankDefinition[]> = {};
|
||||||
const nations: string[] = [];
|
const nations: string[] = [];
|
||||||
|
|
||||||
@@ -56,71 +56,28 @@ export const ownedTanksCommand = new Promise<CommandRegistry>((resolve) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
nations.sort();
|
const awaitedTankDefinitions = await tankDefinitions;
|
||||||
|
const awaitedNations = await NATIONS;
|
||||||
|
const lines = tankStats
|
||||||
|
.map(({ tank_id }) => awaitedTankDefinitions[tank_id])
|
||||||
|
.filter((tank) => tank.tier === tier)
|
||||||
|
.sort(
|
||||||
|
(a, b) =>
|
||||||
|
TANK_CLASSES.indexOf(a.class) - TANK_CLASSES.indexOf(b.class),
|
||||||
|
)
|
||||||
|
.sort(
|
||||||
|
(a, b) =>
|
||||||
|
awaitedNations.indexOf(a.nation) - awaitedNations.indexOf(b.nation),
|
||||||
|
)
|
||||||
|
.map((tank) => `${flags[tank.nation]} ${markdownEscape(tank.name)}`);
|
||||||
|
|
||||||
return (
|
return chunk(lines, TANKS_PER_MESSAGE).map((lines, index) =>
|
||||||
<CommandWrapper>
|
index === 0
|
||||||
<TitleBar
|
? `${translate('bot.commands.owned_tanks.body.title', [
|
||||||
title={accountInfo.nickname}
|
markdownEscape(accountInfo.nickname),
|
||||||
image={
|
|
||||||
clanAccountInfo?.clan
|
|
||||||
? emblemIdToURL(clanAccountInfo.clan.emblem_set_id)
|
|
||||||
: undefined
|
|
||||||
}
|
|
||||||
description={translate('bot.commands.owned_tanks.body.subtitle', [
|
|
||||||
TIER_ROMAN_NUMERALS[tier as Tier],
|
TIER_ROMAN_NUMERALS[tier as Tier],
|
||||||
])}
|
])}${lines.join('\n')}`
|
||||||
/>
|
: lines.join('\n'),
|
||||||
|
|
||||||
{filteredTanks.length === 0 && (
|
|
||||||
<NoData type="tanks_found" locale={interaction.locale} />
|
|
||||||
)}
|
|
||||||
|
|
||||||
{filteredTanks.length > 0 &&
|
|
||||||
(await Promise.all(
|
|
||||||
nations.map(async (nation) => {
|
|
||||||
const tanks = groupedTanks[nation];
|
|
||||||
const leftColumnSize = Math.ceil(tanks.length / 2);
|
|
||||||
const leftColumn = tanks.slice(0, leftColumnSize);
|
|
||||||
const rightColumn = tanks.slice(leftColumnSize);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<Tanks.Root key={nation}>
|
|
||||||
<Tanks.Title>
|
|
||||||
{translate(`common.nations.${nation}`)}
|
|
||||||
</Tanks.Title>
|
|
||||||
|
|
||||||
<Tanks.Row>
|
|
||||||
<Tanks.Column>
|
|
||||||
{await Promise.all(
|
|
||||||
leftColumn.map(async (tank) => (
|
|
||||||
<Tanks.Item
|
|
||||||
key={tank.id}
|
|
||||||
name={tank.name}
|
|
||||||
tankType={tank.class}
|
|
||||||
treeType={tank.treeType}
|
|
||||||
/>
|
|
||||||
)),
|
|
||||||
)}
|
|
||||||
</Tanks.Column>
|
|
||||||
<Tanks.Column>
|
|
||||||
{await Promise.all(
|
|
||||||
rightColumn.map(async (tank) => (
|
|
||||||
<Tanks.Item
|
|
||||||
key={tank.id}
|
|
||||||
name={tank.name}
|
|
||||||
tankType={tank.class}
|
|
||||||
treeType={tank.treeType}
|
|
||||||
/>
|
|
||||||
)),
|
|
||||||
)}
|
|
||||||
</Tanks.Column>
|
|
||||||
</Tanks.Row>
|
|
||||||
</Tanks.Root>
|
|
||||||
);
|
|
||||||
}),
|
|
||||||
))}
|
|
||||||
</CommandWrapper>
|
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
@@ -27,6 +27,10 @@ export const permissionsCommand = new Promise<CommandRegistry>((resolve) => {
|
|||||||
interaction.appPermissions?.has('AttachFiles'),
|
interaction.appPermissions?.has('AttachFiles'),
|
||||||
t`bot.commands.permissions.body.attach_files`,
|
t`bot.commands.permissions.body.attach_files`,
|
||||||
],
|
],
|
||||||
|
[
|
||||||
|
interaction.appPermissions?.has('UseExternalEmojis'),
|
||||||
|
t`bot.commands.permissions.body.external_emojis`,
|
||||||
|
],
|
||||||
];
|
];
|
||||||
const body = permissions
|
const body = permissions
|
||||||
.map(([hasPermission, description]) => {
|
.map(([hasPermission, description]) => {
|
||||||
|
|||||||
@@ -185,3 +185,15 @@ export const NATIONS = tanksDefinitionsArray.then((tanks) => {
|
|||||||
|
|
||||||
return nations;
|
return nations;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const flags: Record<string, string> = {
|
||||||
|
ussr: '<:ussr:1218421042033197197>',
|
||||||
|
germany: '🇩🇪',
|
||||||
|
usa: '🇺🇸',
|
||||||
|
china: '🇨🇳',
|
||||||
|
uk: '🇬🇧',
|
||||||
|
france: '🇫🇷',
|
||||||
|
japan: '🇯🇵',
|
||||||
|
european: '🇪🇺',
|
||||||
|
other: '<:other:1218421572243558482>',
|
||||||
|
};
|
||||||
@@ -4,6 +4,7 @@ import {
|
|||||||
ButtonBuilder,
|
ButtonBuilder,
|
||||||
EmbedBuilder,
|
EmbedBuilder,
|
||||||
InteractionEditReplyOptions,
|
InteractionEditReplyOptions,
|
||||||
|
InteractionReplyOptions,
|
||||||
} from 'discord.js';
|
} from 'discord.js';
|
||||||
import { InteractionReturnable } from '../../events/interactionCreate';
|
import { InteractionReturnable } from '../../events/interactionCreate';
|
||||||
import jsxToPngThreaded from '../blitzkrieg/jsxToPngThreaded';
|
import jsxToPngThreaded from '../blitzkrieg/jsxToPngThreaded';
|
||||||
@@ -12,7 +13,7 @@ export default async function normalizeInteractionReturnable(
|
|||||||
returnable: InteractionReturnable,
|
returnable: InteractionReturnable,
|
||||||
) {
|
) {
|
||||||
const images: [number, Buffer][] = [];
|
const images: [number, Buffer][] = [];
|
||||||
const reply: InteractionEditReplyOptions = {};
|
const reply: InteractionEditReplyOptions & InteractionReplyOptions = {};
|
||||||
|
|
||||||
const awaitedReturnable = await returnable;
|
const awaitedReturnable = await returnable;
|
||||||
const normalizedReturnable = Array.isArray(awaitedReturnable)
|
const normalizedReturnable = Array.isArray(awaitedReturnable)
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import {
|
|||||||
ChatInputCommandInteraction,
|
ChatInputCommandInteraction,
|
||||||
InteractionReplyOptions,
|
InteractionReplyOptions,
|
||||||
} from 'discord.js';
|
} from 'discord.js';
|
||||||
import { commands } from '..';
|
import { InteractionRawReturnable, commands } from '..';
|
||||||
import buttonLink from '../../../core/discord/buttonLink';
|
import buttonLink from '../../../core/discord/buttonLink';
|
||||||
import embedWarning from '../../../core/discord/embedWarning';
|
import embedWarning from '../../../core/discord/embedWarning';
|
||||||
import normalizeInteractionReturnable from '../../../core/discord/normalizeInteractionReturnable';
|
import normalizeInteractionReturnable from '../../../core/discord/normalizeInteractionReturnable';
|
||||||
@@ -28,7 +28,19 @@ export default async function handleChatInputCommand(
|
|||||||
|
|
||||||
if (registry.handlesInteraction) return;
|
if (registry.handlesInteraction) return;
|
||||||
|
|
||||||
const reply = await normalizeInteractionReturnable(returnable!);
|
const chunks: InteractionRawReturnable[][] = [[]];
|
||||||
|
|
||||||
|
(Array.isArray(returnable) ? returnable : [returnable]).forEach(
|
||||||
|
(object) => {
|
||||||
|
if (typeof object === 'string') {
|
||||||
|
chunks.push([object]);
|
||||||
|
} else chunks.at(-1)?.push(object!);
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
let index = 0;
|
||||||
|
for (const chunk of chunks.filter((chunk) => chunk.length > 0)) {
|
||||||
|
const reply = await normalizeInteractionReturnable(chunk);
|
||||||
|
|
||||||
if (
|
if (
|
||||||
psa.data &&
|
psa.data &&
|
||||||
@@ -50,13 +62,19 @@ export default async function handleChatInputCommand(
|
|||||||
reply.components = [new ActionRowBuilder<ButtonBuilder>()];
|
reply.components = [new ActionRowBuilder<ButtonBuilder>()];
|
||||||
}
|
}
|
||||||
|
|
||||||
(reply.components[0] as ActionRowBuilder<ButtonBuilder>).addComponents(
|
(
|
||||||
|
reply.components[0] as ActionRowBuilder<ButtonBuilder>
|
||||||
|
).addComponents(
|
||||||
psa.data.links.map(({ title, url }) => buttonLink(url, title)),
|
psa.data.links.map(({ title, url }) => buttonLink(url, title)),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (index === 0) {
|
||||||
await interaction.editReply(reply);
|
await interaction.editReply(reply);
|
||||||
|
} else {
|
||||||
|
await interaction.followUp(reply);
|
||||||
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
psa.data &&
|
psa.data &&
|
||||||
@@ -93,6 +111,9 @@ export default async function handleChatInputCommand(
|
|||||||
|
|
||||||
interaction.followUp(followUp);
|
interaction.followUp(followUp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
index++;
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const { t } = translator(interaction.locale);
|
const { t } = translator(interaction.locale);
|
||||||
const components = [
|
const components = [
|
||||||
|
|||||||
+5
-4
@@ -486,7 +486,7 @@
|
|||||||
"description": "Shows all own tanks in a tier",
|
"description": "Shows all own tanks in a tier",
|
||||||
|
|
||||||
"body": {
|
"body": {
|
||||||
"subtitle": "Tier %s tanks"
|
"title": "# %s's owned tier %s tanks\n\n"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -496,9 +496,10 @@
|
|||||||
|
|
||||||
"body": {
|
"body": {
|
||||||
"title": "# Permissions\n\n%s",
|
"title": "# Permissions\n\n%s",
|
||||||
"view_channels": "View channels: needed for the refresh button to work",
|
"view_channels": "View channels: needed to find the message the refresh button was used on",
|
||||||
"read_message_history": "Read message history: needed for the refresh button to find the message to refresh",
|
"read_message_history": "Read message history: needed to find the message to refresh",
|
||||||
"attach_files": "Attach files: needed for image-based commands to work"
|
"attach_files": "Attach files: needed to add images to image-based commands",
|
||||||
|
"external_emojis": "External emojis: needed to fill add national flags and tank class emojis"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user