Skip to content

Commit

Permalink
Don't totally deprecate !mdn
Browse files Browse the repository at this point in the history
  • Loading branch information
vcarl committed Oct 15, 2024
1 parent 85fa1a7 commit 496b7bb
Showing 1 changed file with 45 additions and 3 deletions.
48 changes: 45 additions & 3 deletions src/features/commands.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable @typescript-eslint/no-use-before-define */
import fetch from "node-fetch";
import {
APIEmbed,
ChannelType,
Expand Down Expand Up @@ -386,9 +388,49 @@ Here's an article explaining the difference between the two: https://goshakkk.na
help: `allows you to search something on MDN, usage: !mdn Array.prototype.map`,
category: "Web",
handleMessage: async (msg) => {
await msg.channel.send(
"The !mdn command has been replaced with a slash command! Try out /mdn",
);
const [, ...args] = msg.content.split(" ");
const query = args.join(" ");
const [fetchMsg, res] = await Promise.all([
msg.channel.send(`Fetching "${query}"...`),
fetch(
`https://developer.mozilla.org/api/v1/search?highlight=false&locale=en-us&q=${query}`,
),
]);

const { documents } = (await res.json()) as {
documents: (
| { title: string; excerpt: string; mdn_url: string }
| undefined
)[];
};
const [topResult] = documents;

if (!topResult) {
fetchMsg.edit(`Could not find anything on MDN for '${query}'`);
return;
}

const { title, excerpt: description, mdn_url: mdnUrl } = topResult;

await msg.channel.send({
content: "-# also did you know there's `/mdn` too",
embeds: [
{
author: {
name: "MDN",
url: "https://developer.mozilla.org",
icon_url:
"https://developer.mozilla.org/favicon-48x48.cbbd161b.png",
},
title,
description,
color: 0x83d0f2,
url: `https://developer.mozilla.org${mdnUrl}`,
},
],
});

fetchMsg.delete();
},
},
{
Expand Down

0 comments on commit 496b7bb

Please sign in to comment.