Skip to content

Commit

Permalink
new embed interactions
Browse files Browse the repository at this point in the history
  • Loading branch information
LunakisDev committed May 27, 2024
1 parent 5b25376 commit 855cdc6
Showing 1 changed file with 60 additions and 42 deletions.
102 changes: 60 additions & 42 deletions src/commands/moderation/clearMessages.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand All @@ -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<ButtonBuilder>().addComponents(confirmButton, cancelButton);

const amount = interaction.options.getInteger('amount');

if (!amount) {
Expand All @@ -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: '' });
}
});

0 comments on commit 855cdc6

Please sign in to comment.