aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMax Isom <[email protected]>2021-12-16 15:18:50 -0500
committerMax Isom <[email protected]>2021-12-21 21:02:07 -0600
commit812d01edbd929c4d6140ed54a49cd493e9ab2b06 (patch)
tree40e0cd4a088bc7819ca46ddafcfd53f40b730f14 /src
parent09908f4af55f02447e3fe9227f41b63c3094d212 (diff)
downloadmuse-812d01edbd929c4d6140ed54a49cd493e9ab2b06.tar.xz
muse-812d01edbd929c4d6140ed54a49cd493e9ab2b06.zip
Handle if queue reply is deleted
Diffstat (limited to 'src')
-rw-r--r--src/bot.ts13
-rw-r--r--src/services/updating-queue-embed.ts53
2 files changed, 51 insertions, 15 deletions
diff --git a/src/bot.ts b/src/bot.ts
index 6152d2f..b9c31a7 100644
--- a/src/bot.ts
+++ b/src/bot.ts
@@ -70,11 +70,14 @@ export default class {
} catch (error: unknown) {
debug(error);
- if (interaction.replied || interaction.deferred) {
- await interaction.editReply(errorMsg('something went wrong'));
- } else {
- await interaction.reply({content: errorMsg(error as Error), ephemeral: true});
- }
+ // This can fail if the message was deleted, and we don't want to crash the whole bot
+ try {
+ if (interaction.replied || interaction.deferred) {
+ await interaction.editReply(errorMsg('something went wrong'));
+ } else {
+ await interaction.reply({content: errorMsg(error as Error), ephemeral: true});
+ }
+ } catch {}
}
});
diff --git a/src/services/updating-queue-embed.ts b/src/services/updating-queue-embed.ts
index 31d58ff..6496fb5 100644
--- a/src/services/updating-queue-embed.ts
+++ b/src/services/updating-queue-embed.ts
@@ -1,4 +1,4 @@
-import {CommandInteraction, MessageActionRow, MessageButton, MessageEmbed} from 'discord.js';
+import {CommandInteraction, MessageActionRow, MessageButton, MessageEmbed, DiscordAPIError} from 'discord.js';
import getYouTubeID from 'get-youtube-id';
import getProgressBar from '../utils/get-progress-bar.js';
import {prettyTime} from '../utils/time.js';
@@ -40,13 +40,23 @@ export default class {
* @param interaction
*/
async createFromInteraction(interaction: CommandInteraction) {
+ const oldInteraction = this.interaction;
+
+ this.resetState();
+
this.interaction = interaction;
- this.currentPage = 1;
- await interaction.reply({
- embeds: [this.buildEmbed()],
- components: this.buildButtons(this.player),
- });
+ await Promise.all([
+ interaction.reply({
+ embeds: [this.buildEmbed()],
+ components: this.buildButtons(this.player),
+ }),
+ (async () => {
+ if (oldInteraction) {
+ await oldInteraction.deleteReply();
+ }
+ })(),
+ ]);
if (!this.refreshTimeout) {
this.refreshTimeout = setInterval(async () => this.update(), REFRESH_INTERVAL_MS);
@@ -58,10 +68,23 @@ export default class {
this.currentPage = 1;
}
- await this.interaction?.editReply({
- embeds: [this.buildEmbed()],
- components: this.buildButtons(this.player),
- });
+ try {
+ await this.interaction?.editReply({
+ embeds: [this.buildEmbed()],
+ components: this.buildButtons(this.player),
+ });
+ } catch (error: unknown) {
+ if (error instanceof DiscordAPIError) {
+ // Interaction / message was deleted
+ if (error.code === 10008) {
+ this.resetState();
+
+ return;
+ }
+ }
+
+ throw error;
+ }
}
async pageBack() {
@@ -80,6 +103,16 @@ export default class {
await this.update();
}
+ private resetState() {
+ if (this.refreshTimeout) {
+ clearInterval(this.refreshTimeout);
+ this.refreshTimeout = undefined;
+ }
+
+ this.currentPage = 1;
+ this.interaction = undefined;
+ }
+
private buildButtons(player: Player): MessageActionRow[] {
const queuePageControls = new MessageActionRow()
.addComponents(