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 { Command } from '../structures/Command';
import { findTags, getTag, Tag } from '../utils/tagsUtils';
import { findTags, getTag } from '../utils/tagsUtils';
new Command({
name: 'tags',

View file

@ -1,4 +1,5 @@
import Collection from '@discordjs/collection';
import { APIApplicationCommandOptionChoice } from 'discord-api-types/v10';
// @ts-expect-error Types :(
import tags from '../../files/tags.toml';
@ -7,6 +8,10 @@ export interface Tag {
content: string;
}
export interface ExtendedTag extends Tag {
match: '✅' | '🔑' | '📄'
}
const tagCache: Collection<string, Tag> = new Collection();
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);
}
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) {
const tags = [...tagCache.filter(tag => tag.keywords.some(k => k.includes(name))).values()];
return tags as T extends true ? Tag[] : Tag;
const exactKeywords: APIApplicationCommandOptionChoice[] = [];
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 {
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)
return [
...tagCache.map((tag, name) => new Object({
name: name.replaceAll('-', ' '),
name: `🚀 ${name.replaceAll('-', ' ')}`,
value: name
})).slice(0, 25)
];
else {
const tags = getTag(name, true);
if (tags.length > 0)
return tags.map(tag => new Object({
name: tag.keywords[0].replaceAll('-', ' '),
value: tag.keywords[0]
}));
if (tags.length > 0) return tags;
else return findTags(null);
}
}