blob: f248a3bcb6a2a9bb087dd4ddfa6937639cde29a1 [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) {
60 dmMessage = await (interaction.options.getMember("user") as GuildMember).send({
Skyler Grey75ea9172022-08-06 10:22:23 +010061 embeds: [
62 new EmojiEmbed()
PineaFan0d06edc2023-01-17 22:10:31 +000063 .setEmoji("PUNISH.MUTE.GREEN")
64 .setTitle("Unmuted")
65 .setDescription(
66 `You have been unmuted in ${interaction.guild.name}` +
67 (reason ? ` for:\n> ${reason}` : " with no reason provided.")
68 )
69 .setStatus("Success")
70 ]
Skyler Grey75ea9172022-08-06 10:22:23 +010071 });
PineaFan0d06edc2023-01-17 22:10:31 +000072 dmSent = true;
pineafan5d1908e2022-02-28 21:34:47 +000073 }
PineaFan0d06edc2023-01-17 22:10:31 +000074 } catch {
75 dmSent = false;
pineafan5d1908e2022-02-28 21:34:47 +000076 }
PineaFan0d06edc2023-01-17 22:10:31 +000077 const member = interaction.options.getMember("user") as GuildMember;
78 try {
79 member.timeout(0, reason ?? "*No reason provided*");
80 } catch {
81 await interaction.editReply({
82 embeds: [
83 new EmojiEmbed()
84 .setEmoji("PUNISH.MUTE.RED")
85 .setTitle("Unmute")
86 .setDescription("Something went wrong and the user was not unmuted")
87 .setStatus("Danger")
88 ],
89 components: []
90 });
91 if (dmSent && dmMessage) await dmMessage.delete();
92 return;
93 }
94 await client.database.history.create(
95 "unmute",
96 interaction.guild.id,
97 (interaction.options.getMember("user") as GuildMember).user,
98 interaction.user,
99 reason
100 );
101 const data = {
102 meta: {
103 type: "memberUnmute",
104 displayName: "Unmuted",
105 calculateType: "guildMemberPunish",
106 color: NucleusColors.green,
107 emoji: "PUNISH.MUTE.GREEN",
TheCodedProf6ec331b2023-02-20 12:13:06 -0500108 timestamp: Date.now()
PineaFan0d06edc2023-01-17 22:10:31 +0000109 },
110 list: {
111 memberId: entry(member.user.id, `\`${member.user.id}\``),
112 name: entry(member.user.id, renderUser(member.user)),
TheCodedProf6ec331b2023-02-20 12:13:06 -0500113 unmuted: entry(Date.now().toString(), renderDelta(Date.now())),
PineaFan0d06edc2023-01-17 22:10:31 +0000114 unmutedBy: entry(interaction.user.id, renderUser(interaction.user))
115 },
TheCodedProf94ff6de2023-02-22 17:47:26 -0500116 separate: {
Skyler Greyda16adf2023-03-05 10:22:12 +0000117 end:
118 getEmojiByName("ICONS.NOTIFY." + (notify ? "ON" : "OFF")) +
119 ` The user was ${notify ? "" : "not "}notified`
TheCodedProf94ff6de2023-02-22 17:47:26 -0500120 },
PineaFan0d06edc2023-01-17 22:10:31 +0000121 hidden: {
122 guild: interaction.guild.id
123 }
124 };
125 log(data);
126 const failed = !dmSent && notify;
127 await interaction.editReply({
128 embeds: [
129 new EmojiEmbed()
130 .setEmoji(`PUNISH.MUTE.${failed ? "YELLOW" : "GREEN"}`)
131 .setTitle("Unmute")
132 .setDescription("The member was unmuted" + (failed ? ", but could not be notified" : ""))
133 .setStatus(failed ? "Warning" : "Success")
134 ],
135 components: []
136 });
pineafan63fc5e22022-08-04 22:04:10 +0100137};
pineafan4f164f32022-02-26 22:07:12 +0000138
TheCodedProff86ba092023-01-27 17:10:07 -0500139const check = (interaction: CommandInteraction, partial: boolean = false) => {
PineaFana00db1b2023-01-02 15:32:54 +0000140 if (!interaction.guild) return;
Skyler Grey75ea9172022-08-06 10:22:23 +0100141 const member = interaction.member as GuildMember;
TheCodedProff86ba092023-01-27 17:10:07 -0500142 // Check if the user has moderate_members permission
Skyler Greyda16adf2023-03-05 10:22:12 +0000143 if (!member.permissions.has("ModerateMembers")) return "You do not have the *Moderate Members* permission";
TheCodedProff86ba092023-01-27 17:10:07 -0500144 if (partial) return true;
PineaFana00db1b2023-01-02 15:32:54 +0000145 const me = interaction.guild.members.me!;
Skyler Grey75ea9172022-08-06 10:22:23 +0100146 const apply = interaction.options.getMember("user") as GuildMember;
pineafan62ce1922022-08-25 20:34:45 +0100147 const memberPos = member.roles.cache.size > 1 ? member.roles.highest.position : 0;
148 const mePos = me.roles.cache.size > 1 ? me.roles.highest.position : 0;
149 const applyPos = apply.roles.cache.size > 1 ? apply.roles.highest.position : 0;
pineafanc1c18792022-08-03 21:41:36 +0100150 // Do not allow unmuting the owner
PineaFan0d06edc2023-01-17 22:10:31 +0000151 if (member.id === interaction.guild.ownerId) return "You cannot unmute the owner of the server";
pineafan5d1908e2022-02-28 21:34:47 +0000152 // Check if Nucleus can unmute the member
TheCodedProf0941da42023-02-18 20:28:04 -0500153 if (!(mePos > applyPos)) return `I do not have a role higher than <@${apply.id}>`;
pineafan5d1908e2022-02-28 21:34:47 +0000154 // Check if Nucleus has permission to unmute
PineaFan0d06edc2023-01-17 22:10:31 +0000155 if (!me.permissions.has("ModerateMembers")) return "I do not have the *Moderate Members* permission";
pineafan5d1908e2022-02-28 21:34:47 +0000156 // Allow the owner to unmute anyone
pineafan63fc5e22022-08-04 22:04:10 +0100157 if (member.id === interaction.guild.ownerId) return true;
pineafan5d1908e2022-02-28 21:34:47 +0000158 // Check if the user is below on the role list
TheCodedProf0941da42023-02-18 20:28:04 -0500159 if (!(memberPos > applyPos)) return `You do not have a role higher than <@${apply.id}>`;
pineafan5d1908e2022-02-28 21:34:47 +0000160 // Allow unmute
pineafan63fc5e22022-08-04 22:04:10 +0100161 return true;
162};
pineafan4f164f32022-02-26 22:07:12 +0000163
Skyler Grey75ea9172022-08-06 10:22:23 +0100164export { command, callback, check };