pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 1 | import Discord, { CommandInteraction, GuildMember, MessageActionRow, MessageButton } from "discord.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 2 | import { SlashCommandSubcommandBuilder } from "@discordjs/builders"; |
| 3 | import { WrappedCheck } from "jshaiku"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 4 | import confirmationMessage from "../../utils/confirmationMessage.js"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 5 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 6 | import keyValueList from "../../utils/generateKeyValueList.js"; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 7 | import { create, areTicketsEnabled } from "../../actions/createModActionTicket.js"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 8 | import client from "../../utils/client.js" |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 9 | |
| 10 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 11 | builder |
| 12 | .setName("warn") |
| 13 | .setDescription("Warns a user") |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [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 | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 16 | const callback = async (interaction: CommandInteraction): Promise<any> => { |
PineappleFan | b3dd83c | 2022-06-17 10:53:48 +0100 | [diff] [blame] | 17 | const { log, NucleusColors, renderUser, entry } = client.logger |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 18 | // TODO:[Modals] Replace this with a modal |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 19 | let reason = null; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 20 | let notify = true; |
| 21 | let createAppealTicket = false; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 22 | let confirmation; |
| 23 | while (true) { |
| 24 | confirmation = await new confirmationMessage(interaction) |
| 25 | .setEmoji("PUNISH.WARN.RED") |
| 26 | .setTitle("Warn") |
| 27 | .setDescription(keyValueList({ |
| 28 | "user": renderUser(interaction.options.getUser("user")), |
| 29 | "reason": reason ? ("\n> " + ((reason ?? "").replaceAll("\n", "\n> "))) : "*No reason provided*" |
| 30 | }) |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 31 | + `The user **will${notify ? '' : ' not'}** be notified\n\n` |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 32 | + `Are you sure you want to warn <@!${(interaction.options.getMember("user") as GuildMember).id}>?`) |
| 33 | .setColor("Danger") |
| 34 | .addCustomBoolean( |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 35 | "appeal", "Create appeal ticket", !(await areTicketsEnabled(interaction.guild.id)), |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 36 | async () => await create(interaction.guild, interaction.options.getUser("user"), interaction.user, reason), |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 37 | "An appeal ticket will be created when Confirm is clicked", "CONTROL.TICKET", createAppealTicket) |
| 38 | .addCustomBoolean("notify", "Notify user", false, null, null, "ICONS.NOTIFY." + (notify ? "ON" : "OFF" ), notify) |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 39 | .addReasonButton(reason ?? "") |
| 40 | .send(reason !== null) |
| 41 | reason = reason ?? "" |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 42 | if (confirmation.cancelled) return |
| 43 | if (confirmation.success) break |
| 44 | if (confirmation.newReason) reason = confirmation.newReason |
| 45 | if (confirmation.components) { |
| 46 | notify = confirmation.components.notify.active |
| 47 | createAppealTicket = confirmation.components.appeal.active |
| 48 | } |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 49 | } |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 50 | if (confirmation.success) { |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 51 | let dmd = false |
| 52 | try { |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 53 | if (notify) { |
| 54 | const config = await client.database.guilds.read(interaction.guild.id) |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 55 | await (interaction.options.getMember("user") as GuildMember).send({ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 56 | embeds: [new EmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 57 | .setEmoji("PUNISH.WARN.RED") |
| 58 | .setTitle("Warned") |
| 59 | .setDescription(`You have been warned in ${interaction.guild.name}` + |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 60 | (reason ? ` for:\n> ${reason}` : ".") + "\n\n" + |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 61 | (confirmation.components.appeal.response ? `You can appeal this here ticket: <#${confirmation.components.appeal.response}>` : ``)) |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 62 | .setStatus("Danger") |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 63 | .setFooter({ |
| 64 | text: config.moderation.warn.text ? "The button below is set by the server admins. Do not enter any passwords or other account details on the linked site." : "", |
| 65 | iconURL: "https://cdn.discordapp.com/emojis/952295894370369587.webp?size=128&quality=lossless" |
| 66 | }) |
| 67 | ], |
| 68 | components: config.moderation.warn.text ? [new MessageActionRow().addComponents([new MessageButton() |
| 69 | .setStyle("LINK") |
| 70 | .setLabel(config.moderation.warn.text) |
| 71 | .setURL(config.moderation.warn.link) |
| 72 | ])] : [] |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 73 | }) |
| 74 | dmd = true |
| 75 | } |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 76 | } catch {} |
pineafan | 1dc1572 | 2022-03-14 21:27:34 +0000 | [diff] [blame] | 77 | let data = { |
| 78 | meta:{ |
| 79 | type: 'memberWarn', |
| 80 | displayName: 'Member warned', |
| 81 | calculateType: 'guildMemberPunish', |
| 82 | color: NucleusColors.yellow, |
| 83 | emoji: 'PUNISH.WARN.YELLOW', |
| 84 | timestamp: new Date().getTime() |
| 85 | }, |
| 86 | list: { |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 87 | user: entry((interaction.options.getMember("user") as GuildMember).user.id, renderUser((interaction.options.getMember("user") as GuildMember).user)), |
| 88 | warnedBy: entry(interaction.member.user.id, renderUser(interaction.member.user)), |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 89 | reason: reason ? `\n> ${reason}` : "No reason provided" |
pineafan | 1dc1572 | 2022-03-14 21:27:34 +0000 | [diff] [blame] | 90 | }, |
| 91 | hidden: { |
| 92 | guild: interaction.guild.id |
| 93 | } |
| 94 | } |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 95 | try { await client.database.history.create( |
| 96 | "warn", interaction.guild.id, |
| 97 | (interaction.options.getMember("user") as GuildMember).user, |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 98 | interaction.user, reason |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 99 | )} catch {} |
| 100 | log(data); |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 101 | let failed = (dmd === false && notify) |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 102 | if (!failed) { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 103 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 104 | .setEmoji(`PUNISH.WARN.GREEN`) |
| 105 | .setTitle(`Warn`) |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 106 | .setDescription("The user was warned" + (confirmation.components.appeal.response ? ` and an appeal ticket was opened in <#${confirmation.components.appeal.response}>` : ``)) |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 107 | .setStatus("Success") |
| 108 | ], components: []}) |
| 109 | } else { |
pineafan | c1c1879 | 2022-08-03 21:41:36 +0100 | [diff] [blame^] | 110 | let canSeeChannel = (interaction.options.getMember("user") as GuildMember).permissionsIn(interaction.channel as Discord.TextChannel).has("VIEW_CHANNEL") |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 111 | let m = await interaction.editReply({ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 112 | embeds: [new EmojiEmbed() |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 113 | .setEmoji(`PUNISH.WARN.RED`) |
| 114 | .setTitle(`Warn`) |
| 115 | .setDescription("The user's DMs are not open\n\nWhat would you like to do?") |
| 116 | .setStatus("Danger") |
| 117 | ], components: [ |
| 118 | new MessageActionRow().addComponents([ |
| 119 | new Discord.MessageButton() |
| 120 | .setCustomId("log") |
| 121 | .setLabel("Ignore and log") |
| 122 | .setStyle("SECONDARY"), |
| 123 | new Discord.MessageButton() |
| 124 | .setCustomId("here") |
| 125 | .setLabel("Warn here") |
pineafan | c1c1879 | 2022-08-03 21:41:36 +0100 | [diff] [blame^] | 126 | .setStyle(canSeeChannel ? "PRIMARY" : "SECONDARY") |
| 127 | .setDisabled(!canSeeChannel), |
| 128 | new Discord.MessageButton() |
| 129 | .setCustomId("ticket") |
| 130 | .setLabel("Create ticket") |
| 131 | .setStyle(canSeeChannel ? "SECONDARY" : "PRIMARY") |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 132 | ]) |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 133 | ] |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 134 | }) |
| 135 | let component; |
| 136 | try { |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 137 | component = await (m as Discord.Message).awaitMessageComponent({filter: (m) => m.user.id === interaction.user.id, time: 300000}); |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 138 | } catch (e) { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 139 | return await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 140 | .setEmoji(`PUNISH.WARN.GREEN`) |
| 141 | .setTitle(`Warn`) |
| 142 | .setDescription("No changes were made") |
| 143 | .setStatus("Success") |
| 144 | ], components: []}) |
| 145 | } |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 146 | if ( component.customId === "here" ) { |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 147 | await interaction.channel.send({ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 148 | embeds: [new EmojiEmbed() |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 149 | .setEmoji(`PUNISH.WARN.RED`) |
| 150 | .setTitle(`Warn`) |
| 151 | .setDescription(`You have been warned` + |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 152 | (reason ? ` for:\n> ${reason}` : ".")) |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 153 | .setStatus("Danger") |
| 154 | ], |
| 155 | content: `<@!${(interaction.options.getMember("user") as GuildMember).id}>`, |
| 156 | allowedMentions: {users: [(interaction.options.getMember("user") as GuildMember).id]} |
| 157 | }) |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 158 | return await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 159 | .setEmoji(`PUNISH.WARN.GREEN`) |
| 160 | .setTitle(`Warn`) |
pineafan | 4092b86 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 161 | .setDescription("The user was warned" + (confirmation.response ? ` and an appeal ticket was opened in <#${confirmation.response}>` : ``)) |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 162 | .setStatus("Success") |
| 163 | ], components: []}) |
pineafan | c1c1879 | 2022-08-03 21:41:36 +0100 | [diff] [blame^] | 164 | } else if (component.customId === "log") { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 165 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 166 | .setEmoji(`PUNISH.WARN.GREEN`) |
| 167 | .setTitle(`Warn`) |
| 168 | .setDescription("The warn was logged") |
| 169 | .setStatus("Success") |
| 170 | ], components: []}) |
pineafan | c1c1879 | 2022-08-03 21:41:36 +0100 | [diff] [blame^] | 171 | } else if (component.customId === "ticket") { |
| 172 | let ticketChannel = await create(interaction.guild, interaction.options.getUser("user"), interaction.user, reason, "Warn Notification") |
| 173 | if (ticketChannel === null) { |
| 174 | return await interaction.editReply({embeds: [new EmojiEmbed() |
| 175 | .setEmoji(`PUNISH.WARN.RED`) |
| 176 | .setTitle(`Warn`) |
| 177 | .setDescription("A ticket could not be created") |
| 178 | .setStatus("Danger") |
| 179 | ], components: []}) |
| 180 | } |
| 181 | await interaction.editReply({embeds: [new EmojiEmbed() |
| 182 | .setEmoji(`PUNISH.WARN.GREEN`) |
| 183 | .setTitle(`Warn`) |
| 184 | .setDescription(`A ticket was created in <#${ticketChannel}>`) |
| 185 | .setStatus("Success") |
| 186 | ], components: []}) |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 187 | } |
| 188 | } |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 189 | } else { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 190 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 191 | .setEmoji("PUNISH.WARN.GREEN") |
| 192 | .setTitle(`Warn`) |
| 193 | .setDescription("No changes were made") |
| 194 | .setStatus("Success") |
| 195 | ], components: []}) |
| 196 | } |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => { |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 200 | let member = (interaction.member as GuildMember) |
| 201 | let me = (interaction.guild.me as GuildMember) |
| 202 | let apply = (interaction.options.getMember("user") as GuildMember) |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 203 | if (member === null || me === null || apply === null) throw "That member is not in the server" |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 204 | let memberPos = member.roles ? member.roles.highest.position : 0 |
| 205 | let mePos = me.roles ? me.roles.highest.position : 0 |
| 206 | let applyPos = apply.roles ? apply.roles.highest.position : 0 |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 207 | // Do not allow warning bots |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 208 | if (member.user.bot) throw "I cannot warn bots" |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 209 | // Allow the owner to warn anyone |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 210 | if (member.id === interaction.guild.ownerId) return true |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 211 | // Check if the user has moderate_members permission |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 212 | if (! member.permissions.has("MODERATE_MEMBERS")) throw "You do not have the *Moderate Members* permission"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 213 | // Check if the user is below on the role list |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 214 | if (! (memberPos > applyPos)) throw "You do not have a role higher than that member" |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 215 | // Allow warn |
| 216 | return true |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 217 | } |
| 218 | |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 219 | export { command, callback, check }; |