2023-05-30 17:24:24 +00:00
|
|
|
import { CacheType, ChatInputCommandInteraction } from 'discord.js';
|
|
|
|
|
import { go } from 'fuzzysort';
|
2023-06-04 00:01:10 +00:00
|
|
|
import negativeEmbed from '../interaction/negativeEmbed.js';
|
2023-05-30 18:42:34 +00:00
|
|
|
import { TANKS, tankopedia } from './tankopedia.js';
|
2023-05-30 17:24:24 +00:00
|
|
|
|
|
|
|
|
export default async function resolveTankId(
|
|
|
|
|
interaction: ChatInputCommandInteraction<CacheType>,
|
|
|
|
|
tank: string,
|
|
|
|
|
) {
|
|
|
|
|
const number = Number(tank);
|
|
|
|
|
|
|
|
|
|
if (Number.isNaN(number)) {
|
2023-05-30 18:42:34 +00:00
|
|
|
const searchResult = go(tank, TANKS, { keys: ['name'], limit: 1 });
|
2023-05-30 17:24:24 +00:00
|
|
|
|
|
|
|
|
if (searchResult.length === 0) {
|
|
|
|
|
await interaction.editReply({
|
|
|
|
|
embeds: [
|
2023-06-04 00:01:10 +00:00
|
|
|
negativeEmbed(
|
2023-05-30 17:24:24 +00:00
|
|
|
'Tank not found',
|
|
|
|
|
`Could not find tank by the name "${tank}".`,
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
throw new Error(`Tank not found by name "${tank}"`);
|
|
|
|
|
} else {
|
|
|
|
|
return searchResult[0].obj.tank_id;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (tankopedia[number]) {
|
|
|
|
|
return number;
|
|
|
|
|
} else {
|
|
|
|
|
await interaction.editReply({
|
|
|
|
|
embeds: [
|
2023-06-04 00:01:10 +00:00
|
|
|
negativeEmbed(
|
2023-05-30 17:24:24 +00:00
|
|
|
'Tank not found',
|
|
|
|
|
`Could not find tank by the ID "${number}".`,
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
throw new Error(`Tank not found by ID "${number}"`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|