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