pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 1 | import { LoadingEmbed } from "./../../utils/defaultEmbeds.js"; |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 2 | import Discord, { CommandInteraction, MessageActionRow, MessageButton, TextInputComponent } from "discord.js"; |
| 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") |
| 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 | |
| 18 | const callback = async (interaction: CommandInteraction): Promise<any> => { |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 19 | await interaction.reply({embeds: LoadingEmbed, ephemeral: true, fetchReply: true}); |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 20 | let m; |
| 21 | let clicked = ""; |
| 22 | if (interaction.options.getRole("role")) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 23 | const confirmation = await new confirmationMessage(interaction) |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 24 | .setEmoji("GUILD.ROLES.DELETE") |
| 25 | .setTitle("Moderation Commands") |
| 26 | .setDescription(keyValueList({ |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 27 | role: `<@&${interaction.options.getRole("role").id}>` |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 28 | })) |
| 29 | .setColor("Danger") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 30 | .send(true); |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 31 | if (confirmation.cancelled) return await interaction.editReply({embeds: [new EmojiEmbed() |
| 32 | .setTitle("Moderation Commands") |
| 33 | .setDescription("No changes were made") |
| 34 | .setStatus("Success") |
| 35 | .setEmoji("GUILD.ROLES.CREATE") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 36 | ]}); |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 37 | if (confirmation.success) { |
| 38 | await client.database.guilds.write(interaction.guild.id, {["moderation.mute.role"]: interaction.options.getRole("role").id}); |
| 39 | } |
| 40 | } |
| 41 | while (true) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 42 | const config = await client.database.guilds.read(interaction.guild.id); |
| 43 | const moderation = config.getKey("moderation"); |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 44 | m = await interaction.editReply({embeds: [new EmojiEmbed() |
| 45 | .setTitle("Moderation Commands") |
| 46 | .setEmoji("PUNISH.BAN.GREEN") |
| 47 | .setStatus("Success") |
| 48 | .setDescription( |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 49 | "These links are shown below the message sent in a user's DM when they are punished.\n\n" + |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 50 | "**Mute Role:** " + (moderation.mute.role ? `<@&${moderation.mute.role}>` : "*None set*") |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 51 | ) |
| 52 | ], components: [new MessageActionRow().addComponents([ |
| 53 | new MessageButton().setLabel("Warn").setEmoji(getEmojiByName("PUNISH.WARN.YELLOW", "id")).setCustomId("warn").setStyle("SECONDARY"), |
| 54 | new MessageButton().setLabel("Mute").setEmoji(getEmojiByName("PUNISH.MUTE.YELLOW", "id")).setCustomId("mute").setStyle("SECONDARY"), |
| 55 | new MessageButton().setLabel("Nickname").setEmoji(getEmojiByName("PUNISH.NICKNAME.GREEN", "id")).setCustomId("nickname").setStyle("SECONDARY") |
| 56 | ]), new MessageActionRow().addComponents([ |
| 57 | new MessageButton().setLabel("Kick").setEmoji(getEmojiByName("PUNISH.KICK.RED", "id")).setCustomId("kick").setStyle("SECONDARY"), |
| 58 | new MessageButton().setLabel("Softban").setEmoji(getEmojiByName("PUNISH.BAN.YELLOW", "id")).setCustomId("softban").setStyle("SECONDARY"), |
| 59 | new MessageButton().setLabel("Ban").setEmoji(getEmojiByName("PUNISH.BAN.RED", "id")).setCustomId("ban").setStyle("SECONDARY") |
| 60 | ]), new MessageActionRow().addComponents([ |
| 61 | new MessageButton().setLabel( |
| 62 | clicked === "clearMuteRole" ? "Click again to confirm" : "Clear mute role" |
| 63 | ).setEmoji(getEmojiByName("CONTROL.CROSS", "id")).setCustomId("clearMuteRole").setStyle("DANGER").setDisabled(!moderation.mute.role), |
| 64 | new MessageButton() |
| 65 | .setCustomId("timeout") |
| 66 | .setLabel("Mute timeout " + (moderation.mute.timeout ? "Enabled" : "Disabled")) |
| 67 | .setStyle(moderation.mute.timeout ? "SUCCESS" : "DANGER") |
| 68 | .setEmoji(getEmojiByName("CONTROL." + (moderation.mute.timeout ? "TICK" : "CROSS"), "id")) |
| 69 | ])]}); |
| 70 | let i; |
| 71 | try { |
| 72 | i = await m.awaitMessageComponent({ time: 300000 }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 73 | } catch (e) { return; } |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 74 | let chosen = moderation[i.customId] ?? {text: null, url: null}; |
| 75 | if (i.component.customId === "clearMuteRole") { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 76 | i.deferUpdate(); |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 77 | if (clicked === "clearMuteRole") { |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 78 | await client.database.guilds.write(interaction.guild.id, {"moderation.mute.role": null }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 79 | } else { clicked = "clearMuteRole"; } |
| 80 | continue; |
| 81 | } else { clicked = ""; } |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 82 | if (i.component.customId === "timeout") { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 83 | await i.deferUpdate(); |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 84 | await client.database.guilds.write(interaction.guild.id, {"moderation.mute.timeout": !moderation.mute.timeout } ); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 85 | continue; |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 86 | } else if (i.customId) { |
| 87 | await i.showModal(new Discord.Modal().setCustomId("modal").setTitle(`Options for ${i.customId}`).addComponents( |
| 88 | new MessageActionRow<TextInputComponent>().addComponents(new TextInputComponent() |
| 89 | .setCustomId("name") |
| 90 | .setLabel("Button text") |
| 91 | .setMaxLength(100) |
| 92 | .setRequired(false) |
| 93 | .setStyle("SHORT") |
| 94 | .setValue(chosen.text ?? "") |
| 95 | ), |
| 96 | new MessageActionRow<TextInputComponent>().addComponents(new TextInputComponent() |
| 97 | .setCustomId("url") |
| 98 | .setLabel("URL - Type {id} to insert the user's ID") |
| 99 | .setMaxLength(2000) |
| 100 | .setRequired(false) |
| 101 | .setStyle("SHORT") |
| 102 | .setValue(chosen.link ?? "") |
| 103 | ) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 104 | )); |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 105 | await interaction.editReply({ |
| 106 | embeds: [new EmojiEmbed() |
| 107 | .setTitle("Moderation Links") |
| 108 | .setDescription("Modal opened. If you can't see it, click back and try again.") |
| 109 | .setStatus("Success") |
| 110 | .setEmoji("GUILD.TICKET.OPEN") |
| 111 | ], components: [new MessageActionRow().addComponents([new MessageButton() |
| 112 | .setLabel("Back") |
| 113 | .setEmoji(getEmojiByName("CONTROL.LEFT", "id")) |
| 114 | .setStyle("PRIMARY") |
| 115 | .setCustomId("back") |
| 116 | ])] |
| 117 | }); |
| 118 | let out; |
| 119 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 120 | out = await modalInteractionCollector(m, (m) => m.channel.id === interaction.channel.id, (m) => true); |
| 121 | } catch (e) { continue; } |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 122 | if (out.fields) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 123 | const buttonText = out.fields.getTextInputValue("name"); |
| 124 | const buttonLink = out.fields.getTextInputValue("url").replace(/{id}/gi, "{id}"); |
| 125 | const current = chosen; |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 126 | if (current.text !== buttonText || current.link !== buttonLink) { |
| 127 | chosen = { text: buttonText, link: buttonLink }; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 128 | await client.database.guilds.write(interaction.guild.id, { ["moderation." + i.customId]: { text: buttonText, link: buttonLink }}); |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 129 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 130 | } else { continue; } |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 131 | } |
| 132 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 133 | }; |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 134 | |
| 135 | |
| 136 | const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 137 | const member = (interaction.member as Discord.GuildMember); |
| 138 | if (!member.permissions.has("MANAGE_GUILD")) throw "You must have the *Manage Server* permission to use this command"; |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 139 | return true; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 140 | }; |
pineafan | 0bc0416 | 2022-07-25 17:22:26 +0100 | [diff] [blame] | 141 | |
| 142 | export { command }; |
| 143 | export { callback }; |
| 144 | export { check }; |