pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 1 | import { LoadingEmbed } from "./../../utils/defaultEmbeds.js"; |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 2 | import Discord, { CommandInteraction, GuildMember, Message, MessageActionRow, MessageButton } from "discord.js"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 3 | import { SlashCommandSubcommandBuilder } from "@discordjs/builders"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 4 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 5 | import getEmojiByName from "../../utils/getEmojiByName.js"; |
| 6 | import confirmationMessage from "../../utils/confirmationMessage.js"; |
| 7 | import keyValueList from "../../utils/generateKeyValueList.js"; |
| 8 | import humanizeDuration from "humanize-duration"; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 9 | import client from "../../utils/client.js"; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 10 | import { areTicketsEnabled, create } from "../../actions/createModActionTicket.js"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 11 | |
| 12 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 13 | builder |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 14 | .setName("mute") |
| 15 | .setDescription("Mutes a member, stopping them from talking in the server") |
| 16 | .addUserOption(option => option.setName("user").setDescription("The user to mute").setRequired(true)) |
| 17 | .addIntegerOption(option => option.setName("days").setDescription("The number of days to mute the user for | Default: 0").setMinValue(0).setMaxValue(27).setRequired(false)) |
| 18 | .addIntegerOption(option => option.setName("hours").setDescription("The number of hours to mute the user for | Default: 0").setMinValue(0).setMaxValue(23).setRequired(false)) |
| 19 | .addIntegerOption(option => option.setName("minutes").setDescription("The number of minutes to mute the user for | Default: 0").setMinValue(0).setMaxValue(59).setRequired(false)) |
| 20 | .addIntegerOption(option => option.setName("seconds").setDescription("The number of seconds to mute the user for | Default: 0").setMinValue(0).setMaxValue(59).setRequired(false)); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 21 | |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 22 | const callback = async (interaction: CommandInteraction): Promise<void | unknown> => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 23 | const { log, NucleusColors, renderUser, entry, renderDelta } = client.logger; |
| 24 | const user = interaction.options.getMember("user") as GuildMember; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 25 | const time = { |
| 26 | days: interaction.options.getInteger("days") || 0, |
| 27 | hours: interaction.options.getInteger("hours") || 0, |
| 28 | minutes: interaction.options.getInteger("minutes") || 0, |
| 29 | seconds: interaction.options.getInteger("seconds") || 0 |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 30 | }; |
| 31 | const config = await client.database.guilds.read(interaction.guild.id); |
| 32 | let serverSettingsDescription = (config.moderation.mute.timeout ? "given a timeout" : ""); |
| 33 | if (config.moderation.mute.role) serverSettingsDescription += (serverSettingsDescription ? " and " : "") + `given the <@&${config.moderation.mute.role}> role`; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 34 | |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 35 | let muteTime = (time.days * 24 * 60 * 60) + (time.hours * 60 * 60) + (time.minutes * 60) + time.seconds; |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 36 | if (muteTime === 0) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 37 | const m = await interaction.reply({embeds: [ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 38 | new EmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 39 | .setEmoji("PUNISH.MUTE.GREEN") |
| 40 | .setTitle("Mute") |
| 41 | .setDescription("How long should the user be muted") |
| 42 | .setStatus("Success") |
| 43 | ], components: [ |
| 44 | new MessageActionRow().addComponents([ |
| 45 | new Discord.MessageButton() |
| 46 | .setCustomId("1m") |
| 47 | .setLabel("1 Minute") |
| 48 | .setStyle("SECONDARY"), |
| 49 | new Discord.MessageButton() |
| 50 | .setCustomId("10m") |
| 51 | .setLabel("10 Minutes") |
| 52 | .setStyle("SECONDARY"), |
| 53 | new Discord.MessageButton() |
| 54 | .setCustomId("30m") |
| 55 | .setLabel("30 Minutes") |
| 56 | .setStyle("SECONDARY"), |
| 57 | new Discord.MessageButton() |
| 58 | .setCustomId("1h") |
| 59 | .setLabel("1 Hour") |
| 60 | .setStyle("SECONDARY") |
| 61 | ]), |
| 62 | new MessageActionRow().addComponents([ |
| 63 | new Discord.MessageButton() |
| 64 | .setCustomId("6h") |
| 65 | .setLabel("6 Hours") |
| 66 | .setStyle("SECONDARY"), |
| 67 | new Discord.MessageButton() |
| 68 | .setCustomId("12h") |
| 69 | .setLabel("12 Hours") |
| 70 | .setStyle("SECONDARY"), |
| 71 | new Discord.MessageButton() |
| 72 | .setCustomId("1d") |
| 73 | .setLabel("1 Day") |
| 74 | .setStyle("SECONDARY"), |
| 75 | new Discord.MessageButton() |
| 76 | .setCustomId("1w") |
| 77 | .setLabel("1 Week") |
| 78 | .setStyle("SECONDARY") |
| 79 | ]), |
| 80 | new MessageActionRow().addComponents([ |
| 81 | new Discord.MessageButton() |
| 82 | .setCustomId("cancel") |
| 83 | .setLabel("Cancel") |
| 84 | .setStyle("DANGER") |
| 85 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
| 86 | ]) |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 87 | ], ephemeral: true, fetchReply: true}) as Message; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 88 | let component; |
| 89 | try { |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 90 | component = await m.awaitMessageComponent({filter: (m) => m.user.id === interaction.user.id, time: 300000}); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 91 | } catch { return; } |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 92 | component.deferUpdate(); |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 93 | if (component.customId === "cancel") return interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 94 | .setEmoji("PUNISH.MUTE.RED") |
| 95 | .setTitle("Mute") |
| 96 | .setDescription("Mute cancelled") |
| 97 | .setStatus("Danger") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 98 | ]}); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 99 | switch (component.customId) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 100 | case "1m": { muteTime = 60; break; } |
| 101 | case "10m": { muteTime = 60 * 10; break; } |
| 102 | case "30m": { muteTime = 60 * 30; break; } |
| 103 | case "1h": { muteTime = 60 * 60; break; } |
| 104 | case "6h": { muteTime = 60 * 60 * 6; break; } |
| 105 | case "12h": { muteTime = 60 * 60 * 12; break; } |
| 106 | case "1d": { muteTime = 60 * 60 * 24; break; } |
| 107 | case "1w": { muteTime = 60 * 60 * 24 * 7; break; } |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 108 | } |
| 109 | } else { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 110 | await interaction.reply({embeds: LoadingEmbed, ephemeral: true, fetchReply: true}); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 111 | } |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 112 | // TODO:[Modals] Replace this with a modal |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 113 | let reason = null; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 114 | let notify = true; |
| 115 | let createAppealTicket = false; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 116 | let confirmation; |
| 117 | while (true) { |
| 118 | confirmation = await new confirmationMessage(interaction) |
| 119 | .setEmoji("PUNISH.MUTE.RED") |
| 120 | .setTitle("Mute") |
| 121 | .setDescription(keyValueList({ |
| 122 | "user": renderUser(user.user), |
| 123 | "time": `${humanizeDuration(muteTime * 1000, {round: true})}`, |
| 124 | "reason": reason ? ("\n> " + ((reason ?? "").replaceAll("\n", "\n> "))) : "*No reason provided*" |
| 125 | }) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 126 | + "The user will be " + serverSettingsDescription + "\n" |
| 127 | + `The user **will${notify ? "" : " not"}** be notified\n\n` |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 128 | + `Are you sure you want to mute <@!${user.id}>?`) |
| 129 | .setColor("Danger") |
| 130 | .addCustomBoolean( |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 131 | "appeal", "Create appeal ticket", !(await areTicketsEnabled(interaction.guild.id)), |
| 132 | async () => await create(interaction.guild, interaction.options.getUser("user"), interaction.user, reason), |
| 133 | "An appeal ticket will be created when Confirm is clicked", "CONTROL.TICKET", createAppealTicket) |
| 134 | .addCustomBoolean("notify", "Notify user", false, null, null, "ICONS.NOTIFY." + (notify ? "ON" : "OFF" ), notify) |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 135 | .addReasonButton(reason ?? "") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 136 | .send(true); |
| 137 | reason = reason ?? ""; |
| 138 | if (confirmation.cancelled) return; |
| 139 | if (confirmation.success) break; |
| 140 | if (confirmation.newReason) reason = confirmation.newReason; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 141 | if (confirmation.components) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 142 | notify = confirmation.components.notify.active; |
| 143 | createAppealTicket = confirmation.components.appeal.active; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 144 | } |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 145 | } |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 146 | if (confirmation.success) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 147 | let dmd = false; |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 148 | let dm; |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 149 | const config = await client.database.guilds.read(interaction.guild.id); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 150 | try { |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 151 | if (notify) { |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 152 | dm = await user.send({ |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 153 | embeds: [new EmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 154 | .setEmoji("PUNISH.MUTE.RED") |
| 155 | .setTitle("Muted") |
| 156 | .setDescription(`You have been muted in ${interaction.guild.name}` + |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 157 | (reason ? ` for:\n> ${reason}` : ".\n\n" + |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 158 | `You will be unmuted at: <t:${Math.round((new Date).getTime() / 1000) + muteTime}:D> at <t:${Math.round((new Date).getTime() / 1000) + muteTime}:T> (<t:${Math.round((new Date).getTime() / 1000) + muteTime}:R>)`) + |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 159 | (confirmation.components.appeal.response ? `You can appeal this here: <#${confirmation.components.appeal.response}>` : "")) |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 160 | .setStatus("Danger") |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 161 | ], |
| 162 | components: [new MessageActionRow().addComponents(config.moderation.mute.text ? [new MessageButton() |
| 163 | .setStyle("LINK") |
| 164 | .setLabel(config.moderation.mute.text) |
| 165 | .setURL(config.moderation.mute.link) |
| 166 | ] : [])] |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 167 | }); |
| 168 | dmd = true; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 169 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 170 | } catch { dmd = false; } |
| 171 | const member = user; |
| 172 | let errors = 0; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 173 | try { |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 174 | if (config.moderation.mute.timeout) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 175 | await member.timeout(muteTime * 1000, reason || "No reason provided"); |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 176 | if (config.moderation.mute.role !== null) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 177 | await member.roles.add(config.moderation.mute.role); |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 178 | await client.database.eventScheduler.schedule("naturalUnmute", new Date().getTime() + muteTime * 1000, { |
| 179 | guild: interaction.guild.id, |
| 180 | user: user.id, |
| 181 | expires: new Date().getTime() + muteTime * 1000 |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 182 | }); |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 183 | } |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 184 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 185 | } catch { errors++; } |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 186 | try { |
| 187 | if (config.moderation.mute.role !== null) { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 188 | await member.roles.add(config.moderation.mute.role); |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 189 | await client.database.eventScheduler.schedule("unmuteRole", new Date().getTime() + muteTime * 1000, { |
| 190 | guild: interaction.guild.id, |
| 191 | user: user.id, |
| 192 | role: config.moderation.mute.role |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 193 | }); |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 194 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 195 | } catch (e){ console.log(e); errors++; } |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 196 | if (errors === 2) { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 197 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 198 | .setEmoji("PUNISH.MUTE.RED") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 199 | .setTitle("Mute") |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 200 | .setDescription("Something went wrong and the user was not muted") |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 201 | .setStatus("Danger") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 202 | ], components: []}); // TODO: make this clearer |
| 203 | if (dmd) await dm.delete(); |
| 204 | return; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 205 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 206 | await client.database.history.create("mute", interaction.guild.id, member.user, interaction.user, reason); |
| 207 | const failed = (dmd === false && notify); |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 208 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 209 | .setEmoji(`PUNISH.MUTE.${failed ? "YELLOW" : "GREEN"}`) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 210 | .setTitle("Mute") |
| 211 | .setDescription("The member was muted" + (failed ? ", but could not be notified" : "") + (confirmation.components.appeal.response ? ` and an appeal ticket was opened in <#${confirmation.components.appeal.response}>` : "")) |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 212 | .setStatus(failed ? "Warning" : "Success") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 213 | ], components: []}); |
| 214 | const data = { |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 215 | meta:{ |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 216 | type: "memberMute", |
| 217 | displayName: "Member Muted", |
| 218 | calculateType: "guildMemberPunish", |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 219 | color: NucleusColors.yellow, |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 220 | emoji: "PUNISH.WARN.YELLOW", |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 221 | timestamp: new Date().getTime() |
| 222 | }, |
| 223 | list: { |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 224 | memberId: entry(member.user.id, `\`${member.user.id}\``), |
| 225 | name: entry(member.user.id, renderUser(member.user)), |
| 226 | mutedUntil: entry(new Date().getTime() + muteTime * 1000, renderDelta(new Date().getTime() + muteTime * 1000)), |
| 227 | muted: entry(new Date().getTime(), renderDelta(new Date().getTime() - 1000)), |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 228 | mutedBy: entry(interaction.member.user.id, renderUser(interaction.member.user)), |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 229 | reason: entry(reason, reason ? reason : "*No reason provided*") |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 230 | }, |
| 231 | hidden: { |
| 232 | guild: interaction.guild.id |
| 233 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 234 | }; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 235 | log(data); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 236 | } else { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 237 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 238 | .setEmoji("PUNISH.MUTE.GREEN") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 239 | .setTitle("Mute") |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 240 | .setDescription("No changes were made") |
| 241 | .setStatus("Success") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 242 | ], components: []}); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 243 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 244 | }; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 245 | |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 246 | const check = (interaction: CommandInteraction) => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 247 | const member = (interaction.member as GuildMember); |
| 248 | const me = (interaction.guild.me as GuildMember); |
| 249 | const apply = (interaction.options.getMember("user") as GuildMember); |
| 250 | if (member === null || me === null || apply === null) throw "That member is not in the server"; |
| 251 | const memberPos = member.roles ? member.roles.highest.position : 0; |
| 252 | const mePos = me.roles ? me.roles.highest.position : 0; |
| 253 | const applyPos = apply.roles ? apply.roles.highest.position : 0; |
pineafan | c1c1879 | 2022-08-03 21:41:36 +0100 | [diff] [blame] | 254 | // Do not allow muting the owner |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 255 | if (member.id === interaction.guild.ownerId) throw "You cannot mute the owner of the server"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 256 | // Check if Nucleus can mute the member |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 257 | if (! (mePos > applyPos)) throw "I do not have a role higher than that member"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 258 | // Check if Nucleus has permission to mute |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 259 | if (! me.permissions.has("MODERATE_MEMBERS")) throw "I do not have the *Moderate Members* permission"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 260 | // Do not allow muting Nucleus |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 261 | if (member.id === me.id) throw "I cannot mute myself"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 262 | // Allow the owner to mute anyone |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 263 | if (member.id === interaction.guild.ownerId) return true; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 264 | // Check if the user has moderate_members permission |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 265 | if (! member.permissions.has("MODERATE_MEMBERS")) throw "You do not have the *Moderate Members* permission"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 266 | // Check if the user is below on the role list |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 267 | if (! (memberPos > applyPos)) throw "You do not have a role higher than that member"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 268 | // Allow mute |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 269 | return true; |
| 270 | }; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 271 | |
| 272 | export { command, callback, check }; |