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) => { |
| 19 | // TODO:[Modals] Replace this with a modal |
| 20 | if (await new confirmationMessage(interaction) |
| 21 | .setEmoji("PUNISH.WARN.RED") |
| 22 | .setTitle("Warn") |
| 23 | .setDescription(keyValueList({ |
| 24 | "user": `<@!${(interaction.options.getMember("user") as GuildMember).id}> (${(interaction.options.getMember("user") as GuildMember).user.username})`, |
| 25 | "reason": `\n> ${interaction.options.getString("reason") ? interaction.options.getString("reason") : "*No reason provided*"}` |
| 26 | }) |
| 27 | + `The user **will${interaction.options.getString("notify") === "no" ? ' not' : ''}** be notified\n\n`) |
| 28 | .setColor("Danger") |
| 29 | // pluralize("day", interaction.options.getInteger("delete")) |
| 30 | // const pluralize = (word: string, count: number) => { return count === 1 ? word : word + "s" } |
| 31 | .send()) { |
| 32 | let dmd = false |
| 33 | try { |
| 34 | if (interaction.options.getString("notify") != "no") { |
| 35 | await (interaction.options.getMember("user") as GuildMember).send({ |
| 36 | embeds: [new EmojiEmbed() |
| 37 | .setEmoji("PUNISH.WARN.RED") |
| 38 | .setTitle("Warned") |
| 39 | .setDescription(`You have been warned in ${interaction.guild.name}` + |
| 40 | (interaction.options.getString("reason") ? ` for:\n> ${interaction.options.getString("reason")}` : " with no reason provided.")) |
| 41 | .setStatus("Danger") |
| 42 | ] |
| 43 | }) |
| 44 | dmd = true |
| 45 | } |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 46 | } catch { |
| 47 | await interaction.editReply({embeds: [new EmojiEmbed() |
| 48 | .setEmoji("PUNISH.WARN.RED") |
| 49 | .setTitle(`Warn`) |
| 50 | .setDescription("Something went wrong and the user was not warned") |
| 51 | .setStatus("Danger") |
| 52 | ], components: []}) |
| 53 | } |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame^] | 54 | let failed = (dmd == false && interaction.options.getString("notify") != "no") |
| 55 | if (!failed) { |
| 56 | await interaction.editReply({embeds: [new EmojiEmbed() |
| 57 | .setEmoji(`PUNISH.WARN.GREEN`) |
| 58 | .setTitle(`Warn`) |
| 59 | .setDescription("The user was warned") |
| 60 | .setStatus("Success") |
| 61 | ], components: []}) |
| 62 | } else { |
| 63 | let m = await interaction.editReply({ |
| 64 | embeds: [new EmojiEmbed() |
| 65 | .setEmoji(`PUNISH.WARN.RED`) |
| 66 | .setTitle(`Warn`) |
| 67 | .setDescription("The user's DMs are not open\n\nWhat would you like to do?") |
| 68 | .setStatus("Danger") |
| 69 | ], components: [ |
| 70 | new MessageActionRow().addComponents([ |
| 71 | new Discord.MessageButton() |
| 72 | .setCustomId("log") |
| 73 | .setLabel("Ignore and log") |
| 74 | .setStyle("SECONDARY"), |
| 75 | new Discord.MessageButton() |
| 76 | .setCustomId("here") |
| 77 | .setLabel("Warn here") |
| 78 | .setStyle("SECONDARY") |
| 79 | .setDisabled((interaction.options.getMember("user") as GuildMember).permissionsIn(interaction.channel as Discord.TextChannel).has("VIEW_CHANNEL") === false), |
| 80 | ]) |
| 81 | ], |
| 82 | }) |
| 83 | let component; |
| 84 | try { |
| 85 | component = await (m as Discord.Message).awaitMessageComponent({filter: (m) => m.user.id === interaction.user.id, time: 2.5 * 60 * 1000}); |
| 86 | } catch (e) { |
| 87 | return await interaction.editReply({embeds: [new EmojiEmbed() |
| 88 | .setEmoji(`PUNISH.WARN.GREEN`) |
| 89 | .setTitle(`Warn`) |
| 90 | .setDescription("No changes were made") |
| 91 | .setStatus("Success") |
| 92 | ], components: []}) |
| 93 | } |
| 94 | if ( component.customId == "here" ) { |
| 95 | await interaction.channel.send({ |
| 96 | embeds: [new EmojiEmbed() |
| 97 | .setEmoji(`PUNISH.WARN.RED`) |
| 98 | .setTitle(`Warn`) |
| 99 | .setDescription(`You have been warned` + |
| 100 | (interaction.options.getString("reason") ? ` for:\n> ${interaction.options.getString("reason")}` : " with no reason provided.")) |
| 101 | .setStatus("Danger") |
| 102 | ], |
| 103 | content: `<@!${(interaction.options.getMember("user") as GuildMember).id}>`, |
| 104 | allowedMentions: {users: [(interaction.options.getMember("user") as GuildMember).id]} |
| 105 | }) |
| 106 | return await interaction.editReply({embeds: [new EmojiEmbed() |
| 107 | .setEmoji(`PUNISH.WARN.GREEN`) |
| 108 | .setTitle(`Warn`) |
| 109 | .setDescription("The user was warned") |
| 110 | .setStatus("Success") |
| 111 | ], components: []}) |
| 112 | } else { |
| 113 | await interaction.editReply({embeds: [new EmojiEmbed() |
| 114 | .setEmoji(`PUNISH.WARN.GREEN`) |
| 115 | .setTitle(`Warn`) |
| 116 | .setDescription("The warn was logged") |
| 117 | .setStatus("Success") |
| 118 | ], components: []}) |
| 119 | } |
| 120 | } |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 121 | } else { |
| 122 | await interaction.editReply({embeds: [new EmojiEmbed() |
| 123 | .setEmoji("PUNISH.WARN.GREEN") |
| 124 | .setTitle(`Warn`) |
| 125 | .setDescription("No changes were made") |
| 126 | .setStatus("Success") |
| 127 | ], components: []}) |
| 128 | } |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => { |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame^] | 132 | return true |
| 133 | let member = (interaction.member as GuildMember) |
| 134 | let me = (interaction.guild.me as GuildMember) |
| 135 | let apply = (interaction.options.getMember("user") as GuildMember) |
| 136 | if (member == null || me == null || apply == null) throw "That member is not in the server" |
| 137 | let memberPos = member.roles ? member.roles.highest.position : 0 |
| 138 | let mePos = me.roles ? me.roles.highest.position : 0 |
| 139 | let applyPos = apply.roles ? apply.roles.highest.position : 0 |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 140 | // Do not allow warning bots |
| 141 | if ((interaction.member as GuildMember).user.bot) throw "I cannot warn bots" |
| 142 | // Allow the owner to warn anyone |
| 143 | if ((interaction.member as GuildMember).id == interaction.guild.ownerId) return true |
| 144 | // Check if the user has moderate_members permission |
| 145 | if (! (interaction.member as GuildMember).permissions.has("MODERATE_MEMBERS")) throw "You do not have the `moderate_members` permission"; |
| 146 | // Check if the user is below on the role list |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame^] | 147 | 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] | 148 | // Allow warn |
| 149 | return true |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 150 | } |
| 151 | |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 152 | export { command, callback, check }; |