From c60793dbffa36fc7a7ab60e1ffd662be10842baf Mon Sep 17 00:00:00 2001 From: xHyroM Date: Thu, 24 Aug 2023 22:56:08 +0200 Subject: [PATCH] feat: make docs working --- src/commands/docs.ts | 69 ++++++++++++++++++++++++----- src/listeners/interaction_create.ts | 2 - 2 files changed, 57 insertions(+), 14 deletions(-) diff --git a/src/commands/docs.ts b/src/commands/docs.ts index 75f426e..681d7c1 100644 --- a/src/commands/docs.ts +++ b/src/commands/docs.ts @@ -5,6 +5,7 @@ import { InteractionCommandContext } from "../structs/context/CommandContext"; import algoliasearch from "algoliasearch"; const algoliaClient = algoliasearch("2527C13E0N", "4efc87205e1fce4a1f267cadcab42cb2"); +const algoliaIndex = algoliaClient.initIndex("bun"); defineCommand({ name: "docs", @@ -17,22 +18,66 @@ defineCommand({ .setAutocomplete(true) .setDescription("Select query") .toJSON(), - run: (context: AutocompleteContext) => { - return context.respond([{ name: "heh", value: "heh" }]); + run: async(context: AutocompleteContext) => { + const query = context.options.getString("query"); + const result = await algoliaIndex.search(query, { + hitsPerPage: 25, + }); + + return context.respond( + result.hits.map(hit => { + const name = getHitName(hit); + + return { + name: name.full.length > 100 ? name.full.slice(0, 100) : name.full, + value: name.name.length > 100 ? name.name.slice(0, 100) : name.name, + } + }) + ); } } ], run: async(context: InteractionCommandContext) => { - await context.interaction.deferReply(); - console.log(await algoliaClient.search([ - { - query: "qwe", - indexName: "bun" - } - ])); + await context.interaction.deferReply({ + ephemeral: true, + }); + + const query = context.interaction.options.getString("query"); - await context.interaction.editReply({ - content: "asdsad" - }) + const result = await algoliaIndex.search(query, { + hitsPerPage: 1, + }); + + const hit = result.hits[0]; + // @ts-expect-error exist + const url = hit.url; + const name = getHitName(hit); + // @ts-expect-error can exist + const snippetContent = hit._snippetResult?.content?.value?.replace(/<[^>]+>/g, ""); + // @ts-expect-error can exist + const notice = hit.content?.replace(/\r/g, ""); + + const content = [ + `[*${name.full}*](<${url}>)`, + snippetContent ? snippetContent : "", + notice ? notice.split("\n").map(s => `> ${s}`).join("\n") : "" + ].join("\n") + + + await context.interaction.editReply(content); } }) + +function getHitName(hit) { + const type = hit.hierarchy.lvl0 === "Documentation" ? "📖" : "🗺️"; + const hierarchy = Object.values(hit.hierarchy).filter(v => v); + hierarchy.shift(); + + const name = hierarchy.join(" > "); + + return { + full: `${type} ${name}`, + name: name, + emoji: type, + } +} diff --git a/src/listeners/interaction_create.ts b/src/listeners/interaction_create.ts index 24b0ba3..54ba7a1 100644 --- a/src/listeners/interaction_create.ts +++ b/src/listeners/interaction_create.ts @@ -50,12 +50,10 @@ async function handleAutocomplete(interaction: AutocompleteInteraction) { if (interaction.options.getSubcommand(false)) options = (options.find(o => o.name === interaction.options.getSubcommand()) as APIApplicationCommandSubcommandOption)?.options as Option[]; - const focused = interaction.options.getFocused(true); const option = options.find(o => o.name === focused.name) as StringOption; if (!option) return; - const context = new AutocompleteContext(option, command, interaction); await option.run(context); }