feat: make docs working

This commit is contained in:
Jozef Steinhübl 2023-08-24 22:56:08 +02:00
parent 5a1042e08f
commit c60793dbff
2 changed files with 57 additions and 14 deletions

View file

@ -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,
});
await context.interaction.editReply({
content: "asdsad"
})
const query = context.interaction.options.getString("query");
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,
}
}

View file

@ -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);
}