Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions events/interaction.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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!');
}
};
2 changes: 1 addition & 1 deletion events/reactionRole.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
147 changes: 81 additions & 66 deletions events/ticket.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
);
}
}

Expand All @@ -68,31 +79,26 @@ 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,
});
} else {
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.'
);
}
}

Expand All @@ -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.',
Expand All @@ -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.'
);
}
}
}
};
Expand Down
13 changes: 13 additions & 0 deletions utils/replyError.js
Original file line number Diff line number Diff line change
@@ -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,
});
}
};