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