pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 1 | import { CommandInteraction, GuildMember } from "discord.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 2 | import { SlashCommandSubcommandBuilder } from "@discordjs/builders"; |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 3 | import confirmationMessage from "../../utils/confirmationMessage.js"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 4 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 5 | import keyValueList from "../../utils/generateKeyValueList.js"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 6 | import client from "../../utils/client.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 7 | |
| 8 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 9 | builder |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 10 | .setName("unmute") |
| 11 | .setDescription("Unmutes a user") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 12 | .addUserOption((option) => option.setName("user").setDescription("The user to unmute").setRequired(true)); |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 13 | |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 14 | const callback = async (interaction: CommandInteraction): Promise<unknown> => { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 15 | const { log, NucleusColors, renderUser, entry, renderDelta } = client.logger; |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 16 | // TODO:[Modals] Replace this with a modal |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 17 | let reason = null; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 18 | let notify = false; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 19 | let confirmation; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 20 | let success = false; |
| 21 | while (!success) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 22 | confirmation = await new confirmationMessage(interaction) |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 23 | .setEmoji("PUNISH.MUTE.RED") |
| 24 | .setTitle("Unmute") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 25 | .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 Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 31 | `Are you sure you want to unmute <@!${(interaction.options.getMember("user") as GuildMember).id}>?` |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 32 | ) |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 33 | .setColor("Danger") |
| 34 | .addReasonButton(reason ?? "") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 35 | .send(reason !== null); |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 36 | if (confirmation.success) success = true; |
| 37 | else if (confirmation.newReason) reason = confirmation.newReason; |
| 38 | else if (confirmation.components) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 39 | notify = confirmation.components.notify.active; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 40 | } |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 41 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 42 | if (confirmation.cancelled) return; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 43 | if (confirmation.success) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 44 | let dmd = false; |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 45 | let dm; |
| 46 | try { |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 47 | if (notify) { |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 48 | dm = await (interaction.options.getMember("user") as GuildMember).send({ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 49 | embeds: [ |
| 50 | new EmojiEmbed() |
| 51 | .setEmoji("PUNISH.MUTE.GREEN") |
| 52 | .setTitle("Unmuted") |
| 53 | .setDescription( |
| 54 | `You have been unmuted in ${interaction.guild.name}` + |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 55 | (reason ? ` for:\n> ${reason}` : " with no reason provided.") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 56 | ) |
| 57 | .setStatus("Success") |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 58 | ] |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 59 | }); |
| 60 | dmd = true; |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 61 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 62 | } catch { |
| 63 | dmd = false; |
| 64 | } |
| 65 | const member = interaction.options.getMember("user") as GuildMember; |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 66 | try { |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 67 | member.timeout(0, reason ?? "No reason provided"); |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 68 | } catch { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 69 | await interaction.editReply({ |
| 70 | embeds: [ |
| 71 | new EmojiEmbed() |
| 72 | .setEmoji("PUNISH.MUTE.RED") |
| 73 | .setTitle("Unmute") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 74 | .setDescription("Something went wrong and the user was not unmuted") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 75 | .setStatus("Danger") |
| 76 | ], |
| 77 | components: [] |
| 78 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 79 | if (dmd) await dm.delete(); |
| 80 | return; |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 81 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 82 | 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 | ); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 89 | const data = { |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 90 | meta: { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 91 | type: "memberUnmute", |
| 92 | displayName: "Unmuted", |
| 93 | calculateType: "guildMemberPunish", |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 94 | 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 Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 101 | unmuted: entry(new Date().getTime(), renderDelta(new Date().getTime())), |
| 102 | unmutedBy: entry(interaction.user.id, renderUser(interaction.user)) |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 103 | }, |
| 104 | hidden: { |
| 105 | guild: interaction.guild.id |
| 106 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 107 | }; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 108 | log(data); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 109 | const failed = !dmd && notify; |
| 110 | await interaction.editReply({ |
| 111 | embeds: [ |
| 112 | new EmojiEmbed() |
| 113 | .setEmoji(`PUNISH.MUTE.${failed ? "YELLOW" : "GREEN"}`) |
| 114 | .setTitle("Unmute") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 115 | .setDescription("The member was unmuted" + (failed ? ", but could not be notified" : "")) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 116 | .setStatus(failed ? "Warning" : "Success") |
| 117 | ], |
| 118 | components: [] |
| 119 | }); |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 120 | } else { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 121 | 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 | }); |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 131 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 132 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 133 | |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 134 | const check = (interaction: CommandInteraction) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 135 | const member = interaction.member as GuildMember; |
| 136 | const me = interaction.guild.me!; |
| 137 | const apply = interaction.options.getMember("user") as GuildMember; |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 138 | if (member === null || me === null || apply === null) throw new Error("That member is not in the server"); |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 139 | const memberPos = member.roles.cache.size > 1 ? member.roles.highest.position : 0; |
| 140 | const mePos = me.roles.cache.size > 1 ? me.roles.highest.position : 0; |
| 141 | const applyPos = apply.roles.cache.size > 1 ? apply.roles.highest.position : 0; |
pineafan | c1c1879 | 2022-08-03 21:41:36 +0100 | [diff] [blame] | 142 | // Do not allow unmuting the owner |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 143 | if (member.id === interaction.guild.ownerId) throw new Error("You cannot unmute the owner of the server"); |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 144 | // Check if Nucleus can unmute the member |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 145 | if (!(mePos > applyPos)) throw new Error("I do not have a role higher than that member"); |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 146 | // Check if Nucleus has permission to unmute |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 147 | if (!me.permissions.has("MODERATE_MEMBERS")) throw new Error("I do not have the *Moderate Members* permission"); |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 148 | // Allow the owner to unmute anyone |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 149 | if (member.id === interaction.guild.ownerId) return true; |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 150 | // Check if the user has moderate_members permission |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 151 | if (!member.permissions.has("MODERATE_MEMBERS")) |
| 152 | throw new Error("You do not have the *Moderate Members* permission"); |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 153 | // Check if the user is below on the role list |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 154 | if (!(memberPos > applyPos)) throw new Error("You do not have a role higher than that member"); |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 155 | // Allow unmute |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 156 | return true; |
| 157 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 158 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 159 | export { command, callback, check }; |