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