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