blob: e00c16d03b5aef5fdfa170604c951528d22bab85 [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";
3import { WrappedCheck } from "jshaiku";
pineafan5d1908e2022-02-28 21:34:47 +00004import confirmationMessage from "../../utils/confirmationMessage.js";
pineafan4edb7762022-06-26 19:21:04 +01005import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
pineafan5d1908e2022-02-28 21:34:47 +00006import keyValueList from "../../utils/generateKeyValueList.js";
pineafan4edb7762022-06-26 19:21:04 +01007import client from "../../utils/client.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")
13 .addUserOption(option => option.setName("user").setDescription("The user to unmute").setRequired(true));
pineafan4f164f32022-02-26 22:07:12 +000014
pineafan4edb7762022-06-26 19:21:04 +010015const callback = async (interaction: CommandInteraction): Promise<any> => {
pineafan63fc5e22022-08-04 22:04:10 +010016 const { log, NucleusColors, renderUser, entry, renderDelta } = client.logger;
pineafan5d1908e2022-02-28 21:34:47 +000017 // TODO:[Modals] Replace this with a modal
pineafan73a7c4a2022-07-24 10:38:04 +010018 let reason = null;
pineafan02ba0232022-07-24 22:16:15 +010019 let notify = false;
pineafan73a7c4a2022-07-24 10:38:04 +010020 let confirmation;
21 while (true) {
22 confirmation = await new confirmationMessage(interaction)
23 .setEmoji("PUNISH.MUTE.RED")
24 .setTitle("Unmute")
25 .setDescription(keyValueList({
26 "user": renderUser(interaction.options.getUser("user")),
27 "reason": `\n> ${reason ? reason : "*No reason provided*"}`
28 })
pineafan63fc5e22022-08-04 22:04:10 +010029 + `The user **will${notify ? "" : " not"}** be notified\n\n`
pineafan73a7c4a2022-07-24 10:38:04 +010030 + `Are you sure you want to unmute <@!${(interaction.options.getMember("user") as GuildMember).id}>?`)
31 .setColor("Danger")
32 .addReasonButton(reason ?? "")
pineafan63fc5e22022-08-04 22:04:10 +010033 .send(reason !== null);
34 if (confirmation.success) break;
35 if (confirmation.newReason) reason = confirmation.newReason;
pineafan02ba0232022-07-24 22:16:15 +010036 if (confirmation.components) {
pineafan63fc5e22022-08-04 22:04:10 +010037 notify = confirmation.components.notify.active;
pineafan02ba0232022-07-24 22:16:15 +010038 }
pineafan73a7c4a2022-07-24 10:38:04 +010039 }
pineafan63fc5e22022-08-04 22:04:10 +010040 if (confirmation.cancelled) return;
pineafan377794f2022-04-18 19:01:01 +010041 if (confirmation.success) {
pineafan63fc5e22022-08-04 22:04:10 +010042 let dmd = false;
pineafan5d1908e2022-02-28 21:34:47 +000043 let dm;
44 try {
pineafan02ba0232022-07-24 22:16:15 +010045 if (notify) {
pineafan5d1908e2022-02-28 21:34:47 +000046 dm = await (interaction.options.getMember("user") as GuildMember).send({
pineafan4edb7762022-06-26 19:21:04 +010047 embeds: [new EmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +000048 .setEmoji("PUNISH.MUTE.GREEN")
49 .setTitle("Unmuted")
50 .setDescription(`You have been unmuted in ${interaction.guild.name}` +
pineafan73a7c4a2022-07-24 10:38:04 +010051 (reason ? ` for:\n> ${reason}` : " with no reason provided."))
pineafan5d1908e2022-02-28 21:34:47 +000052 .setStatus("Success")
53 ]
pineafan63fc5e22022-08-04 22:04:10 +010054 });
55 dmd = true;
pineafan5d1908e2022-02-28 21:34:47 +000056 }
pineafan63fc5e22022-08-04 22:04:10 +010057 } catch { dmd = false; }
58 const member = (interaction.options.getMember("user") as GuildMember);
pineafan5d1908e2022-02-28 21:34:47 +000059 try {
pineafan63fc5e22022-08-04 22:04:10 +010060 member.timeout(0, reason || "No reason provided");
pineafan5d1908e2022-02-28 21:34:47 +000061 } catch {
pineafan4edb7762022-06-26 19:21:04 +010062 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +000063 .setEmoji("PUNISH.MUTE.RED")
pineafan63fc5e22022-08-04 22:04:10 +010064 .setTitle("Unmute")
pineafan5d1908e2022-02-28 21:34:47 +000065 .setDescription("Something went wrong and the user was not unmuted")
66 .setStatus("Danger")
pineafan63fc5e22022-08-04 22:04:10 +010067 ], components: []});
68 if (dmd) await dm.delete();
69 return;
pineafan5d1908e2022-02-28 21:34:47 +000070 }
pineafan63fc5e22022-08-04 22:04:10 +010071 await client.database.history.create("unmute", interaction.guild.id, (interaction.options.getMember("user") as GuildMember).user, interaction.user, reason);
72 const data = {
pineafan73a7c4a2022-07-24 10:38:04 +010073 meta: {
pineafan63fc5e22022-08-04 22:04:10 +010074 type: "memberUnmute",
75 displayName: "Unmuted",
76 calculateType: "guildMemberPunish",
pineafan73a7c4a2022-07-24 10:38:04 +010077 color: NucleusColors.green,
78 emoji: "PUNISH.MUTE.GREEN",
79 timestamp: new Date().getTime()
80 },
81 list: {
82 memberId: entry(member.user.id, `\`${member.user.id}\``),
83 name: entry(member.user.id, renderUser(member.user)),
84 unmuted: entry(new Date().getTime(), renderDelta(new Date().getTime())),
pineafan63fc5e22022-08-04 22:04:10 +010085 unmutedBy: entry(interaction.user.id, renderUser(interaction.user))
pineafan73a7c4a2022-07-24 10:38:04 +010086 },
87 hidden: {
88 guild: interaction.guild.id
89 }
pineafan63fc5e22022-08-04 22:04:10 +010090 };
pineafan73a7c4a2022-07-24 10:38:04 +010091 log(data);
pineafan63fc5e22022-08-04 22:04:10 +010092 const failed = (dmd === false && notify);
pineafan4edb7762022-06-26 19:21:04 +010093 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +000094 .setEmoji(`PUNISH.MUTE.${failed ? "YELLOW" : "GREEN"}`)
pineafan63fc5e22022-08-04 22:04:10 +010095 .setTitle("Unmute")
pineafan5d1908e2022-02-28 21:34:47 +000096 .setDescription("The member was unmuted" + (failed ? ", but could not be notified" : ""))
97 .setStatus(failed ? "Warning" : "Success")
pineafan63fc5e22022-08-04 22:04:10 +010098 ], components: []});
pineafan5d1908e2022-02-28 21:34:47 +000099 } else {
pineafan4edb7762022-06-26 19:21:04 +0100100 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan5d1908e2022-02-28 21:34:47 +0000101 .setEmoji("PUNISH.MUTE.GREEN")
pineafan63fc5e22022-08-04 22:04:10 +0100102 .setTitle("Unmute")
pineafan5d1908e2022-02-28 21:34:47 +0000103 .setDescription("No changes were made")
104 .setStatus("Success")
pineafan63fc5e22022-08-04 22:04:10 +0100105 ], components: []});
pineafan5d1908e2022-02-28 21:34:47 +0000106 }
pineafan63fc5e22022-08-04 22:04:10 +0100107};
pineafan4f164f32022-02-26 22:07:12 +0000108
109const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan63fc5e22022-08-04 22:04:10 +0100110 const member = (interaction.member as GuildMember);
111 const me = (interaction.guild.me as GuildMember);
112 const apply = (interaction.options.getMember("user") as GuildMember);
113 if (member === null || me === null || apply === null) throw "That member is not in the server";
114 const memberPos = member.roles ? member.roles.highest.position : 0;
115 const mePos = me.roles ? me.roles.highest.position : 0;
116 const applyPos = apply.roles ? apply.roles.highest.position : 0;
pineafanc1c18792022-08-03 21:41:36 +0100117 // Do not allow unmuting the owner
pineafan63fc5e22022-08-04 22:04:10 +0100118 if (member.id === interaction.guild.ownerId) throw "You cannot unmute the owner of the server";
pineafan5d1908e2022-02-28 21:34:47 +0000119 // Check if Nucleus can unmute the member
pineafan63fc5e22022-08-04 22:04:10 +0100120 if (! (mePos > applyPos)) throw "I do not have a role higher than that member";
pineafan5d1908e2022-02-28 21:34:47 +0000121 // Check if Nucleus has permission to unmute
pineafane23c4ec2022-07-27 21:56:27 +0100122 if (! me.permissions.has("MODERATE_MEMBERS")) throw "I do not have the *Moderate Members* permission";
pineafan5d1908e2022-02-28 21:34:47 +0000123 // Allow the owner to unmute anyone
pineafan63fc5e22022-08-04 22:04:10 +0100124 if (member.id === interaction.guild.ownerId) return true;
pineafan5d1908e2022-02-28 21:34:47 +0000125 // Check if the user has moderate_members permission
pineafane23c4ec2022-07-27 21:56:27 +0100126 if (! member.permissions.has("MODERATE_MEMBERS")) throw "You do not have the *Moderate Members* permission";
pineafan5d1908e2022-02-28 21:34:47 +0000127 // Check if the user is below on the role list
pineafan63fc5e22022-08-04 22:04:10 +0100128 if (! (memberPos > applyPos)) throw "You do not have a role higher than that member";
pineafan5d1908e2022-02-28 21:34:47 +0000129 // Allow unmute
pineafan63fc5e22022-08-04 22:04:10 +0100130 return true;
131};
pineafan4f164f32022-02-26 22:07:12 +0000132
pineafan8b4b17f2022-02-27 20:42:52 +0000133export { command, callback, check };