3
0
Fork 0
mirror of https://github.com/ZeppelinBot/Zeppelin.git synced 2025-07-07 19:17:19 +00:00

fix: interaction error handling

This commit is contained in:
Obliie 2023-07-16 00:38:35 +01:00
parent 7f2f2c8f98
commit 8fcbd50ea1
No known key found for this signature in database
GPG key ID: 9189A18F0D5B547E
6 changed files with 217 additions and 155 deletions

View file

@ -9,6 +9,7 @@ import {
} from "discord.js";
import { GuildPluginData, guildPluginUserContextMenuCommand } from "knub";
import { Case } from "../../../data/entities/Case";
import { logger } from "../../../logger";
import { SECONDS, UnknownUser, emptyEmbedValue, renderUserUsername, resolveUser, trimLines } from "../../../utils";
import { asyncMap } from "../../../utils/async";
import { getChunkedEmbedFields } from "../../../utils/getChunkedEmbedFields";
@ -36,7 +37,9 @@ const CASES_PER_PAGE = 10;
export const ModMenuCmd = guildPluginUserContextMenuCommand({
name: "Mod Menu",
async run({ pluginData, interaction }) {
await interaction.deferReply({ ephemeral: true });
await interaction
.deferReply({ ephemeral: true })
.catch((err) => logger.error(`Mod menu interaction defer failed: ${err}`));
// Run permission checks for executing user.
const executingMember = await pluginData.guild.members.fetch(interaction.user.id);
@ -49,13 +52,17 @@ export const ModMenuCmd = guildPluginUserContextMenuCommand({
!userCfg.can_use ||
(await !utility.hasPermission(executingMember, interaction.channelId, "can_open_mod_menu"))
) {
await interaction.followUp({ content: "Error: Insufficient Permissions" });
await interaction
.followUp({ content: "Error: Insufficient Permissions" })
.catch((err) => logger.error(`Mod menu interaction follow up failed: ${err}`));
return;
}
const user = await resolveUser(pluginData.client, interaction.targetId);
if (!user.id) {
await interaction.followUp("Error: User not found");
await interaction
.followUp("Error: User not found")
.catch((err) => logger.error(`Mod menu interaction follow up failed: ${err}`));
return;
}
@ -120,7 +127,7 @@ async function displayModMenu(
infoEmbed: APIEmbed | null,
) {
if (interaction.deferred == false) {
await interaction.deferReply();
await interaction.deferReply().catch((err) => logger.error(`Mod menu interaction defer failed: ${err}`));
}
const firstButton = new ButtonBuilder()
@ -177,98 +184,109 @@ async function displayModMenu(
const moderationRow = new ActionRowBuilder<ButtonBuilder>().addComponents(moderationButtons);
let page = 1;
const currentPage = await interaction.editReply({
embeds: [await loadPage(page)],
components: [navigationRow, moderationRow],
});
const collector = await currentPage.createMessageComponentCollector({
time: MOD_MENU_TIMEOUT,
});
collector.on("collect", async (i) => {
const opts = deserializeCustomId(i.customId);
if (opts.action == ModMenuActionType.PAGE) {
await i.deferUpdate();
}
// Update displayed embed if any navigation buttons were used
if (opts.action == ModMenuActionType.PAGE && opts.target == ModMenuNavigationType.INFO && infoEmbed != null) {
infoButton
.setLabel("Cases")
.setCustomId(serializeCustomId({ action: ModMenuActionType.PAGE, target: ModMenuNavigationType.CASES }));
firstButton.setDisabled(true);
prevButton.setDisabled(true);
nextButton.setDisabled(true);
lastButton.setDisabled(true);
await i.editReply({
embeds: [infoEmbed],
components: [navigationRow, moderationRow],
await interaction
.editReply({
embeds: [await loadPage(page)],
components: [navigationRow, moderationRow],
})
.then(async (currentPage) => {
const collector = await currentPage.createMessageComponentCollector({
time: MOD_MENU_TIMEOUT,
});
} else if (opts.action == ModMenuActionType.PAGE && opts.target == ModMenuNavigationType.CASES) {
infoButton
.setLabel("Info")
.setCustomId(serializeCustomId({ action: ModMenuActionType.PAGE, target: ModMenuNavigationType.INFO }));
updateNavButtonState(firstButton, prevButton, nextButton, lastButton, page, totalPages);
await i.editReply({
embeds: [await loadPage(page)],
components: [navigationRow, moderationRow],
collector.on("collect", async (i) => {
const opts = deserializeCustomId(i.customId);
if (opts.action == ModMenuActionType.PAGE) {
await i.deferUpdate().catch((err) => logger.error(`Mod menu defer failed: ${err}`));
}
// Update displayed embed if any navigation buttons were used
if (opts.action == ModMenuActionType.PAGE && opts.target == ModMenuNavigationType.INFO && infoEmbed != null) {
infoButton
.setLabel("Cases")
.setCustomId(serializeCustomId({ action: ModMenuActionType.PAGE, target: ModMenuNavigationType.CASES }));
firstButton.setDisabled(true);
prevButton.setDisabled(true);
nextButton.setDisabled(true);
lastButton.setDisabled(true);
await i
.editReply({
embeds: [infoEmbed],
components: [navigationRow, moderationRow],
})
.catch((err) => logger.error(`Mod menu info view failed: ${err}`));
} else if (opts.action == ModMenuActionType.PAGE && opts.target == ModMenuNavigationType.CASES) {
infoButton
.setLabel("Info")
.setCustomId(serializeCustomId({ action: ModMenuActionType.PAGE, target: ModMenuNavigationType.INFO }));
updateNavButtonState(firstButton, prevButton, nextButton, lastButton, page, totalPages);
await i
.editReply({
embeds: [await loadPage(page)],
components: [navigationRow, moderationRow],
})
.catch((err) => logger.error(`Mod menu cases view failed: ${err}`));
} else if (opts.action == ModMenuActionType.PAGE) {
let pageDelta = 0;
switch (opts.target) {
case ModMenuNavigationType.PREV:
pageDelta = -1;
break;
case ModMenuNavigationType.NEXT:
pageDelta = 1;
break;
}
let newPage = 1;
if (opts.target == ModMenuNavigationType.PREV || opts.target == ModMenuNavigationType.NEXT) {
newPage = Math.max(Math.min(page + pageDelta, totalPages), 1);
} else if (opts.target == ModMenuNavigationType.FIRST) {
newPage = 1;
} else if (opts.target == ModMenuNavigationType.LAST) {
newPage = totalPages;
}
if (newPage != page) {
updateNavButtonState(firstButton, prevButton, nextButton, lastButton, newPage, totalPages);
await i
.editReply({
embeds: [await loadPage(newPage)],
components: [navigationRow, moderationRow],
})
.catch((err) => logger.error(`Mod menu navigation failed: ${err}`));
page = newPage;
}
} else if (opts.action == ModMenuActionType.NOTE) {
await launchNoteActionModal(pluginData, i as ButtonInteraction, opts.target);
} else if (opts.action == ModMenuActionType.WARN) {
await launchWarnActionModal(pluginData, i as ButtonInteraction, opts.target);
} else if (opts.action == ModMenuActionType.CLEAN) {
await launchCleanActionModal(pluginData, i as ButtonInteraction, opts.target);
} else if (opts.action == ModMenuActionType.MUTE) {
await launchMuteActionModal(pluginData, i as ButtonInteraction, opts.target);
} else if (opts.action == ModMenuActionType.BAN) {
await launchBanActionModal(pluginData, i as ButtonInteraction, opts.target);
}
collector.resetTimer();
});
} else if (opts.action == ModMenuActionType.PAGE) {
let pageDelta = 0;
switch (opts.target) {
case ModMenuNavigationType.PREV:
pageDelta = -1;
break;
case ModMenuNavigationType.NEXT:
pageDelta = 1;
break;
}
let newPage = 1;
if (opts.target == ModMenuNavigationType.PREV || opts.target == ModMenuNavigationType.NEXT) {
newPage = Math.max(Math.min(page + pageDelta, totalPages), 1);
} else if (opts.target == ModMenuNavigationType.FIRST) {
newPage = 1;
} else if (opts.target == ModMenuNavigationType.LAST) {
newPage = totalPages;
}
if (newPage != page) {
updateNavButtonState(firstButton, prevButton, nextButton, lastButton, newPage, totalPages);
await i.editReply({
embeds: [await loadPage(newPage)],
components: [navigationRow, moderationRow],
});
page = newPage;
}
} else if (opts.action == ModMenuActionType.NOTE) {
await launchNoteActionModal(pluginData, i as ButtonInteraction, opts.target);
} else if (opts.action == ModMenuActionType.WARN) {
await launchWarnActionModal(pluginData, i as ButtonInteraction, opts.target);
} else if (opts.action == ModMenuActionType.CLEAN) {
await launchCleanActionModal(pluginData, i as ButtonInteraction, opts.target);
} else if (opts.action == ModMenuActionType.MUTE) {
await launchMuteActionModal(pluginData, i as ButtonInteraction, opts.target);
} else if (opts.action == ModMenuActionType.BAN) {
await launchBanActionModal(pluginData, i as ButtonInteraction, opts.target);
}
collector.resetTimer();
});
// Remove components on timeout.
collector.on("end", async (_, reason) => {
if (reason !== "messageDelete") {
interaction.editReply({
components: [],
// Remove components on timeout.
collector.on("end", async (_, reason) => {
if (reason !== "messageDelete") {
await interaction
.editReply({
components: [],
})
.catch((err) => logger.error(`Mod menu timeout failed: ${err}`));
}
});
}
});
})
.catch((err) => logger.error(`Mod menu setup failed: ${err}`));
}
function serializeCustomId(opts: ModMenuActionOpts) {