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