diff --git a/src/commands/system/webhook.ts b/src/commands/system/webhook.ts new file mode 100644 index 0000000..d6d58bc --- /dev/null +++ b/src/commands/system/webhook.ts @@ -0,0 +1,75 @@ +import { createCommand } from "@/lib/utils"; +import { SlashCommandBuilder, PermissionFlagsBits, TextChannel, NewsChannel, VoiceChannel } from "discord.js"; +import { createErrorEmbed } from "@/lib/embeds"; + +export const webhook = createCommand({ + data: new SlashCommandBuilder() + .setName("webhook") + .setDescription("Send a message via webhook using a JSON payload") + .setDefaultMemberPermissions(PermissionFlagsBits.ManageWebhooks) + .addStringOption(option => + option.setName("payload") + .setDescription("The JSON payload for the webhook message") + .setRequired(true) + ), + execute: async (interaction) => { + await interaction.deferReply({ ephemeral: true }); + + const payloadString = interaction.options.getString("payload", true); + let payload; + + try { + payload = JSON.parse(payloadString); + } catch (error) { + await interaction.editReply({ + embeds: [createErrorEmbed("The provided payload is not valid JSON.", "Invalid JSON")] + }); + return; + } + + const channel = interaction.channel; + + if (!channel || !('createWebhook' in channel)) { + await interaction.editReply({ + embeds: [createErrorEmbed("This channel does not support webhooks.", "Unsupported Channel")] + }); + return; + } + + let webhook; + try { + webhook = await channel.createWebhook({ + name: `${interaction.client.user.username} - Proxy`, + avatar: interaction.client.user.displayAvatarURL(), + reason: `Proxy message requested by ${interaction.user.tag}` + }); + + + // Support snake_case keys for raw API compatibility + if (payload.avatar_url && !payload.avatarURL) { + payload.avatarURL = payload.avatar_url; + delete payload.avatar_url; + } + + await webhook.send(payload); + + await webhook.delete("Proxy message sent"); + + await interaction.editReply({ content: "Message sent successfully!" }); + } catch (error) { + console.error("Webhook error:", error); + // Attempt cleanup if webhook was created but sending failed + if (webhook) { + try { + await webhook.delete("Cleanup after failure"); + } catch (cleanupError) { + console.error("Failed to delete webhook during cleanup:", cleanupError); + } + } + + await interaction.editReply({ + embeds: [createErrorEmbed("Failed to send message via webhook. Ensure the bot has 'Manage Webhooks' permission and the payload is valid.", "Delivery Failed")] + }); + } + } +});