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