mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-21 18:57:03 +02:00
34 lines
888 B
TypeScript
34 lines
888 B
TypeScript
import { CacheType, ChatInputCommandInteraction } from 'discord.js';
|
|
import { go } from 'fuzzysort';
|
|
import errorWithCause from '../process/errorWithCause.js';
|
|
import { TANKS, tankopedia } from './tankopedia.js';
|
|
|
|
export default async function resolveTankId(
|
|
interaction: ChatInputCommandInteraction<CacheType>,
|
|
tank: string,
|
|
) {
|
|
const number = Number(tank);
|
|
|
|
if (Number.isNaN(number)) {
|
|
const searchResult = go(tank, TANKS, { keys: ['name'], limit: 1 });
|
|
|
|
if (searchResult.length === 0) {
|
|
throw errorWithCause(
|
|
'Tank not found',
|
|
`Could not find tank by the name "${tank}".`,
|
|
);
|
|
} else {
|
|
return searchResult[0].obj.tank_id;
|
|
}
|
|
} else {
|
|
if (tankopedia[number]) {
|
|
return number;
|
|
} else {
|
|
throw errorWithCause(
|
|
'Tank not found',
|
|
`Could not find tank by the ID "${number}".`,
|
|
);
|
|
}
|
|
}
|
|
}
|