Skip to content

Commit

Permalink
GithubCommand: Edit initial reply if successful
Browse files Browse the repository at this point in the history
Making the initial deferred reply ephemeral caused the follow-up
messages to show an ugly "Message could not be loaded" at the top, since
the ephemeral message was deleted. And even if we didn't delete it,
other users would not be able to load it.

This changes the initial reply to be visible to everyone and get edited
on success. On failure, the initial reply is deleted again and a
follow-up ephemeral message is posted to the user.
  • Loading branch information
gmta committed Dec 18, 2024
1 parent e8e50a6 commit bd1aa26
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions src/commands/githubCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
type TextBasedChannel,
SlashCommandStringOption,
} from "discord.js";
import { MessageFlags } from "discord-api-types/v10";
import githubAPI, { type Repository, LADYBIRD_REPO } from "@/apis/githubAPI";
import { embedFromIssueOrPull } from "@/util/embedFromIssueOrPull";
import { getSadCaret } from "@/util/emoji";
Expand Down Expand Up @@ -131,7 +132,7 @@ export class GithubCommand extends Command {
}

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

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

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

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

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

await interaction.deleteReply();
const sadcaret = await getSadCaret(interaction);
await interaction.editReply({
await interaction.followUp({
content: `No matching issues or pull requests found ${sadcaret ?? ":^("}`,
flags: MessageFlags.Ephemeral,
});
}
}
Expand All @@ -206,7 +206,7 @@ export class ReviewListCommand extends Command {
}

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

const repositoryName = interaction.options.getString("repository");
const unparsedNumbers = interaction.options.getString("numbers");
Expand All @@ -223,10 +223,12 @@ export class ReviewListCommand extends Command {
}

if (unparsedNumbers === null) {
await interaction.editReply({
await interaction.deleteReply();
await interaction.followUp({
content: `No matching issues or pull requests found ${
(await getSadCaret(interaction)) ?? ":^("
}`,
flags: MessageFlags.Ephemeral,
});
return undefined;
}
Expand All @@ -236,10 +238,12 @@ export class ReviewListCommand extends Command {
);

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

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

await interaction.deleteReply();
await interaction.followUp({ content: descriptionList });
await interaction.editReply({ content: descriptionList });
}
}

0 comments on commit bd1aa26

Please sign in to comment.