diff options
Diffstat (limited to 'packages/gateway/src/commands/deleteWebhook.ts')
| -rw-r--r-- | packages/gateway/src/commands/deleteWebhook.ts | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/packages/gateway/src/commands/deleteWebhook.ts b/packages/gateway/src/commands/deleteWebhook.ts new file mode 100644 index 0000000..7408c17 --- /dev/null +++ b/packages/gateway/src/commands/deleteWebhook.ts @@ -0,0 +1,76 @@ +import { Message } from "discord.js"; +import { logUnexpectedDiscordAPIError, replyWithCleanup } from "../utilities"; + +export const handleDeleteWebhookCommand = async ( + message: Message, +): Promise<boolean> => { + if (message.author.bot) 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; + } +}; |