mirror of
https://github.com/xHyroM/bun-discord-bot.git
synced 2024-11-10 01:08:07 +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";
|
import algoliasearch from "algoliasearch";
|
||||||
|
|
||||||
const algoliaClient = algoliasearch("2527C13E0N", "4efc87205e1fce4a1f267cadcab42cb2");
|
const algoliaClient = algoliasearch("2527C13E0N", "4efc87205e1fce4a1f267cadcab42cb2");
|
||||||
|
const algoliaIndex = algoliaClient.initIndex("bun");
|
||||||
|
|
||||||
defineCommand({
|
defineCommand({
|
||||||
name: "docs",
|
name: "docs",
|
||||||
|
@ -17,22 +18,66 @@ defineCommand({
|
||||||
.setAutocomplete(true)
|
.setAutocomplete(true)
|
||||||
.setDescription("Select query")
|
.setDescription("Select query")
|
||||||
.toJSON(),
|
.toJSON(),
|
||||||
run: (context: AutocompleteContext) => {
|
run: async(context: AutocompleteContext) => {
|
||||||
return context.respond([{ name: "heh", value: "heh" }]);
|
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) => {
|
run: async(context: InteractionCommandContext) => {
|
||||||
await context.interaction.deferReply();
|
await context.interaction.deferReply({
|
||||||
console.log(await algoliaClient.search([
|
ephemeral: true,
|
||||||
{
|
});
|
||||||
query: "qwe",
|
|
||||||
indexName: "bun"
|
const query = context.interaction.options.getString("query");
|
||||||
}
|
|
||||||
]));
|
|
||||||
|
|
||||||
await context.interaction.editReply({
|
const result = await algoliaIndex.search(query, {
|
||||||
content: "asdsad"
|
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))
|
if (interaction.options.getSubcommand(false))
|
||||||
options = (options.find(o => o.name === interaction.options.getSubcommand()) as APIApplicationCommandSubcommandOption)?.options as Option[];
|
options = (options.find(o => o.name === interaction.options.getSubcommand()) as APIApplicationCommandSubcommandOption)?.options as Option[];
|
||||||
|
|
||||||
|
|
||||||
const focused = interaction.options.getFocused(true);
|
const focused = interaction.options.getFocused(true);
|
||||||
const option = options.find(o => o.name === focused.name) as StringOption;
|
const option = options.find(o => o.name === focused.name) as StringOption;
|
||||||
if (!option) return;
|
if (!option) return;
|
||||||
|
|
||||||
|
|
||||||
const context = new AutocompleteContext(option, command, interaction);
|
const context = new AutocompleteContext(option, command, interaction);
|
||||||
await option.run(context);
|
await option.run(context);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue