blob: f62546187d55f113c132f8b65c55db32fb090780 [file] [log] [blame]
pineafan5d1908e2022-02-28 21:34:47 +00001import { CommandInteraction, GuildMember } from "discord.js";
pineafan4f164f32022-02-26 22:07:12 +00002import { 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 Grey75ea9172022-08-06 10:22:23 +010012 .addUserOption((option) =>
13 option
14 .setName("user")
15 .setDescription("The user to unmute")
16 .setRequired(true)
17 );
pineafan4f164f32022-02-26 22:07:12 +000018
Skyler Grey75ea9172022-08-06 10:22:23 +010019const callback = async (
20 interaction: CommandInteraction
21): Promise<void | unknown> => {
22 const { log, NucleusColors, renderUser, entry, renderDelta } =
23 client.logger;
pineafan5d1908e2022-02-28 21:34:47 +000024 // TODO:[Modals] Replace this with a modal
pineafan73a7c4a2022-07-24 10:38:04 +010025 let reason = null;
pineafan02ba0232022-07-24 22:16:15 +010026 let notify = false;
pineafan73a7c4a2022-07-24 10:38:04 +010027 let confirmation;
28 while (true) {
Skyler Grey75ea9172022-08-06 10:22:23 +010029 confirmation = await new confirmationMessage(interaction)
pineafan73a7c4a2022-07-24 10:38:04 +010030 .setEmoji("PUNISH.MUTE.RED")
31 .setTitle("Unmute")
Skyler Grey75ea9172022-08-06 10:22:23 +010032 .setDescription(
33 keyValueList({
34 user: renderUser(interaction.options.getUser("user")),
35 reason: `\n> ${reason ? reason : "*No reason provided*"}`
36 }) +
37 `The user **will${notify ? "" : " not"}** be notified\n\n` +
38 `Are you sure you want to unmute <@!${
39 (interaction.options.getMember("user") as GuildMember)
40 .id
41 }>?`
42 )
pineafan73a7c4a2022-07-24 10:38:04 +010043 .setColor("Danger")
44 .addReasonButton(reason ?? "")
pineafan63fc5e22022-08-04 22:04:10 +010045 .send(reason !== null);
46 if (confirmation.success) break;
47 if (confirmation.newReason) reason = confirmation.newReason;
pineafan02ba0232022-07-24 22:16:15 +010048 if (confirmation.components) {
pineafan63fc5e22022-08-04 22:04:10 +010049 notify = confirmation.components.notify.active;
pineafan02ba0232022-07-24 22:16:15 +010050 }
pineafan73a7c4a2022-07-24 10:38:04 +010051 }
pineafan63fc5e22022-08-04 22:04:10 +010052 if (confirmation.cancelled) return;
pineafan377794f2022-04-18 19:01:01 +010053 if (confirmation.success) {
pineafan63fc5e22022-08-04 22:04:10 +010054 let dmd = false;
pineafan5d1908e2022-02-28 21:34:47 +000055 let dm;
56 try {
pineafan02ba0232022-07-24 22:16:15 +010057 if (notify) {
Skyler Grey75ea9172022-08-06 10:22:23 +010058 dm = await (
59 interaction.options.getMember("user") as GuildMember
60 ).send({
61 embeds: [
62 new EmojiEmbed()
63 .setEmoji("PUNISH.MUTE.GREEN")
64 .setTitle("Unmuted")
65 .setDescription(
66 `You have been unmuted in ${interaction.guild.name}` +
67 (reason
68 ? ` for:\n> ${reason}`
69 : " with no reason provided.")
70 )
71 .setStatus("Success")
pineafan5d1908e2022-02-28 21:34:47 +000072 ]
pineafan63fc5e22022-08-04 22:04:10 +010073 });
74 dmd = true;
pineafan5d1908e2022-02-28 21:34:47 +000075 }
Skyler Grey75ea9172022-08-06 10:22:23 +010076 } catch {
77 dmd = false;
78 }
79 const member = interaction.options.getMember("user") as GuildMember;
pineafan5d1908e2022-02-28 21:34:47 +000080 try {
pineafan63fc5e22022-08-04 22:04:10 +010081 member.timeout(0, reason || "No reason provided");
pineafan5d1908e2022-02-28 21:34:47 +000082 } catch {
Skyler Grey75ea9172022-08-06 10:22:23 +010083 await interaction.editReply({
84 embeds: [
85 new EmojiEmbed()
86 .setEmoji("PUNISH.MUTE.RED")
87 .setTitle("Unmute")
88 .setDescription(
89 "Something went wrong and the user was not unmuted"
90 )
91 .setStatus("Danger")
92 ],
93 components: []
94 });
pineafan63fc5e22022-08-04 22:04:10 +010095 if (dmd) await dm.delete();
96 return;
pineafan5d1908e2022-02-28 21:34:47 +000097 }
Skyler Grey75ea9172022-08-06 10:22:23 +010098 await client.database.history.create(
99 "unmute",
100 interaction.guild.id,
101 (interaction.options.getMember("user") as GuildMember).user,
102 interaction.user,
103 reason
104 );
pineafan63fc5e22022-08-04 22:04:10 +0100105 const data = {
pineafan73a7c4a2022-07-24 10:38:04 +0100106 meta: {
pineafan63fc5e22022-08-04 22:04:10 +0100107 type: "memberUnmute",
108 displayName: "Unmuted",
109 calculateType: "guildMemberPunish",
pineafan73a7c4a2022-07-24 10:38:04 +0100110 color: NucleusColors.green,
111 emoji: "PUNISH.MUTE.GREEN",
112 timestamp: new Date().getTime()
113 },
114 list: {
115 memberId: entry(member.user.id, `\`${member.user.id}\``),
116 name: entry(member.user.id, renderUser(member.user)),
Skyler Grey75ea9172022-08-06 10:22:23 +0100117 unmuted: entry(
118 new Date().getTime(),
119 renderDelta(new Date().getTime())
120 ),
121 unmutedBy: entry(
122 interaction.user.id,
123 renderUser(interaction.user)
124 )
pineafan73a7c4a2022-07-24 10:38:04 +0100125 },
126 hidden: {
127 guild: interaction.guild.id
128 }
pineafan63fc5e22022-08-04 22:04:10 +0100129 };
pineafan73a7c4a2022-07-24 10:38:04 +0100130 log(data);
Skyler Grey75ea9172022-08-06 10:22:23 +0100131 const failed = !dmd && notify;
132 await interaction.editReply({
133 embeds: [
134 new EmojiEmbed()
135 .setEmoji(`PUNISH.MUTE.${failed ? "YELLOW" : "GREEN"}`)
136 .setTitle("Unmute")
137 .setDescription(
138 "The member was unmuted" +
139 (failed ? ", but could not be notified" : "")
140 )
141 .setStatus(failed ? "Warning" : "Success")
142 ],
143 components: []
144 });
pineafan5d1908e2022-02-28 21:34:47 +0000145 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +0100146 await interaction.editReply({
147 embeds: [
148 new EmojiEmbed()
149 .setEmoji("PUNISH.MUTE.GREEN")
150 .setTitle("Unmute")
151 .setDescription("No changes were made")
152 .setStatus("Success")
153 ],
154 components: []
155 });
pineafan5d1908e2022-02-28 21:34:47 +0000156 }
pineafan63fc5e22022-08-04 22:04:10 +0100157};
pineafan4f164f32022-02-26 22:07:12 +0000158
pineafanbd02b4a2022-08-05 22:01:38 +0100159const check = (interaction: CommandInteraction) => {
Skyler Grey75ea9172022-08-06 10:22:23 +0100160 const member = interaction.member as GuildMember;
161 const me = interaction.guild.me!;
162 const apply = interaction.options.getMember("user") as GuildMember;
163 if (member === null || me === null || apply === null)
164 throw "That member is not in the server";
pineafan63fc5e22022-08-04 22:04:10 +0100165 const memberPos = member.roles ? member.roles.highest.position : 0;
166 const mePos = me.roles ? me.roles.highest.position : 0;
167 const applyPos = apply.roles ? apply.roles.highest.position : 0;
pineafanc1c18792022-08-03 21:41:36 +0100168 // Do not allow unmuting the owner
Skyler Grey75ea9172022-08-06 10:22:23 +0100169 if (member.id === interaction.guild.ownerId)
170 throw "You cannot unmute the owner of the server";
pineafan5d1908e2022-02-28 21:34:47 +0000171 // Check if Nucleus can unmute the member
Skyler Grey75ea9172022-08-06 10:22:23 +0100172 if (!(mePos > applyPos))
173 throw "I do not have a role higher than that member";
pineafan5d1908e2022-02-28 21:34:47 +0000174 // Check if Nucleus has permission to unmute
Skyler Grey75ea9172022-08-06 10:22:23 +0100175 if (!me.permissions.has("MODERATE_MEMBERS"))
176 throw "I do not have the *Moderate Members* permission";
pineafan5d1908e2022-02-28 21:34:47 +0000177 // Allow the owner to unmute anyone
pineafan63fc5e22022-08-04 22:04:10 +0100178 if (member.id === interaction.guild.ownerId) return true;
pineafan5d1908e2022-02-28 21:34:47 +0000179 // Check if the user has moderate_members permission
Skyler Grey75ea9172022-08-06 10:22:23 +0100180 if (!member.permissions.has("MODERATE_MEMBERS"))
181 throw "You do not have the *Moderate Members* permission";
pineafan5d1908e2022-02-28 21:34:47 +0000182 // Check if the user is below on the role list
Skyler Grey75ea9172022-08-06 10:22:23 +0100183 if (!(memberPos > applyPos))
184 throw "You do not have a role higher than that member";
pineafan5d1908e2022-02-28 21:34:47 +0000185 // Allow unmute
pineafan63fc5e22022-08-04 22:04:10 +0100186 return true;
187};
pineafan4f164f32022-02-26 22:07:12 +0000188
Skyler Grey75ea9172022-08-06 10:22:23 +0100189export { command, callback, check };