diff --git a/events/interaction.js b/events/interaction.js index 7ab332d..1a67213 100644 --- a/events/interaction.js +++ b/events/interaction.js @@ -1,4 +1,5 @@ import Logger from '../utils/logger.js'; +import { replyError } from '../utils/replyError.js'; export const autocompleteHandler = async (interaction) => { const command = interaction.client.commands.get(interaction.commandName); @@ -27,16 +28,6 @@ export const commandsHandler = async (interaction) => { await command.execute(interaction); } catch (error) { Logger.error(error); - if (interaction.replied || interaction.deferred) { - await interaction.followUp({ - content: 'There was an error while executing this command!', - ephemeral: true, - }); - } else { - await interaction.reply({ - content: 'There was an error while executing this command!', - ephemeral: true, - }); - } + replyError(interaction, 'There was an error while executing this command!'); } }; diff --git a/events/reactionRole.js b/events/reactionRole.js index 88e8c95..4c781d9 100644 --- a/events/reactionRole.js +++ b/events/reactionRole.js @@ -12,7 +12,7 @@ export const handleReactionRole = async (reaction, user, type) => { try { member = await reaction.message.guild.members.fetch(user.id); } catch (error) { - Logger.error(`Could not get member: ${error}`); + Logger.error(`Could not get member for reaction role: ${error}`); return; } const emoji = reaction.emoji.toString(); diff --git a/events/ticket.js b/events/ticket.js index 0b7d029..86a7946 100644 --- a/events/ticket.js +++ b/events/ticket.js @@ -10,42 +10,53 @@ import { import { apiFetch } from '../utils/apiFetch.js'; import ticketState from '../states/TicketState.js'; import { replaceUser, replaceChannel } from '../utils/mentions.js'; +import { replyError } from '../utils/replyError.js'; import Logger from '../utils/logger.js'; export const ticketHandler = async (interaction) => { if (interaction.isModalSubmit()) { - const modalMatch = interaction.customId.match( - /^ticket-([0-9]+)-close-with-reason$/ - ); - if (!modalMatch) { - return; - } - const ticketId = modalMatch[1]; // id - - await interaction.reply({ - content: 'this ticket will be closed.', - ephemeral: true, - }); - const reason = interaction.fields.getTextInputValue('reason'); - const response = await apiFetch(`/ticket/${ticketId}/close`, { - method: 'POST', - body: { - closed_by_discord_user_id: interaction.user.id, - closed_reason: reason, - }, - }); - - if (!response.ok) { - Logger.error( - `Could not close ticket ${ticketId} with reason: ${await response.text()}` + try { + const modalMatch = interaction.customId.match( + /^ticket-([0-9]+)-close-with-reason$/ ); - await interaction.editReply({ - content: - 'An error occurred while closing this ticket. Please try again later. If this error persists, please report to the staff team', + if (!modalMatch) { + return; + } + const ticketId = modalMatch[1]; // id + + await interaction.reply({ + content: 'this ticket will be closed.', ephemeral: true, }); - } else { - ticketState.removeChannelId(interaction.channelId); + const reason = interaction.fields.getTextInputValue('reason'); + const response = await apiFetch(`/ticket/${ticketId}/close`, { + method: 'POST', + body: { + closed_by_discord_user_id: interaction.user.id, + closed_reason: reason, + }, + }); + + if (!response.ok) { + Logger.error( + `Could not close ticket ${ticketId} with reason: ${await response.text()}` + ); + await interaction.editReply({ + content: + 'An error occurred while closing this ticket. Please try again later. If this error persists, please report to the staff team', + ephemeral: true, + }); + } else { + ticketState.removeChannelId(interaction.channelId); + } + } catch (error) { + Logger.error( + `An error occurred while closing a ticket with reason: ${error}` + ); + await replyError( + interaction, + 'An error occurred while creating your ticket. Please try again later. If this error persists, please report to the staff team.' + ); } } @@ -68,11 +79,8 @@ export const ticketHandler = async (interaction) => { }); const ticket = await response.json(); if (response.ok) { - await ticketState.addChannelId( - `${ticket.data.id}`, - ticket.data.channel_id - ); - interaction.reply({ + ticketState.addChannelId(`${ticket.data.id}`, ticket.data.channel_id); + await interaction.reply({ content: `Your ticket has been created: <#${ticket.data.channel_id}>.`, ephemeral: true, }); @@ -80,19 +88,17 @@ export const ticketHandler = async (interaction) => { Logger.error( `An API error occurred while creating a ticket: ${await response.text()}` ); - await interaction.reply({ - content: - 'An error occurred while creating your ticket. Please try again later. If this error persists, please report to the staff team.', - ephemeral: true, - }); + await replyError( + interaction, + 'An error occurred while creating your ticket. Please try again later. If this error persists, please report to the staff team.' + ); } } catch (error) { Logger.error(`An error occurred while creating a ticket: ${error}`); - interaction.reply({ - content: - 'An error occurred while creating your ticket. Please try again later. If this error persists, please report to the staff team.', - ephemeral: true, - }); + await replyError( + interaction, + 'An error occurred while creating your ticket. Please try again later. If this error persists, please report to the staff team.' + ); } } @@ -116,16 +122,15 @@ export const ticketHandler = async (interaction) => { Logger.error( `An error occurred while sending close confirmation for ticket ${id}: ${error}` ); - await interaction.editReply({ - content: - 'An error occurred while closing this ticket. Please try again later. If this error persists, please report to the staff team', - ephemeral: true, - }); + await replyError( + interaction, + 'An error occurred while closing this ticket. Please try again later. If this error persists, please report to the staff team.' + ); } } if (action === 'closeConfirm') { - // close existing ticket + // confirm close existing ticket try { await interaction.reply({ content: 'This ticket will be closed.', @@ -151,30 +156,40 @@ export const ticketHandler = async (interaction) => { Logger.error( `An error occurred while closing the ticket ${id}: ${error}` ); - await interaction.editReply({ - content: - 'An error occurred while closing this ticket. Please try again later. If this error persists, please report to the staff team', - ephemeral: true, - }); + await replyError( + interaction, + 'An error occurred while closing this ticket. Please try again later. If this error persists, please report to the staff team.' + ); } } if (action === 'closeWithReason') { // close existing ticket with reason + try { + const modal = new ModalBuilder() + .setCustomId(`ticket-${id}-close-with-reason`) + .setTitle('Close ticket with reason'); - const modal = new ModalBuilder() - .setCustomId(`ticket-${id}-close-with-reason`) - .setTitle('Close ticket with reason'); - - const reasonInput = new TextInputBuilder() - .setCustomId('reason') - .setLabel('Reason:') - .setStyle(TextInputStyle.Paragraph); + const reasonInput = new TextInputBuilder() + .setCustomId('reason') + .setLabel('Reason:') + .setStyle(TextInputStyle.Paragraph); - const answerActionRow = new ActionRowBuilder().addComponents(reasonInput); + const answerActionRow = new ActionRowBuilder().addComponents( + reasonInput + ); - modal.addComponents(answerActionRow); - await interaction.showModal(modal); + modal.addComponents(answerActionRow); + await interaction.showModal(modal); + } catch (error) { + Logger.error( + `An error occurred while creating the close ticket with reason modal: ${error}` + ); + await replyError( + interaction, + 'An error occurred while closing this ticket. Please try again later. If this error persists, please report to the staff team.' + ); + } } } }; diff --git a/utils/replyError.js b/utils/replyError.js new file mode 100644 index 0000000..b8ddf54 --- /dev/null +++ b/utils/replyError.js @@ -0,0 +1,13 @@ +export const replyError = async (interaction, content) => { + if (interaction.replied || interaction.deferred) { + await interaction.followUp({ + content, + ephemeral: true, + }); + } else { + await interaction.reply({ + content, + ephemeral: true, + }); + } +};