import { Message } from "discord.js"; import { replyWithCleanup } from "../utilities"; export const handleDeleteCommand = async (message: Message) => { if (message.author.bot) return; if (message.content.toLowerCase().startsWith("uma!delete")) { const application = await message.client.application?.fetch(); const ownerId = application?.owner?.id; if (message.author.id !== ownerId) { await replyWithCleanup( message, "❌ Only the server owner can use this command.", ); return; } const parameters = message.content.split(" ").slice(1); if (parameters.length < 1) { await replyWithCleanup( message, "❌ Usage: `uma!delete [channel_id] [message_id2] [message_id3] ...`\nExamples:\n- `uma!delete 1234567890123456789` (current channel)\n- `uma!delete 9876543210987654321 1234567890123456789 1111111111111111111` (specific channel, multiple messages)", ); return; } let targetChannel = null; let messageIds: string[] = []; if (parameters.length === 1) { messageIds = [parameters[0]]; targetChannel = message.channel; } else { const channelId = parameters[0]; messageIds = parameters.slice(1); if (!/^\d{17,19}$/.test(channelId)) { await message.reply( "❌ Invalid channel ID format. Please provide a valid Discord channel ID.", ); return; } targetChannel = message.client.channels.cache.get(channelId); if (!targetChannel || !targetChannel.isTextBased()) { await message.reply("❌ Channel not found or is not a text channel."); return; } } for (const messageId of messageIds) if (!/^\d{17,19}$/.test(messageId)) { await message.reply( "❌ Invalid message ID format. Please provide valid Discord message IDs.", ); return; } try { let failedCount = 0; for (const messageId of messageIds) { try { const targetMessage = await targetChannel.messages.fetch(messageId); await targetMessage.delete(); } catch { failedCount += 1; } } await message.delete(); if (failedCount > 0) console.warn( `Failed to delete ${failedCount} out of ${messageIds.length} messages`, ); } catch (error) { console.error("Error deleting messages:", error); await message.reply( "❌ Failed to delete messages. Check bot permissions and try again.", ); } } };