blob: 18363d5de5b05ed9755a1d7317734642a6f43df4 [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;
20 while (true) {
Skyler Grey75ea9172022-08-06 10:22:23 +010021 confirmation = await new confirmationMessage(interaction)
pineafan73a7c4a2022-07-24 10:38:04 +010022 .setEmoji("PUNISH.MUTE.RED")
23 .setTitle("Unmute")
Skyler Grey75ea9172022-08-06 10:22:23 +010024 .setDescription(
25 keyValueList({
26 user: renderUser(interaction.options.getUser("user")),
27 reason: `\n> ${reason ? reason : "*No reason provided*"}`
28 }) +
29 `The user **will${notify ? "" : " not"}** be notified\n\n` +
Skyler Grey11236ba2022-08-08 21:13:33 +010030 `Are you sure you want to unmute <@!${(interaction.options.getMember("user") as GuildMember).id}>?`
Skyler Grey75ea9172022-08-06 10:22:23 +010031 )
pineafan73a7c4a2022-07-24 10:38:04 +010032 .setColor("Danger")
33 .addReasonButton(reason ?? "")
pineafan63fc5e22022-08-04 22:04:10 +010034 .send(reason !== null);
35 if (confirmation.success) break;
36 if (confirmation.newReason) reason = confirmation.newReason;
pineafan02ba0232022-07-24 22:16:15 +010037 if (confirmation.components) {
pineafan63fc5e22022-08-04 22:04:10 +010038 notify = confirmation.components.notify.active;
pineafan02ba0232022-07-24 22:16:15 +010039 }
pineafan73a7c4a2022-07-24 10:38:04 +010040 }
pineafan63fc5e22022-08-04 22:04:10 +010041 if (confirmation.cancelled) return;
pineafan377794f2022-04-18 19:01:01 +010042 if (confirmation.success) {
pineafan63fc5e22022-08-04 22:04:10 +010043 let dmd = false;
pineafan5d1908e2022-02-28 21:34:47 +000044 let dm;
45 try {
pineafan02ba0232022-07-24 22:16:15 +010046 if (notify) {
Skyler Grey11236ba2022-08-08 21:13:33 +010047 dm = await (interaction.options.getMember("user") as GuildMember).send({
Skyler Grey75ea9172022-08-06 10:22:23 +010048 embeds: [
49 new EmojiEmbed()
50 .setEmoji("PUNISH.MUTE.GREEN")
51 .setTitle("Unmuted")
52 .setDescription(
53 `You have been unmuted in ${interaction.guild.name}` +
Skyler Grey11236ba2022-08-08 21:13:33 +010054 (reason ? ` for:\n> ${reason}` : " with no reason provided.")
Skyler Grey75ea9172022-08-06 10:22:23 +010055 )
56 .setStatus("Success")
pineafan5d1908e2022-02-28 21:34:47 +000057 ]
pineafan63fc5e22022-08-04 22:04:10 +010058 });
59 dmd = true;
pineafan5d1908e2022-02-28 21:34:47 +000060 }
Skyler Grey75ea9172022-08-06 10:22:23 +010061 } catch {
62 dmd = false;
63 }
64 const member = interaction.options.getMember("user") as GuildMember;
pineafan5d1908e2022-02-28 21:34:47 +000065 try {
pineafan3a02ea32022-08-11 21:35:04 +010066 member.timeout(0, reason ?? "No reason provided");
pineafan5d1908e2022-02-28 21:34:47 +000067 } catch {
Skyler Grey75ea9172022-08-06 10:22:23 +010068 await interaction.editReply({
69 embeds: [
70 new EmojiEmbed()
71 .setEmoji("PUNISH.MUTE.RED")
72 .setTitle("Unmute")
Skyler Grey11236ba2022-08-08 21:13:33 +010073 .setDescription("Something went wrong and the user was not unmuted")
Skyler Grey75ea9172022-08-06 10:22:23 +010074 .setStatus("Danger")
75 ],
76 components: []
77 });
pineafan63fc5e22022-08-04 22:04:10 +010078 if (dmd) await dm.delete();
79 return;
pineafan5d1908e2022-02-28 21:34:47 +000080 }
Skyler Grey75ea9172022-08-06 10:22:23 +010081 await client.database.history.create(
82 "unmute",
83 interaction.guild.id,
84 (interaction.options.getMember("user") as GuildMember).user,
85 interaction.user,
86 reason
87 );
pineafan63fc5e22022-08-04 22:04:10 +010088 const data = {
pineafan73a7c4a2022-07-24 10:38:04 +010089 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010090 type: "memberUnmute",
91 displayName: "Unmuted",
92 calculateType: "guildMemberPunish",
pineafan73a7c4a2022-07-24 10:38:04 +010093 color: NucleusColors.green,
94 emoji: "PUNISH.MUTE.GREEN",
95 timestamp: new Date().getTime()
96 },
97 list: {
98 memberId: entry(member.user.id, `\`${member.user.id}\``),
99 name: entry(member.user.id, renderUser(member.user)),
Skyler Grey11236ba2022-08-08 21:13:33 +0100100 unmuted: entry(new Date().getTime(), renderDelta(new Date().getTime())),
101 unmutedBy: entry(interaction.user.id, renderUser(interaction.user))
pineafan73a7c4a2022-07-24 10:38:04 +0100102 },
103 hidden: {
104 guild: interaction.guild.id
105 }
pineafan63fc5e22022-08-04 22:04:10 +0100106 };
pineafan73a7c4a2022-07-24 10:38:04 +0100107 log(data);
Skyler Grey75ea9172022-08-06 10:22:23 +0100108 const failed = !dmd && notify;
109 await interaction.editReply({
110 embeds: [
111 new EmojiEmbed()
112 .setEmoji(`PUNISH.MUTE.${failed ? "YELLOW" : "GREEN"}`)
113 .setTitle("Unmute")
Skyler Grey11236ba2022-08-08 21:13:33 +0100114 .setDescription("The member was unmuted" + (failed ? ", but could not be notified" : ""))
Skyler Grey75ea9172022-08-06 10:22:23 +0100115 .setStatus(failed ? "Warning" : "Success")
116 ],
117 components: []
118 });
pineafan5d1908e2022-02-28 21:34:47 +0000119 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +0100120 await interaction.editReply({
121 embeds: [
122 new EmojiEmbed()
123 .setEmoji("PUNISH.MUTE.GREEN")
124 .setTitle("Unmute")
125 .setDescription("No changes were made")
126 .setStatus("Success")
127 ],
128 components: []
129 });
pineafan5d1908e2022-02-28 21:34:47 +0000130 }
pineafan63fc5e22022-08-04 22:04:10 +0100131};
pineafan4f164f32022-02-26 22:07:12 +0000132
pineafanbd02b4a2022-08-05 22:01:38 +0100133const check = (interaction: CommandInteraction) => {
Skyler Grey75ea9172022-08-06 10:22:23 +0100134 const member = interaction.member as GuildMember;
135 const me = interaction.guild.me!;
136 const apply = interaction.options.getMember("user") as GuildMember;
pineafan3a02ea32022-08-11 21:35:04 +0100137 if (member === null || me === null || apply === null) throw new Error("That member is not in the server");
pineafan63fc5e22022-08-04 22:04:10 +0100138 const memberPos = member.roles ? member.roles.highest.position : 0;
139 const mePos = me.roles ? me.roles.highest.position : 0;
140 const applyPos = apply.roles ? apply.roles.highest.position : 0;
pineafanc1c18792022-08-03 21:41:36 +0100141 // Do not allow unmuting the owner
pineafan3a02ea32022-08-11 21:35:04 +0100142 if (member.id === interaction.guild.ownerId) throw new Error("You cannot unmute the owner of the server");
pineafan5d1908e2022-02-28 21:34:47 +0000143 // Check if Nucleus can unmute the member
pineafan3a02ea32022-08-11 21:35:04 +0100144 if (!(mePos > applyPos)) throw new Error("I do not have a role higher than that member");
pineafan5d1908e2022-02-28 21:34:47 +0000145 // Check if Nucleus has permission to unmute
pineafan3a02ea32022-08-11 21:35:04 +0100146 if (!me.permissions.has("MODERATE_MEMBERS")) throw new Error("I do not have the *Moderate Members* permission");
pineafan5d1908e2022-02-28 21:34:47 +0000147 // Allow the owner to unmute anyone
pineafan63fc5e22022-08-04 22:04:10 +0100148 if (member.id === interaction.guild.ownerId) return true;
pineafan5d1908e2022-02-28 21:34:47 +0000149 // Check if the user has moderate_members permission
pineafan3a02ea32022-08-11 21:35:04 +0100150 if (!member.permissions.has("MODERATE_MEMBERS"))
151 throw new Error("You do not have the *Moderate Members* permission");
pineafan5d1908e2022-02-28 21:34:47 +0000152 // Check if the user is below on the role list
pineafan3a02ea32022-08-11 21:35:04 +0100153 if (!(memberPos > applyPos)) throw new Error("You do not have a role higher than that member");
pineafan5d1908e2022-02-28 21:34:47 +0000154 // Allow unmute
pineafan63fc5e22022-08-04 22:04:10 +0100155 return true;
156};
pineafan4f164f32022-02-26 22:07:12 +0000157
Skyler Grey75ea9172022-08-06 10:22:23 +0100158export { command, callback, check };