mirror of
https://github.com/xHyroM/bun-discord-bot.git
synced 2024-12-22 12:11:06 +01:00
feat: make docs working
This commit is contained in:
parent
5a1042e08f
commit
c60793dbff
2 changed files with 57 additions and 14 deletions
|
@ -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,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue