Skip to content

Commit b7d3f13

Browse files
refactor replies to always defer
1 parent c12a559 commit b7d3f13

File tree

3 files changed

+24
-21
lines changed

3 files changed

+24
-21
lines changed

src/commands/docs/djs.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const command: Command = {
4343
const doc = await Doc.fetch(source, { force: true }).catch(console.error);
4444

4545
if (!doc) {
46-
await interaction.reply({ content: "Couldn't fetch docs", ephemeral: true }).catch(console.error);
46+
await interaction.editReply({ content: "Couldn't fetch docs" }).catch(console.error);
4747
return;
4848
}
4949
// const resultEmbed = doc.resolveEmbed(query, { excludePrivateElements: !searchPrivate });
@@ -57,7 +57,7 @@ const command: Command = {
5757
// Satisfies the method's MessageEmbedOption type
5858
const embedObj = { ...notFoundEmbed, timestamp: timeStampDate };
5959

60-
await interaction.reply({ embeds: [embedObj], ephemeral: true }).catch(console.error);
60+
await interaction.editReply({ embeds: [embedObj] }).catch(console.error);
6161
return;
6262
} else if (Array.isArray(result)) {
6363
// If there are multiple results, send a select menu from which the user can choose which one to send
@@ -69,9 +69,8 @@ const command: Command = {
6969
.setPlaceholder("Select documentation to send"),
7070
);
7171
await interaction
72-
.reply({
72+
.editReply({
7373
content: "Didn't find an exact match, please select one from below",
74-
ephemeral: true,
7574
components: [selectMenuRow],
7675
})
7776
.catch(console.error);
@@ -87,7 +86,10 @@ const command: Command = {
8786
// The final field should be the View Source button
8887
embedObj.fields = [embedObj.fields?.at(-1)];
8988
}
90-
await interaction.reply({ embeds: [embedObj], components: [deleteButtonRow] }).catch(console.error);
89+
await interaction.editReply({
90+
content: "Sent documentations for " + (query.length >= 100 ? query.slice(0, 100) + "..." : query),
91+
});
92+
await interaction.followUp({ embeds: [embedObj], components: [deleteButtonRow] }).catch(console.error);
9193
return;
9294
},
9395
};

src/commands/docs/mdn.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,19 @@ const command: Command = {
4646

4747
if (!search.length) {
4848
embed.setColor(0xff0000).setDescription("No results found...");
49-
await interaction.reply({ embeds: [embed], ephemeral: true }).catch(console.error);
49+
await interaction.editReply({ embeds: [embed] }).catch(console.error);
5050
return;
5151
} else if (search.length === 1) {
5252
const resultEmbed = await getSingleMDNSearchResults(search[0]);
5353
if (!resultEmbed) {
54-
await interaction.reply({ content: "Couldn't find any results", ephemeral: true }).catch(console.error);
54+
await interaction.editReply({ content: "Couldn't find any results" }).catch(console.error);
5555
return;
5656
}
57-
5857
await interaction
59-
.reply({
58+
.editReply("Sent documentation for " + (query.length >= 100 ? query.slice(0, 100) + "..." : query))
59+
.catch(console.error);
60+
await interaction
61+
.followUp({
6062
embeds: [resultEmbed],
6163
components: [deleteButtonRow],
6264
})
@@ -77,9 +79,8 @@ const command: Command = {
7779
.setPlaceholder("Select documentation to send"),
7880
);
7981
await interaction
80-
.reply({
82+
.editReply({
8183
content: "Didn't find an exact match, please select one from below",
82-
ephemeral: true,
8384
components: [selectMenuRow],
8485
})
8586
.catch(console.error);

src/handlers/InteractionCreateHandler.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,10 @@ export function loadCommands(context: MyContext) {
5858
});
5959
}
6060
async function commandInteractionHandler(context: MyContext, interaction: CommandInteraction) {
61+
await interaction.deferReply({ ephemeral: true }).catch(console.error);
62+
6163
const command = context.commands.get(interaction.commandName);
62-
if (!command) return interaction.reply({ content: "Command not found", ephemeral: true });
64+
if (!command) return interaction.editReply({ content: "Command not found" }).catch(console.error);
6365

6466
if (commandPermissionCheck(interaction, command)) return;
6567
if (commandCooldownCheck(interaction, command, context)) return;
@@ -68,15 +70,13 @@ async function commandInteractionHandler(context: MyContext, interaction: Comman
6870
} catch (e) {
6971
console.error(e);
7072
const errorMessage = "An error has occurred";
71-
interaction
72-
.reply({
73-
content: errorMessage,
74-
ephemeral: true,
75-
})
76-
.catch(console.error);
73+
await interaction[interaction.replied ? "editReply" : "reply"]?.({
74+
content: errorMessage,
75+
}).catch(console.error);
7776
}
7877
}
7978
async function selectMenuInteractionHandler(context: MyContext, interaction: SelectMenuInteraction) {
79+
await interaction.deferUpdate().catch(console.error);
8080
const CommandName = interaction.customId.split("/")[0];
8181
switch (CommandName) {
8282
case "mdnselect": {
@@ -87,7 +87,7 @@ async function selectMenuInteractionHandler(context: MyContext, interaction: Sel
8787

8888
// Remove the menu and update the ephemeral message
8989
await interaction
90-
.update({ content: "Sent documentations for " + selectedValue, components: [] })
90+
.editReply({ content: "Sent documentations for " + selectedValue, components: [] })
9191
.catch(console.error);
9292
// Send documentation
9393
await interaction.followUp({ embeds: [resultEmbed], components: [deleteButtonRow] }).catch(console.error);
@@ -105,14 +105,14 @@ async function selectMenuInteractionHandler(context: MyContext, interaction: Sel
105105

106106
// Remove the menu and update the ephemeral message
107107
await interaction
108-
.update({ content: "Sent documentations for " + selectedValue, components: [] })
108+
.editReply({ content: "Sent documentations for " + selectedValue, components: [] })
109109
.catch(console.error);
110110
// Send documentation
111111
await interaction.followUp({ embeds: [resultEmbed], components: [deleteButtonRow] }).catch(console.error);
112112
break;
113113
}
114114
default: {
115-
interaction.reply({ content: "Unknown menu", ephemeral: true }).catch(console.error);
115+
interaction.editReply({ content: "Unknown menu" }).catch(console.error);
116116
}
117117
}
118118
}

0 commit comments

Comments
 (0)