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