Skip to content

Commit e7bb5a5

Browse files
Add more comments
1 parent 7af4cd5 commit e7bb5a5

File tree

3 files changed

+58
-33
lines changed

3 files changed

+58
-33
lines changed

src/commands/docs/djs.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Doc, { sources } from "discord.js-docs";
44
import { checkEmbedLimits } from "../../utils/EmbedUtils";
55
import { deleteButton } from "../../utils/CommandUtils";
66
import { MessageActionRow, MessageSelectMenu } from "discord.js";
7-
import { APIEmbed } from "discord-api-types";
7+
import type { APIEmbed } from "discord-api-types";
88

99
const supportedBranches = Object.keys(sources).map((branch) => [capitalize(branch), branch] as [string, string]);
1010

@@ -55,6 +55,8 @@ const command: Command = {
5555
await interaction.reply({ embeds: [embedObj], ephemeral: true }).catch(console.error);
5656
return;
5757
} else if (Array.isArray(result)) {
58+
// If there are multiple results, send a select menu from which the user can choose which one to send
59+
5860
const selectMenuRow = new MessageActionRow().addComponents(
5961
new MessageSelectMenu()
6062
.setCustomId(`djsselect/${source}/${searchPrivate}/${interaction.user.id}`)
@@ -92,15 +94,18 @@ function capitalize(str: string) {
9294
.join("-");
9395
}
9496

97+
// Export to reuse on the select menu handler
9598
export function searchDJSDoc(doc: Doc, query: string, searchPrivate?: boolean) {
9699
const options = { excludePrivateElements: !searchPrivate };
97100

98101
const singleElement = doc.get(...query.split(/\.|#/));
102+
// Return embed for the single element, the exact match
99103
if (singleElement) return singleElement.embed(options);
100104

101105
const searchResults = doc.search(query, options);
102106
if (!searchResults) return null;
103107
return searchResults.map((res) => {
108+
// Labels and values have a limit of 100 characters
104109
const description = res.description.length >= 99 ? res.description.slice(0, 96) + "..." : res.description;
105110
return {
106111
label: res.formattedName,
@@ -110,6 +115,11 @@ export function searchDJSDoc(doc: Doc, query: string, searchPrivate?: boolean) {
110115
};
111116
});
112117
}
118+
/**
119+
* Return the unicode version of the regional emojis
120+
* @param regionalEmoji
121+
* @returns The unicode version of the emoji
122+
*/
113123
function resolveRegionalEmoji(regionalEmoji: string) {
114124
const character = regionalEmoji.match(/:regional_indicator_(.):/)?.[1];
115125
if (!character) return null;

src/commands/docs/mdn.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ const command: Command = {
5858

5959
return;
6060
} else {
61+
// If there are multiple results, send a select menu from which the user can choose which one to send
6162
const results = search.map((path) => `**• [${path.replace(/_|-/g, " ")}](${MDN_BASE_URL}${path})**`);
6263

6364
embed.setDescription(results.join("\n"));
@@ -84,6 +85,7 @@ const command: Command = {
8485
},
8586
};
8687

88+
// Export to reuse on the select menu handler
8789
export async function getSingleMDNSearchResults(searchQuery: string) {
8890
const res = await fetch(`${MDN_BASE_URL + searchQuery}/index.json`);
8991
const doc: MdnDoc = (await res.json()).doc;

src/handlers/CommandHandler.ts

Lines changed: 45 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
1-
import glob from "glob";
21
import type { Command, MyContext } from "../interfaces";
3-
import {
4-
ButtonInteraction,
5-
CommandInteraction,
6-
Interaction,
7-
Message,
8-
MessageActionRow,
9-
SelectMenuInteraction,
10-
} from "discord.js";
2+
import type { ButtonInteraction, CommandInteraction, Interaction, Message, SelectMenuInteraction } from "discord.js";
3+
import type { APIEmbed } from "discord-api-types";
4+
115
import { commandCooldownCheck, commandPermissionCheck, deleteButton } from "../utils/CommandUtils";
126
import { getSingleMDNSearchResults } from "../commands/docs/mdn";
137
import { searchDJSDoc } from "../commands/docs/djs";
8+
import { MessageActionRow } from "discord.js";
9+
10+
import glob from "glob";
1411
import Doc from "discord.js-docs";
15-
import { APIEmbed } from "discord-api-types";
1612

1713
export async function interactionCreateHandler(context: MyContext, interaction: Interaction<"cached">) {
1814
if (interaction.isCommand()) {
@@ -69,32 +65,48 @@ async function commandInteractionHandler(context: MyContext, interaction: Comman
6965
}
7066
async function selectMenuInteractionHandler(context: MyContext, interaction: SelectMenuInteraction) {
7167
const CommandName = interaction.customId.split("/")[0];
72-
if (!CommandName) interaction.reply({ content: "Unknown menu", ephemeral: true }).catch(console.error);
68+
if (!CommandName)
69+
switch (CommandName) {
70+
case "mdnselect": {
71+
const Initiator = interaction.customId.split("/")[1];
72+
const deleteButtonRow = new MessageActionRow().addComponents([deleteButton(Initiator)]);
73+
const selectedValue = interaction.values[0];
74+
const resultEmbed = await getSingleMDNSearchResults(selectedValue);
7375

74-
if (CommandName === "mdnselect") {
75-
const Initiator = interaction.customId.split("/")[1];
76-
const deleteButtonRow = new MessageActionRow().addComponents([deleteButton(Initiator)]);
77-
const selectedValue = interaction.values[0];
78-
const resultEmbed = await getSingleMDNSearchResults(selectedValue);
76+
// Remove the menu and update the ephemeral message
77+
await interaction
78+
.update({ content: "Sent documentation for " + selectedValue, components: [] })
79+
.catch(console.error);
80+
// Send documentation
81+
await interaction
82+
.followUp({ embeds: [resultEmbed], components: [deleteButtonRow] })
83+
.catch(console.error);
84+
break;
85+
}
7986

80-
await interaction
81-
.update({ content: "Sent documentation for " + selectedValue, components: [] })
82-
.catch(console.error);
83-
await interaction.followUp({ embeds: [resultEmbed], components: [deleteButtonRow] }).catch(console.error);
84-
} else if (CommandName === "djsselect") {
85-
const selectedValue = interaction.values[0];
86-
const [, source, searchPrivate, Initiator] = interaction.customId.split("/");
87-
const deleteButtonRow = new MessageActionRow().addComponents([deleteButton(Initiator)]);
87+
case "djsselect": {
88+
const selectedValue = interaction.values[0];
89+
const [, source, searchPrivate, Initiator] = interaction.customId.split("/");
90+
const deleteButtonRow = new MessageActionRow().addComponents([deleteButton(Initiator)]);
8891

89-
const doc = await Doc.fetch(source, { force: true });
92+
const doc = await Doc.fetch(source, { force: true });
9093

91-
const resultEmbed = searchDJSDoc(doc, selectedValue, searchPrivate === "true") as APIEmbed;
94+
const resultEmbed = searchDJSDoc(doc, selectedValue, searchPrivate === "true") as APIEmbed;
9295

93-
await interaction
94-
.update({ content: "Sent documentation for " + selectedValue, components: [] })
95-
.catch(console.error);
96-
await interaction.followUp({ embeds: [resultEmbed], components: [deleteButtonRow] }).catch(console.error);
97-
}
96+
// Remove the menu and update the ephemeral message
97+
await interaction
98+
.update({ content: "Sent documentation for " + selectedValue, components: [] })
99+
.catch(console.error);
100+
// Send documentation
101+
await interaction
102+
.followUp({ embeds: [resultEmbed], components: [deleteButtonRow] })
103+
.catch(console.error);
104+
break;
105+
}
106+
default: {
107+
interaction.reply({ content: "Unknown menu", ephemeral: true }).catch(console.error);
108+
}
109+
}
98110
}
99111
async function buttonInteractionHandler(context: MyContext, interaction: ButtonInteraction<"cached">) {
100112
// The delete button
@@ -112,4 +124,5 @@ async function buttonInteractionHandler(context: MyContext, interaction: ButtonI
112124
.catch(console.error);
113125
}
114126
}
115-
// async function autocompleteInteractionHandler(context: MyContext, interaction: AutocompleteInteraction) {}
127+
// TODO add autocomplete
128+
// async function autocompleteInteractionHandler(context: MyContext, interaction: AutocompleteInteraction) {}s

0 commit comments

Comments
 (0)