Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code improvements #77

Merged
merged 3 commits into from
Aug 15, 2024
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
3,048 changes: 0 additions & 3,048 deletions package-lock.json

This file was deleted.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"discord.js": "^14.15.3",
"mathjs": "^13.0.3",
"mongoose": "^8.5.2",
"ms": "^2.1.3",
"os": "^0.1.2"
},
"devDependencies": {
Expand Down
89 changes: 58 additions & 31 deletions src/buttons/claimTicketBtn.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,48 @@ export default {
botPermissions: [PermissionFlagsBits.ManageChannels],

run: async (client, interaction) => {
await interaction.deferReply({ ephemeral: true });

try {
await interaction.deferReply({ ephemeral: true });

const { member, channel, guild } = interaction;

const ticketSetup = await ticketSetupSchema.findOne({
guildID: guild.id,
});
const ticketSetup = await ticketSetupSchema
.findOne({ guildID: guild.id })
.catch(() => null);
if (!ticketSetup) {
return await interaction.editReply('Ticket setup not found.');
return await interaction.editReply(
'Ticket setup not found. Please configure the ticket system.'
);
}

const staffRoleId = ticketSetup.staffRoleID;
if (!member.roles.cache.has(staffRoleId)) {
if (!staffRoleId || !member.roles.cache.has(staffRoleId)) {
return await interaction.editReply(
'You do not have the necessary permissions to claim this ticket.'
);
}

const ticket = await ticketSchema.findOne({
ticketChannelID: channel.id,
});
const ticket = await ticketSchema
.findOne({ ticketChannelID: channel.id })
.catch(() => null);
if (!ticket) {
return await interaction.editReply('Ticket not found.');
return await interaction.editReply(
'This channel is not associated with a valid ticket.'
);
}

if (ticket.claimedBy) {
const claimedMember = await guild.members
.fetch(ticket.claimedBy)
.catch(() => null);
const claimedByText = claimedMember
? claimedMember.toString()
: 'a staff member';
return await interaction.editReply(
`This ticket has already been claimed by <@${ticket.claimedBy}>.`
`This ticket has already been claimed by ${claimedByText}.`
);
}

// Update channel permissions
const permissionUpdates = [
{
id: member.id,
Expand All @@ -57,46 +66,64 @@ export default {
{
id: ticket.ticketMemberID,
allow: [
PermissionFlagsBits.ViewChannel,
PermissionFlagBits.ViewChannel,
PermissionFlagsBits.SendMessages,
],
},
{ id: staffRoleId, deny: [PermissionFlagsBits.ViewChannel] },
{
id: staffRoleId,
deny: [PermissionFlagBits.ViewChannel],
},
];

await Promise.all(
permissionUpdates.map((update) =>
channel.permissionOverwrites.edit(
update.id,
update.allow || update.deny
)
)
permissionUpdates.map(async (update) => {
try {
await channel.permissionOverwrites.edit(
update.id,
update.allow || update.deny
);
} catch (error) {
console.error(
`Failed to update permissions for ${update.id}:`,
error
);
}
})
);

// Update ticket status in the database
ticket.claimedBy = member.id;
await ticket.save();
await ticket.save().catch((error) => {
console.error('Failed to save ticket:', error);
throw new Error('Failed to update ticket information.');
});

const claimEmbed = new EmbedBuilder()
.setColor('#00FF00')
.setDescription(`${member} has claimed this ticket.`)
.setTimestamp();

await interaction.deleteReply();
return await channel.send({ embeds: [claimEmbed] });
await channel.send({ embeds: [claimEmbed] }).catch((error) => {
console.error('Failed to send claim message:', error);
throw new Error('Failed to send claim notification.');
});
} catch (error) {
console.error('Error claiming ticket:', error);

if (error.code === 10062) {
return await interaction.followUp({
content: 'This interaction has expired. Please try again.',
ephemeral: true,
});
return await interaction
.followUp({
content: 'This interaction has expired. Please try again.',
ephemeral: true,
})
.catch(console.error);
}

return await interaction.editReply(
'There was an error claiming the ticket. Please try again later.'
);
const errorMessage =
error.message ||
'There was an error claiming the ticket. Please try again later.';
return await interaction.editReply(errorMessage).catch(console.error);
}
},
};
61 changes: 46 additions & 15 deletions src/buttons/closeTicketBtn.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,39 @@ export default {
try {
const { guild, member } = interaction;

// Defer the reply to buy more time for processing
await interaction.deferReply({ ephemeral: true });

// Get the ticket setup configuration
const ticketSetup = await ticketSetupSchema.findOne({
guildID: guild.id,
});
const ticketSetup = await ticketSetupSchema
.findOne({ guildID: guild.id })
.catch(() => null);
if (!ticketSetup) {
return await interaction.reply({
return await interaction.editReply({
content:
'Ticket system is not configured properly. Please contact an administrator.',
ephemeral: true,
});
}

// Check if the staff role exists
const staffRole = await guild.roles
.fetch(ticketSetup.staffRoleID)
.catch(() => null);
if (!staffRole) {
return await interaction.editReply({
content:
'The configured staff role no longer exists. Please contact an administrator.',
});
}

// Check if the member has the staff role
if (!member.roles.cache.has(ticketSetup.staffRoleID)) {
return await interaction.reply({
if (!member.roles.cache.has(staffRole.id)) {
return await interaction.editReply({
content: 'You do not have permission to close tickets.',
ephemeral: true,
});
}

// Create and show the modal
// Create the modal
const closeTicketModal = new ModalBuilder()
.setCustomId('closeTicketModal')
.setTitle('Close Ticket Confirmation')
Expand All @@ -47,16 +59,35 @@ export default {
.setStyle(TextInputStyle.Paragraph)
.setRequired(false)
.setMaxLength(1000)
.setPlaceholder(
'Enter the reason for closing this ticket...'
)
)
);

// Show the modal
await interaction.deleteReply();
await interaction.showModal(closeTicketModal);
} catch (err) {
await interaction.reply({
content:
'An error occurred while processing your request. Please try again later.',
ephemeral: true,
});
} catch (error) {
console.error('Error in closeTicketBtn:', error);

// Check if the interaction has already been replied to
if (interaction.deferred || interaction.replied) {
await interaction
.editReply({
content:
'An error occurred while processing your request. Please try again later.',
})
.catch(console.error);
} else {
await interaction
.reply({
content:
'An error occurred while processing your request. Please try again later.',
ephemeral: true,
})
.catch(console.error);
}
}
},
};
Loading