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