feat: accent-tolerant tank search

This commit is contained in:
TresAbhi committed 2023-08-10 01:46:39 +00:00
1 parent bb2450eb3c
commit 70575fa9cf
3 files changed
+26 -13

No files matched your search

+5 -4
View File
@@ -1,7 +1,7 @@
import { SlashCommandBuilder } from 'discord.js'; import { SlashCommandBuilder } from 'discord.js';
import { go } from 'fuzzysort'; import { go } from 'fuzzysort';
import markdownEscape from 'markdown-escape'; import markdownEscape from 'markdown-escape';
import { TANK_NAMES } from '../core/blitz/tankopedia'; import { TANK_NAMES_DIACRITICS } from '../core/blitz/tankopedia';
import addTankChoices from '../core/discord/addTankChoices'; import addTankChoices from '../core/discord/addTankChoices';
import embedInfo from '../core/discord/embedInfo'; import embedInfo from '../core/discord/embedInfo';
import { CommandRegistry } from '../events/interactionCreate'; import { CommandRegistry } from '../events/interactionCreate';
@@ -26,9 +26,10 @@ export const searchTanksCommand: CommandRegistry = {
async handler(interaction) { async handler(interaction) {
const tank = interaction.options.getString('tank')!; const tank = interaction.options.getString('tank')!;
const limit = interaction.options.getInteger('limit') ?? 25; const limit = interaction.options.getInteger('limit') ?? 25;
const results = go(tank, await TANK_NAMES, { limit }).map( const results = go(tank, await TANK_NAMES_DIACRITICS, {
(result) => result.target, limit,
); keys: ['combined'],
}).map((result) => result.obj.original);
return embedInfo( return embedInfo(
`Tank search for "${markdownEscape(tank)}"`, `Tank search for "${markdownEscape(tank)}"`,
+15
View File
@@ -1,3 +1,4 @@
import { deburr } from 'lodash';
import { secrets } from '../node/secrets'; import { secrets } from '../node/secrets';
import getWargamingResponse from './getWargamingResponse'; import getWargamingResponse from './getWargamingResponse';
@@ -36,6 +37,20 @@ export const TANKS = new Promise<TankopediaEntry[]>(async (resolve) => {
export const TANK_NAMES = new Promise<string[]>(async (resolve) => { export const TANK_NAMES = new Promise<string[]>(async (resolve) => {
resolve((await entries).map(({ name }) => name)); resolve((await entries).map(({ name }) => name));
}); });
export const TANK_NAMES_DIACRITICS = TANK_NAMES.then((tankNames) =>
Promise.all(
tankNames.map(async (tankName, index) => {
const name = tankName ?? `Unknown Tank ${(await TANKS)[index].tank_id}`;
const diacriticless = deburr(name);
return {
original: name,
diacriticless,
combined: `${name}${diacriticless}`,
};
}),
),
);
export interface TankopediaInfo { export interface TankopediaInfo {
achievement_sections: Record<string, { name: string; order: number }>; achievement_sections: Record<string, { name: string; order: number }>;
+6 -9
View File
@@ -4,8 +4,7 @@ import {
CacheType, CacheType,
} from 'discord.js'; } from 'discord.js';
import { go } from 'fuzzysort'; import { go } from 'fuzzysort';
import resolveTankName from '../blitz/resolveTankName'; import { TANKS, TANK_NAMES_DIACRITICS } from '../blitz/tankopedia';
import { TANKS } from '../blitz/tankopedia';
export default async function autocompleteTanks( export default async function autocompleteTanks(
interaction: AutocompleteInteraction<CacheType>, interaction: AutocompleteInteraction<CacheType>,
@@ -16,16 +15,14 @@ export default async function autocompleteTanks(
await interaction.respond( await interaction.respond(
focusedOption.value focusedOption.value
? await Promise.all( ? await Promise.all(
go(focusedOption.value, await TANKS, { go(focusedOption.value, await TANK_NAMES_DIACRITICS, {
keys: ['name'], keys: ['combined'],
limit: 10, limit: 10,
}).map( }).map(
async (item) => async (item, index) =>
({ ({
name: item.obj.name name: item.obj.original,
? item.obj.name value: `${(await TANKS)[index].tank_id}`,
: await resolveTankName(item.obj.tank_id),
value: `${item.obj.tank_id}`,
}) satisfies ApplicationCommandOptionChoiceData<string>, }) satisfies ApplicationCommandOptionChoiceData<string>,
), ),
) )