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