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