pineafan | 5d1908e | 2022-02-28 21:34:47 +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 | 5d1908e | 2022-02-28 21:34:47 +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 | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 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 |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 10 | .setName("unmute") |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 11 | .setDescription("Unmutes a user") |
| 12 | .addUserOption(option => option.setName("user").setDescription("The user to unmute").setRequired(true)) |
| 13 | .addStringOption(option => option.setName("reason").setDescription("The reason for the unmute").setRequired(false)) |
| 14 | .addStringOption(option => option.setName("notify").setDescription("If the user should get a message when they are unmuted | Default no").setRequired(false) |
| 15 | .addChoices([["Yes", "yes"], ["No", "no"]]) |
| 16 | ) |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 17 | |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 18 | const callback = async (interaction: CommandInteraction) => { |
| 19 | // TODO:[Modals] Replace this with a modal |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 20 | let confirmation = await new confirmationMessage(interaction) |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 21 | .setEmoji("PUNISH.MUTE.RED") |
| 22 | .setTitle("Unmute") |
| 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") === "yes" ? '' : ' not'}** be notified\n\n` |
| 28 | + `Are you sure you want to unmute <@!${(interaction.options.getMember("user") as GuildMember).id}>?`) |
| 29 | .setColor("Danger") |
| 30 | // pluralize("day", interaction.options.getInteger("delete")) |
| 31 | // const pluralize = (word: string, count: number) => { return count === 1 ? word : word + "s" } |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 32 | .send() |
| 33 | if (confirmation.success) { |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 34 | let dmd = false |
| 35 | let dm; |
| 36 | try { |
| 37 | if (interaction.options.getString("notify") != "no") { |
| 38 | dm = await (interaction.options.getMember("user") as GuildMember).send({ |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 39 | embeds: [new generateEmojiEmbed() |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 40 | .setEmoji("PUNISH.MUTE.GREEN") |
| 41 | .setTitle("Unmuted") |
| 42 | .setDescription(`You have been unmuted in ${interaction.guild.name}` + |
| 43 | (interaction.options.getString("reason") ? ` for:\n> ${interaction.options.getString("reason")}` : " with no reason provided.")) |
| 44 | .setStatus("Success") |
| 45 | ] |
| 46 | }) |
| 47 | dmd = true |
| 48 | } |
| 49 | } catch {} |
| 50 | try { |
| 51 | (interaction.options.getMember("user") as GuildMember).timeout(0, interaction.options.getString("reason") || "No reason provided") |
| 52 | } catch { |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 53 | await interaction.editReply({embeds: [new generateEmojiEmbed() |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 54 | .setEmoji("PUNISH.MUTE.RED") |
| 55 | .setTitle(`Unmute`) |
| 56 | .setDescription("Something went wrong and the user was not unmuted") |
| 57 | .setStatus("Danger") |
| 58 | ], components: []}) |
| 59 | if (dmd) await dm.delete() |
| 60 | return |
| 61 | } |
| 62 | let failed = (dmd == false && interaction.options.getString("notify") != "no") |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 63 | await interaction.editReply({embeds: [new generateEmojiEmbed() |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 64 | .setEmoji(`PUNISH.MUTE.${failed ? "YELLOW" : "GREEN"}`) |
| 65 | .setTitle(`Unmute`) |
| 66 | .setDescription("The member was unmuted" + (failed ? ", but could not be notified" : "")) |
| 67 | .setStatus(failed ? "Warning" : "Success") |
| 68 | ], components: []}) |
| 69 | } else { |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 70 | await interaction.editReply({embeds: [new generateEmojiEmbed() |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 71 | .setEmoji("PUNISH.MUTE.GREEN") |
| 72 | .setTitle(`Unmute`) |
| 73 | .setDescription("No changes were made") |
| 74 | .setStatus("Success") |
| 75 | ], components: []}) |
| 76 | } |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => { |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 80 | let member = (interaction.member as GuildMember) |
| 81 | let me = (interaction.guild.me as GuildMember) |
| 82 | let apply = (interaction.options.getMember("user") as GuildMember) |
| 83 | if (member == null || me == null || apply == null) throw "That member is not in the server" |
| 84 | let memberPos = member.roles ? member.roles.highest.position : 0 |
| 85 | let mePos = me.roles ? me.roles.highest.position : 0 |
| 86 | let applyPos = apply.roles ? apply.roles.highest.position : 0 |
| 87 | // Check if Nucleus can unmute the member |
| 88 | if (! (mePos > applyPos)) throw "I do not have a role higher than that member" |
| 89 | // Check if Nucleus has permission to unmute |
| 90 | if (! interaction.guild.me.permissions.has("MODERATE_MEMBERS")) throw "I do not have the `moderate_members` permission"; |
| 91 | // Do not allow the user to have admin or be the owner |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 92 | if (apply.permissions.has("ADMINISTRATOR") || (interaction.options.getMember("user") as GuildMember).id == interaction.guild.ownerId) throw "You cannot unmute an admin or the owner" |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 93 | // Allow the owner to unmute anyone |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 94 | if (member.id == interaction.guild.ownerId) return true |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 95 | // Check if the user has moderate_members permission |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 96 | if (! member.permissions.has("MODERATE_MEMBERS")) throw "You do not have the `moderate_members` permission"; |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 97 | // Check if the user is below on the role list |
| 98 | if (! (memberPos > applyPos)) throw "You do not have a role higher than that member" |
| 99 | // Allow unmute |
| 100 | return true |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 101 | } |
| 102 | |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 103 | export { command, callback, check }; |