Skip to content

Commit

Permalink
mirror command yay
Browse files Browse the repository at this point in the history
  • Loading branch information
aMeow69 committed Jan 19, 2025
1 parent eabd865 commit dfd8075
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/assets/lang/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
75 changes: 75 additions & 0 deletions src/modules/messages/mirror-message.ts
Original file line number Diff line number Diff line change
@@ -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}`)
},
})

0 comments on commit dfd8075

Please sign in to comment.