blob: 5a1b67ce201cd720c537996b19866fcb96b2172a [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";
pineafan4edb7762022-06-26 19:21:04 +01005import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
pineafan5d1908e2022-02-28 21:34:47 +00006import keyValueList from "../../utils/generateKeyValueList.js";
pineafan4edb7762022-06-26 19:21:04 +01007import client from "../../utils/client.js";
pineafan4f164f32022-02-26 22:07:12 +00008
9const command = (builder: SlashCommandSubcommandBuilder) =>
10 builder
pineafan8b4b17f2022-02-27 20:42:52 +000011 .setName("unmute")
pineafan5d1908e2022-02-28 21:34:47 +000012 .setDescription("Unmutes a user")
13 .addUserOption(option => option.setName("user").setDescription("The user to unmute").setRequired(true))
14 .addStringOption(option => option.setName("reason").setDescription("The reason for the unmute").setRequired(false))
15 .addStringOption(option => option.setName("notify").setDescription("If the user should get a message when they are unmuted | Default no").setRequired(false)
16 .addChoices([["Yes", "yes"], ["No", "no"]])
17 )
pineafan4f164f32022-02-26 22:07:12 +000018
pineafan4edb7762022-06-26 19:21:04 +010019const callback = async (interaction: CommandInteraction): Promise<any> => {
20 const { renderUser } = client.logger
pineafan5d1908e2022-02-28 21:34:47 +000021 // TODO:[Modals] Replace this with a modal
pineafan377794f2022-04-18 19:01:01 +010022 let confirmation = await new confirmationMessage(interaction)
pineafan5d1908e2022-02-28 21:34:47 +000023 .setEmoji("PUNISH.MUTE.RED")
24 .setTitle("Unmute")
25 .setDescription(keyValueList({
pineafan4edb7762022-06-26 19:21:04 +010026 "user": renderUser(interaction.options.getUser("user")),
pineafan5d1908e2022-02-28 21:34:47 +000027 "reason": `\n> ${interaction.options.getString("reason") ? interaction.options.getString("reason") : "*No reason provided*"}`
28 })
29 + `The user **will${interaction.options.getString("notify") === "yes" ? '' : ' not'}** be notified\n\n`
30 + `Are you sure you want to unmute <@!${(interaction.options.getMember("user") as GuildMember).id}>?`)
31 .setColor("Danger")
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({
pineafan4edb7762022-06-26 19:21:04 +010039 embeds: [new EmojiEmbed()
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 {
pineafan4edb7762022-06-26 19:21:04 +010053 await interaction.editReply({embeds: [new EmojiEmbed()
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 }
pineafan4edb7762022-06-26 19:21:04 +010062 try { await client.database.history.create("unmute", interaction.guild.id, (interaction.options.getMember("user") as GuildMember).user, interaction.user, interaction.options.getString("reason")) } catch {}
pineafan5d1908e2022-02-28 21:34:47 +000063 let failed = (dmd == false && interaction.options.getString("notify") != "no")
pineafan4edb7762022-06-26 19:21:04 +010064 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +000065 .setEmoji(`PUNISH.MUTE.${failed ? "YELLOW" : "GREEN"}`)
66 .setTitle(`Unmute`)
67 .setDescription("The member was unmuted" + (failed ? ", but could not be notified" : ""))
68 .setStatus(failed ? "Warning" : "Success")
69 ], components: []})
70 } else {
pineafan4edb7762022-06-26 19:21:04 +010071 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +000072 .setEmoji("PUNISH.MUTE.GREEN")
73 .setTitle(`Unmute`)
74 .setDescription("No changes were made")
75 .setStatus("Success")
76 ], components: []})
77 }
pineafan4f164f32022-02-26 22:07:12 +000078}
79
80const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan5d1908e2022-02-28 21:34:47 +000081 let member = (interaction.member as GuildMember)
82 let me = (interaction.guild.me as GuildMember)
83 let apply = (interaction.options.getMember("user") as GuildMember)
84 if (member == null || me == null || apply == null) throw "That member is not in the server"
85 let memberPos = member.roles ? member.roles.highest.position : 0
86 let mePos = me.roles ? me.roles.highest.position : 0
87 let applyPos = apply.roles ? apply.roles.highest.position : 0
88 // Check if Nucleus can unmute the member
89 if (! (mePos > applyPos)) throw "I do not have a role higher than that member"
90 // Check if Nucleus has permission to unmute
pineafan4edb7762022-06-26 19:21:04 +010091 if (! me.permissions.has("MODERATE_MEMBERS")) throw "I do not have the Moderate members permission";
pineafan5d1908e2022-02-28 21:34:47 +000092 // Do not allow the user to have admin or be the owner
PineappleFan5fe720d2022-05-19 12:01:49 +010093 if (apply.permissions.has("ADMINISTRATOR") || apply.id == interaction.guild.ownerId) throw "You cannot unmute an admin or the owner"
pineafan5d1908e2022-02-28 21:34:47 +000094 // Allow the owner to unmute anyone
pineafan663dc472022-05-10 18:13:47 +010095 if (member.id == interaction.guild.ownerId) return true
pineafan5d1908e2022-02-28 21:34:47 +000096 // Check if the user has moderate_members permission
pineafan4edb7762022-06-26 19:21:04 +010097 if (! member.permissions.has("MODERATE_MEMBERS")) throw "You do not have the Moderate members permission";
pineafan5d1908e2022-02-28 21:34:47 +000098 // Check if the user is below on the role list
99 if (! (memberPos > applyPos)) throw "You do not have a role higher than that member"
100 // Allow unmute
101 return true
pineafan4f164f32022-02-26 22:07:12 +0000102}
103
pineafan8b4b17f2022-02-27 20:42:52 +0000104export { command, callback, check };