blob: 25aacfa4ef248d54b3442f22b96e179ec10849d1 [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))
pineafan4f164f32022-02-26 22:07:12 +000014
pineafan4edb7762022-06-26 19:21:04 +010015const callback = async (interaction: CommandInteraction): Promise<any> => {
pineafan73a7c4a2022-07-24 10:38:04 +010016 const { log, NucleusColors, renderUser, entry, renderDelta } = client.logger
pineafan5d1908e2022-02-28 21:34:47 +000017 // TODO:[Modals] Replace this with a modal
pineafan73a7c4a2022-07-24 10:38:04 +010018 let reason = null;
pineafan02ba0232022-07-24 22:16:15 +010019 let notify = false;
pineafan73a7c4a2022-07-24 10:38:04 +010020 let confirmation;
21 while (true) {
22 confirmation = await new confirmationMessage(interaction)
23 .setEmoji("PUNISH.MUTE.RED")
24 .setTitle("Unmute")
25 .setDescription(keyValueList({
26 "user": renderUser(interaction.options.getUser("user")),
27 "reason": `\n> ${reason ? reason : "*No reason provided*"}`
28 })
pineafan02ba0232022-07-24 22:16:15 +010029 + `The user **will${notify ? '' : ' not'}** be notified\n\n`
pineafan73a7c4a2022-07-24 10:38:04 +010030 + `Are you sure you want to unmute <@!${(interaction.options.getMember("user") as GuildMember).id}>?`)
31 .setColor("Danger")
32 .addReasonButton(reason ?? "")
33 .send(reason !== null)
pineafan02ba0232022-07-24 22:16:15 +010034 if (confirmation.success) break
35 if (confirmation.newReason) reason = confirmation.newReason
36 if (confirmation.components) {
37 notify = confirmation.components.notify.active
38 }
pineafan73a7c4a2022-07-24 10:38:04 +010039 }
pineafan02ba0232022-07-24 22:16:15 +010040 if (confirmation.cancelled) return
pineafan377794f2022-04-18 19:01:01 +010041 if (confirmation.success) {
pineafan5d1908e2022-02-28 21:34:47 +000042 let dmd = false
43 let dm;
44 try {
pineafan02ba0232022-07-24 22:16:15 +010045 if (notify) {
pineafan5d1908e2022-02-28 21:34:47 +000046 dm = await (interaction.options.getMember("user") as GuildMember).send({
pineafan4edb7762022-06-26 19:21:04 +010047 embeds: [new EmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +000048 .setEmoji("PUNISH.MUTE.GREEN")
49 .setTitle("Unmuted")
50 .setDescription(`You have been unmuted in ${interaction.guild.name}` +
pineafan73a7c4a2022-07-24 10:38:04 +010051 (reason ? ` for:\n> ${reason}` : " with no reason provided."))
pineafan5d1908e2022-02-28 21:34:47 +000052 .setStatus("Success")
53 ]
54 })
55 dmd = true
56 }
57 } catch {}
pineafan73a7c4a2022-07-24 10:38:04 +010058 let member = (interaction.options.getMember("user") as GuildMember)
pineafan5d1908e2022-02-28 21:34:47 +000059 try {
pineafan73a7c4a2022-07-24 10:38:04 +010060 member.timeout(0, reason || "No reason provided")
pineafan5d1908e2022-02-28 21:34:47 +000061 } catch {
pineafan4edb7762022-06-26 19:21:04 +010062 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +000063 .setEmoji("PUNISH.MUTE.RED")
64 .setTitle(`Unmute`)
65 .setDescription("Something went wrong and the user was not unmuted")
66 .setStatus("Danger")
67 ], components: []})
68 if (dmd) await dm.delete()
69 return
70 }
pineafan73a7c4a2022-07-24 10:38:04 +010071 try { await client.database.history.create("unmute", interaction.guild.id, (interaction.options.getMember("user") as GuildMember).user, interaction.user, reason) } catch {}
72 let data = {
73 meta: {
74 type: 'memberUnmute',
75 displayName: 'Unmuted',
76 calculateType: 'guildMemberPunish',
77 color: NucleusColors.green,
78 emoji: "PUNISH.MUTE.GREEN",
79 timestamp: new Date().getTime()
80 },
81 list: {
82 memberId: entry(member.user.id, `\`${member.user.id}\``),
83 name: entry(member.user.id, renderUser(member.user)),
84 unmuted: entry(new Date().getTime(), renderDelta(new Date().getTime())),
85 unmutedBy: entry(interaction.user.id, renderUser(interaction.user)),
86 },
87 hidden: {
88 guild: interaction.guild.id
89 }
90 }
91 log(data);
pineafane23c4ec2022-07-27 21:56:27 +010092 let failed = (dmd === false && notify)
pineafan4edb7762022-06-26 19:21:04 +010093 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +000094 .setEmoji(`PUNISH.MUTE.${failed ? "YELLOW" : "GREEN"}`)
95 .setTitle(`Unmute`)
96 .setDescription("The member was unmuted" + (failed ? ", but could not be notified" : ""))
97 .setStatus(failed ? "Warning" : "Success")
98 ], components: []})
99 } else {
pineafan4edb7762022-06-26 19:21:04 +0100100 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +0000101 .setEmoji("PUNISH.MUTE.GREEN")
102 .setTitle(`Unmute`)
103 .setDescription("No changes were made")
104 .setStatus("Success")
105 ], components: []})
106 }
pineafan4f164f32022-02-26 22:07:12 +0000107}
108
109const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan5d1908e2022-02-28 21:34:47 +0000110 let member = (interaction.member as GuildMember)
111 let me = (interaction.guild.me as GuildMember)
112 let apply = (interaction.options.getMember("user") as GuildMember)
pineafane23c4ec2022-07-27 21:56:27 +0100113 if (member === null || me === null || apply === null) throw "That member is not in the server"
pineafan5d1908e2022-02-28 21:34:47 +0000114 let memberPos = member.roles ? member.roles.highest.position : 0
115 let mePos = me.roles ? me.roles.highest.position : 0
116 let applyPos = apply.roles ? apply.roles.highest.position : 0
117 // Check if Nucleus can unmute the member
118 if (! (mePos > applyPos)) throw "I do not have a role higher than that member"
119 // Check if Nucleus has permission to unmute
pineafane23c4ec2022-07-27 21:56:27 +0100120 if (! me.permissions.has("MODERATE_MEMBERS")) throw "I do not have the *Moderate Members* permission";
pineafan5d1908e2022-02-28 21:34:47 +0000121 // Do not allow the user to have admin or be the owner
pineafane23c4ec2022-07-27 21:56:27 +0100122 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 +0000123 // Allow the owner to unmute anyone
pineafane23c4ec2022-07-27 21:56:27 +0100124 if (member.id === interaction.guild.ownerId) return true
pineafan5d1908e2022-02-28 21:34:47 +0000125 // Check if the user has moderate_members permission
pineafane23c4ec2022-07-27 21:56:27 +0100126 if (! member.permissions.has("MODERATE_MEMBERS")) throw "You do not have the *Moderate Members* permission";
pineafan5d1908e2022-02-28 21:34:47 +0000127 // Check if the user is below on the role list
128 if (! (memberPos > applyPos)) throw "You do not have a role higher than that member"
129 // Allow unmute
130 return true
pineafan4f164f32022-02-26 22:07:12 +0000131}
132
pineafan8b4b17f2022-02-27 20:42:52 +0000133export { command, callback, check };