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