blob: 2a98c543ca5194a6a4731e808fa6424554ddd764 [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")
pineafan377794f2022-04-18 19:01:01 +010030 .send()
31 if (confirmation.success) {
pineafan5d1908e2022-02-28 21:34:47 +000032 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({
pineafan377794f2022-04-18 19:01:01 +010037 embeds: [new generateEmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +000038 .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 {
pineafan377794f2022-04-18 19:01:01 +010051 await interaction.editReply({embeds: [new generateEmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +000052 .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")
pineafan377794f2022-04-18 19:01:01 +010061 await interaction.editReply({embeds: [new generateEmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +000062 .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 {
pineafan377794f2022-04-18 19:01:01 +010068 await interaction.editReply({embeds: [new generateEmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +000069 .setEmoji("PUNISH.MUTE.GREEN")
70 .setTitle(`Unmute`)
71 .setDescription("No changes were made")
72 .setStatus("Success")
73 ], components: []})
74 }
pineafan4f164f32022-02-26 22:07:12 +000075}
76
77const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan5d1908e2022-02-28 21:34:47 +000078 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
PineappleFan5fe720d2022-05-19 12:01:49 +010088 if (! me.permissions.has("MODERATE_MEMBERS")) throw "I do not have the `moderate_members` permission";
pineafan5d1908e2022-02-28 21:34:47 +000089 // Do not allow the user to have admin or be the owner
PineappleFan5fe720d2022-05-19 12:01:49 +010090 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 +000091 // Allow the owner to unmute anyone
pineafan663dc472022-05-10 18:13:47 +010092 if (member.id == interaction.guild.ownerId) return true
pineafan5d1908e2022-02-28 21:34:47 +000093 // Check if the user has moderate_members permission
pineafan663dc472022-05-10 18:13:47 +010094 if (! member.permissions.has("MODERATE_MEMBERS")) throw "You do not have the `moderate_members` permission";
pineafan5d1908e2022-02-28 21:34:47 +000095 // 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
pineafan4f164f32022-02-26 22:07:12 +000099}
100
pineafan8b4b17f2022-02-27 20:42:52 +0000101export { command, callback, check };