Files
blitzkit/packages/bot/src/commands/auction.ts
T

132 lines
3.7 KiB
TypeScript
Raw Normal View History

2024-12-13 10:58:16 -06:00
import {
BlitzTankClass,
Region,
regionToRegionSubdomain,
} from '@blitzkit/core';
import { tankDefinitions } from '../core/blitzkit/nonBlockingPromises';
import { createLocalizedCommand } from '../core/discord/createLocalizedCommand';
import { translator } from '../core/localization/translator';
import { CommandRegistry } from '../events/interactionCreate';
interface Auction {
count: number;
has_next: boolean;
results: Result[];
}
type Result =
| {
id?: number;
type: 'vehicle';
countable?: boolean;
entity?: Entity;
2024-12-13 16:25:37 -06:00
initial_count: number;
current_count: number;
2024-12-13 10:58:16 -06:00
saleable?: boolean;
available_from?: Date;
available_before?: Date;
price?: Price;
next_price?: Price | null;
available: true;
display?: boolean;
2024-12-13 16:11:43 -06:00
next_price_datetime: Date;
next_price_timestamp: number;
2024-12-13 10:58:16 -06:00
}
| {
available: false;
type: 'vehicle';
};
interface Entity {
id: number;
name: string;
nation: string;
subnation: string;
use_subnation_flag: boolean;
type_slug: BlitzTankClass;
level: number;
roman_level: string;
user_string: string;
image_url: string;
preview_image_url: string;
is_premium: boolean;
is_collectible: boolean;
}
interface Price {
currency: Currency;
value: number;
}
interface Currency {
name: 'gold';
count: number;
title: 'Gold';
image_url: string;
sizes: unknown;
type: 'currency';
}
export const auctionCommand = new Promise<CommandRegistry>((resolve) => {
resolve({
2024-12-13 16:11:43 -06:00
command: createLocalizedCommand('auction'),
2024-12-13 10:58:16 -06:00
async handler(interaction) {
2024-12-13 16:11:43 -06:00
const { t, translate } = translator(interaction.locale);
2024-12-13 10:58:16 -06:00
const awaitedTankDefinitions = await tankDefinitions;
const region: Region = 'com';
const pageSize = 80;
const saleable = true;
const data = await fetch(
`https://${regionToRegionSubdomain(
region,
)}.wotblitz.com/en/api/events/items/auction/?page_size=${
pageSize
}&type[]=vehicle&saleable=${saleable}`,
).then((response) => response.json() as Promise<Auction>);
2024-12-13 16:11:43 -06:00
const saleableTanks = data.results.filter((result) => result.available);
2024-12-13 10:58:16 -06:00
2024-12-13 16:11:43 -06:00
if (saleableTanks.length === 0) {
return t`bot.commands.auction.errors.no_auction`;
}
const nextPricesTimestamp = saleableTanks[0].next_price_timestamp;
const title = `# ${t`bot.commands.auction.body.title`}`;
const subtitle = `${translate('bot.commands.auction.body.subtitle', [
`${saleableTanks.length} / ${data.results.length}`,
`<t:${nextPricesTimestamp}:R>`,
'<https://wotblitz.com/auction/>',
])}`;
const body = saleableTanks
2024-12-13 10:58:16 -06:00
.map((data, index) => {
const tank = awaitedTankDefinitions.tanks[data.entity!.id];
if (!tank) return '';
const name = `[${tank.name}](<https://blitzkit.app/tools/tankopedia/${
tank.id
}>)`;
2024-12-13 16:11:43 -06:00
const next = translate('bot.commands.auction.body.next', [
`<:gold:1317173197082333244> ${data.next_price!.value.toLocaleString(
interaction.locale,
)}`,
]);
2024-12-13 16:25:37 -06:00
const available = translate('bot.commands.auction.body.available', [
data.current_count.toLocaleString(interaction.locale),
data.initial_count.toLocaleString(interaction.locale),
]);
2024-12-13 10:58:16 -06:00
return `${
index + 1
}. ${name} <:gold:1317173197082333244> ${data.price!.value.toLocaleString(
interaction.locale,
2024-12-13 16:25:37 -06:00
)}\n-# ${available}\n-# ${next}`;
2024-12-13 10:58:16 -06:00
})
.join('\n\n');
2024-12-13 16:11:43 -06:00
const content = `${title}\n${subtitle}\n\n${body}`;
2024-12-13 10:58:16 -06:00
2024-12-13 16:11:43 -06:00
return content;
2024-12-13 10:58:16 -06:00
},
});
});