blob: fbe6d66e9dcbb99e889512e8b09842d26216d897 [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 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> => {
Skyler Grey11236ba2022-08-08 21:13:33 +010015 const { log, NucleusColors, renderUser, entry, renderDelta } = client.logger;
pineafan5d1908e2022-02-28 21:34:47 +000016 // TODO:[Modals] Replace this with a modal
pineafan73a7c4a2022-07-24 10:38:04 +010017 let reason = null;
pineafan02ba0232022-07-24 22:16:15 +010018 let notify = false;
pineafan73a7c4a2022-07-24 10:38:04 +010019 let confirmation;
Skyler Greyad002172022-08-16 18:48:26 +010020 let success = false;
21 while (!success) {
Skyler Grey75ea9172022-08-06 10:22:23 +010022 confirmation = await new confirmationMessage(interaction)
pineafan73a7c4a2022-07-24 10:38:04 +010023 .setEmoji("PUNISH.MUTE.RED")
24 .setTitle("Unmute")
Skyler Grey75ea9172022-08-06 10:22:23 +010025 .setDescription(
26 keyValueList({
27 user: renderUser(interaction.options.getUser("user")),
28 reason: `\n> ${reason ? reason : "*No reason provided*"}`
29 }) +
30 `The user **will${notify ? "" : " not"}** be notified\n\n` +
Skyler Grey11236ba2022-08-08 21:13:33 +010031 `Are you sure you want to unmute <@!${(interaction.options.getMember("user") as GuildMember).id}>?`
Skyler Grey75ea9172022-08-06 10:22:23 +010032 )
pineafan73a7c4a2022-07-24 10:38:04 +010033 .setColor("Danger")
34 .addReasonButton(reason ?? "")
pineafan63fc5e22022-08-04 22:04:10 +010035 .send(reason !== null);
Skyler Greyad002172022-08-16 18:48:26 +010036 if (confirmation.success) success = true;
37 else if (confirmation.newReason) reason = confirmation.newReason;
38 else if (confirmation.components) {
pineafan63fc5e22022-08-04 22:04:10 +010039 notify = confirmation.components.notify.active;
pineafan02ba0232022-07-24 22:16:15 +010040 }
pineafan73a7c4a2022-07-24 10:38:04 +010041 }
pineafan63fc5e22022-08-04 22:04:10 +010042 if (confirmation.cancelled) return;
pineafan377794f2022-04-18 19:01:01 +010043 if (confirmation.success) {
pineafan63fc5e22022-08-04 22:04:10 +010044 let dmd = false;
pineafan5d1908e2022-02-28 21:34:47 +000045 let dm;
46 try {
pineafan02ba0232022-07-24 22:16:15 +010047 if (notify) {
Skyler Grey11236ba2022-08-08 21:13:33 +010048 dm = await (interaction.options.getMember("user") as GuildMember).send({
Skyler Grey75ea9172022-08-06 10:22:23 +010049 embeds: [
50 new EmojiEmbed()
51 .setEmoji("PUNISH.MUTE.GREEN")
52 .setTitle("Unmuted")
53 .setDescription(
54 `You have been unmuted in ${interaction.guild.name}` +
Skyler Grey11236ba2022-08-08 21:13:33 +010055 (reason ? ` for:\n> ${reason}` : " with no reason provided.")
Skyler Grey75ea9172022-08-06 10:22:23 +010056 )
57 .setStatus("Success")
pineafan5d1908e2022-02-28 21:34:47 +000058 ]
pineafan63fc5e22022-08-04 22:04:10 +010059 });
60 dmd = true;
pineafan5d1908e2022-02-28 21:34:47 +000061 }
Skyler Grey75ea9172022-08-06 10:22:23 +010062 } catch {
63 dmd = false;
64 }
65 const member = interaction.options.getMember("user") as GuildMember;
pineafan5d1908e2022-02-28 21:34:47 +000066 try {
pineafan3a02ea32022-08-11 21:35:04 +010067 member.timeout(0, reason ?? "No reason provided");
pineafan5d1908e2022-02-28 21:34:47 +000068 } catch {
Skyler Grey75ea9172022-08-06 10:22:23 +010069 await interaction.editReply({
70 embeds: [
71 new EmojiEmbed()
72 .setEmoji("PUNISH.MUTE.RED")
73 .setTitle("Unmute")
Skyler Grey11236ba2022-08-08 21:13:33 +010074 .setDescription("Something went wrong and the user was not unmuted")
Skyler Grey75ea9172022-08-06 10:22:23 +010075 .setStatus("Danger")
76 ],
77 components: []
78 });
pineafan63fc5e22022-08-04 22:04:10 +010079 if (dmd) await dm.delete();
80 return;
pineafan5d1908e2022-02-28 21:34:47 +000081 }
Skyler Grey75ea9172022-08-06 10:22:23 +010082 await client.database.history.create(
83 "unmute",
84 interaction.guild.id,
85 (interaction.options.getMember("user") as GuildMember).user,
86 interaction.user,
87 reason
88 );
pineafan63fc5e22022-08-04 22:04:10 +010089 const data = {
pineafan73a7c4a2022-07-24 10:38:04 +010090 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010091 type: "memberUnmute",
92 displayName: "Unmuted",
93 calculateType: "guildMemberPunish",
pineafan73a7c4a2022-07-24 10:38:04 +010094 color: NucleusColors.green,
95 emoji: "PUNISH.MUTE.GREEN",
96 timestamp: new Date().getTime()
97 },
98 list: {
99 memberId: entry(member.user.id, `\`${member.user.id}\``),
100 name: entry(member.user.id, renderUser(member.user)),
Skyler Grey11236ba2022-08-08 21:13:33 +0100101 unmuted: entry(new Date().getTime(), renderDelta(new Date().getTime())),
102 unmutedBy: entry(interaction.user.id, renderUser(interaction.user))
pineafan73a7c4a2022-07-24 10:38:04 +0100103 },
104 hidden: {
105 guild: interaction.guild.id
106 }
pineafan63fc5e22022-08-04 22:04:10 +0100107 };
pineafan73a7c4a2022-07-24 10:38:04 +0100108 log(data);
Skyler Grey75ea9172022-08-06 10:22:23 +0100109 const failed = !dmd && notify;
110 await interaction.editReply({
111 embeds: [
112 new EmojiEmbed()
113 .setEmoji(`PUNISH.MUTE.${failed ? "YELLOW" : "GREEN"}`)
114 .setTitle("Unmute")
Skyler Grey11236ba2022-08-08 21:13:33 +0100115 .setDescription("The member was unmuted" + (failed ? ", but could not be notified" : ""))
Skyler Grey75ea9172022-08-06 10:22:23 +0100116 .setStatus(failed ? "Warning" : "Success")
117 ],
118 components: []
119 });
pineafan5d1908e2022-02-28 21:34:47 +0000120 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +0100121 await interaction.editReply({
122 embeds: [
123 new EmojiEmbed()
124 .setEmoji("PUNISH.MUTE.GREEN")
125 .setTitle("Unmute")
126 .setDescription("No changes were made")
127 .setStatus("Success")
128 ],
129 components: []
130 });
pineafan5d1908e2022-02-28 21:34:47 +0000131 }
pineafan63fc5e22022-08-04 22:04:10 +0100132};
pineafan4f164f32022-02-26 22:07:12 +0000133
pineafanbd02b4a2022-08-05 22:01:38 +0100134const check = (interaction: CommandInteraction) => {
Skyler Grey75ea9172022-08-06 10:22:23 +0100135 const member = interaction.member as GuildMember;
136 const me = interaction.guild.me!;
137 const apply = interaction.options.getMember("user") as GuildMember;
pineafan3a02ea32022-08-11 21:35:04 +0100138 if (member === null || me === null || apply === null) throw new Error("That member is not in the server");
pineafan63fc5e22022-08-04 22:04:10 +0100139 const memberPos = member.roles ? member.roles.highest.position : 0;
140 const mePos = me.roles ? me.roles.highest.position : 0;
141 const applyPos = apply.roles ? apply.roles.highest.position : 0;
pineafanc1c18792022-08-03 21:41:36 +0100142 // Do not allow unmuting the owner
pineafan3a02ea32022-08-11 21:35:04 +0100143 if (member.id === interaction.guild.ownerId) throw new Error("You cannot unmute the owner of the server");
pineafan5d1908e2022-02-28 21:34:47 +0000144 // Check if Nucleus can unmute the member
pineafan3a02ea32022-08-11 21:35:04 +0100145 if (!(mePos > applyPos)) throw new Error("I do not have a role higher than that member");
pineafan5d1908e2022-02-28 21:34:47 +0000146 // Check if Nucleus has permission to unmute
pineafan3a02ea32022-08-11 21:35:04 +0100147 if (!me.permissions.has("MODERATE_MEMBERS")) throw new Error("I do not have the *Moderate Members* permission");
pineafan5d1908e2022-02-28 21:34:47 +0000148 // Allow the owner to unmute anyone
pineafan63fc5e22022-08-04 22:04:10 +0100149 if (member.id === interaction.guild.ownerId) return true;
pineafan5d1908e2022-02-28 21:34:47 +0000150 // Check if the user has moderate_members permission
pineafan3a02ea32022-08-11 21:35:04 +0100151 if (!member.permissions.has("MODERATE_MEMBERS"))
152 throw new Error("You do not have the *Moderate Members* permission");
pineafan5d1908e2022-02-28 21:34:47 +0000153 // Check if the user is below on the role list
pineafan3a02ea32022-08-11 21:35:04 +0100154 if (!(memberPos > applyPos)) throw new Error("You do not have a role higher than that member");
pineafan5d1908e2022-02-28 21:34:47 +0000155 // Allow unmute
pineafan63fc5e22022-08-04 22:04:10 +0100156 return true;
157};
pineafan4f164f32022-02-26 22:07:12 +0000158
Skyler Grey75ea9172022-08-06 10:22:23 +0100159export { command, callback, check };