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