import { Message } from "discord.js"; import { logUnexpectedDiscordAPIError, replyWithCleanup } from "../utilities"; export const handleDeleteWebhookCommand = async ( message: Message, ): Promise => { if (message.author.bot) return false; const application = await message.client.application?.fetch(); const ownerId = application?.owner?.id; if (message.author.id !== ownerId) return false; const content = message.content.trim(); const commandMatch = content.match(/^uma!delwh\s+(\d+)\s*,\s*(.+)$/s); if (!commandMatch) return false; const [, channelId, webhookName] = commandMatch; if (!webhookName.trim()) { await replyWithCleanup( message, "❌ You need to provide a webhook name to delete.", ); return true; } try { const channel = await message.client.channels.fetch(channelId); if (!channel || !channel.isTextBased() || channel.isDMBased()) { await replyWithCleanup( message, "❌ Channel not found or is not a text channel.", ); return true; } if (!("fetchWebhooks" in channel)) { await replyWithCleanup( message, "❌ This channel does not support webhooks.", ); return true; } const webhooks = await channel.fetchWebhooks(); const targetWebhook = webhooks.find( (webhook) => webhook.name === webhookName.trim(), ); if (!targetWebhook) { await replyWithCleanup( message, `❌ No webhook found with name "${webhookName.trim()}" in <#${channelId}>.`, ); return true; } await targetWebhook.delete(); await replyWithCleanup( message, `✅ Successfully deleted webhook "${webhookName.trim()}" from <#${channelId}>.`, ); return true; } catch (error) { logUnexpectedDiscordAPIError(error); await replyWithCleanup( message, "❌ Failed to delete webhook. Make sure I have permission to manage webhooks.", ); return true; } };