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