pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 1 | import { LoadingEmbed } from "./../../utils/defaultEmbeds.js"; |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 2 | import Discord, { CommandInteraction, MessageActionRow, MessageButton, TextInputComponent } 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"; |
| 5 | import { SlashCommandSubcommandBuilder } from "@discordjs/builders"; |
| 6 | import { WrappedCheck } from "jshaiku"; |
| 7 | import client from "../../utils/client.js"; |
| 8 | import { modalInteractionCollector } from "../../utils/dualCollector.js"; |
| 9 | import confirmationMessage from "../../utils/confirmationMessage.js"; |
| 10 | import keyValueList from "../../utils/generateKeyValueList.js"; |
| 11 | |
| 12 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 13 | builder |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 14 | .setName("commands") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 15 | .setDescription("Links and text shown to a user after a moderator action is performed") |
| 16 | .addRoleOption((o) => o.setName("role").setDescription("The role given when a member is muted")); |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 17 | |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 18 | const callback = async (interaction: CommandInteraction): Promise<unknown> => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 19 | await interaction.reply({ |
| 20 | embeds: LoadingEmbed, |
| 21 | ephemeral: true, |
| 22 | fetchReply: true |
| 23 | }); |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 24 | let m; |
| 25 | let clicked = ""; |
| 26 | if (interaction.options.getRole("role")) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 27 | const confirmation = await new confirmationMessage(interaction) |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 28 | .setEmoji("GUILD.ROLES.DELETE") |
| 29 | .setTitle("Moderation Commands") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 30 | .setDescription( |
| 31 | keyValueList({ |
| 32 | role: `<@&${interaction.options.getRole("role").id}>` |
| 33 | }) |
| 34 | ) |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 35 | .setColor("Danger") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 36 | .send(true); |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame^] | 37 | if (confirmation.cancelled) return |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 38 | if (confirmation.success) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 39 | await client.database.guilds.write(interaction.guild.id, { |
| 40 | ["moderation.mute.role"]: interaction.options.getRole("role").id |
| 41 | }); |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 42 | } |
| 43 | } |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 44 | let timedOut = false; |
| 45 | while (!timedOut) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 46 | const config = await client.database.guilds.read(interaction.guild.id); |
| 47 | const moderation = config.getKey("moderation"); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 48 | m = await interaction.editReply({ |
| 49 | embeds: [ |
| 50 | new EmojiEmbed() |
| 51 | .setTitle("Moderation Commands") |
| 52 | .setEmoji("PUNISH.BAN.GREEN") |
| 53 | .setStatus("Success") |
| 54 | .setDescription( |
| 55 | "These links are shown below the message sent in a user's DM when they are punished.\n\n" + |
| 56 | "**Mute Role:** " + |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 57 | (moderation.mute.role ? `<@&${moderation.mute.role}>` : "*None set*") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 58 | ) |
| 59 | ], |
| 60 | components: [ |
| 61 | new MessageActionRow().addComponents([ |
| 62 | new MessageButton() |
| 63 | .setLabel("Warn") |
| 64 | .setEmoji(getEmojiByName("PUNISH.WARN.YELLOW", "id")) |
| 65 | .setCustomId("warn") |
| 66 | .setStyle("SECONDARY"), |
| 67 | new MessageButton() |
| 68 | .setLabel("Mute") |
| 69 | .setEmoji(getEmojiByName("PUNISH.MUTE.YELLOW", "id")) |
| 70 | .setCustomId("mute") |
| 71 | .setStyle("SECONDARY"), |
| 72 | new MessageButton() |
| 73 | .setLabel("Nickname") |
| 74 | .setEmoji(getEmojiByName("PUNISH.NICKNAME.GREEN", "id")) |
| 75 | .setCustomId("nickname") |
| 76 | .setStyle("SECONDARY") |
| 77 | ]), |
| 78 | new MessageActionRow().addComponents([ |
| 79 | new MessageButton() |
| 80 | .setLabel("Kick") |
| 81 | .setEmoji(getEmojiByName("PUNISH.KICK.RED", "id")) |
| 82 | .setCustomId("kick") |
| 83 | .setStyle("SECONDARY"), |
| 84 | new MessageButton() |
| 85 | .setLabel("Softban") |
| 86 | .setEmoji(getEmojiByName("PUNISH.BAN.YELLOW", "id")) |
| 87 | .setCustomId("softban") |
| 88 | .setStyle("SECONDARY"), |
| 89 | new MessageButton() |
| 90 | .setLabel("Ban") |
| 91 | .setEmoji(getEmojiByName("PUNISH.BAN.RED", "id")) |
| 92 | .setCustomId("ban") |
| 93 | .setStyle("SECONDARY") |
| 94 | ]), |
| 95 | new MessageActionRow().addComponents([ |
| 96 | new MessageButton() |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 97 | .setLabel(clicked === "clearMuteRole" ? "Click again to confirm" : "Clear mute role") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 98 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
| 99 | .setCustomId("clearMuteRole") |
| 100 | .setStyle("DANGER") |
| 101 | .setDisabled(!moderation.mute.role), |
| 102 | new MessageButton() |
| 103 | .setCustomId("timeout") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 104 | .setLabel("Mute timeout " + (moderation.mute.timeout ? "Enabled" : "Disabled")) |
| 105 | .setStyle(moderation.mute.timeout ? "SUCCESS" : "DANGER") |
| 106 | .setEmoji(getEmojiByName("CONTROL." + (moderation.mute.timeout ? "TICK" : "CROSS"), "id")) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 107 | ]) |
| 108 | ] |
| 109 | }); |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 110 | let i; |
| 111 | try { |
| 112 | i = await m.awaitMessageComponent({ time: 300000 }); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 113 | } catch (e) { |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 114 | timedOut = true; |
| 115 | continue; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 116 | } |
| 117 | let chosen = moderation[i.customId] ?? { text: null, url: null }; |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 118 | if (i.component.customId === "clearMuteRole") { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 119 | i.deferUpdate(); |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 120 | if (clicked === "clearMuteRole") { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 121 | await client.database.guilds.write(interaction.guild.id, { |
| 122 | "moderation.mute.role": null |
| 123 | }); |
| 124 | } else { |
| 125 | clicked = "clearMuteRole"; |
| 126 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 127 | continue; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 128 | } else { |
| 129 | clicked = ""; |
| 130 | } |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 131 | if (i.component.customId === "timeout") { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 132 | await i.deferUpdate(); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 133 | await client.database.guilds.write(interaction.guild.id, { |
| 134 | "moderation.mute.timeout": !moderation.mute.timeout |
| 135 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 136 | continue; |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 137 | } else if (i.customId) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 138 | await i.showModal( |
| 139 | new Discord.Modal() |
| 140 | .setCustomId("modal") |
| 141 | .setTitle(`Options for ${i.customId}`) |
| 142 | .addComponents( |
| 143 | new MessageActionRow<TextInputComponent>().addComponents( |
| 144 | new TextInputComponent() |
| 145 | .setCustomId("name") |
| 146 | .setLabel("Button text") |
| 147 | .setMaxLength(100) |
| 148 | .setRequired(false) |
| 149 | .setStyle("SHORT") |
| 150 | .setValue(chosen.text ?? "") |
| 151 | ), |
| 152 | new MessageActionRow<TextInputComponent>().addComponents( |
| 153 | new TextInputComponent() |
| 154 | .setCustomId("url") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 155 | .setLabel("URL - Type {id} to insert the user's ID") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 156 | .setMaxLength(2000) |
| 157 | .setRequired(false) |
| 158 | .setStyle("SHORT") |
| 159 | .setValue(chosen.link ?? "") |
| 160 | ) |
| 161 | ) |
| 162 | ); |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 163 | await interaction.editReply({ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 164 | embeds: [ |
| 165 | new EmojiEmbed() |
| 166 | .setTitle("Moderation Links") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 167 | .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] | 168 | .setStatus("Success") |
| 169 | .setEmoji("GUILD.TICKET.OPEN") |
| 170 | ], |
| 171 | components: [ |
| 172 | new MessageActionRow().addComponents([ |
| 173 | new MessageButton() |
| 174 | .setLabel("Back") |
| 175 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id")) |
| 176 | .setStyle("PRIMARY") |
| 177 | .setCustomId("back") |
| 178 | ]) |
| 179 | ] |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 180 | }); |
| 181 | let out; |
| 182 | try { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 183 | out = await modalInteractionCollector( |
| 184 | m, |
| 185 | (m) => m.channel.id === interaction.channel.id, |
| 186 | (_) => true |
| 187 | ); |
| 188 | } catch (e) { |
| 189 | continue; |
| 190 | } |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 191 | if (out.fields) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 192 | const buttonText = out.fields.getTextInputValue("name"); |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 193 | const buttonLink = out.fields.getTextInputValue("url").replace(/{id}/gi, "{id}"); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 194 | const current = chosen; |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 195 | if (current.text !== buttonText || current.link !== buttonLink) { |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 196 | chosen = { text: buttonText, link: buttonLink }; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 197 | await client.database.guilds.write(interaction.guild.id, { |
| 198 | ["moderation." + i.customId]: { |
| 199 | text: buttonText, |
| 200 | link: buttonLink |
| 201 | } |
| 202 | }); |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 203 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 204 | } else { |
| 205 | continue; |
| 206 | } |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 207 | } |
| 208 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 209 | }; |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 210 | |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 211 | const check = (interaction: CommandInteraction, _defaultCheck: WrappedCheck) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 212 | const member = interaction.member as Discord.GuildMember; |
| 213 | if (!member.permissions.has("MANAGE_GUILD")) |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 214 | throw new Error("You must have the *Manage Server* permission to use this command"); |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 215 | return true; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 216 | }; |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 217 | |
| 218 | export { command }; |
| 219 | export { callback }; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 220 | export { check }; |