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 | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 14 | .addStringOption(option => option.setName("notify").setDescription("If the user should get a message when they are unmuted | Default: No").setRequired(false) |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 15 | .addChoices([["Yes", "yes"], ["No", "no"]]) |
| 16 | ) |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 17 | |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 18 | const callback = async (interaction: CommandInteraction): Promise<any> => { |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 19 | const { log, NucleusColors, renderUser, entry, renderDelta } = client.logger |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 20 | // TODO:[Modals] Replace this with a modal |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 21 | let reason = null; |
| 22 | let confirmation; |
| 23 | while (true) { |
| 24 | confirmation = await new confirmationMessage(interaction) |
| 25 | .setEmoji("PUNISH.MUTE.RED") |
| 26 | .setTitle("Unmute") |
| 27 | .setDescription(keyValueList({ |
| 28 | "user": renderUser(interaction.options.getUser("user")), |
| 29 | "reason": `\n> ${reason ? reason : "*No reason provided*"}` |
| 30 | }) |
| 31 | + `The user **will${interaction.options.getString("notify") === "yes" ? '' : ' not'}** be notified\n\n` |
| 32 | + `Are you sure you want to unmute <@!${(interaction.options.getMember("user") as GuildMember).id}>?`) |
| 33 | .setColor("Danger") |
| 34 | .addReasonButton(reason ?? "") |
| 35 | .send(reason !== null) |
| 36 | reason = reason ?? "" |
| 37 | if (confirmation.newReason === undefined) break |
| 38 | reason = confirmation.newReason |
| 39 | } |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 40 | if (confirmation.success) { |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 41 | let dmd = false |
| 42 | let dm; |
| 43 | try { |
| 44 | if (interaction.options.getString("notify") != "no") { |
| 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 | ] |
| 53 | }) |
| 54 | dmd = true |
| 55 | } |
| 56 | } catch {} |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 57 | let member = (interaction.options.getMember("user") as GuildMember) |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 58 | try { |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +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") |
| 63 | .setTitle(`Unmute`) |
| 64 | .setDescription("Something went wrong and the user was not unmuted") |
| 65 | .setStatus("Danger") |
| 66 | ], components: []}) |
| 67 | if (dmd) await dm.delete() |
| 68 | return |
| 69 | } |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 70 | try { await client.database.history.create("unmute", interaction.guild.id, (interaction.options.getMember("user") as GuildMember).user, interaction.user, reason) } catch {} |
| 71 | let data = { |
| 72 | meta: { |
| 73 | type: 'memberUnmute', |
| 74 | displayName: 'Unmuted', |
| 75 | calculateType: 'guildMemberPunish', |
| 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())), |
| 84 | unmutedBy: entry(interaction.user.id, renderUser(interaction.user)), |
| 85 | }, |
| 86 | hidden: { |
| 87 | guild: interaction.guild.id |
| 88 | } |
| 89 | } |
| 90 | log(data); |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 91 | let failed = (dmd == false && interaction.options.getString("notify") != "no") |
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"}`) |
| 94 | .setTitle(`Unmute`) |
| 95 | .setDescription("The member was unmuted" + (failed ? ", but could not be notified" : "")) |
| 96 | .setStatus(failed ? "Warning" : "Success") |
| 97 | ], components: []}) |
| 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") |
| 101 | .setTitle(`Unmute`) |
| 102 | .setDescription("No changes were made") |
| 103 | .setStatus("Success") |
| 104 | ], components: []}) |
| 105 | } |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => { |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 109 | let member = (interaction.member as GuildMember) |
| 110 | let me = (interaction.guild.me as GuildMember) |
| 111 | let apply = (interaction.options.getMember("user") as GuildMember) |
| 112 | if (member == null || me == null || apply == null) throw "That member is not in the server" |
| 113 | let memberPos = member.roles ? member.roles.highest.position : 0 |
| 114 | let mePos = me.roles ? me.roles.highest.position : 0 |
| 115 | let applyPos = apply.roles ? apply.roles.highest.position : 0 |
| 116 | // Check if Nucleus can unmute the member |
| 117 | if (! (mePos > applyPos)) throw "I do not have a role higher than that member" |
| 118 | // Check if Nucleus has permission to unmute |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 119 | 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] | 120 | // Do not allow the user to have admin or be the owner |
PineappleFan | 5fe720d | 2022-05-19 12:01:49 +0100 | [diff] [blame] | 121 | 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] | 122 | // Allow the owner to unmute anyone |
pineafan | 663dc47 | 2022-05-10 18:13:47 +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 | 4edb776 | 2022-06-26 19:21:04 +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 |
| 127 | if (! (memberPos > applyPos)) throw "You do not have a role higher than that member" |
| 128 | // Allow unmute |
| 129 | return true |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 130 | } |
| 131 | |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 132 | export { command, callback, check }; |