blob: 40510a829f59099a244eb7ed75ecdacc1229a701 [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",
41 "ICONS.NOTIFY." + (notify ? "ON" : "OFF"),
42 notify
43 )
pineafan73a7c4a2022-07-24 10:38:04 +010044 .addReasonButton(reason ?? "")
pineafan63fc5e22022-08-04 22:04:10 +010045 .send(reason !== null);
PineaFana00db1b2023-01-02 15:32:54 +000046 if (confirmation.cancelled) timedOut = true;
47 else if (confirmation.success !== undefined) success = true;
Skyler Greyad002172022-08-16 18:48:26 +010048 else if (confirmation.newReason) reason = confirmation.newReason;
49 else if (confirmation.components) {
PineaFana00db1b2023-01-02 15:32:54 +000050 notify = confirmation.components!["notify"]!.active;
pineafan02ba0232022-07-24 22:16:15 +010051 }
PineaFana00db1b2023-01-02 15:32:54 +000052 } while (!timedOut && !success);
pineafan63fc5e22022-08-04 22:04:10 +010053 if (confirmation.cancelled) return;
pineafan377794f2022-04-18 19:01:01 +010054 if (confirmation.success) {
PineaFana00db1b2023-01-02 15:32:54 +000055 let dmSent = false;
56 let dmMessage;
pineafan5d1908e2022-02-28 21:34:47 +000057 try {
pineafan02ba0232022-07-24 22:16:15 +010058 if (notify) {
PineaFana00db1b2023-01-02 15:32:54 +000059 dmMessage = await (interaction.options.getMember("user") as GuildMember).send({
Skyler Grey75ea9172022-08-06 10:22:23 +010060 embeds: [
61 new EmojiEmbed()
62 .setEmoji("PUNISH.MUTE.GREEN")
63 .setTitle("Unmuted")
64 .setDescription(
65 `You have been unmuted in ${interaction.guild.name}` +
Skyler Grey11236ba2022-08-08 21:13:33 +010066 (reason ? ` for:\n> ${reason}` : " with no reason provided.")
Skyler Grey75ea9172022-08-06 10:22:23 +010067 )
68 .setStatus("Success")
pineafan5d1908e2022-02-28 21:34:47 +000069 ]
pineafan63fc5e22022-08-04 22:04:10 +010070 });
PineaFana00db1b2023-01-02 15:32:54 +000071 dmSent = true;
pineafan5d1908e2022-02-28 21:34:47 +000072 }
Skyler Grey75ea9172022-08-06 10:22:23 +010073 } catch {
PineaFana00db1b2023-01-02 15:32:54 +000074 dmSent = false;
Skyler Grey75ea9172022-08-06 10:22:23 +010075 }
76 const member = interaction.options.getMember("user") as GuildMember;
pineafan5d1908e2022-02-28 21:34:47 +000077 try {
PineaFan100df682023-01-02 13:26:08 +000078 member.timeout(0, reason ?? "*No reason provided*");
pineafan5d1908e2022-02-28 21:34:47 +000079 } catch {
Skyler Grey75ea9172022-08-06 10:22:23 +010080 await interaction.editReply({
81 embeds: [
82 new EmojiEmbed()
83 .setEmoji("PUNISH.MUTE.RED")
84 .setTitle("Unmute")
Skyler Grey11236ba2022-08-08 21:13:33 +010085 .setDescription("Something went wrong and the user was not unmuted")
Skyler Grey75ea9172022-08-06 10:22:23 +010086 .setStatus("Danger")
87 ],
88 components: []
89 });
PineaFana00db1b2023-01-02 15:32:54 +000090 if (dmSent && dmMessage) await dmMessage.delete();
pineafan63fc5e22022-08-04 22:04:10 +010091 return;
pineafan5d1908e2022-02-28 21:34:47 +000092 }
Skyler Grey75ea9172022-08-06 10:22:23 +010093 await client.database.history.create(
94 "unmute",
95 interaction.guild.id,
96 (interaction.options.getMember("user") as GuildMember).user,
97 interaction.user,
98 reason
99 );
pineafan63fc5e22022-08-04 22:04:10 +0100100 const data = {
pineafan73a7c4a2022-07-24 10:38:04 +0100101 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100102 type: "memberUnmute",
103 displayName: "Unmuted",
104 calculateType: "guildMemberPunish",
pineafan73a7c4a2022-07-24 10:38:04 +0100105 color: NucleusColors.green,
106 emoji: "PUNISH.MUTE.GREEN",
107 timestamp: new Date().getTime()
108 },
109 list: {
110 memberId: entry(member.user.id, `\`${member.user.id}\``),
111 name: entry(member.user.id, renderUser(member.user)),
PineaFana00db1b2023-01-02 15:32:54 +0000112 unmuted: entry(new Date().getTime().toString(), renderDelta(new Date().getTime())),
Skyler Grey11236ba2022-08-08 21:13:33 +0100113 unmutedBy: entry(interaction.user.id, renderUser(interaction.user))
pineafan73a7c4a2022-07-24 10:38:04 +0100114 },
115 hidden: {
116 guild: interaction.guild.id
117 }
pineafan63fc5e22022-08-04 22:04:10 +0100118 };
pineafan73a7c4a2022-07-24 10:38:04 +0100119 log(data);
PineaFana00db1b2023-01-02 15:32:54 +0000120 const failed = !dmSent && notify;
Skyler Grey75ea9172022-08-06 10:22:23 +0100121 await interaction.editReply({
122 embeds: [
123 new EmojiEmbed()
124 .setEmoji(`PUNISH.MUTE.${failed ? "YELLOW" : "GREEN"}`)
125 .setTitle("Unmute")
Skyler Grey11236ba2022-08-08 21:13:33 +0100126 .setDescription("The member was unmuted" + (failed ? ", but could not be notified" : ""))
Skyler Grey75ea9172022-08-06 10:22:23 +0100127 .setStatus(failed ? "Warning" : "Success")
128 ],
129 components: []
130 });
pineafan5d1908e2022-02-28 21:34:47 +0000131 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +0100132 await interaction.editReply({
133 embeds: [
134 new EmojiEmbed()
135 .setEmoji("PUNISH.MUTE.GREEN")
136 .setTitle("Unmute")
137 .setDescription("No changes were made")
138 .setStatus("Success")
139 ],
140 components: []
141 });
pineafan5d1908e2022-02-28 21:34:47 +0000142 }
pineafan63fc5e22022-08-04 22:04:10 +0100143};
pineafan4f164f32022-02-26 22:07:12 +0000144
pineafanbd02b4a2022-08-05 22:01:38 +0100145const check = (interaction: CommandInteraction) => {
PineaFana00db1b2023-01-02 15:32:54 +0000146 if (!interaction.guild) return;
Skyler Grey75ea9172022-08-06 10:22:23 +0100147 const member = interaction.member as GuildMember;
PineaFana00db1b2023-01-02 15:32:54 +0000148 const me = interaction.guild.members.me!;
Skyler Grey75ea9172022-08-06 10:22:23 +0100149 const apply = interaction.options.getMember("user") as GuildMember;
pineafan62ce1922022-08-25 20:34:45 +0100150 const memberPos = member.roles.cache.size > 1 ? member.roles.highest.position : 0;
151 const mePos = me.roles.cache.size > 1 ? me.roles.highest.position : 0;
152 const applyPos = apply.roles.cache.size > 1 ? apply.roles.highest.position : 0;
pineafanc1c18792022-08-03 21:41:36 +0100153 // Do not allow unmuting the owner
pineafan3a02ea32022-08-11 21:35:04 +0100154 if (member.id === interaction.guild.ownerId) throw new Error("You cannot unmute the owner of the server");
pineafan5d1908e2022-02-28 21:34:47 +0000155 // Check if Nucleus can unmute the member
pineafan3a02ea32022-08-11 21:35:04 +0100156 if (!(mePos > applyPos)) throw new Error("I do not have a role higher than that member");
pineafan5d1908e2022-02-28 21:34:47 +0000157 // Check if Nucleus has permission to unmute
PineaFana00db1b2023-01-02 15:32:54 +0000158 if (!me.permissions.has("ModerateMembers")) throw new Error("I do not have the *Moderate Members* permission");
pineafan5d1908e2022-02-28 21:34:47 +0000159 // Allow the owner to unmute anyone
pineafan63fc5e22022-08-04 22:04:10 +0100160 if (member.id === interaction.guild.ownerId) return true;
pineafan5d1908e2022-02-28 21:34:47 +0000161 // Check if the user has moderate_members permission
PineaFana00db1b2023-01-02 15:32:54 +0000162 if (!member.permissions.has("ModerateMembers"))
pineafan3a02ea32022-08-11 21:35:04 +0100163 throw new Error("You do not have the *Moderate Members* permission");
pineafan5d1908e2022-02-28 21:34:47 +0000164 // Check if the user is below on the role list
pineafan3a02ea32022-08-11 21:35:04 +0100165 if (!(memberPos > applyPos)) throw new Error("You do not have a role higher than that member");
pineafan5d1908e2022-02-28 21:34:47 +0000166 // Allow unmute
pineafan63fc5e22022-08-04 22:04:10 +0100167 return true;
168};
pineafan4f164f32022-02-26 22:07:12 +0000169
Skyler Grey75ea9172022-08-06 10:22:23 +0100170export { command, callback, check };