blob: 8562c4c1263266cd5c39e0efe2c187dbdccd61d5 [file] [log] [blame]
PineaFana00db1b2023-01-02 15:32:54 +00001import type { CommandInteraction, GuildMember } from "discord.js";
Samuel Shuert27bf3cd2023-03-03 15:51:25 -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";
Samuel Shuert27bf3cd2023-03-03 15:51:25 -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*"}`
32 }) +
Skyler Grey11236ba2022-08-08 21:13:33 +010033 `Are you sure you want to unmute <@!${(interaction.options.getMember("user") as GuildMember).id}>?`
Skyler Grey75ea9172022-08-06 10:22:23 +010034 )
pineafan73a7c4a2022-07-24 10:38:04 +010035 .setColor("Danger")
PineaFana00db1b2023-01-02 15:32:54 +000036 .addCustomBoolean(
37 "notify",
38 "Notify user",
39 false,
40 null,
41 "The user will be sent a DM",
PineaFana34d04b2023-01-03 22:05:42 +000042 null,
PineaFana00db1b2023-01-02 15:32:54 +000043 "ICONS.NOTIFY." + (notify ? "ON" : "OFF"),
44 notify
45 )
pineafan73a7c4a2022-07-24 10:38:04 +010046 .addReasonButton(reason ?? "")
PineaFan0d06edc2023-01-17 22:10:31 +000047 .setFailedMessage("No changes were made", "Success", "PUNISH.MUTE.GREEN")
pineafan63fc5e22022-08-04 22:04:10 +010048 .send(reason !== null);
PineaFana00db1b2023-01-02 15:32:54 +000049 if (confirmation.cancelled) timedOut = true;
50 else if (confirmation.success !== undefined) success = true;
Skyler Greyad002172022-08-16 18:48:26 +010051 else if (confirmation.newReason) reason = confirmation.newReason;
52 else if (confirmation.components) {
PineaFana00db1b2023-01-02 15:32:54 +000053 notify = confirmation.components!["notify"]!.active;
pineafan02ba0232022-07-24 22:16:15 +010054 }
PineaFana00db1b2023-01-02 15:32:54 +000055 } while (!timedOut && !success);
PineaFan0d06edc2023-01-17 22:10:31 +000056 if (confirmation.cancelled || !confirmation.success) return;
57 let dmSent = false;
58 let dmMessage;
59 try {
60 if (notify) {
61 dmMessage = await (interaction.options.getMember("user") as GuildMember).send({
Skyler Grey75ea9172022-08-06 10:22:23 +010062 embeds: [
63 new EmojiEmbed()
PineaFan0d06edc2023-01-17 22:10:31 +000064 .setEmoji("PUNISH.MUTE.GREEN")
65 .setTitle("Unmuted")
66 .setDescription(
67 `You have been unmuted in ${interaction.guild.name}` +
68 (reason ? ` for:\n> ${reason}` : " with no reason provided.")
69 )
70 .setStatus("Success")
71 ]
Skyler Grey75ea9172022-08-06 10:22:23 +010072 });
PineaFan0d06edc2023-01-17 22:10:31 +000073 dmSent = true;
pineafan5d1908e2022-02-28 21:34:47 +000074 }
PineaFan0d06edc2023-01-17 22:10:31 +000075 } catch {
76 dmSent = false;
pineafan5d1908e2022-02-28 21:34:47 +000077 }
PineaFan0d06edc2023-01-17 22:10:31 +000078 const member = interaction.options.getMember("user") as GuildMember;
79 try {
80 member.timeout(0, reason ?? "*No reason provided*");
81 } catch {
82 await interaction.editReply({
83 embeds: [
84 new EmojiEmbed()
85 .setEmoji("PUNISH.MUTE.RED")
86 .setTitle("Unmute")
87 .setDescription("Something went wrong and the user was not unmuted")
88 .setStatus("Danger")
89 ],
90 components: []
91 });
92 if (dmSent && dmMessage) await dmMessage.delete();
93 return;
94 }
95 await client.database.history.create(
96 "unmute",
97 interaction.guild.id,
98 (interaction.options.getMember("user") as GuildMember).user,
99 interaction.user,
100 reason
101 );
102 const data = {
103 meta: {
104 type: "memberUnmute",
105 displayName: "Unmuted",
106 calculateType: "guildMemberPunish",
107 color: NucleusColors.green,
108 emoji: "PUNISH.MUTE.GREEN",
Samuel Shuert27bf3cd2023-03-03 15:51:25 -0500109 timestamp: Date.now()
PineaFan0d06edc2023-01-17 22:10:31 +0000110 },
111 list: {
112 memberId: entry(member.user.id, `\`${member.user.id}\``),
113 name: entry(member.user.id, renderUser(member.user)),
Samuel Shuert27bf3cd2023-03-03 15:51:25 -0500114 unmuted: entry(Date.now().toString(), renderDelta(Date.now())),
PineaFan0d06edc2023-01-17 22:10:31 +0000115 unmutedBy: entry(interaction.user.id, renderUser(interaction.user))
116 },
Samuel Shuert27bf3cd2023-03-03 15:51:25 -0500117 separate: {
118 end: getEmojiByName("ICONS.NOTIFY." + (notify ? "ON" : "OFF")) + ` The user was ${notify ? "" : "not "}notified`
119 },
PineaFan0d06edc2023-01-17 22:10:31 +0000120 hidden: {
121 guild: interaction.guild.id
122 }
123 };
124 log(data);
125 const failed = !dmSent && notify;
126 await interaction.editReply({
127 embeds: [
128 new EmojiEmbed()
129 .setEmoji(`PUNISH.MUTE.${failed ? "YELLOW" : "GREEN"}`)
130 .setTitle("Unmute")
131 .setDescription("The member was unmuted" + (failed ? ", but could not be notified" : ""))
132 .setStatus(failed ? "Warning" : "Success")
133 ],
134 components: []
135 });
pineafan63fc5e22022-08-04 22:04:10 +0100136};
pineafan4f164f32022-02-26 22:07:12 +0000137
Samuel Shuert27bf3cd2023-03-03 15:51:25 -0500138const check = (interaction: CommandInteraction, partial: boolean = false) => {
PineaFana00db1b2023-01-02 15:32:54 +0000139 if (!interaction.guild) return;
Skyler Grey75ea9172022-08-06 10:22:23 +0100140 const member = interaction.member as GuildMember;
Samuel Shuert27bf3cd2023-03-03 15:51:25 -0500141 // Check if the user has moderate_members permission
142 if (!member.permissions.has("ModerateMembers"))
143 return "You do not have the *Moderate Members* permission";
144 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
Samuel Shuert27bf3cd2023-03-03 15:51:25 -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
Samuel Shuert27bf3cd2023-03-03 15:51:25 -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 };