PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame^] | 1 | import type { CommandInteraction, GuildMember } from "discord.js"; |
| 2 | import type { 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> => { |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame^] | 15 | if (!interaction.guild) return; |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 16 | const { log, NucleusColors, renderUser, entry, renderDelta } = client.logger; |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 17 | // TODO:[Modals] Replace this with a modal |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame^] | 18 | let reason: string | null = null; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 19 | let notify = false; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 20 | let confirmation; |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame^] | 21 | let timedOut = false; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 22 | let success = false; |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame^] | 23 | do { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 24 | confirmation = await new confirmationMessage(interaction) |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 25 | .setEmoji("PUNISH.MUTE.RED") |
| 26 | .setTitle("Unmute") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 27 | .setDescription( |
| 28 | keyValueList({ |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame^] | 29 | user: renderUser(interaction.options.getUser("user")!), |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 30 | reason: `\n> ${reason ? reason : "*No reason provided*"}` |
| 31 | }) + |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 32 | `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] | 33 | ) |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 34 | .setColor("Danger") |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame^] | 35 | .addCustomBoolean( |
| 36 | "notify", |
| 37 | "Notify user", |
| 38 | false, |
| 39 | null, |
| 40 | "The user will be sent a DM", |
| 41 | "ICONS.NOTIFY." + (notify ? "ON" : "OFF"), |
| 42 | notify |
| 43 | ) |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 44 | .addReasonButton(reason ?? "") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 45 | .send(reason !== null); |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame^] | 46 | if (confirmation.cancelled) timedOut = true; |
| 47 | else if (confirmation.success !== undefined) success = true; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 48 | else if (confirmation.newReason) reason = confirmation.newReason; |
| 49 | else if (confirmation.components) { |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame^] | 50 | notify = confirmation.components!["notify"]!.active; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 51 | } |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame^] | 52 | } while (!timedOut && !success); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 53 | if (confirmation.cancelled) return; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 54 | if (confirmation.success) { |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame^] | 55 | let dmSent = false; |
| 56 | let dmMessage; |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 57 | try { |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 58 | if (notify) { |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame^] | 59 | dmMessage = await (interaction.options.getMember("user") as GuildMember).send({ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 60 | embeds: [ |
| 61 | new EmojiEmbed() |
| 62 | .setEmoji("PUNISH.MUTE.GREEN") |
| 63 | .setTitle("Unmuted") |
| 64 | .setDescription( |
| 65 | `You have been unmuted in ${interaction.guild.name}` + |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 66 | (reason ? ` for:\n> ${reason}` : " with no reason provided.") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 67 | ) |
| 68 | .setStatus("Success") |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 69 | ] |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 70 | }); |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame^] | 71 | dmSent = true; |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 72 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 73 | } catch { |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame^] | 74 | dmSent = false; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 75 | } |
| 76 | const member = interaction.options.getMember("user") as GuildMember; |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 77 | try { |
PineaFan | 100df68 | 2023-01-02 13:26:08 +0000 | [diff] [blame] | 78 | member.timeout(0, reason ?? "*No reason provided*"); |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 79 | } catch { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 80 | await interaction.editReply({ |
| 81 | embeds: [ |
| 82 | new EmojiEmbed() |
| 83 | .setEmoji("PUNISH.MUTE.RED") |
| 84 | .setTitle("Unmute") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 85 | .setDescription("Something went wrong and the user was not unmuted") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 86 | .setStatus("Danger") |
| 87 | ], |
| 88 | components: [] |
| 89 | }); |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame^] | 90 | if (dmSent && dmMessage) await dmMessage.delete(); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 91 | return; |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 92 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 93 | await client.database.history.create( |
| 94 | "unmute", |
| 95 | interaction.guild.id, |
| 96 | (interaction.options.getMember("user") as GuildMember).user, |
| 97 | interaction.user, |
| 98 | reason |
| 99 | ); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 100 | const data = { |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 101 | meta: { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 102 | type: "memberUnmute", |
| 103 | displayName: "Unmuted", |
| 104 | calculateType: "guildMemberPunish", |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 105 | color: NucleusColors.green, |
| 106 | emoji: "PUNISH.MUTE.GREEN", |
| 107 | timestamp: new Date().getTime() |
| 108 | }, |
| 109 | list: { |
| 110 | memberId: entry(member.user.id, `\`${member.user.id}\``), |
| 111 | name: entry(member.user.id, renderUser(member.user)), |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame^] | 112 | unmuted: entry(new Date().getTime().toString(), renderDelta(new Date().getTime())), |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 113 | unmutedBy: entry(interaction.user.id, renderUser(interaction.user)) |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 114 | }, |
| 115 | hidden: { |
| 116 | guild: interaction.guild.id |
| 117 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 118 | }; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 119 | log(data); |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame^] | 120 | const failed = !dmSent && notify; |
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.${failed ? "YELLOW" : "GREEN"}`) |
| 125 | .setTitle("Unmute") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 126 | .setDescription("The member was unmuted" + (failed ? ", but could not be notified" : "")) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 127 | .setStatus(failed ? "Warning" : "Success") |
| 128 | ], |
| 129 | components: [] |
| 130 | }); |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 131 | } else { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 132 | await interaction.editReply({ |
| 133 | embeds: [ |
| 134 | new EmojiEmbed() |
| 135 | .setEmoji("PUNISH.MUTE.GREEN") |
| 136 | .setTitle("Unmute") |
| 137 | .setDescription("No changes were made") |
| 138 | .setStatus("Success") |
| 139 | ], |
| 140 | components: [] |
| 141 | }); |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 142 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 143 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 144 | |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 145 | const check = (interaction: CommandInteraction) => { |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame^] | 146 | if (!interaction.guild) return; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 147 | const member = interaction.member as GuildMember; |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame^] | 148 | const me = interaction.guild.members.me!; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 149 | const apply = interaction.options.getMember("user") as GuildMember; |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 150 | const memberPos = member.roles.cache.size > 1 ? member.roles.highest.position : 0; |
| 151 | const mePos = me.roles.cache.size > 1 ? me.roles.highest.position : 0; |
| 152 | const applyPos = apply.roles.cache.size > 1 ? apply.roles.highest.position : 0; |
pineafan | c1c1879 | 2022-08-03 21:41:36 +0100 | [diff] [blame] | 153 | // Do not allow unmuting the owner |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 154 | 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] | 155 | // Check if Nucleus can unmute the member |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 156 | 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] | 157 | // Check if Nucleus has permission to unmute |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame^] | 158 | if (!me.permissions.has("ModerateMembers")) throw new Error("I do not have the *Moderate Members* permission"); |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 159 | // Allow the owner to unmute anyone |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 160 | if (member.id === interaction.guild.ownerId) return true; |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 161 | // Check if the user has moderate_members permission |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame^] | 162 | if (!member.permissions.has("ModerateMembers")) |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 163 | throw new Error("You do not have the *Moderate Members* permission"); |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 164 | // Check if the user is below on the role list |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 165 | 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] | 166 | // Allow unmute |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 167 | return true; |
| 168 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 169 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 170 | export { command, callback, check }; |