pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame^] | 1 | import { CommandInteraction, GuildMember } 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 | } |
| 46 | } catch {} |
| 47 | try { |
| 48 | let failed = (dmd == false && interaction.options.getString("notify") != "no") // TODO: some way of dealing with not DMing users |
| 49 | await interaction.editReply({embeds: [new EmojiEmbed() |
| 50 | .setEmoji(`PUNISH.WARN.${failed ? "YELLOW" : "GREEN"}`) |
| 51 | .setTitle(`Warn`) |
| 52 | .setDescription(failed ? "The user cannot be messaged and was not warned" : "The user was warned") |
| 53 | .setStatus(failed ? "Warning" : "Success") |
| 54 | ], components: []}) |
| 55 | } catch { |
| 56 | await interaction.editReply({embeds: [new EmojiEmbed() |
| 57 | .setEmoji("PUNISH.WARN.RED") |
| 58 | .setTitle(`Warn`) |
| 59 | .setDescription("Something went wrong and the user was not warned") |
| 60 | .setStatus("Danger") |
| 61 | ], components: []}) |
| 62 | } |
| 63 | } else { |
| 64 | await interaction.editReply({embeds: [new EmojiEmbed() |
| 65 | .setEmoji("PUNISH.WARN.GREEN") |
| 66 | .setTitle(`Warn`) |
| 67 | .setDescription("No changes were made") |
| 68 | .setStatus("Success") |
| 69 | ], components: []}) |
| 70 | } |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => { |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame^] | 74 | // Do not allow warning bots |
| 75 | if ((interaction.member as GuildMember).user.bot) throw "I cannot warn bots" |
| 76 | // Allow the owner to warn anyone |
| 77 | if ((interaction.member as GuildMember).id == interaction.guild.ownerId) return true |
| 78 | // Check if the user has moderate_members permission |
| 79 | if (! (interaction.member as GuildMember).permissions.has("MODERATE_MEMBERS")) throw "You do not have the `moderate_members` permission"; |
| 80 | // Check if the user is below on the role list |
| 81 | if (! ((interaction.member as GuildMember).roles.highest.position > (interaction.options.getMember("user") as GuildMember).roles.highest.position)) throw "You do not have a role higher than that member" |
| 82 | // Allow warn |
| 83 | return true |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 84 | } |
| 85 | |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame^] | 86 | export { command, callback, check }; |