Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complement !mdn with /mdn #420

Merged
merged 2 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/features/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ Here's an article explaining the difference between the two: https://goshakkk.na
const { title, excerpt: description, mdn_url: mdnUrl } = topResult;

await msg.channel.send({
content: "-# also did you know there's `/mdn` too",
embeds: [
{
author: {
Expand Down
63 changes: 63 additions & 0 deletions src/features/mdn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { SlashCommandBuilder } from "discord.js";
import { SlashCommand } from "../helpers/discord";

const SOURCE_LINK =
"-# [source](<https://github.com/reactiflux/reactibot/blob/main/src/features/mdn.ts>)";

export const mdnSearch: SlashCommand = {
command: new SlashCommandBuilder()
.setName("mdn")
.setDescription("Link to an MDN resource")
.addStringOption((option) =>
option
.setName("query")
.setDescription("Which page?")
.setRequired(true)
.setAutocomplete(true),
) as SlashCommandBuilder,
handler: async (interaction) => {
const mdnUrl = interaction.options.getString("query", true);

if (mdnUrl.startsWith("/")) {
return await interaction.reply(`https://developer.mozilla.org${mdnUrl}
${SOURCE_LINK}`);
}

const res = await fetch(
`https://developer.mozilla.org/api/v1/search?highlight=false&locale=en-us&q=${mdnUrl}`,
);
const { documents } = (await res.json()) as {
documents?: { title: string; excerpt: string; mdn_url: string }[];
};

return await interaction.reply(
`Maybe one of these?
${documents
?.slice(0, 3)
.map((d) => `- [${d.title}](<https://developer.mozilla.org${d.mdn_url}>)`)
.join("\n")}
${SOURCE_LINK}`,
);
},
autocomplete: async (interaction) => {
const partial = interaction.options.getFocused();

const res = await fetch(
`https://developer.mozilla.org/api/v1/search?highlight=false&locale=en-us&q=${partial}`,
);
const { documents } = (await res.json()) as {
documents?: { title: string; excerpt: string; mdn_url: string }[];
};

await interaction.respond(
documents
? documents
.filter((d) => d.mdn_url.length < 100)
.map((b) => ({
name: b.title,
value: b.mdn_url,
}))
: [],
);
},
};
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import troll from "./features/troll";
import { modActivity } from "./features/mod-activity";
import { debugEventButtonHandler, debugEvents } from "./features/debug-events";
import { recommendBookCommand } from "./features/book-list";
import { mdnSearch } from "./features/mdn";

export const bot = new discord.Client({
intents: [
Expand All @@ -57,6 +58,7 @@ export const bot = new discord.Client({

registerCommand(resetJobCacheCommand);
registerCommand(recommendBookCommand);
registerCommand(mdnSearch);

if (!isProd()) {
registerCommand(debugEvents);
Expand Down
Loading