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