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