blob: 65e5e97be413ff0bae35a1d77dc05cbb7ef42a0a [file] [log] [blame]
pineafan5d1908e2022-02-28 21:34:47 +00001import { CommandInteraction, GuildMember } from "discord.js";
pineafan4f164f32022-02-26 22:07:12 +00002import { SlashCommandSubcommandBuilder } from "@discordjs/builders";
3import { WrappedCheck } from "jshaiku";
pineafan5d1908e2022-02-28 21:34:47 +00004import confirmationMessage from "../../utils/confirmationMessage.js";
pineafan377794f2022-04-18 19:01:01 +01005import generateEmojiEmbed from "../../utils/generateEmojiEmbed.js";
pineafan5d1908e2022-02-28 21:34:47 +00006import keyValueList from "../../utils/generateKeyValueList.js";
pineafan4f164f32022-02-26 22:07:12 +00007
8const command = (builder: SlashCommandSubcommandBuilder) =>
9 builder
pineafan8b4b17f2022-02-27 20:42:52 +000010 .setName("unmute")
pineafan5d1908e2022-02-28 21:34:47 +000011 .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 )
pineafan4f164f32022-02-26 22:07:12 +000017
pineafan5d1908e2022-02-28 21:34:47 +000018const callback = async (interaction: CommandInteraction) => {
19 // TODO:[Modals] Replace this with a modal
pineafan377794f2022-04-18 19:01:01 +010020 let confirmation = await new confirmationMessage(interaction)
pineafan5d1908e2022-02-28 21:34:47 +000021 .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" }
pineafan377794f2022-04-18 19:01:01 +010032 .send()
33 if (confirmation.success) {
pineafan5d1908e2022-02-28 21:34:47 +000034 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({
pineafan377794f2022-04-18 19:01:01 +010039 embeds: [new generateEmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +000040 .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 {
pineafan377794f2022-04-18 19:01:01 +010053 await interaction.editReply({embeds: [new generateEmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +000054 .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")
pineafan377794f2022-04-18 19:01:01 +010063 await interaction.editReply({embeds: [new generateEmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +000064 .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 {
pineafan377794f2022-04-18 19:01:01 +010070 await interaction.editReply({embeds: [new generateEmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +000071 .setEmoji("PUNISH.MUTE.GREEN")
72 .setTitle(`Unmute`)
73 .setDescription("No changes were made")
74 .setStatus("Success")
75 ], components: []})
76 }
pineafan4f164f32022-02-26 22:07:12 +000077}
78
79const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan5d1908e2022-02-28 21:34:47 +000080 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
92 if ((interaction.options.getMember("user") as GuildMember).permissions.has("ADMINISTRATOR") || (interaction.options.getMember("user") as GuildMember).id == interaction.guild.ownerId) throw "You cannot unmute an admin or the owner"
93 // Allow the owner to unmute anyone
94 if ((interaction.member as GuildMember).id == interaction.guild.ownerId) return true
95 // Check if the user has moderate_members permission
96 if (! (interaction.member as GuildMember).permissions.has("MODERATE_MEMBERS")) throw "You do not have the `moderate_members` permission";
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
pineafan4f164f32022-02-26 22:07:12 +0000101}
102
pineafan8b4b17f2022-02-27 20:42:52 +0000103export { command, callback, check };