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