|
1 | | -import { Command } from "discord-akairo"; |
2 | | -import { Message } from "discord.js"; |
| 1 | +import { MessageEmbed } from "discord.js"; |
3 | 2 | import { gunzipSync } from "zlib"; |
4 | 3 | import { XMLParser } from "fast-xml-parser"; |
5 | | -import BotClient from "../../client/client"; |
6 | 4 | import fetch from "node-fetch"; |
7 | 5 | import flexsearch from "flexsearch"; |
8 | 6 |
|
| 7 | +import { Command } from "../../interfaces"; |
| 8 | +import { SlashCommandBuilder } from "@discordjs/builders"; |
9 | 9 | interface SitemapEntry<T extends string | number> { |
10 | | - loc: string; |
11 | | - lastmod: T; |
| 10 | + loc: string; |
| 11 | + lastmod: T; |
12 | 12 | } |
13 | 13 | type Sitemap<T extends string | number> = SitemapEntry<T>[]; |
14 | 14 |
|
15 | 15 | const baseURL = "https://developer.mozilla.org/en-US/docs/" as const; |
16 | 16 | let sources = { |
17 | | - index: null as flexsearch.Index, |
18 | | - sitemap: null as Sitemap<number>, |
19 | | - lastUpdated: null as number, |
| 17 | + index: null as flexsearch.Index, |
| 18 | + sitemap: null as Sitemap<number>, |
| 19 | + lastUpdated: null as number, |
20 | 20 | }; |
| 21 | +const MDN_BLUE_COLOR = 0x83bfff as const; |
| 22 | +const MDN_ICON_URL = "https://i.imgur.com/1P4wotC.png" as const; |
| 23 | +const command: Command = { |
| 24 | + data: new SlashCommandBuilder() |
| 25 | + .setName("mdn") |
| 26 | + .setDescription("Searches MDN documentation.") |
| 27 | + .addStringOption((opt) => |
| 28 | + opt |
| 29 | + .setName("query") |
| 30 | + .setDescription("Enter the phrase you'd like to search for. Example: Array.filter") |
| 31 | + .setRequired(true), |
| 32 | + ), |
| 33 | + async execute(interaction) { |
| 34 | + const query = interaction.options.getString("query"); |
| 35 | + const { index, sitemap } = await getSources(); |
| 36 | + const search: string[] = index.search(query, { limit: 10 }).map((id) => sitemap[<number>id].loc); |
| 37 | + const embed = new MessageEmbed() |
| 38 | + .setColor(MDN_BLUE_COLOR) |
| 39 | + .setAuthor("MDN Documentation", MDN_ICON_URL) |
| 40 | + .setTitle(`Search for: ${query}`); |
21 | 41 |
|
22 | | -async function getSources(): Promise<typeof sources> { |
23 | | - if (sources.lastUpdated && Date.now() - sources.lastUpdated < 43200000 /* 12 hours */) return sources; |
24 | | - |
25 | | - const res = await fetch("https://developer.mozilla.org/sitemaps/en-us/sitemap.xml.gz"); |
26 | | - if (!res.ok) return sources; // Fallback to old sources if the new ones are not available for any reason |
27 | | - |
28 | | - const sitemap: Sitemap<number> = new XMLParser() |
29 | | - .parse(gunzipSync(await res.buffer()).toString()).urlset.url |
30 | | - .map((entry: SitemapEntry<string>) => ({ |
31 | | - loc: entry.loc.slice(baseURL.length), |
32 | | - lastmod: new Date(entry.lastmod).valueOf() |
33 | | - })); |
34 | | - |
35 | | - const index = new flexsearch.Index(); |
36 | | - sitemap.forEach((entry, idx) => index.add(idx, entry.loc)); |
| 42 | + if (!search.length) { |
| 43 | + embed.setColor(0xff0000).setDescription("No results found..."); |
| 44 | + interaction.editReply({ embeds: [embed] }); |
| 45 | + return; |
| 46 | + } |
37 | 47 |
|
38 | | - sources = { index, sitemap, lastUpdated: Date.now() }; |
39 | | - return sources; |
40 | | -} |
| 48 | + if (search.length === 1) { |
| 49 | + const res = await fetch(`${baseURL + search[0]}/index.json`); |
| 50 | + const doc: MdnDoc = (await res.json()).doc; |
| 51 | + const docEmbed = embed |
| 52 | + .setColor(0xffffff) |
| 53 | + .setTitle(doc.pageTitle) |
| 54 | + .setURL(`https://developer.mozilla.org/${doc.mdn_url}`) |
| 55 | + .setThumbnail(this.MDN_ICON_URL) |
| 56 | + .setDescription(doc.summary); |
| 57 | + interaction.editReply({ embeds: [docEmbed] }); |
| 58 | + return; |
| 59 | + } |
41 | 60 |
|
42 | | -export default class MdnCommand extends Command { |
43 | | - public client: BotClient; |
44 | | - private MDN_BLUE_COLOR = 0x83BFFF as const; |
45 | | - private MDN_ICON_URL = "https://i.imgur.com/1P4wotC.png" as const; |
| 61 | + const results = search.map((path) => `**• [${path.replace(/_|-/g, " ")}](${baseURL}${path})**`); |
| 62 | + embed.setDescription(results.join("\n")); |
| 63 | + interaction.editReply({ embeds: [embed] }); |
| 64 | + return; |
| 65 | + }, |
| 66 | +}; |
46 | 67 |
|
47 | | - public constructor() { |
48 | | - super("mdn-docs", { |
49 | | - aliases: ["mdn", "mdndocs"], |
50 | | - description: { |
51 | | - content: "Searches MDN documentation.", |
52 | | - usage: "<query>", |
53 | | - examples: ['TODO'] |
54 | | - }, |
55 | | - channel: "guild", |
56 | | - clientPermissions: ["EMBED_LINKS"], |
57 | | - ratelimit: 2, |
58 | | - args: [ |
59 | | - { |
60 | | - id: "query", |
61 | | - match: "rest", |
62 | | - type: "string", |
63 | | - prompt: { |
64 | | - start: "```\n" + "Enter the phrase you'd like to search for.\n" + "Example: Array.filter" + "```", |
65 | | - retry: "Not a valid search phrase.", |
66 | | - }, |
67 | | - } |
68 | | - ], |
69 | | - }); |
70 | | - } |
| 68 | +async function getSources(): Promise<typeof sources> { |
| 69 | + if (sources.lastUpdated && Date.now() - sources.lastUpdated < 43200000 /* 12 hours */) return sources; |
71 | 70 |
|
72 | | - public async exec(message: Message, { query }: { query: string }): Promise<Message | Message[]> { |
73 | | - const { index, sitemap } = await getSources(); |
74 | | - const search: string[] = index.search(query, { limit: 10 }).map((id) => sitemap[<number>id].loc); |
75 | | - const embed = this.client.util |
76 | | - .embed() |
77 | | - .setColor(this.MDN_BLUE_COLOR) |
78 | | - .setAuthor("MDN Documentation", this.MDN_ICON_URL) |
79 | | - .setTitle(`Search for: ${query}`); |
| 71 | + const res = await fetch("https://developer.mozilla.org/sitemaps/en-us/sitemap.xml.gz"); |
| 72 | + if (!res.ok) return sources; // Fallback to old sources if the new ones are not available for any reason |
80 | 73 |
|
81 | | - if (!search.length) { |
82 | | - embed.setColor(0xFF0000).setDescription("No results found..."); |
83 | | - return message.util.send(embed); |
84 | | - } |
| 74 | + const sitemap: Sitemap<number> = new XMLParser() |
| 75 | + .parse(gunzipSync(await res.buffer()).toString()) |
| 76 | + .urlset.url.map((entry: SitemapEntry<string>) => ({ |
| 77 | + loc: entry.loc.slice(baseURL.length), |
| 78 | + lastmod: new Date(entry.lastmod).valueOf(), |
| 79 | + })); |
85 | 80 |
|
86 | | - if (search.length === 1) { |
87 | | - const res = await fetch(`${baseURL + search[0]}/index.json`); |
88 | | - const doc: MdnDoc = (await res.json()).doc; |
89 | | - const docEmbed = this.client.util |
90 | | - .embed() |
91 | | - .setColor(0xFFFFFF) |
92 | | - .setTitle(doc.pageTitle) |
93 | | - .setURL(`https://developer.mozilla.org/${doc.mdn_url}`) |
94 | | - .setThumbnail(this.MDN_ICON_URL) |
95 | | - .setDescription(doc.summary); |
96 | | - return message.util.send(docEmbed); |
97 | | - } |
| 81 | + const index = new flexsearch.Index(); |
| 82 | + sitemap.forEach((entry, idx) => index.add(idx, entry.loc)); |
98 | 83 |
|
99 | | - let results = search.map((path) => `**• [${path.replace(/_|-/g, " ")}](${baseURL}${path})**`); |
100 | | - embed.setDescription(results.join("\n")); |
101 | | - return message.util.send(embed); |
102 | | - } |
| 84 | + sources = { index, sitemap, lastUpdated: Date.now() }; |
| 85 | + return sources; |
103 | 86 | } |
104 | 87 |
|
105 | 88 | interface MdnDoc { |
106 | | - isMarkdown: boolean, |
107 | | - isTranslated: boolean, |
108 | | - isActive: boolean, |
109 | | - flaws: {}, |
110 | | - title: string, |
111 | | - mdn_url: string, |
112 | | - locale: string, |
113 | | - native: string, |
114 | | - sidebarHTML: string, |
115 | | - body: ({ |
116 | | - type: "prose" | "specifications" | "browser_compatibility", |
117 | | - value: { |
118 | | - id: string | null, |
119 | | - title: string | null, |
120 | | - isH3: boolean, |
121 | | - // type:prose |
122 | | - content?: string, |
123 | | - // type:specifications |
124 | | - specifications?: ({ bcdSpecificationURL: string, title: string, shortTitle: string })[], |
125 | | - // type:browser_compatibility |
126 | | - dataURL?: string, |
127 | | - // type:specifications | type:browser_compatibility |
128 | | - query?: string, |
129 | | - } |
130 | | - })[], |
131 | | - toc: ({ text: string, id: string })[], |
132 | | - summary: string, |
133 | | - popularity: number, |
134 | | - modified: string, // ISO Date String |
135 | | - other_translations: ({ title: string, locale: string, native: string })[], |
136 | | - source: { |
137 | | - folder: string, |
138 | | - github_url: string, |
139 | | - last_commit_url: string, |
140 | | - filename: string |
141 | | - }, |
142 | | - parents: ({ uri: string, title: string })[], |
143 | | - pageTitle: string, |
144 | | - noIndexing: boolean |
| 89 | + isMarkdown: boolean; |
| 90 | + isTranslated: boolean; |
| 91 | + isActive: boolean; |
| 92 | + //TODO Fix this |
| 93 | + // eslint-disable-next-line |
| 94 | + flaws: {}; |
| 95 | + title: string; |
| 96 | + mdn_url: string; |
| 97 | + locale: string; |
| 98 | + native: string; |
| 99 | + sidebarHTML: string; |
| 100 | + body: { |
| 101 | + type: "prose" | "specifications" | "browser_compatibility"; |
| 102 | + value: { |
| 103 | + id: string | null; |
| 104 | + title: string | null; |
| 105 | + isH3: boolean; |
| 106 | + // type:prose |
| 107 | + content?: string; |
| 108 | + // type:specifications |
| 109 | + specifications?: { bcdSpecificationURL: string; title: string; shortTitle: string }[]; |
| 110 | + // type:browser_compatibility |
| 111 | + dataURL?: string; |
| 112 | + // type:specifications | type:browser_compatibility |
| 113 | + query?: string; |
| 114 | + }; |
| 115 | + }[]; |
| 116 | + toc: { text: string; id: string }[]; |
| 117 | + summary: string; |
| 118 | + popularity: number; |
| 119 | + modified: string; // ISO Date String |
| 120 | + other_translations: { title: string; locale: string; native: string }[]; |
| 121 | + source: { |
| 122 | + folder: string; |
| 123 | + github_url: string; |
| 124 | + last_commit_url: string; |
| 125 | + filename: string; |
| 126 | + }; |
| 127 | + parents: { uri: string; title: string }[]; |
| 128 | + pageTitle: string; |
| 129 | + noIndexing: boolean; |
145 | 130 | } |
| 131 | +export default command; |
0 commit comments