From dfd80758cd985ca2ce646c2f2d9d898ecf8babc5 Mon Sep 17 00:00:00 2001
From: aMeow <152869470+aMeow69@users.noreply.github.com>
Date: Sun, 19 Jan 2025 21:33:09 +0100
Subject: [PATCH] mirror command yay

---
 src/assets/lang/en-US.json             | 16 ++++++
 src/modules/messages/mirror-message.ts | 75 ++++++++++++++++++++++++++
 2 files changed, 91 insertions(+)
 create mode 100644 src/modules/messages/mirror-message.ts

diff --git a/src/assets/lang/en-US.json b/src/assets/lang/en-US.json
index ad2a3d1..661387f 100644
--- a/src/assets/lang/en-US.json
+++ b/src/assets/lang/en-US.json
@@ -63,6 +63,22 @@
                 "description": "The identifier of the message you would like to send."
             }
         },
+        "mirror": {
+            "name": "mirror",
+            "description": "Used to mirror a custom message.",
+            "channel_option": {
+                "name": "channel",
+                "description": "The channel to send / edit the message in. (Default: Current Channel)"
+            },
+            "ping_option": {
+                "name": "ping-roles",
+                "description": "If mentioned roles should be pinged in the message."
+            },
+            "old_messageId_option": {
+                "name": "old-message-id",
+                "description": "If you want to edit an existing message."
+            }
+        },
         "position_roles": {
             "name": "position-roles",
             "description": "Set up meaningful roles for the application.",
diff --git a/src/modules/messages/mirror-message.ts b/src/modules/messages/mirror-message.ts
new file mode 100644
index 0000000..899c4a0
--- /dev/null
+++ b/src/modules/messages/mirror-message.ts
@@ -0,0 +1,75 @@
+import { ActionRowBuilder, channelMention, ModalBuilder, TextInputBuilder, TextInputStyle } from "discord.js"
+import { LocalizedSlashCommandBuilder, SlashCommand, UserError } from "lib"
+
+SlashCommand({
+    builder: new LocalizedSlashCommandBuilder()
+        .setNameAndDescription("commands.mirror")
+        .addChannelOption((option) =>
+            option.setNameAndDescription("commands.mirror.channel_option").setRequired(false),
+        )
+        .addBooleanOption((option) =>
+            option.setNameAndDescription("commands.mirror.ping_option").setRequired(false),
+        )
+        .addStringOption((option) =>
+            option.setNameAndDescription("commands.mirror.old_messageId_option").setRequired(false),
+        )
+        .setDefaultMemberPermissions("0"),
+    config: { forceGuild: true },
+
+    async handler(interaction) {
+        const channelId = interaction.options.getChannel("channel")?.id ?? interaction.channelId
+        const pingRoles = !!interaction.options.getBoolean("ping-roles")
+        const oldMessageId = interaction.options.getString("old-message-id") ?? ""
+
+        const messageInputField = new TextInputBuilder()
+            .setLabel("The Message")
+            .setStyle(TextInputStyle.Paragraph)
+            .setPlaceholder("A properly formatted Discord message.")
+            .setCustomId("message")
+            .setMinLength(1)
+            .setMaxLength(2000)
+            .setRequired(true)
+
+        const modal = new ModalBuilder()
+            .setTitle("Mirror A Message")
+            .setCustomId([interaction.path, channelId, pingRoles, oldMessageId].join("/"))
+            .setComponents(new ActionRowBuilder<TextInputBuilder>().addComponents(messageInputField))
+
+        await interaction.showModal(modal)
+    },
+
+    async handleModalSubmit(interaction) {
+        await interaction.deferReply({ ephemeral: true })
+
+        const channelId = interaction.args.shift()!
+        const pingRoles = interaction.args.shift()! === "true"
+        const oldMessageId = interaction.args.shift()
+
+        const content = interaction.fields.getField("message").value
+
+        const channel = await interaction.guild?.channels.fetch(channelId)
+        if (!channel?.isSendable()) {
+            throw new UserError("Invalid Channel", "Please pick a sendable channel and try again!")
+        }
+
+        if (oldMessageId) {
+            const message = await channel.messages.fetch(oldMessageId)
+            if (!message) {
+                throw new UserError(
+                    "Invalid Message ID",
+                    `The message ID provided was not found in ${channelMention(channelId)}.`,
+                )
+            }
+
+            await message.edit(content)
+            return interaction.editReply(`Message Edited! ${message.url}`)
+        }
+
+        const message = await channel.send({
+            content,
+            allowedMentions: { parse: pingRoles ? ["everyone", "roles", "users"] : ["users"] },
+        })
+
+        await interaction.editReply(`Message "Sent"! ${message.url}`)
+    },
+})