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