const { EmbedBuilder, AuditLogEvent } = require('discord.js'); const config = require('../../config.json'); module.exports = { async execute(client, message) { try { const logChannel = await client.channels.fetch(config.deletelogChannelId); if (!logChannel) return console.error("Log channel not found"); // Fetch audit logs for MESSAGE_DELETE to identify the deleter const fetchedLogs = await message.guild.fetchAuditLogs({ limit: 5, type: AuditLogEvent.MessageDelete, }); const deletionLog = fetchedLogs.entries.find(entry => entry.target.id === message.author.id && Date.now() - entry.createdTimestamp < 5000 // Within 5 seconds of deletion ); // If deletionLog exists, get executor (deleter), otherwise log an error let deleter; if (deletionLog) { deleter = deletionLog.executor.tag; } else { console.error("Deleter not found in audit logs."); deleter = "Unknown"; } // Proceed to log the deletion await logDeletedMessage(logChannel, message, deleter); } catch (error) { console.error("Error sending delete log:", error); } } }; async function logDeletedMessage(logChannel, message, deleter) { const authorTag = message.author ? message.author.tag : "Unknown User"; const authorId = message.author ? message.author.id : "Unknown ID"; const channelName = message.channel ? message.channel.toString() : "Unknown channel"; const content = message.content ? message.content : "No content available"; // Formatted timestamp for when the message was originally sent const messageTimestamp = ``; // Embed description format const deletionText = `Message from **${authorTag}** deleted by **${deleter}** in ${channelName}. It was sent on ${messageTimestamp}.`; // Create the embed with formatted details const deleteEmbed = new EmbedBuilder() .setAuthor({ name: `${authorTag}`, iconURL: message.author.displayAvatarURL() }) .setDescription(deletionText) .setColor('Red') .addFields({ name: 'Message Content', value: content }) .setFooter({ text: `User ID: ${authorId} • ${new Date().toLocaleString()}` }); // Send the embed to the log channel await logChannel.send({ embeds: [deleteEmbed] }); }