feat(tagsUtils): better search

This commit is contained in:
xHyroM 2022-07-16 14:04:22 +02:00
parent b8b67bfc9f
commit f031102cc0
2 changed files with 40 additions and 11 deletions

View file

@ -1,6 +1,6 @@
import { APIApplicationCommandInteractionDataStringOption, ApplicationCommandOptionType, InteractionResponseType, MessageFlags } from 'discord-api-types/v10'; import { APIApplicationCommandInteractionDataStringOption, ApplicationCommandOptionType, InteractionResponseType, MessageFlags } from 'discord-api-types/v10';
import { Command } from '../structures/Command'; import { Command } from '../structures/Command';
import { findTags, getTag, Tag } from '../utils/tagsUtils'; import { findTags, getTag } from '../utils/tagsUtils';
new Command({ new Command({
name: 'tags', name: 'tags',

View file

@ -1,4 +1,5 @@
import Collection from '@discordjs/collection'; import Collection from '@discordjs/collection';
import { APIApplicationCommandOptionChoice } from 'discord-api-types/v10';
// @ts-expect-error Types :( // @ts-expect-error Types :(
import tags from '../../files/tags.toml'; import tags from '../../files/tags.toml';
@ -7,6 +8,10 @@ export interface Tag {
content: string; content: string;
} }
export interface ExtendedTag extends Tag {
match: '✅' | '🔑' | '📄'
}
const tagCache: Collection<string, Tag> = new Collection(); const tagCache: Collection<string, Tag> = new Collection();
for (const [key, value] of Object.entries(tags)) { for (const [key, value] of Object.entries(tags)) {
@ -14,13 +19,41 @@ for (const [key, value] of Object.entries(tags)) {
tagCache.set(key, value as unknown as Tag); tagCache.set(key, value as unknown as Tag);
} }
export const getTag = <T extends boolean>(name: string, more?: T): T extends true ? Tag[] : Tag => { export const getTag = <T extends boolean>(name: string, more?: T): T extends true ? APIApplicationCommandOptionChoice[] : Tag => {
if (more) { if (more) {
const tags = [...tagCache.filter(tag => tag.keywords.some(k => k.includes(name))).values()]; const exactKeywords: APIApplicationCommandOptionChoice[] = [];
return tags as T extends true ? Tag[] : Tag; const keywordMatches: APIApplicationCommandOptionChoice[] = [];
const contentMatches: APIApplicationCommandOptionChoice[] = [];
const query = name.toLowerCase();
for (const [tagName, tag] of tagCache.entries()) {
const exactKeyword = tag.keywords.find((t) => t.toLowerCase() === query);
const includesKeyword = tag.keywords.find((t) => t.toLowerCase().includes(query));
const contentMatch = tag.content.toLowerCase().includes(query);
if (exactKeyword) {
exactKeywords.push({
name: `${tagName.replaceAll('-', ' ')}`,
value: tagName
});
} else if (includesKeyword) {
keywordMatches.push({
name: `🔑 ${tagName.replaceAll('-', ' ')}`,
value: tagName
});
} else if (contentMatch) {
contentMatches.push({
name: `📄 ${tagName.replaceAll('-', ' ')}`,
value: tagName
});
}
}
const tags = [...exactKeywords, ...keywordMatches, ...contentMatches];
return tags as T extends true ? APIApplicationCommandOptionChoice[] : Tag;
} else { } else {
const tag = tagCache.get(name) || tagCache.find(tag => tag.keywords.some(k => k.includes(name))); const tag = tagCache.get(name) || tagCache.find(tag => tag.keywords.some(k => k.includes(name)));
return tag as T extends true ? Tag[] : Tag; return tag as T extends true ? APIApplicationCommandOptionChoice[] : Tag;
} }
} }
@ -28,17 +61,13 @@ export const findTags = (name: string) => {
if (!name) if (!name)
return [ return [
...tagCache.map((tag, name) => new Object({ ...tagCache.map((tag, name) => new Object({
name: name.replaceAll('-', ' '), name: `🚀 ${name.replaceAll('-', ' ')}`,
value: name value: name
})).slice(0, 25) })).slice(0, 25)
]; ];
else { else {
const tags = getTag(name, true); const tags = getTag(name, true);
if (tags.length > 0) if (tags.length > 0) return tags;
return tags.map(tag => new Object({
name: tag.keywords[0].replaceAll('-', ' '),
value: tag.keywords[0]
}));
else return findTags(null); else return findTags(null);
} }
} }