Skyler Grey | 11236ba | 2022-08-08 21:13:33 +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"; |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +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") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 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 | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 15 | const callback = async (interaction: CommandInteraction): Promise<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") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 26 | .setDescription( |
| 27 | keyValueList({ |
| 28 | user: renderUser(interaction.options.getUser("user")), |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 29 | reason: reason ? "\n> " + (reason ?? "").replaceAll("\n", "\n> ") : "*No reason provided*" |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 30 | }) + |
| 31 | `The user **will${notify ? "" : " not"}** be notified\n\n` + |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 32 | `Are you sure you want to warn <@!${(interaction.options.getMember("user") as GuildMember).id}>?` |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 33 | ) |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 34 | .setColor("Danger") |
| 35 | .addCustomBoolean( |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 36 | "appeal", |
| 37 | "Create appeal ticket", |
| 38 | !(await areTicketsEnabled(interaction.guild.id)), |
| 39 | async () => |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 40 | await create(interaction.guild, interaction.options.getUser("user"), interaction.user, reason), |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 41 | "An appeal ticket will be created when Confirm is clicked", |
| 42 | "CONTROL.TICKET", |
| 43 | createAppealTicket |
| 44 | ) |
| 45 | .addCustomBoolean( |
| 46 | "notify", |
| 47 | "Notify user", |
| 48 | false, |
| 49 | null, |
| 50 | null, |
| 51 | "ICONS.NOTIFY." + (notify ? "ON" : "OFF"), |
| 52 | notify |
| 53 | ) |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 54 | .addReasonButton(reason ?? "") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 55 | .send(reason !== null); |
| 56 | reason = reason ?? ""; |
| 57 | if (confirmation.cancelled) return; |
| 58 | if (confirmation.success) break; |
| 59 | if (confirmation.newReason) reason = confirmation.newReason; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 60 | if (confirmation.components) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 61 | notify = confirmation.components.notify.active; |
| 62 | createAppealTicket = confirmation.components.appeal.active; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 63 | } |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 64 | } |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 65 | if (confirmation.success) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 66 | let dmd = false; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 67 | try { |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 68 | if (notify) { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 69 | const config = await client.database.guilds.read(interaction.guild.id); |
| 70 | await (interaction.options.getMember("user") as GuildMember).send({ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 71 | embeds: [ |
| 72 | new EmojiEmbed() |
| 73 | .setEmoji("PUNISH.WARN.RED") |
| 74 | .setTitle("Warned") |
| 75 | .setDescription( |
| 76 | `You have been warned in ${interaction.guild.name}` + |
| 77 | (reason ? ` for:\n> ${reason}` : ".") + |
| 78 | "\n\n" + |
| 79 | (confirmation.components.appeal.response |
| 80 | ? `You can appeal this here ticket: <#${confirmation.components.appeal.response}>` |
| 81 | : "") |
| 82 | ) |
| 83 | .setStatus("Danger") |
| 84 | .setFooter({ |
| 85 | text: config.moderation.warn.text |
| 86 | ? "The button below is set by the server admins. Do not enter any passwords or other account details on the linked site." |
| 87 | : "", |
| 88 | iconURL: |
| 89 | "https://cdn.discordapp.com/emojis/952295894370369587.webp?size=128&quality=lossless" |
| 90 | }) |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 91 | ], |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 92 | components: config.moderation.warn.text |
| 93 | ? [ |
| 94 | new MessageActionRow().addComponents([ |
| 95 | new MessageButton() |
| 96 | .setStyle("LINK") |
| 97 | .setLabel(config.moderation.warn.text) |
| 98 | .setURL(config.moderation.warn.link) |
| 99 | ]) |
| 100 | ] |
| 101 | : [] |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 102 | }); |
| 103 | dmd = true; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 104 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 105 | } catch { |
| 106 | dmd = false; |
| 107 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 108 | const data = { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 109 | meta: { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 110 | type: "memberWarn", |
| 111 | displayName: "Member warned", |
| 112 | calculateType: "guildMemberPunish", |
pineafan | 1dc1572 | 2022-03-14 21:27:34 +0000 | [diff] [blame] | 113 | color: NucleusColors.yellow, |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 114 | emoji: "PUNISH.WARN.YELLOW", |
pineafan | 1dc1572 | 2022-03-14 21:27:34 +0000 | [diff] [blame] | 115 | timestamp: new Date().getTime() |
| 116 | }, |
| 117 | list: { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 118 | user: entry( |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 119 | (interaction.options.getMember("user") as GuildMember).user.id, |
| 120 | renderUser((interaction.options.getMember("user") as GuildMember).user) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 121 | ), |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 122 | warnedBy: entry(interaction.member.user.id, renderUser(interaction.member.user)), |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 123 | reason: reason ? `\n> ${reason}` : "No reason provided" |
pineafan | 1dc1572 | 2022-03-14 21:27:34 +0000 | [diff] [blame] | 124 | }, |
| 125 | hidden: { |
| 126 | guild: interaction.guild.id |
| 127 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 128 | }; |
| 129 | await client.database.history.create( |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 130 | "warn", |
| 131 | interaction.guild.id, |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 132 | (interaction.options.getMember("user") as GuildMember).user, |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 133 | interaction.user, |
| 134 | reason |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 135 | ); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 136 | log(data); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 137 | const failed = !dmd && notify; |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 138 | if (!failed) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 139 | await interaction.editReply({ |
| 140 | embeds: [ |
| 141 | new EmojiEmbed() |
| 142 | .setEmoji("PUNISH.WARN.GREEN") |
| 143 | .setTitle("Warn") |
| 144 | .setDescription( |
| 145 | "The user was warned" + |
| 146 | (confirmation.components.appeal.response |
| 147 | ? ` and an appeal ticket was opened in <#${confirmation.components.appeal.response}>` |
| 148 | : "") |
| 149 | ) |
| 150 | .setStatus("Success") |
| 151 | ], |
| 152 | components: [] |
| 153 | }); |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 154 | } else { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 155 | const canSeeChannel = (interaction.options.getMember("user") as GuildMember) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 156 | .permissionsIn(interaction.channel as Discord.TextChannel) |
| 157 | .has("VIEW_CHANNEL"); |
| 158 | const m = (await interaction.editReply({ |
| 159 | embeds: [ |
| 160 | new EmojiEmbed() |
| 161 | .setEmoji("PUNISH.WARN.RED") |
| 162 | .setTitle("Warn") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 163 | .setDescription("The user's DMs are not open\n\nWhat would you like to do?") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 164 | .setStatus("Danger") |
| 165 | ], |
| 166 | components: [ |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 167 | new MessageActionRow().addComponents([ |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 168 | new Discord.MessageButton().setCustomId("log").setLabel("Ignore and log").setStyle("SECONDARY"), |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 169 | new Discord.MessageButton() |
| 170 | .setCustomId("here") |
| 171 | .setLabel("Warn here") |
pineafan | c1c1879 | 2022-08-03 21:41:36 +0100 | [diff] [blame] | 172 | .setStyle(canSeeChannel ? "PRIMARY" : "SECONDARY") |
| 173 | .setDisabled(!canSeeChannel), |
| 174 | new Discord.MessageButton() |
| 175 | .setCustomId("ticket") |
| 176 | .setLabel("Create ticket") |
| 177 | .setStyle(canSeeChannel ? "SECONDARY" : "PRIMARY") |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 178 | ]) |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 179 | ] |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 180 | })) as Discord.Message; |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 181 | let component; |
| 182 | try { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 183 | component = await m.awaitMessageComponent({ |
| 184 | filter: (m) => m.user.id === interaction.user.id, |
| 185 | time: 300000 |
| 186 | }); |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 187 | } catch (e) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 188 | return await interaction.editReply({ |
| 189 | embeds: [ |
| 190 | new EmojiEmbed() |
| 191 | .setEmoji("PUNISH.WARN.GREEN") |
| 192 | .setTitle("Warn") |
| 193 | .setDescription("No changes were made") |
| 194 | .setStatus("Success") |
| 195 | ], |
| 196 | components: [] |
| 197 | }); |
| 198 | } |
| 199 | if (component.customId === "here") { |
| 200 | await interaction.channel.send({ |
| 201 | embeds: [ |
| 202 | new EmojiEmbed() |
| 203 | .setEmoji("PUNISH.WARN.RED") |
| 204 | .setTitle("Warn") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 205 | .setDescription("You have been warned" + (reason ? ` for:\n> ${reason}` : ".")) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 206 | .setStatus("Danger") |
| 207 | ], |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 208 | content: `<@!${(interaction.options.getMember("user") as GuildMember).id}>`, |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 209 | allowedMentions: { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 210 | users: [(interaction.options.getMember("user") as GuildMember).id] |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 211 | } |
| 212 | }); |
| 213 | return await interaction.editReply({ |
| 214 | embeds: [ |
| 215 | new EmojiEmbed() |
| 216 | .setEmoji("PUNISH.WARN.GREEN") |
| 217 | .setTitle("Warn") |
| 218 | .setDescription( |
| 219 | "The user was warned" + |
| 220 | (confirmation.response |
| 221 | ? ` and an appeal ticket was opened in <#${confirmation.response}>` |
| 222 | : "") |
| 223 | ) |
| 224 | .setStatus("Success") |
| 225 | ], |
| 226 | components: [] |
| 227 | }); |
| 228 | } else if (component.customId === "log") { |
| 229 | await interaction.editReply({ |
| 230 | embeds: [ |
| 231 | new EmojiEmbed() |
| 232 | .setEmoji("PUNISH.WARN.GREEN") |
| 233 | .setTitle("Warn") |
| 234 | .setDescription("The warn was logged") |
| 235 | .setStatus("Success") |
| 236 | ], |
| 237 | components: [] |
| 238 | }); |
| 239 | } else if (component.customId === "ticket") { |
| 240 | const ticketChannel = await create( |
| 241 | interaction.guild, |
| 242 | interaction.options.getUser("user"), |
| 243 | interaction.user, |
| 244 | reason, |
| 245 | "Warn Notification" |
| 246 | ); |
| 247 | if (ticketChannel === null) { |
| 248 | return await interaction.editReply({ |
| 249 | embeds: [ |
| 250 | new EmojiEmbed() |
| 251 | .setEmoji("PUNISH.WARN.RED") |
| 252 | .setTitle("Warn") |
| 253 | .setDescription("A ticket could not be created") |
| 254 | .setStatus("Danger") |
| 255 | ], |
| 256 | components: [] |
| 257 | }); |
| 258 | } |
| 259 | await interaction.editReply({ |
| 260 | embeds: [ |
| 261 | new EmojiEmbed() |
| 262 | .setEmoji("PUNISH.WARN.GREEN") |
| 263 | .setTitle("Warn") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 264 | .setDescription(`A ticket was created in <#${ticketChannel}>`) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 265 | .setStatus("Success") |
| 266 | ], |
| 267 | components: [] |
| 268 | }); |
| 269 | } |
| 270 | } |
| 271 | } else { |
| 272 | await interaction.editReply({ |
| 273 | embeds: [ |
| 274 | new EmojiEmbed() |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 275 | .setEmoji("PUNISH.WARN.GREEN") |
| 276 | .setTitle("Warn") |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 277 | .setDescription("No changes were made") |
| 278 | .setStatus("Success") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 279 | ], |
| 280 | components: [] |
| 281 | }); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 282 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 283 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 284 | |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 285 | const check = (interaction: CommandInteraction) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 286 | const member = interaction.member as GuildMember; |
| 287 | const me = interaction.guild.me!; |
| 288 | const apply = interaction.options.getMember("user") as GuildMember; |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 289 | if (member === null || me === null || apply === null) throw new Error("That member is not in the server"); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 290 | const memberPos = member.roles ? member.roles.highest.position : 0; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 291 | const applyPos = apply.roles ? apply.roles.highest.position : 0; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 292 | // Do not allow warning bots |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 293 | if (member.user.bot) throw new Error("I cannot warn bots"); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 294 | // Allow the owner to warn anyone |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 295 | if (member.id === interaction.guild.ownerId) return true; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 296 | // Check if the user has moderate_members permission |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 297 | if (!member.permissions.has("MODERATE_MEMBERS")) |
| 298 | throw new Error("You do not have the *Moderate Members* permission"); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 299 | // Check if the user is below on the role list |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 300 | if (!(memberPos > applyPos)) throw new Error("You do not have a role higher than that member"); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 301 | // Allow warn |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 302 | return true; |
| 303 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 304 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 305 | export { command, callback, check }; |