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