PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 1 | import { LoadingEmbed } from "../../utils/defaults.js"; |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 2 | import Discord, { CommandInteraction, ActionRowBuilder, ButtonBuilder, ButtonStyle, ButtonComponent, TextInputBuilder, RoleSelectMenuBuilder } from "discord.js"; |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 3 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
| 4 | import getEmojiByName from "../../utils/getEmojiByName.js"; |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 5 | import type { SlashCommandSubcommandBuilder } from "discord.js"; |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 6 | import client from "../../utils/client.js"; |
| 7 | import { modalInteractionCollector } from "../../utils/dualCollector.js"; |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 8 | |
| 9 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 10 | builder |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame] | 11 | .setName("moderation") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 12 | .setDescription("Links and text shown to a user after a moderator action is performed") |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 13 | |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame] | 14 | const callback = async (interaction: CommandInteraction): Promise<void> => { |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 15 | const m = await interaction.reply({ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 16 | embeds: LoadingEmbed, |
| 17 | ephemeral: true, |
| 18 | fetchReply: true |
| 19 | }); |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 20 | let timedOut = false; |
| 21 | while (!timedOut) { |
TheCodedProf | d9636e8 | 2023-01-17 22:13:06 -0500 | [diff] [blame] | 22 | const config = await client.database.guilds.read(interaction.guild!.id); |
PineaFan | 9b2ac4d | 2023-01-18 14:41:07 +0000 | [diff] [blame] | 23 | const moderation = config.moderation; |
TheCodedProf | f8ef794 | 2023-03-03 15:32:32 -0500 | [diff] [blame] | 24 | console.log(moderation) |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 25 | await interaction.editReply({ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 26 | embeds: [ |
| 27 | new EmojiEmbed() |
| 28 | .setTitle("Moderation Commands") |
| 29 | .setEmoji("PUNISH.BAN.GREEN") |
| 30 | .setStatus("Success") |
| 31 | .setDescription( |
| 32 | "These links are shown below the message sent in a user's DM when they are punished.\n\n" + |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame] | 33 | "**Mute Role:** " + (moderation.mute.role ? `<@&${moderation.mute.role}>` : "*None set*") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 34 | ) |
| 35 | ], |
| 36 | components: [ |
TheCodedProf | d9636e8 | 2023-01-17 22:13:06 -0500 | [diff] [blame] | 37 | new ActionRowBuilder<ButtonBuilder>().addComponents([ |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 38 | new ButtonBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 39 | .setLabel("Warn") |
| 40 | .setEmoji(getEmojiByName("PUNISH.WARN.YELLOW", "id")) |
| 41 | .setCustomId("warn") |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 42 | .setStyle(ButtonStyle.Secondary), |
| 43 | new ButtonBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 44 | .setLabel("Mute") |
| 45 | .setEmoji(getEmojiByName("PUNISH.MUTE.YELLOW", "id")) |
| 46 | .setCustomId("mute") |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 47 | .setStyle(ButtonStyle.Secondary), |
| 48 | new ButtonBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 49 | .setLabel("Nickname") |
| 50 | .setEmoji(getEmojiByName("PUNISH.NICKNAME.GREEN", "id")) |
| 51 | .setCustomId("nickname") |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 52 | .setStyle(ButtonStyle.Secondary) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 53 | ]), |
TheCodedProf | d9636e8 | 2023-01-17 22:13:06 -0500 | [diff] [blame] | 54 | new ActionRowBuilder<ButtonBuilder>().addComponents([ |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 55 | new ButtonBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 56 | .setLabel("Kick") |
| 57 | .setEmoji(getEmojiByName("PUNISH.KICK.RED", "id")) |
| 58 | .setCustomId("kick") |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 59 | .setStyle(ButtonStyle.Secondary), |
| 60 | new ButtonBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 61 | .setLabel("Softban") |
| 62 | .setEmoji(getEmojiByName("PUNISH.BAN.YELLOW", "id")) |
| 63 | .setCustomId("softban") |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 64 | .setStyle(ButtonStyle.Secondary), |
| 65 | new ButtonBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 66 | .setLabel("Ban") |
| 67 | .setEmoji(getEmojiByName("PUNISH.BAN.RED", "id")) |
| 68 | .setCustomId("ban") |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 69 | .setStyle(ButtonStyle.Secondary) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 70 | ]), |
TheCodedProf | d9636e8 | 2023-01-17 22:13:06 -0500 | [diff] [blame] | 71 | new ActionRowBuilder<ButtonBuilder>().addComponents([ |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 72 | new ButtonBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 73 | .setCustomId("timeout") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 74 | .setLabel("Mute timeout " + (moderation.mute.timeout ? "Enabled" : "Disabled")) |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 75 | .setStyle(moderation.mute.timeout ? ButtonStyle.Success : ButtonStyle.Danger) |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 76 | .setEmoji(getEmojiByName("CONTROL." + (moderation.mute.timeout ? "TICK" : "CROSS"), "id")) |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame] | 77 | ]), |
| 78 | new ActionRowBuilder<RoleSelectMenuBuilder>().addComponents( |
| 79 | new RoleSelectMenuBuilder() |
| 80 | .setCustomId("muteRole") |
| 81 | .setPlaceholder("Select a new mute role") |
| 82 | ) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 83 | ] |
| 84 | }); |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 85 | let i; |
| 86 | try { |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 87 | i = await m.awaitMessageComponent({ |
| 88 | time: 300000, |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 89 | filter: (i) => { return i.user.id === interaction.user.id && i.channel!.id === interaction.channel!.id && i.message.id === m.id } |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 90 | }); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 91 | } catch (e) { |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 92 | timedOut = true; |
| 93 | continue; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 94 | } |
TheCodedProf | d9636e8 | 2023-01-17 22:13:06 -0500 | [diff] [blame] | 95 | type modIDs = "mute" | "kick" | "ban" | "softban" | "warn" | "role"; |
PineaFan | 9b2ac4d | 2023-01-18 14:41:07 +0000 | [diff] [blame] | 96 | let chosen = moderation[i.customId as modIDs]; |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame] | 97 | if (i.isRoleSelectMenu()) { |
TheCodedProf | 267563a | 2023-01-21 17:00:57 -0500 | [diff] [blame] | 98 | await i.deferUpdate(); |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame] | 99 | await client.database.guilds.write(interaction.guild!.id, { |
| 100 | "moderation.mute.role": i.values[0]! |
| 101 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 102 | continue; |
TheCodedProf | 1f67504 | 2023-02-16 17:01:29 -0500 | [diff] [blame] | 103 | } else if ((i.component as ButtonComponent).customId === "timeout") { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 104 | await i.deferUpdate(); |
TheCodedProf | d9636e8 | 2023-01-17 22:13:06 -0500 | [diff] [blame] | 105 | await client.database.guilds.write(interaction.guild!.id, { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 106 | "moderation.mute.timeout": !moderation.mute.timeout |
| 107 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 108 | continue; |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 109 | } else if (i.customId) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 110 | await i.showModal( |
TheCodedProf | d9636e8 | 2023-01-17 22:13:06 -0500 | [diff] [blame] | 111 | new Discord.ModalBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 112 | .setCustomId("modal") |
| 113 | .setTitle(`Options for ${i.customId}`) |
| 114 | .addComponents( |
TheCodedProf | d9636e8 | 2023-01-17 22:13:06 -0500 | [diff] [blame] | 115 | new ActionRowBuilder<TextInputBuilder>().addComponents( |
| 116 | new TextInputBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 117 | .setCustomId("name") |
| 118 | .setLabel("Button text") |
| 119 | .setMaxLength(100) |
| 120 | .setRequired(false) |
TheCodedProf | d9636e8 | 2023-01-17 22:13:06 -0500 | [diff] [blame] | 121 | .setStyle(Discord.TextInputStyle.Short) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 122 | .setValue(chosen.text ?? "") |
TheCodedProf | d9636e8 | 2023-01-17 22:13:06 -0500 | [diff] [blame] | 123 | ).toJSON(), |
| 124 | new ActionRowBuilder<TextInputBuilder>().addComponents( |
| 125 | new TextInputBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 126 | .setCustomId("url") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 127 | .setLabel("URL - Type {id} to insert the user's ID") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 128 | .setMaxLength(2000) |
| 129 | .setRequired(false) |
TheCodedProf | d9636e8 | 2023-01-17 22:13:06 -0500 | [diff] [blame] | 130 | .setStyle(Discord.TextInputStyle.Short) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 131 | .setValue(chosen.link ?? "") |
TheCodedProf | d9636e8 | 2023-01-17 22:13:06 -0500 | [diff] [blame] | 132 | ).toJSON() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 133 | ) |
| 134 | ); |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 135 | await interaction.editReply({ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 136 | embeds: [ |
| 137 | new EmojiEmbed() |
| 138 | .setTitle("Moderation Links") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 139 | .setDescription("Modal opened. If you can't see it, click back and try again.") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 140 | .setStatus("Success") |
| 141 | .setEmoji("GUILD.TICKET.OPEN") |
| 142 | ], |
| 143 | components: [ |
TheCodedProf | d9636e8 | 2023-01-17 22:13:06 -0500 | [diff] [blame] | 144 | new ActionRowBuilder<ButtonBuilder>().addComponents([ |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 145 | new ButtonBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 146 | .setLabel("Back") |
| 147 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id")) |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 148 | .setStyle(ButtonStyle.Primary) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 149 | .setCustomId("back") |
| 150 | ]) |
| 151 | ] |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 152 | }); |
PineaFan | 9b2ac4d | 2023-01-18 14:41:07 +0000 | [diff] [blame] | 153 | let out: Discord.ModalSubmitInteraction | null; |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 154 | try { |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 155 | out = await modalInteractionCollector(m, interaction.user) as Discord.ModalSubmitInteraction | null; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 156 | } catch (e) { |
| 157 | continue; |
| 158 | } |
TheCodedProf | 4a6d571 | 2023-01-19 15:54:40 -0500 | [diff] [blame] | 159 | if (!out || out.isButton()) continue |
PineaFan | 9b2ac4d | 2023-01-18 14:41:07 +0000 | [diff] [blame] | 160 | const buttonText = out.fields.getTextInputValue("name"); |
| 161 | const buttonLink = out.fields.getTextInputValue("url").replace(/{id}/gi, "{id}"); |
| 162 | const current = chosen; |
| 163 | if (current.text !== buttonText || current.link !== buttonLink) { |
| 164 | chosen = { text: buttonText, link: buttonLink }; |
| 165 | await client.database.guilds.write(interaction.guild!.id, { |
| 166 | ["moderation." + i.customId]: { |
| 167 | text: buttonText, |
| 168 | link: buttonLink |
| 169 | } |
| 170 | }); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 171 | } |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 172 | } |
| 173 | } |
TheCodedProf | 01cba76 | 2023-02-18 15:55:05 -0500 | [diff] [blame] | 174 | await interaction.deleteReply() |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 175 | }; |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 176 | |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 177 | const check = (interaction: CommandInteraction, _partial: boolean = false) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 178 | const member = interaction.member as Discord.GuildMember; |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 179 | if (!member.permissions.has("ManageGuild")) |
| 180 | return "You must have the *Manage Server* permission to use this command"; |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 181 | return true; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 182 | }; |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 183 | |
| 184 | export { command }; |
| 185 | export { callback }; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 186 | export { check }; |