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"; |
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", |
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) { |
| 60 | dmMessage = await (interaction.options.getMember("user") as GuildMember).send({ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 61 | embeds: [ |
| 62 | new EmojiEmbed() |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 63 | .setEmoji("PUNISH.MUTE.GREEN") |
| 64 | .setTitle("Unmuted") |
| 65 | .setDescription( |
| 66 | `You have been unmuted in ${interaction.guild.name}` + |
| 67 | (reason ? ` for:\n> ${reason}` : " with no reason provided.") |
| 68 | ) |
| 69 | .setStatus("Success") |
| 70 | ] |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 71 | }); |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 72 | dmSent = true; |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 73 | } |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 74 | } catch { |
| 75 | dmSent = false; |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 76 | } |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 77 | const member = interaction.options.getMember("user") as GuildMember; |
| 78 | try { |
| 79 | member.timeout(0, reason ?? "*No reason provided*"); |
| 80 | } catch { |
| 81 | await interaction.editReply({ |
| 82 | embeds: [ |
| 83 | new EmojiEmbed() |
| 84 | .setEmoji("PUNISH.MUTE.RED") |
| 85 | .setTitle("Unmute") |
| 86 | .setDescription("Something went wrong and the user was not unmuted") |
| 87 | .setStatus("Danger") |
| 88 | ], |
| 89 | components: [] |
| 90 | }); |
| 91 | if (dmSent && dmMessage) await dmMessage.delete(); |
| 92 | return; |
| 93 | } |
| 94 | await client.database.history.create( |
| 95 | "unmute", |
| 96 | interaction.guild.id, |
| 97 | (interaction.options.getMember("user") as GuildMember).user, |
| 98 | interaction.user, |
| 99 | reason |
| 100 | ); |
| 101 | const data = { |
| 102 | meta: { |
| 103 | type: "memberUnmute", |
| 104 | displayName: "Unmuted", |
| 105 | calculateType: "guildMemberPunish", |
| 106 | color: NucleusColors.green, |
| 107 | emoji: "PUNISH.MUTE.GREEN", |
| 108 | timestamp: new Date().getTime() |
| 109 | }, |
| 110 | list: { |
| 111 | memberId: entry(member.user.id, `\`${member.user.id}\``), |
| 112 | name: entry(member.user.id, renderUser(member.user)), |
| 113 | unmuted: entry(new Date().getTime().toString(), renderDelta(new Date().getTime())), |
| 114 | unmutedBy: entry(interaction.user.id, renderUser(interaction.user)) |
| 115 | }, |
| 116 | hidden: { |
| 117 | guild: interaction.guild.id |
| 118 | } |
| 119 | }; |
| 120 | log(data); |
| 121 | const failed = !dmSent && notify; |
| 122 | await interaction.editReply({ |
| 123 | embeds: [ |
| 124 | new EmojiEmbed() |
| 125 | .setEmoji(`PUNISH.MUTE.${failed ? "YELLOW" : "GREEN"}`) |
| 126 | .setTitle("Unmute") |
| 127 | .setDescription("The member was unmuted" + (failed ? ", but could not be notified" : "")) |
| 128 | .setStatus(failed ? "Warning" : "Success") |
| 129 | ], |
| 130 | components: [] |
| 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 | |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 134 | const check = (interaction: CommandInteraction, partial: boolean = false) => { |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 135 | if (!interaction.guild) return; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 136 | const member = interaction.member as GuildMember; |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 137 | // Check if the user has moderate_members permission |
| 138 | if (!member.permissions.has("ModerateMembers")) |
| 139 | return "You do not have the *Moderate Members* permission"; |
| 140 | if (partial) return true; |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 141 | const me = interaction.guild.members.me!; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 142 | const apply = interaction.options.getMember("user") as GuildMember; |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 143 | const memberPos = member.roles.cache.size > 1 ? member.roles.highest.position : 0; |
| 144 | const mePos = me.roles.cache.size > 1 ? me.roles.highest.position : 0; |
| 145 | const applyPos = apply.roles.cache.size > 1 ? apply.roles.highest.position : 0; |
pineafan | c1c1879 | 2022-08-03 21:41:36 +0100 | [diff] [blame] | 146 | // Do not allow unmuting the owner |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 147 | 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] | 148 | // Check if Nucleus can unmute the member |
TheCodedProf | 0941da4 | 2023-02-18 20:28:04 -0500 | [diff] [blame] | 149 | 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] | 150 | // Check if Nucleus has permission to unmute |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 151 | 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] | 152 | // Allow the owner to unmute anyone |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 153 | if (member.id === interaction.guild.ownerId) return true; |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 154 | // Check if the user is below on the role list |
TheCodedProf | 0941da4 | 2023-02-18 20:28:04 -0500 | [diff] [blame] | 155 | 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] | 156 | // Allow unmute |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 157 | return true; |
| 158 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 159 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 160 | export { command, callback, check }; |