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