blob: 0200c222e1a331c1590045bb2594658b6cc5448e [file] [log] [blame]
PineaFana00db1b2023-01-02 15:32:54 +00001import type { CommandInteraction, GuildMember } from "discord.js";
TheCodedProff86ba092023-01-27 17:10:07 -05002import type { SlashCommandSubcommandBuilder } from "discord.js";
pineafan5d1908e2022-02-28 21:34:47 +00003import confirmationMessage from "../../utils/confirmationMessage.js";
pineafan4edb7762022-06-26 19:21:04 +01004import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
pineafan5d1908e2022-02-28 21:34:47 +00005import keyValueList from "../../utils/generateKeyValueList.js";
pineafan4edb7762022-06-26 19:21:04 +01006import client from "../../utils/client.js";
TheCodedProf94ff6de2023-02-22 17:47:26 -05007import getEmojiByName from "../../utils/getEmojiByName.js";
pineafan4f164f32022-02-26 22:07:12 +00008
9const command = (builder: SlashCommandSubcommandBuilder) =>
10 builder
pineafan63fc5e22022-08-04 22:04:10 +010011 .setName("unmute")
12 .setDescription("Unmutes a user")
Skyler Grey11236ba2022-08-08 21:13:33 +010013 .addUserOption((option) => option.setName("user").setDescription("The user to unmute").setRequired(true));
pineafan4f164f32022-02-26 22:07:12 +000014
pineafan3a02ea32022-08-11 21:35:04 +010015const callback = async (interaction: CommandInteraction): Promise<unknown> => {
PineaFana00db1b2023-01-02 15:32:54 +000016 if (!interaction.guild) return;
Skyler Grey11236ba2022-08-08 21:13:33 +010017 const { log, NucleusColors, renderUser, entry, renderDelta } = client.logger;
pineafan5d1908e2022-02-28 21:34:47 +000018 // TODO:[Modals] Replace this with a modal
PineaFana00db1b2023-01-02 15:32:54 +000019 let reason: string | null = null;
pineafan02ba0232022-07-24 22:16:15 +010020 let notify = false;
pineafan73a7c4a2022-07-24 10:38:04 +010021 let confirmation;
PineaFana00db1b2023-01-02 15:32:54 +000022 let timedOut = false;
Skyler Greyad002172022-08-16 18:48:26 +010023 let success = false;
PineaFana00db1b2023-01-02 15:32:54 +000024 do {
Skyler Grey75ea9172022-08-06 10:22:23 +010025 confirmation = await new confirmationMessage(interaction)
pineafan73a7c4a2022-07-24 10:38:04 +010026 .setEmoji("PUNISH.MUTE.RED")
27 .setTitle("Unmute")
Skyler Grey75ea9172022-08-06 10:22:23 +010028 .setDescription(
29 keyValueList({
PineaFana00db1b2023-01-02 15:32:54 +000030 user: renderUser(interaction.options.getUser("user")!),
Skyler Grey75ea9172022-08-06 10:22:23 +010031 reason: `\n> ${reason ? reason : "*No reason provided*"}`
Skyler Greyda16adf2023-03-05 10:22:12 +000032 }) + `Are you sure you want to unmute <@!${(interaction.options.getMember("user") as GuildMember).id}>?`
Skyler Grey75ea9172022-08-06 10:22:23 +010033 )
pineafan73a7c4a2022-07-24 10:38:04 +010034 .setColor("Danger")
PineaFana00db1b2023-01-02 15:32:54 +000035 .addCustomBoolean(
36 "notify",
37 "Notify user",
38 false,
39 null,
40 "The user will be sent a DM",
PineaFana34d04b2023-01-03 22:05:42 +000041 null,
PineaFana00db1b2023-01-02 15:32:54 +000042 "ICONS.NOTIFY." + (notify ? "ON" : "OFF"),
43 notify
44 )
pineafan73a7c4a2022-07-24 10:38:04 +010045 .addReasonButton(reason ?? "")
PineaFan0d06edc2023-01-17 22:10:31 +000046 .setFailedMessage("No changes were made", "Success", "PUNISH.MUTE.GREEN")
pineafan63fc5e22022-08-04 22:04:10 +010047 .send(reason !== null);
PineaFana00db1b2023-01-02 15:32:54 +000048 if (confirmation.cancelled) timedOut = true;
49 else if (confirmation.success !== undefined) success = true;
Skyler Greyad002172022-08-16 18:48:26 +010050 else if (confirmation.newReason) reason = confirmation.newReason;
51 else if (confirmation.components) {
PineaFana00db1b2023-01-02 15:32:54 +000052 notify = confirmation.components!["notify"]!.active;
pineafan02ba0232022-07-24 22:16:15 +010053 }
PineaFana00db1b2023-01-02 15:32:54 +000054 } while (!timedOut && !success);
PineaFan0d06edc2023-01-17 22:10:31 +000055 if (confirmation.cancelled || !confirmation.success) return;
56 let dmSent = false;
57 let dmMessage;
58 try {
59 if (notify) {
PineaFana6e37932023-09-25 22:09:08 +010060 let formattedReason: string | null = null;
61 if (reason) {
62 formattedReason = reason
63 .split("\n")
64 .map((line) => "> " + line)
65 .join("\n");
66 }
PineaFan0d06edc2023-01-17 22:10:31 +000067 dmMessage = await (interaction.options.getMember("user") as GuildMember).send({
Skyler Grey75ea9172022-08-06 10:22:23 +010068 embeds: [
69 new EmojiEmbed()
PineaFan0d06edc2023-01-17 22:10:31 +000070 .setEmoji("PUNISH.MUTE.GREEN")
71 .setTitle("Unmuted")
72 .setDescription(
73 `You have been unmuted in ${interaction.guild.name}` +
PineaFana6e37932023-09-25 22:09:08 +010074 (formattedReason ? ` for:\n> ${formattedReason}` : " with no reason provided.")
PineaFan0d06edc2023-01-17 22:10:31 +000075 )
76 .setStatus("Success")
77 ]
Skyler Grey75ea9172022-08-06 10:22:23 +010078 });
PineaFan0d06edc2023-01-17 22:10:31 +000079 dmSent = true;
pineafan5d1908e2022-02-28 21:34:47 +000080 }
PineaFan0d06edc2023-01-17 22:10:31 +000081 } catch {
82 dmSent = false;
pineafan5d1908e2022-02-28 21:34:47 +000083 }
PineaFan0d06edc2023-01-17 22:10:31 +000084 const member = interaction.options.getMember("user") as GuildMember;
85 try {
Skyler Greyf4f21c42023-03-08 14:36:29 +000086 await member.timeout(0, reason ?? "*No reason provided*");
PineaFan0d06edc2023-01-17 22:10:31 +000087 } catch {
88 await interaction.editReply({
89 embeds: [
90 new EmojiEmbed()
91 .setEmoji("PUNISH.MUTE.RED")
92 .setTitle("Unmute")
93 .setDescription("Something went wrong and the user was not unmuted")
94 .setStatus("Danger")
95 ],
96 components: []
97 });
98 if (dmSent && dmMessage) await dmMessage.delete();
99 return;
100 }
101 await client.database.history.create(
102 "unmute",
103 interaction.guild.id,
104 (interaction.options.getMember("user") as GuildMember).user,
105 interaction.user,
106 reason
107 );
108 const data = {
109 meta: {
110 type: "memberUnmute",
111 displayName: "Unmuted",
112 calculateType: "guildMemberPunish",
113 color: NucleusColors.green,
114 emoji: "PUNISH.MUTE.GREEN",
TheCodedProf6ec331b2023-02-20 12:13:06 -0500115 timestamp: Date.now()
PineaFan0d06edc2023-01-17 22:10:31 +0000116 },
117 list: {
118 memberId: entry(member.user.id, `\`${member.user.id}\``),
119 name: entry(member.user.id, renderUser(member.user)),
TheCodedProf6ec331b2023-02-20 12:13:06 -0500120 unmuted: entry(Date.now().toString(), renderDelta(Date.now())),
PineaFan0d06edc2023-01-17 22:10:31 +0000121 unmutedBy: entry(interaction.user.id, renderUser(interaction.user))
122 },
TheCodedProf94ff6de2023-02-22 17:47:26 -0500123 separate: {
Skyler Greyda16adf2023-03-05 10:22:12 +0000124 end:
125 getEmojiByName("ICONS.NOTIFY." + (notify ? "ON" : "OFF")) +
126 ` The user was ${notify ? "" : "not "}notified`
TheCodedProf94ff6de2023-02-22 17:47:26 -0500127 },
PineaFan0d06edc2023-01-17 22:10:31 +0000128 hidden: {
129 guild: interaction.guild.id
130 }
131 };
Skyler Greyf4f21c42023-03-08 14:36:29 +0000132 await log(data);
PineaFan0d06edc2023-01-17 22:10:31 +0000133 const failed = !dmSent && notify;
134 await interaction.editReply({
135 embeds: [
136 new EmojiEmbed()
137 .setEmoji(`PUNISH.MUTE.${failed ? "YELLOW" : "GREEN"}`)
138 .setTitle("Unmute")
139 .setDescription("The member was unmuted" + (failed ? ", but could not be notified" : ""))
140 .setStatus(failed ? "Warning" : "Success")
141 ],
142 components: []
143 });
pineafan63fc5e22022-08-04 22:04:10 +0100144};
pineafan4f164f32022-02-26 22:07:12 +0000145
TheCodedProff86ba092023-01-27 17:10:07 -0500146const check = (interaction: CommandInteraction, partial: boolean = false) => {
PineaFana00db1b2023-01-02 15:32:54 +0000147 if (!interaction.guild) return;
Skyler Grey75ea9172022-08-06 10:22:23 +0100148 const member = interaction.member as GuildMember;
TheCodedProff86ba092023-01-27 17:10:07 -0500149 // Check if the user has moderate_members permission
Skyler Greyda16adf2023-03-05 10:22:12 +0000150 if (!member.permissions.has("ModerateMembers")) return "You do not have the *Moderate Members* permission";
TheCodedProff86ba092023-01-27 17:10:07 -0500151 if (partial) return true;
PineaFana00db1b2023-01-02 15:32:54 +0000152 const me = interaction.guild.members.me!;
Skyler Grey75ea9172022-08-06 10:22:23 +0100153 const apply = interaction.options.getMember("user") as GuildMember;
pineafan62ce1922022-08-25 20:34:45 +0100154 const memberPos = member.roles.cache.size > 1 ? member.roles.highest.position : 0;
155 const mePos = me.roles.cache.size > 1 ? me.roles.highest.position : 0;
156 const applyPos = apply.roles.cache.size > 1 ? apply.roles.highest.position : 0;
pineafanc1c18792022-08-03 21:41:36 +0100157 // Do not allow unmuting the owner
PineaFan0d06edc2023-01-17 22:10:31 +0000158 if (member.id === interaction.guild.ownerId) return "You cannot unmute the owner of the server";
pineafan5d1908e2022-02-28 21:34:47 +0000159 // Check if Nucleus can unmute the member
TheCodedProf0941da42023-02-18 20:28:04 -0500160 if (!(mePos > applyPos)) return `I do not have a role higher than <@${apply.id}>`;
pineafan5d1908e2022-02-28 21:34:47 +0000161 // Check if Nucleus has permission to unmute
PineaFan0d06edc2023-01-17 22:10:31 +0000162 if (!me.permissions.has("ModerateMembers")) return "I do not have the *Moderate Members* permission";
pineafan5d1908e2022-02-28 21:34:47 +0000163 // Allow the owner to unmute anyone
pineafan63fc5e22022-08-04 22:04:10 +0100164 if (member.id === interaction.guild.ownerId) return true;
pineafan5d1908e2022-02-28 21:34:47 +0000165 // Check if the user is below on the role list
TheCodedProf0941da42023-02-18 20:28:04 -0500166 if (!(memberPos > applyPos)) return `You do not have a role higher than <@${apply.id}>`;
pineafan5d1908e2022-02-28 21:34:47 +0000167 // Allow unmute
pineafan63fc5e22022-08-04 22:04:10 +0100168 return true;
169};
pineafan4f164f32022-02-26 22:07:12 +0000170
Skyler Grey75ea9172022-08-06 10:22:23 +0100171export { command, callback, check };