pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 1 | import Discord, { CommandInteraction, GuildMember, MessageActionRow } 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 | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 15 | .addStringOption(option => option.setName("notify").setDescription("If the user should get a message when they are warned | Default: Yes").setRequired(false) |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 16 | .addChoices([["Yes", "yes"], ["No", "no"]]) |
| 17 | ) |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 18 | |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 19 | const callback = async (interaction: CommandInteraction): Promise<any> => { |
PineappleFan | b3dd83c | 2022-06-17 10:53:48 +0100 | [diff] [blame] | 20 | const { log, NucleusColors, renderUser, entry } = client.logger |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 21 | // TODO:[Modals] Replace this with a modal |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 22 | let reason = null; |
| 23 | let confirmation; |
| 24 | while (true) { |
| 25 | confirmation = await new confirmationMessage(interaction) |
| 26 | .setEmoji("PUNISH.WARN.RED") |
| 27 | .setTitle("Warn") |
| 28 | .setDescription(keyValueList({ |
| 29 | "user": renderUser(interaction.options.getUser("user")), |
| 30 | "reason": reason ? ("\n> " + ((reason ?? "").replaceAll("\n", "\n> "))) : "*No reason provided*" |
| 31 | }) |
| 32 | + `The user **will${interaction.options.getString("notify") === "no" ? ' not' : ''}** be notified\n\n` |
| 33 | + `Are you sure you want to warn <@!${(interaction.options.getMember("user") as GuildMember).id}>?`) |
| 34 | .setColor("Danger") |
| 35 | .addCustomBoolean( |
| 36 | "Create appeal ticket", !(await areTicketsEnabled(interaction.guild.id)), |
| 37 | async () => await create(interaction.guild, interaction.options.getUser("user"), interaction.user, reason), |
| 38 | "An appeal ticket will be created when Confirm is clicked") |
| 39 | .addReasonButton(reason) |
| 40 | .addReasonButton(reason ?? "") |
| 41 | .send(reason !== null) |
| 42 | reason = reason ?? "" |
| 43 | if (confirmation.newReason === undefined) break |
| 44 | reason = confirmation.newReason |
| 45 | } |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 46 | if (confirmation.success) { |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 47 | let dmd = false |
| 48 | try { |
| 49 | if (interaction.options.getString("notify") != "no") { |
| 50 | await (interaction.options.getMember("user") as GuildMember).send({ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 51 | embeds: [new EmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 52 | .setEmoji("PUNISH.WARN.RED") |
| 53 | .setTitle("Warned") |
| 54 | .setDescription(`You have been warned in ${interaction.guild.name}` + |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 55 | (reason ? ` for:\n> ${reason}` : ".") + "\n\n" + |
PineappleFan | 30b5fda | 2022-05-22 15:41:47 +0100 | [diff] [blame] | 56 | (confirmation.buttonClicked ? `You can appeal this here ticket: <#${confirmation.response}>` : ``)) |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 57 | .setStatus("Danger") |
| 58 | ] |
| 59 | }) |
| 60 | dmd = true |
| 61 | } |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 62 | } catch { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 63 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 64 | .setEmoji("PUNISH.WARN.RED") |
| 65 | .setTitle(`Warn`) |
| 66 | .setDescription("Something went wrong and the user was not warned") |
| 67 | .setStatus("Danger") |
| 68 | ], components: []}) |
| 69 | } |
pineafan | 1dc1572 | 2022-03-14 21:27:34 +0000 | [diff] [blame] | 70 | let data = { |
| 71 | meta:{ |
| 72 | type: 'memberWarn', |
| 73 | displayName: 'Member warned', |
| 74 | calculateType: 'guildMemberPunish', |
| 75 | color: NucleusColors.yellow, |
| 76 | emoji: 'PUNISH.WARN.YELLOW', |
| 77 | timestamp: new Date().getTime() |
| 78 | }, |
| 79 | list: { |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 80 | user: entry((interaction.options.getMember("user") as GuildMember).user.id, renderUser((interaction.options.getMember("user") as GuildMember).user)), |
| 81 | warnedBy: entry(interaction.member.user.id, renderUser(interaction.member.user)), |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 82 | reason: reason ? `\n> ${reason}` : "No reason provided" |
pineafan | 1dc1572 | 2022-03-14 21:27:34 +0000 | [diff] [blame] | 83 | }, |
| 84 | hidden: { |
| 85 | guild: interaction.guild.id |
| 86 | } |
| 87 | } |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 88 | try { await client.database.history.create( |
| 89 | "warn", interaction.guild.id, |
| 90 | (interaction.options.getMember("user") as GuildMember).user, |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 91 | interaction.user, reason |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 92 | )} catch {} |
| 93 | log(data); |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 94 | let failed = (dmd == false && interaction.options.getString("notify") != "no") |
| 95 | if (!failed) { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 96 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 97 | .setEmoji(`PUNISH.WARN.GREEN`) |
| 98 | .setTitle(`Warn`) |
pineafan | 4092b86 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 99 | .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] | 100 | .setStatus("Success") |
| 101 | ], components: []}) |
| 102 | } else { |
| 103 | let m = await interaction.editReply({ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 104 | embeds: [new EmojiEmbed() |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 105 | .setEmoji(`PUNISH.WARN.RED`) |
| 106 | .setTitle(`Warn`) |
| 107 | .setDescription("The user's DMs are not open\n\nWhat would you like to do?") |
| 108 | .setStatus("Danger") |
| 109 | ], components: [ |
| 110 | new MessageActionRow().addComponents([ |
| 111 | new Discord.MessageButton() |
| 112 | .setCustomId("log") |
| 113 | .setLabel("Ignore and log") |
| 114 | .setStyle("SECONDARY"), |
| 115 | new Discord.MessageButton() |
| 116 | .setCustomId("here") |
| 117 | .setLabel("Warn here") |
| 118 | .setStyle("SECONDARY") |
| 119 | .setDisabled((interaction.options.getMember("user") as GuildMember).permissionsIn(interaction.channel as Discord.TextChannel).has("VIEW_CHANNEL") === false), |
| 120 | ]) |
| 121 | ], |
| 122 | }) |
| 123 | let component; |
| 124 | try { |
pineafan | c6158ab | 2022-06-17 16:34:07 +0100 | [diff] [blame] | 125 | 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] | 126 | } catch (e) { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 127 | return await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 128 | .setEmoji(`PUNISH.WARN.GREEN`) |
| 129 | .setTitle(`Warn`) |
| 130 | .setDescription("No changes were made") |
| 131 | .setStatus("Success") |
| 132 | ], components: []}) |
| 133 | } |
| 134 | if ( component.customId == "here" ) { |
| 135 | await interaction.channel.send({ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 136 | embeds: [new EmojiEmbed() |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 137 | .setEmoji(`PUNISH.WARN.RED`) |
| 138 | .setTitle(`Warn`) |
| 139 | .setDescription(`You have been warned` + |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 140 | (reason ? ` for:\n> ${reason}` : ".")) |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 141 | .setStatus("Danger") |
| 142 | ], |
| 143 | content: `<@!${(interaction.options.getMember("user") as GuildMember).id}>`, |
| 144 | allowedMentions: {users: [(interaction.options.getMember("user") as GuildMember).id]} |
| 145 | }) |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 146 | return await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 147 | .setEmoji(`PUNISH.WARN.GREEN`) |
| 148 | .setTitle(`Warn`) |
pineafan | 4092b86 | 2022-05-20 19:27:23 +0100 | [diff] [blame] | 149 | .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] | 150 | .setStatus("Success") |
| 151 | ], components: []}) |
| 152 | } else { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 153 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 154 | .setEmoji(`PUNISH.WARN.GREEN`) |
| 155 | .setTitle(`Warn`) |
| 156 | .setDescription("The warn was logged") |
| 157 | .setStatus("Success") |
| 158 | ], components: []}) |
| 159 | } |
| 160 | } |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 161 | } else { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 162 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 163 | .setEmoji("PUNISH.WARN.GREEN") |
| 164 | .setTitle(`Warn`) |
| 165 | .setDescription("No changes were made") |
| 166 | .setStatus("Success") |
| 167 | ], components: []}) |
| 168 | } |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => { |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 172 | let member = (interaction.member as GuildMember) |
| 173 | let me = (interaction.guild.me as GuildMember) |
| 174 | let apply = (interaction.options.getMember("user") as GuildMember) |
| 175 | if (member == null || me == null || apply == null) throw "That member is not in the server" |
| 176 | let memberPos = member.roles ? member.roles.highest.position : 0 |
| 177 | let mePos = me.roles ? me.roles.highest.position : 0 |
| 178 | let applyPos = apply.roles ? apply.roles.highest.position : 0 |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 179 | // Do not allow warning bots |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 180 | if (member.user.bot) throw "I cannot warn bots" |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 181 | // Allow the owner to warn anyone |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 182 | if (member.id == interaction.guild.ownerId) return true |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 183 | // Check if the user has moderate_members permission |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 184 | 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] | 185 | // Check if the user is below on the role list |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 186 | 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] | 187 | // Allow warn |
| 188 | return true |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 189 | } |
| 190 | |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 191 | export { command, callback, check }; |