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