pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 1 | import Discord, { CommandInteraction, GuildMember, MessageActionRow, MessageButton } from "discord.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 2 | import { SlashCommandSubcommandBuilder } from "@discordjs/builders"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 3 | import confirmationMessage from "../../utils/confirmationMessage.js"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 4 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 5 | import keyValueList from "../../utils/generateKeyValueList.js"; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 6 | import { create, areTicketsEnabled } from "../../actions/createModActionTicket.js"; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 7 | import client from "../../utils/client.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 8 | |
| 9 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 10 | builder |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 11 | .setName("warn") |
| 12 | .setDescription("Warns a user") |
| 13 | .addUserOption(option => option.setName("user").setDescription("The user to warn").setRequired(true)); |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 14 | |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame^] | 15 | const callback = async (interaction: CommandInteraction): Promise<void | unknown> => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 16 | const { log, NucleusColors, renderUser, entry } = client.logger; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 17 | // TODO:[Modals] Replace this with a modal |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 18 | let reason = null; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 19 | let notify = true; |
| 20 | let createAppealTicket = false; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 21 | let confirmation; |
| 22 | while (true) { |
| 23 | confirmation = await new confirmationMessage(interaction) |
| 24 | .setEmoji("PUNISH.WARN.RED") |
| 25 | .setTitle("Warn") |
| 26 | .setDescription(keyValueList({ |
| 27 | "user": renderUser(interaction.options.getUser("user")), |
| 28 | "reason": reason ? ("\n> " + ((reason ?? "").replaceAll("\n", "\n> "))) : "*No reason provided*" |
| 29 | }) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 30 | + `The user **will${notify ? "" : " not"}** be notified\n\n` |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 31 | + `Are you sure you want to warn <@!${(interaction.options.getMember("user") as GuildMember).id}>?`) |
| 32 | .setColor("Danger") |
| 33 | .addCustomBoolean( |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 34 | "appeal", "Create appeal ticket", !(await areTicketsEnabled(interaction.guild.id)), |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 35 | async () => await create(interaction.guild, interaction.options.getUser("user"), interaction.user, reason), |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 36 | "An appeal ticket will be created when Confirm is clicked", "CONTROL.TICKET", createAppealTicket) |
| 37 | .addCustomBoolean("notify", "Notify user", false, null, null, "ICONS.NOTIFY." + (notify ? "ON" : "OFF" ), notify) |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 38 | .addReasonButton(reason ?? "") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 39 | .send(reason !== null); |
| 40 | reason = reason ?? ""; |
| 41 | if (confirmation.cancelled) return; |
| 42 | if (confirmation.success) break; |
| 43 | if (confirmation.newReason) reason = confirmation.newReason; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 44 | if (confirmation.components) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 45 | notify = confirmation.components.notify.active; |
| 46 | createAppealTicket = confirmation.components.appeal.active; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 47 | } |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 48 | } |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 49 | if (confirmation.success) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 50 | let dmd = false; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 51 | try { |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 52 | if (notify) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 53 | const config = await client.database.guilds.read(interaction.guild.id); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 54 | await (interaction.options.getMember("user") as GuildMember).send({ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 55 | embeds: [new EmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 56 | .setEmoji("PUNISH.WARN.RED") |
| 57 | .setTitle("Warned") |
| 58 | .setDescription(`You have been warned in ${interaction.guild.name}` + |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 59 | (reason ? ` for:\n> ${reason}` : ".") + "\n\n" + |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 60 | (confirmation.components.appeal.response ? `You can appeal this here ticket: <#${confirmation.components.appeal.response}>` : "")) |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 61 | .setStatus("Danger") |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 62 | .setFooter({ |
| 63 | text: config.moderation.warn.text ? "The button below is set by the server admins. Do not enter any passwords or other account details on the linked site." : "", |
| 64 | iconURL: "https://cdn.discordapp.com/emojis/952295894370369587.webp?size=128&quality=lossless" |
| 65 | }) |
| 66 | ], |
| 67 | components: config.moderation.warn.text ? [new MessageActionRow().addComponents([new MessageButton() |
| 68 | .setStyle("LINK") |
| 69 | .setLabel(config.moderation.warn.text) |
| 70 | .setURL(config.moderation.warn.link) |
| 71 | ])] : [] |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 72 | }); |
| 73 | dmd = true; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 74 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 75 | } catch { dmd = false; } |
| 76 | const data = { |
pineafan | 1dc1572 | 2022-03-14 21:27:34 +0000 | [diff] [blame] | 77 | meta:{ |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 78 | type: "memberWarn", |
| 79 | displayName: "Member warned", |
| 80 | calculateType: "guildMemberPunish", |
pineafan | 1dc1572 | 2022-03-14 21:27:34 +0000 | [diff] [blame] | 81 | color: NucleusColors.yellow, |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 82 | emoji: "PUNISH.WARN.YELLOW", |
pineafan | 1dc1572 | 2022-03-14 21:27:34 +0000 | [diff] [blame] | 83 | timestamp: new Date().getTime() |
| 84 | }, |
| 85 | list: { |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 86 | user: entry((interaction.options.getMember("user") as GuildMember).user.id, renderUser((interaction.options.getMember("user") as GuildMember).user)), |
| 87 | warnedBy: entry(interaction.member.user.id, renderUser(interaction.member.user)), |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 88 | reason: reason ? `\n> ${reason}` : "No reason provided" |
pineafan | 1dc1572 | 2022-03-14 21:27:34 +0000 | [diff] [blame] | 89 | }, |
| 90 | hidden: { |
| 91 | guild: interaction.guild.id |
| 92 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 93 | }; |
| 94 | await client.database.history.create( |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 95 | "warn", interaction.guild.id, |
| 96 | (interaction.options.getMember("user") as GuildMember).user, |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 97 | interaction.user, reason |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 98 | ); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 99 | log(data); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 100 | const failed = (dmd === false && notify); |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 101 | if (!failed) { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 102 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 103 | .setEmoji("PUNISH.WARN.GREEN") |
| 104 | .setTitle("Warn") |
| 105 | .setDescription("The user was warned" + (confirmation.components.appeal.response ? ` and an appeal ticket was opened in <#${confirmation.components.appeal.response}>` : "")) |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 106 | .setStatus("Success") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 107 | ], components: []}); |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 108 | } else { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 109 | const canSeeChannel = (interaction.options.getMember("user") as GuildMember).permissionsIn(interaction.channel as Discord.TextChannel).has("VIEW_CHANNEL"); |
| 110 | const m = await interaction.editReply({ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 111 | embeds: [new EmojiEmbed() |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 112 | .setEmoji("PUNISH.WARN.RED") |
| 113 | .setTitle("Warn") |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 114 | .setDescription("The user's DMs are not open\n\nWhat would you like to do?") |
| 115 | .setStatus("Danger") |
| 116 | ], components: [ |
| 117 | new MessageActionRow().addComponents([ |
| 118 | new Discord.MessageButton() |
| 119 | .setCustomId("log") |
| 120 | .setLabel("Ignore and log") |
| 121 | .setStyle("SECONDARY"), |
| 122 | new Discord.MessageButton() |
| 123 | .setCustomId("here") |
| 124 | .setLabel("Warn here") |
pineafan | c1c1879 | 2022-08-03 21:41:36 +0100 | [diff] [blame] | 125 | .setStyle(canSeeChannel ? "PRIMARY" : "SECONDARY") |
| 126 | .setDisabled(!canSeeChannel), |
| 127 | new Discord.MessageButton() |
| 128 | .setCustomId("ticket") |
| 129 | .setLabel("Create ticket") |
| 130 | .setStyle(canSeeChannel ? "SECONDARY" : "PRIMARY") |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 131 | ]) |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 132 | ] |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame^] | 133 | }) as Discord.Message; |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 134 | let component; |
| 135 | try { |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame^] | 136 | component = await m.awaitMessageComponent({filter: (m) => m.user.id === interaction.user.id, time: 300000}); |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 137 | } catch (e) { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 138 | return await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 139 | .setEmoji("PUNISH.WARN.GREEN") |
| 140 | .setTitle("Warn") |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 141 | .setDescription("No changes were made") |
| 142 | .setStatus("Success") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 143 | ], components: []}); |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 144 | } |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 145 | if ( component.customId === "here" ) { |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 146 | await interaction.channel.send({ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 147 | embeds: [new EmojiEmbed() |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 148 | .setEmoji("PUNISH.WARN.RED") |
| 149 | .setTitle("Warn") |
| 150 | .setDescription("You have been warned" + |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 151 | (reason ? ` for:\n> ${reason}` : ".")) |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 152 | .setStatus("Danger") |
| 153 | ], |
| 154 | content: `<@!${(interaction.options.getMember("user") as GuildMember).id}>`, |
| 155 | allowedMentions: {users: [(interaction.options.getMember("user") as GuildMember).id]} |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 156 | }); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 157 | return await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 158 | .setEmoji("PUNISH.WARN.GREEN") |
| 159 | .setTitle("Warn") |
| 160 | .setDescription("The user was warned" + (confirmation.response ? ` and an appeal ticket was opened in <#${confirmation.response}>` : "")) |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 161 | .setStatus("Success") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 162 | ], components: []}); |
pineafan | c1c1879 | 2022-08-03 21:41:36 +0100 | [diff] [blame] | 163 | } else if (component.customId === "log") { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 164 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 165 | .setEmoji("PUNISH.WARN.GREEN") |
| 166 | .setTitle("Warn") |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 167 | .setDescription("The warn was logged") |
| 168 | .setStatus("Success") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 169 | ], components: []}); |
pineafan | c1c1879 | 2022-08-03 21:41:36 +0100 | [diff] [blame] | 170 | } else if (component.customId === "ticket") { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 171 | const ticketChannel = await create(interaction.guild, interaction.options.getUser("user"), interaction.user, reason, "Warn Notification"); |
pineafan | c1c1879 | 2022-08-03 21:41:36 +0100 | [diff] [blame] | 172 | if (ticketChannel === null) { |
| 173 | return await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 174 | .setEmoji("PUNISH.WARN.RED") |
| 175 | .setTitle("Warn") |
pineafan | c1c1879 | 2022-08-03 21:41:36 +0100 | [diff] [blame] | 176 | .setDescription("A ticket could not be created") |
| 177 | .setStatus("Danger") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 178 | ], components: []}); |
pineafan | c1c1879 | 2022-08-03 21:41:36 +0100 | [diff] [blame] | 179 | } |
| 180 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 181 | .setEmoji("PUNISH.WARN.GREEN") |
| 182 | .setTitle("Warn") |
pineafan | c1c1879 | 2022-08-03 21:41:36 +0100 | [diff] [blame] | 183 | .setDescription(`A ticket was created in <#${ticketChannel}>`) |
| 184 | .setStatus("Success") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 185 | ], components: []}); |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 186 | } |
| 187 | } |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 188 | } else { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 189 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 190 | .setEmoji("PUNISH.WARN.GREEN") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 191 | .setTitle("Warn") |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 192 | .setDescription("No changes were made") |
| 193 | .setStatus("Success") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 194 | ], components: []}); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 195 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 196 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 197 | |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame^] | 198 | const check = (interaction: CommandInteraction) => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 199 | const member = (interaction.member as GuildMember); |
| 200 | const me = (interaction.guild.me as GuildMember); |
| 201 | const apply = (interaction.options.getMember("user") as GuildMember); |
| 202 | if (member === null || me === null || apply === null) throw "That member is not in the server"; |
| 203 | const memberPos = member.roles ? member.roles.highest.position : 0; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 204 | const applyPos = apply.roles ? apply.roles.highest.position : 0; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 205 | // Do not allow warning bots |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 206 | if (member.user.bot) throw "I cannot warn bots"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 207 | // Allow the owner to warn anyone |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 208 | if (member.id === interaction.guild.ownerId) return true; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 209 | // Check if the user has moderate_members permission |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 210 | if (! member.permissions.has("MODERATE_MEMBERS")) throw "You do not have the *Moderate Members* permission"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 211 | // Check if the user is below on the role list |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 212 | if (! (memberPos > applyPos)) throw "You do not have a role higher than that member"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 213 | // Allow warn |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 214 | return true; |
| 215 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 216 | |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 217 | export { command, callback, check }; |