blob: c93f8ccace2128b6644a4f783dde7993aa21621a [file] [log] [blame]
PineaFana00db1b2023-01-02 15:32:54 +00001import type { CommandInteraction, GuildMember } from "discord.js";
2import type { SlashCommandSubcommandBuilder } from "@discordjs/builders";
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";
pineafan4f164f32022-02-26 22:07:12 +00007
8const command = (builder: SlashCommandSubcommandBuilder) =>
9 builder
pineafan63fc5e22022-08-04 22:04:10 +010010 .setName("unmute")
11 .setDescription("Unmutes a user")
Skyler Grey11236ba2022-08-08 21:13:33 +010012 .addUserOption((option) => option.setName("user").setDescription("The user to unmute").setRequired(true));
pineafan4f164f32022-02-26 22:07:12 +000013
pineafan3a02ea32022-08-11 21:35:04 +010014const callback = async (interaction: CommandInteraction): Promise<unknown> => {
PineaFana00db1b2023-01-02 15:32:54 +000015 if (!interaction.guild) return;
Skyler Grey11236ba2022-08-08 21:13:33 +010016 const { log, NucleusColors, renderUser, entry, renderDelta } = client.logger;
pineafan5d1908e2022-02-28 21:34:47 +000017 // TODO:[Modals] Replace this with a modal
PineaFana00db1b2023-01-02 15:32:54 +000018 let reason: string | null = null;
pineafan02ba0232022-07-24 22:16:15 +010019 let notify = false;
pineafan73a7c4a2022-07-24 10:38:04 +010020 let confirmation;
PineaFana00db1b2023-01-02 15:32:54 +000021 let timedOut = false;
Skyler Greyad002172022-08-16 18:48:26 +010022 let success = false;
PineaFana00db1b2023-01-02 15:32:54 +000023 do {
Skyler Grey75ea9172022-08-06 10:22:23 +010024 confirmation = await new confirmationMessage(interaction)
pineafan73a7c4a2022-07-24 10:38:04 +010025 .setEmoji("PUNISH.MUTE.RED")
26 .setTitle("Unmute")
Skyler Grey75ea9172022-08-06 10:22:23 +010027 .setDescription(
28 keyValueList({
PineaFana00db1b2023-01-02 15:32:54 +000029 user: renderUser(interaction.options.getUser("user")!),
Skyler Grey75ea9172022-08-06 10:22:23 +010030 reason: `\n> ${reason ? reason : "*No reason provided*"}`
31 }) +
Skyler Grey11236ba2022-08-08 21:13:33 +010032 `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 ?? "")
pineafan63fc5e22022-08-04 22:04:10 +010046 .send(reason !== null);
PineaFana00db1b2023-01-02 15:32:54 +000047 if (confirmation.cancelled) timedOut = true;
48 else if (confirmation.success !== undefined) success = true;
Skyler Greyad002172022-08-16 18:48:26 +010049 else if (confirmation.newReason) reason = confirmation.newReason;
50 else if (confirmation.components) {
PineaFana00db1b2023-01-02 15:32:54 +000051 notify = confirmation.components!["notify"]!.active;
pineafan02ba0232022-07-24 22:16:15 +010052 }
PineaFana00db1b2023-01-02 15:32:54 +000053 } while (!timedOut && !success);
pineafan63fc5e22022-08-04 22:04:10 +010054 if (confirmation.cancelled) return;
pineafan377794f2022-04-18 19:01:01 +010055 if (confirmation.success) {
PineaFana00db1b2023-01-02 15:32:54 +000056 let dmSent = false;
57 let dmMessage;
pineafan5d1908e2022-02-28 21:34:47 +000058 try {
pineafan02ba0232022-07-24 22:16:15 +010059 if (notify) {
PineaFana00db1b2023-01-02 15:32:54 +000060 dmMessage = await (interaction.options.getMember("user") as GuildMember).send({
Skyler Grey75ea9172022-08-06 10:22:23 +010061 embeds: [
62 new EmojiEmbed()
63 .setEmoji("PUNISH.MUTE.GREEN")
64 .setTitle("Unmuted")
65 .setDescription(
66 `You have been unmuted in ${interaction.guild.name}` +
Skyler Grey11236ba2022-08-08 21:13:33 +010067 (reason ? ` for:\n> ${reason}` : " with no reason provided.")
Skyler Grey75ea9172022-08-06 10:22:23 +010068 )
69 .setStatus("Success")
pineafan5d1908e2022-02-28 21:34:47 +000070 ]
pineafan63fc5e22022-08-04 22:04:10 +010071 });
PineaFana00db1b2023-01-02 15:32:54 +000072 dmSent = true;
pineafan5d1908e2022-02-28 21:34:47 +000073 }
Skyler Grey75ea9172022-08-06 10:22:23 +010074 } catch {
PineaFana00db1b2023-01-02 15:32:54 +000075 dmSent = false;
Skyler Grey75ea9172022-08-06 10:22:23 +010076 }
77 const member = interaction.options.getMember("user") as GuildMember;
pineafan5d1908e2022-02-28 21:34:47 +000078 try {
PineaFan100df682023-01-02 13:26:08 +000079 member.timeout(0, reason ?? "*No reason provided*");
pineafan5d1908e2022-02-28 21:34:47 +000080 } catch {
Skyler Grey75ea9172022-08-06 10:22:23 +010081 await interaction.editReply({
82 embeds: [
83 new EmojiEmbed()
84 .setEmoji("PUNISH.MUTE.RED")
85 .setTitle("Unmute")
Skyler Grey11236ba2022-08-08 21:13:33 +010086 .setDescription("Something went wrong and the user was not unmuted")
Skyler Grey75ea9172022-08-06 10:22:23 +010087 .setStatus("Danger")
88 ],
89 components: []
90 });
PineaFana00db1b2023-01-02 15:32:54 +000091 if (dmSent && dmMessage) await dmMessage.delete();
pineafan63fc5e22022-08-04 22:04:10 +010092 return;
pineafan5d1908e2022-02-28 21:34:47 +000093 }
Skyler Grey75ea9172022-08-06 10:22:23 +010094 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 );
pineafan63fc5e22022-08-04 22:04:10 +0100101 const data = {
pineafan73a7c4a2022-07-24 10:38:04 +0100102 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100103 type: "memberUnmute",
104 displayName: "Unmuted",
105 calculateType: "guildMemberPunish",
pineafan73a7c4a2022-07-24 10:38:04 +0100106 color: NucleusColors.green,
107 emoji: "PUNISH.MUTE.GREEN",
108 timestamp: new Date().getTime()
109 },
110 list: {
111 memberId: entry(member.user.id, `\`${member.user.id}\``),
112 name: entry(member.user.id, renderUser(member.user)),
PineaFana00db1b2023-01-02 15:32:54 +0000113 unmuted: entry(new Date().getTime().toString(), renderDelta(new Date().getTime())),
Skyler Grey11236ba2022-08-08 21:13:33 +0100114 unmutedBy: entry(interaction.user.id, renderUser(interaction.user))
pineafan73a7c4a2022-07-24 10:38:04 +0100115 },
116 hidden: {
117 guild: interaction.guild.id
118 }
pineafan63fc5e22022-08-04 22:04:10 +0100119 };
pineafan73a7c4a2022-07-24 10:38:04 +0100120 log(data);
PineaFana00db1b2023-01-02 15:32:54 +0000121 const failed = !dmSent && notify;
Skyler Grey75ea9172022-08-06 10:22:23 +0100122 await interaction.editReply({
123 embeds: [
124 new EmojiEmbed()
125 .setEmoji(`PUNISH.MUTE.${failed ? "YELLOW" : "GREEN"}`)
126 .setTitle("Unmute")
Skyler Grey11236ba2022-08-08 21:13:33 +0100127 .setDescription("The member was unmuted" + (failed ? ", but could not be notified" : ""))
Skyler Grey75ea9172022-08-06 10:22:23 +0100128 .setStatus(failed ? "Warning" : "Success")
129 ],
130 components: []
131 });
pineafan5d1908e2022-02-28 21:34:47 +0000132 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +0100133 await interaction.editReply({
134 embeds: [
135 new EmojiEmbed()
136 .setEmoji("PUNISH.MUTE.GREEN")
137 .setTitle("Unmute")
138 .setDescription("No changes were made")
139 .setStatus("Success")
140 ],
141 components: []
142 });
pineafan5d1908e2022-02-28 21:34:47 +0000143 }
pineafan63fc5e22022-08-04 22:04:10 +0100144};
pineafan4f164f32022-02-26 22:07:12 +0000145
pineafanbd02b4a2022-08-05 22:01:38 +0100146const check = (interaction: CommandInteraction) => {
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;
PineaFana00db1b2023-01-02 15:32:54 +0000149 const me = interaction.guild.members.me!;
Skyler Grey75ea9172022-08-06 10:22:23 +0100150 const apply = interaction.options.getMember("user") as GuildMember;
pineafan62ce1922022-08-25 20:34:45 +0100151 const memberPos = member.roles.cache.size > 1 ? member.roles.highest.position : 0;
152 const mePos = me.roles.cache.size > 1 ? me.roles.highest.position : 0;
153 const applyPos = apply.roles.cache.size > 1 ? apply.roles.highest.position : 0;
pineafanc1c18792022-08-03 21:41:36 +0100154 // Do not allow unmuting the owner
pineafan3a02ea32022-08-11 21:35:04 +0100155 if (member.id === interaction.guild.ownerId) throw new Error("You cannot unmute the owner of the server");
pineafan5d1908e2022-02-28 21:34:47 +0000156 // Check if Nucleus can unmute the member
pineafan3a02ea32022-08-11 21:35:04 +0100157 if (!(mePos > applyPos)) throw new Error("I do not have a role higher than that member");
pineafan5d1908e2022-02-28 21:34:47 +0000158 // Check if Nucleus has permission to unmute
PineaFana00db1b2023-01-02 15:32:54 +0000159 if (!me.permissions.has("ModerateMembers")) throw new Error("I do not have the *Moderate Members* permission");
pineafan5d1908e2022-02-28 21:34:47 +0000160 // Allow the owner to unmute anyone
pineafan63fc5e22022-08-04 22:04:10 +0100161 if (member.id === interaction.guild.ownerId) return true;
pineafan5d1908e2022-02-28 21:34:47 +0000162 // Check if the user has moderate_members permission
PineaFana00db1b2023-01-02 15:32:54 +0000163 if (!member.permissions.has("ModerateMembers"))
pineafan3a02ea32022-08-11 21:35:04 +0100164 throw new Error("You do not have the *Moderate Members* permission");
pineafan5d1908e2022-02-28 21:34:47 +0000165 // Check if the user is below on the role list
pineafan3a02ea32022-08-11 21:35:04 +0100166 if (!(memberPos > applyPos)) throw new Error("You do not have a role higher than that member");
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 };