diff --git a/src/commands/moderation/clearMessages.ts b/src/commands/moderation/clearMessages.ts index a84a9b91..5d640811 100644 --- a/src/commands/moderation/clearMessages.ts +++ b/src/commands/moderation/clearMessages.ts @@ -1,4 +1,4 @@ -import { ApplicationCommandOptionType, ApplicationCommandType, TextChannel, Colors } from 'discord.js'; +import { ApplicationCommandOptionType, ApplicationCommandType, TextChannel, Colors, ActionRowBuilder, ButtonBuilder, ButtonStyle, Interaction } from 'discord.js'; import { slashCommand, slashCommandStructure, makeEmbed, constantsConfig, Logger } from '../../lib'; const data = slashCommandStructure({ @@ -18,6 +18,18 @@ const data = slashCommandStructure({ }); export default slashCommand(data, async ({ interaction }) => { + const confirmButton = new ButtonBuilder() + .setCustomId('confirm') + .setLabel('Confirm') + .setStyle(ButtonStyle.Primary); + + const cancelButton = new ButtonBuilder() + .setCustomId('cancel') + .setLabel('Cancel') + .setStyle(ButtonStyle.Primary); + + const buttonRow = new ActionRowBuilder().addComponents(confirmButton, cancelButton); + const amount = interaction.options.getInteger('amount'); if (!amount) { @@ -29,51 +41,57 @@ export default slashCommand(data, async ({ interaction }) => { return interaction.reply({ content: 'The channel could not be resolved.', ephemeral: true }); } - try { - const messages = await (channel as TextChannel).bulkDelete(amount, true); - - const replyEmbed = makeEmbed({ - title: 'Messages Cleared', - description: `Successfully cleared **${messages.size}** messages.`, - color: Colors.Green, - timestamp: new Date(), - }); - - const modLogsChannel = interaction.guild.channels.resolve(constantsConfig.channels.MOD_LOGS) as TextChannel; - - const modLogEmbed = makeEmbed({ - title: '🧹 Messages Cleared', - description: 'Messages have been cleared.', - color: Colors.Green, - fields: [ - { name: 'Moderator', value: `<@${user.id}>`, inline: true }, - { name: 'Channel', value: `<#${channel.id}>`, inline: true }, - { name: 'Amount', value: `${amount}`, inline: true }, - ], - footer: { text: `Moderator ID: ${user.id}`, iconURL: user.displayAvatarURL() }, - timestamp: new Date(), - }); + const response = await interaction.reply({ content: `Do you really want to delete ${amount} messages?`, components: [buttonRow], ephemeral: true }); + const filter = (buttonInteraction: Interaction) => buttonInteraction.user.id === interaction.user.id; - try { - await modLogsChannel.send({ embeds: [modLogEmbed] }); - } catch (e) { - Logger.error('An error occurred while trying to send the mod log:', e); - } + try { + const confirmation = await response.awaitMessageComponent({ filter, time: 120_000 }); + if (confirmation.customId === 'confirm') { + try { + const messages = await (channel as TextChannel).bulkDelete(amount, true); + const replyEmbed = makeEmbed({ + title: 'Messages Cleared', + description: `Successfully cleared **${messages.size}** messages.`, + color: Colors.Green, + timestamp: new Date(), + }); - await interaction.reply({ embeds: [replyEmbed], ephemeral: true }); + const modLogsChannel = interaction.guild.channels.resolve(constantsConfig.channels.MOD_LOGS) as TextChannel; + const modLogEmbed = makeEmbed({ + title: '🧹 Messages Cleared', + description: 'Messages have been cleared.', + color: Colors.Green, + fields: [ + { name: 'Moderator', value: `<@${user.id}>`, inline: true }, + { name: 'Channel', value: `<#${channel.id}>`, inline: true }, + { name: 'Amount', value: `${amount}`, inline: true }, + ], + footer: { text: `Moderator ID: ${user.id}`, iconURL: user.displayAvatarURL() }, + timestamp: new Date(), + }); - setTimeout(async () => { - try { - await interaction.deleteReply(); + try { + await modLogsChannel.send({ embeds: [modLogEmbed] }); + } catch (e) { + Logger.error('An error occurred while trying to send the mod log:', e); + } + setTimeout(async () => { + try { + return interaction.deleteReply(); + } catch (error) { + Logger.error('Failed to delete the reply message:', error); + return interaction.followUp({ content: 'Failed to delete the reply message.', ephemeral: true }); + } + }, 5000); + return interaction.followUp({ embeds: [replyEmbed], ephemeral: true }); } catch (error) { - Logger.error('Failed to delete the reply message:', error); - await interaction.followUp({ content: 'Failed to delete the reply message.', ephemeral: true }); + Logger.error('Error clearing messages:', error); + return interaction.followUp({ content: 'There was an error trying to clear messages in this channel.', ephemeral: true }); } - }, 5000); - - return Promise.resolve(); - } catch (error) { - Logger.error('Error clearing messages:', error); - return interaction.reply({ content: 'There was an error trying to clear messages in this channel.', ephemeral: true }); + } else { + return interaction.followUp({ content: 'Interaction was canceled.', ephemeral: true }); + } + } catch (e) { + return interaction.editReply({ content: '' }); } });