Skip to content

Commit

Permalink
GithubCommand: Defer all initial replies
Browse files Browse the repository at this point in the history
We need to respond to Discord's chat interaction request within 3
seconds. If we don't do so, we sometimes end up in a perpetual broken
state where chat commands simply stop working.

This changes GithubCommand to always send out an ephemeral initial
deferred reply, which then either gets deleted and replaced with the
actual reply or edited when something goes wrong. We need to delete the
initial deferred reply, since the ephemeral status of a message cannot
be changed with `.editReply()`.
  • Loading branch information
gmta committed Dec 18, 2024
1 parent 0cf0992 commit 6e3af34
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/commands/githubCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ export class GithubCommand extends Command {
}

override async handleCommand(interaction: ChatInputCommandInteraction): Promise<void> {
await interaction.deferReply({ ephemeral: true });

const url = interaction.options.getString("url");
const repositoryName = interaction.options.getString("repository");
const number = interaction.options.getNumber("number");
Expand All @@ -148,6 +150,7 @@ export class GithubCommand extends Command {
);

if (result) {
await interaction.deleteReply();
await interaction.reply({ embeds: [result] });
return;
}
Expand All @@ -161,6 +164,7 @@ export class GithubCommand extends Command {
const result = await embedFromIssueOrPull(await githubAPI.getIssueOrPull(number, repository));

if (result) {
await interaction.deleteReply();
await interaction.reply({ embeds: [result] });
return;
}
Expand All @@ -172,15 +176,15 @@ export class GithubCommand extends Command {
);

if (result) {
await interaction.deleteReply();
await interaction.reply({ embeds: [result] });
return;
}
}

const sadcaret = await getSadCaret(interaction);
await interaction.reply({
await interaction.editReply({
content: `No matching issues or pull requests found ${sadcaret ?? ":^("}`,
ephemeral: true,
});
}
}
Expand All @@ -202,6 +206,8 @@ export class ReviewListCommand extends Command {
}

override async handleCommand(interaction: ChatInputCommandInteraction): Promise<void> {
await interaction.deferReply({ ephemeral: true });

const repositoryName = interaction.options.getString("repository");
const unparsedNumbers = interaction.options.getString("numbers");

Expand All @@ -217,11 +223,10 @@ export class ReviewListCommand extends Command {
}

if (unparsedNumbers === null) {
await interaction.reply({
await interaction.editReply({
content: `No matching issues or pull requests found ${
(await getSadCaret(interaction)) ?? ":^("
}`,
ephemeral: true,
});
return undefined;
}
Expand All @@ -231,11 +236,10 @@ export class ReviewListCommand extends Command {
);

if (numbers.length === 0) {
await interaction.reply({
await interaction.editReply({
content: `No numbers found in the PR list text '${unparsedNumbers}' ${
(await getSadCaret(interaction)) ?? ":^("
} `,
ephemeral: true,
});
return undefined;
}
Expand All @@ -248,19 +252,17 @@ export class ReviewListCommand extends Command {
);
const failedDescriptions = descriptions.filter(({ description }) => description === undefined);
if (failedDescriptions.length !== 0) {
await interaction.reply({
await interaction.editReply({
content: `No matching issues or pull requests found for the numbers ${failedDescriptions
.map(({ number }) => number)
.join(", ")} ${(await getSadCaret(interaction)) ?? ":^("} `,
ephemeral: true,
});
return undefined;
}

const descriptionList = descriptions.map(({ description }) => description).join("\n");

await interaction.reply({
content: descriptionList,
});
await interaction.deleteReply();
await interaction.reply({ content: descriptionList });
}
}

0 comments on commit 6e3af34

Please sign in to comment.