diff --git a/src/commands/tags.ts b/src/commands/tags.ts index e7a1927..186bdb8 100644 --- a/src/commands/tags.ts +++ b/src/commands/tags.ts @@ -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', diff --git a/src/utils/tagsUtils.ts b/src/utils/tagsUtils.ts index 47d1723..4ca83c5 100644 --- a/src/utils/tagsUtils.ts +++ b/src/utils/tagsUtils.ts @@ -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 = 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 = (name: string, more?: T): T extends true ? Tag[] : Tag => { +export const getTag = (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); } } \ No newline at end of file