blob: 826ed5ca6c038545235bba489917a741ccc7cab4 [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";
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 ?? "")
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",
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)),
113 unmuted: entry(new Date().getTime().toString(), renderDelta(new Date().getTime())),
114 unmutedBy: entry(interaction.user.id, renderUser(interaction.user))
115 },
116 hidden: {
117 guild: interaction.guild.id
118 }
119 };
120 log(data);
121 const failed = !dmSent && notify;
122 await interaction.editReply({
123 embeds: [
124 new EmojiEmbed()
125 .setEmoji(`PUNISH.MUTE.${failed ? "YELLOW" : "GREEN"}`)
126 .setTitle("Unmute")
127 .setDescription("The member was unmuted" + (failed ? ", but could not be notified" : ""))
128 .setStatus(failed ? "Warning" : "Success")
129 ],
130 components: []
131 });
pineafan63fc5e22022-08-04 22:04:10 +0100132};
pineafan4f164f32022-02-26 22:07:12 +0000133
TheCodedProff86ba092023-01-27 17:10:07 -0500134const check = (interaction: CommandInteraction, partial: boolean = false) => {
PineaFana00db1b2023-01-02 15:32:54 +0000135 if (!interaction.guild) return;
Skyler Grey75ea9172022-08-06 10:22:23 +0100136 const member = interaction.member as GuildMember;
TheCodedProff86ba092023-01-27 17:10:07 -0500137 // Check if the user has moderate_members permission
138 if (!member.permissions.has("ModerateMembers"))
139 return "You do not have the *Moderate Members* permission";
140 if (partial) return true;
PineaFana00db1b2023-01-02 15:32:54 +0000141 const me = interaction.guild.members.me!;
Skyler Grey75ea9172022-08-06 10:22:23 +0100142 const apply = interaction.options.getMember("user") as GuildMember;
pineafan62ce1922022-08-25 20:34:45 +0100143 const memberPos = member.roles.cache.size > 1 ? member.roles.highest.position : 0;
144 const mePos = me.roles.cache.size > 1 ? me.roles.highest.position : 0;
145 const applyPos = apply.roles.cache.size > 1 ? apply.roles.highest.position : 0;
pineafanc1c18792022-08-03 21:41:36 +0100146 // Do not allow unmuting the owner
PineaFan0d06edc2023-01-17 22:10:31 +0000147 if (member.id === interaction.guild.ownerId) return "You cannot unmute the owner of the server";
pineafan5d1908e2022-02-28 21:34:47 +0000148 // Check if Nucleus can unmute the member
TheCodedProf0941da42023-02-18 20:28:04 -0500149 if (!(mePos > applyPos)) return `I do not have a role higher than <@${apply.id}>`;
pineafan5d1908e2022-02-28 21:34:47 +0000150 // Check if Nucleus has permission to unmute
PineaFan0d06edc2023-01-17 22:10:31 +0000151 if (!me.permissions.has("ModerateMembers")) return "I do not have the *Moderate Members* permission";
pineafan5d1908e2022-02-28 21:34:47 +0000152 // Allow the owner to unmute anyone
pineafan63fc5e22022-08-04 22:04:10 +0100153 if (member.id === interaction.guild.ownerId) return true;
pineafan5d1908e2022-02-28 21:34:47 +0000154 // Check if the user is below on the role list
TheCodedProf0941da42023-02-18 20:28:04 -0500155 if (!(memberPos > applyPos)) return `You do not have a role higher than <@${apply.id}>`;
pineafan5d1908e2022-02-28 21:34:47 +0000156 // Allow unmute
pineafan63fc5e22022-08-04 22:04:10 +0100157 return true;
158};
pineafan4f164f32022-02-26 22:07:12 +0000159
Skyler Grey75ea9172022-08-06 10:22:23 +0100160export { command, callback, check };