PineaFan | 5bea7e1 | 2023-01-05 21:20:04 +0000 | [diff] [blame^] | 1 | import { LinkWarningFooter, LoadingEmbed } from "./../../utils/defaultEmbeds.js"; |
| 2 | import Discord, { CommandInteraction, GuildMember, ActionRowBuilder, ButtonBuilder, ButtonStyle } from "discord.js"; |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 3 | import type { 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"; |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 8 | // @ts-expect-error |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 9 | import humanizeDuration from "humanize-duration"; |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 10 | import client from "../../utils/client.js"; |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 11 | import { areTicketsEnabled, create } from "../../actions/createModActionTicket.js"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 12 | |
| 13 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 14 | builder |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 15 | .setName("mute") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 16 | .setDescription("Mutes a member, stopping them from talking in the server") |
| 17 | .addUserOption((option) => option.setName("user").setDescription("The user to mute").setRequired(true)) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 18 | .addIntegerOption((option) => |
| 19 | option |
| 20 | .setName("days") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 21 | .setDescription("The number of days to mute the user for | Default: 0") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 22 | .setMinValue(0) |
| 23 | .setMaxValue(27) |
| 24 | .setRequired(false) |
| 25 | ) |
| 26 | .addIntegerOption((option) => |
| 27 | option |
| 28 | .setName("hours") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 29 | .setDescription("The number of hours to mute the user for | Default: 0") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 30 | .setMinValue(0) |
| 31 | .setMaxValue(23) |
| 32 | .setRequired(false) |
| 33 | ) |
| 34 | .addIntegerOption((option) => |
| 35 | option |
| 36 | .setName("minutes") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 37 | .setDescription("The number of minutes to mute the user for | Default: 0") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 38 | .setMinValue(0) |
| 39 | .setMaxValue(59) |
| 40 | .setRequired(false) |
| 41 | ) |
| 42 | .addIntegerOption((option) => |
| 43 | option |
| 44 | .setName("seconds") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 45 | .setDescription("The number of seconds to mute the user for | Default: 0") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 46 | .setMinValue(0) |
| 47 | .setMaxValue(59) |
| 48 | .setRequired(false) |
| 49 | ); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 50 | |
PineaFan | 5bea7e1 | 2023-01-05 21:20:04 +0000 | [diff] [blame^] | 51 | |
Skyler Grey | c634e2b | 2022-08-06 17:50:48 +0100 | [diff] [blame] | 52 | const callback = async (interaction: CommandInteraction): Promise<unknown> => { |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 53 | if (!interaction.guild) return; |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 54 | const { log, NucleusColors, renderUser, entry, renderDelta } = client.logger; |
PineaFan | 5bea7e1 | 2023-01-05 21:20:04 +0000 | [diff] [blame^] | 55 | const member = interaction.options.getMember("user") as GuildMember; |
| 56 | const time: {days: number, hours: number, minutes: number, seconds: number} = { |
| 57 | days: (interaction.options.get("days")?.value as number | null) ?? 0, |
| 58 | hours: (interaction.options.get("hours")?.value as number | null) ?? 0, |
| 59 | minutes: (interaction.options.get("minutes")?.value as number | null) ?? 0, |
| 60 | seconds: (interaction.options.get("seconds")?.value as number | null) ?? 0 |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 61 | }; |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 62 | const config = await client.database.guilds.read(interaction.guild.id); |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 63 | let serverSettingsDescription = config.moderation.mute.timeout ? "given a timeout" : ""; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 64 | if (config.moderation.mute.role) |
| 65 | serverSettingsDescription += |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 66 | (serverSettingsDescription ? " and " : "") + `given the <@&${config.moderation.mute.role}> role`; |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 67 | |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 68 | 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] | 69 | if (muteTime === 0) { |
PineaFan | 5bea7e1 | 2023-01-05 21:20:04 +0000 | [diff] [blame^] | 70 | const m = await interaction.reply({ |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 71 | embeds: [ |
| 72 | new EmojiEmbed() |
| 73 | .setEmoji("PUNISH.MUTE.GREEN") |
| 74 | .setTitle("Mute") |
PineaFan | 5bea7e1 | 2023-01-05 21:20:04 +0000 | [diff] [blame^] | 75 | .setDescription("How long should the user be muted for?") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 76 | .setStatus("Success") |
| 77 | ], |
| 78 | components: [ |
PineaFan | 5bea7e1 | 2023-01-05 21:20:04 +0000 | [diff] [blame^] | 79 | new ActionRowBuilder<ButtonBuilder>().addComponents([ |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 80 | new Discord.ButtonBuilder().setCustomId("1m").setLabel("1 Minute").setStyle(ButtonStyle.Secondary), |
| 81 | new Discord.ButtonBuilder().setCustomId("10m").setLabel("10 Minutes").setStyle(ButtonStyle.Secondary), |
| 82 | new Discord.ButtonBuilder().setCustomId("30m").setLabel("30 Minutes").setStyle(ButtonStyle.Secondary), |
| 83 | new Discord.ButtonBuilder().setCustomId("1h").setLabel("1 Hour").setStyle(ButtonStyle.Secondary) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 84 | ]), |
PineaFan | 5bea7e1 | 2023-01-05 21:20:04 +0000 | [diff] [blame^] | 85 | new ActionRowBuilder<ButtonBuilder>().addComponents([ |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 86 | new Discord.ButtonBuilder().setCustomId("6h").setLabel("6 Hours").setStyle(ButtonStyle.Secondary), |
| 87 | new Discord.ButtonBuilder().setCustomId("12h").setLabel("12 Hours").setStyle(ButtonStyle.Secondary), |
| 88 | new Discord.ButtonBuilder().setCustomId("1d").setLabel("1 Day").setStyle(ButtonStyle.Secondary), |
| 89 | new Discord.ButtonBuilder().setCustomId("1w").setLabel("1 Week").setStyle(ButtonStyle.Secondary) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 90 | ]), |
PineaFan | 5bea7e1 | 2023-01-05 21:20:04 +0000 | [diff] [blame^] | 91 | new ActionRowBuilder<ButtonBuilder>().addComponents([ |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 92 | new Discord.ButtonBuilder() |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 93 | .setCustomId("cancel") |
| 94 | .setLabel("Cancel") |
TheCodedProf | 21c0859 | 2022-09-13 14:14:43 -0400 | [diff] [blame] | 95 | .setStyle(ButtonStyle.Danger) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 96 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
| 97 | ]) |
| 98 | ], |
| 99 | ephemeral: true, |
| 100 | fetchReply: true |
PineaFan | 5bea7e1 | 2023-01-05 21:20:04 +0000 | [diff] [blame^] | 101 | }); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 102 | let component; |
| 103 | try { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 104 | component = await m.awaitMessageComponent({ |
| 105 | filter: (m) => m.user.id === interaction.user.id, |
| 106 | time: 300000 |
| 107 | }); |
| 108 | } catch { |
| 109 | return; |
| 110 | } |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 111 | component.deferUpdate(); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 112 | if (component.customId === "cancel") |
| 113 | return interaction.editReply({ |
| 114 | embeds: [ |
| 115 | new EmojiEmbed() |
| 116 | .setEmoji("PUNISH.MUTE.RED") |
| 117 | .setTitle("Mute") |
| 118 | .setDescription("Mute cancelled") |
| 119 | .setStatus("Danger") |
| 120 | ] |
| 121 | }); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 122 | switch (component.customId) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 123 | case "1m": { |
| 124 | muteTime = 60; |
| 125 | break; |
| 126 | } |
| 127 | case "10m": { |
| 128 | muteTime = 60 * 10; |
| 129 | break; |
| 130 | } |
| 131 | case "30m": { |
| 132 | muteTime = 60 * 30; |
| 133 | break; |
| 134 | } |
| 135 | case "1h": { |
| 136 | muteTime = 60 * 60; |
| 137 | break; |
| 138 | } |
| 139 | case "6h": { |
| 140 | muteTime = 60 * 60 * 6; |
| 141 | break; |
| 142 | } |
| 143 | case "12h": { |
| 144 | muteTime = 60 * 60 * 12; |
| 145 | break; |
| 146 | } |
| 147 | case "1d": { |
| 148 | muteTime = 60 * 60 * 24; |
| 149 | break; |
| 150 | } |
| 151 | case "1w": { |
| 152 | muteTime = 60 * 60 * 24 * 7; |
| 153 | break; |
| 154 | } |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 155 | } |
| 156 | } else { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 157 | await interaction.reply({ |
| 158 | embeds: LoadingEmbed, |
| 159 | ephemeral: true, |
| 160 | fetchReply: true |
| 161 | }); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 162 | } |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 163 | // TODO:[Modals] Replace this with a modal |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 164 | let reason: string | null = null; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 165 | let notify = true; |
| 166 | let createAppealTicket = false; |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 167 | let confirmation; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 168 | let timedOut = false; |
| 169 | let success = false; |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 170 | do { |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 171 | confirmation = await new confirmationMessage(interaction) |
| 172 | .setEmoji("PUNISH.MUTE.RED") |
| 173 | .setTitle("Mute") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 174 | .setDescription( |
| 175 | keyValueList({ |
PineaFan | 5bea7e1 | 2023-01-05 21:20:04 +0000 | [diff] [blame^] | 176 | user: renderUser(member.user), |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 177 | time: `${humanizeDuration(muteTime * 1000, { |
| 178 | round: true |
| 179 | })}`, |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 180 | reason: reason ? "\n> " + (reason).replaceAll("\n", "\n> ") : "*No reason provided*" |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 181 | }) + |
| 182 | "The user will be " + |
| 183 | serverSettingsDescription + |
PineaFan | 5bea7e1 | 2023-01-05 21:20:04 +0000 | [diff] [blame^] | 184 | "\n\n" + |
| 185 | `Are you sure you want to mute <@!${member.id}>?` |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 186 | ) |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 187 | .setColor("Danger") |
| 188 | .addCustomBoolean( |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 189 | "appeal", |
| 190 | "Create appeal ticket", |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 191 | !(await areTicketsEnabled(interaction.guild.id)), |
PineaFan | 5bea7e1 | 2023-01-05 21:20:04 +0000 | [diff] [blame^] | 192 | async () => await create(interaction.guild!, interaction.options.getUser("user")!, interaction.user, reason), |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 193 | "An appeal ticket will be created when Confirm is clicked", |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 194 | null, |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 195 | "CONTROL.TICKET", |
| 196 | createAppealTicket |
| 197 | ) |
| 198 | .addCustomBoolean( |
| 199 | "notify", |
| 200 | "Notify user", |
| 201 | false, |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 202 | null, |
PineaFan | 5bea7e1 | 2023-01-05 21:20:04 +0000 | [diff] [blame^] | 203 | "The user will be sent a DM", |
PineaFan | a34d04b | 2023-01-03 22:05:42 +0000 | [diff] [blame] | 204 | null, |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 205 | "ICONS.NOTIFY." + (notify ? "ON" : "OFF"), |
| 206 | notify |
| 207 | ) |
pineafan | 73a7c4a | 2022-07-24 10:38:04 +0100 | [diff] [blame] | 208 | .addReasonButton(reason ?? "") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 209 | .send(true); |
| 210 | reason = reason ?? ""; |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 211 | if (confirmation.cancelled) timedOut = true; |
PineaFan | 5bea7e1 | 2023-01-05 21:20:04 +0000 | [diff] [blame^] | 212 | else if (confirmation.success) success = true; |
| 213 | else if (confirmation.newReason) reason = confirmation.newReason; |
| 214 | else if (confirmation.components) { |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 215 | notify = confirmation.components["notify"]!.active; |
| 216 | createAppealTicket = confirmation.components["appeal"]!.active; |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 217 | } |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 218 | } while (!timedOut && !success) |
Skyler Grey | ad00217 | 2022-08-16 18:48:26 +0100 | [diff] [blame] | 219 | if (timedOut) return; |
PineaFan | 5bea7e1 | 2023-01-05 21:20:04 +0000 | [diff] [blame^] | 220 | if (!confirmation.success) { |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 221 | await interaction.editReply({ |
| 222 | embeds: [ |
| 223 | new EmojiEmbed() |
| 224 | .setEmoji("PUNISH.BAN.GREEN") |
| 225 | .setTitle("Softban") |
| 226 | .setDescription("No changes were made") |
| 227 | .setStatus("Success") |
| 228 | ], |
| 229 | components: [] |
| 230 | }); |
PineaFan | 5bea7e1 | 2023-01-05 21:20:04 +0000 | [diff] [blame^] | 231 | return; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 232 | } |
PineaFan | 5bea7e1 | 2023-01-05 21:20:04 +0000 | [diff] [blame^] | 233 | const status: {timeout: boolean | null, role: boolean | null, dm: boolean | null} = {timeout: null, role: null, dm: null}; |
| 234 | let dmMessage; |
| 235 | try { |
| 236 | if (notify) { |
| 237 | const messageData: { |
| 238 | embeds: EmojiEmbed[]; |
| 239 | components: ActionRowBuilder<ButtonBuilder>[]; |
| 240 | } = { |
| 241 | embeds: [ |
| 242 | new EmojiEmbed() |
| 243 | .setEmoji("PUNISH.MUTE.RED") |
| 244 | .setTitle("Muted") |
| 245 | .setDescription( |
| 246 | `You have been muted in ${interaction.guild.name}` + |
| 247 | (reason ? ` for:\n> ${reason}` : ".\n*No reason was provided*") + "\n\n" + |
| 248 | `You will be unmuted at: <t:${Math.round(new Date().getTime() / 1000) + muteTime}:D> at ` + |
| 249 | `<t:${Math.round(new Date().getTime() / 1000) + muteTime}:T> (<t:${Math.round(new Date().getTime() / 1000) + muteTime |
| 250 | }:R>)` + "\n\n" + |
| 251 | (createAppealTicket |
| 252 | ? `You can appeal this in the ticket created in <#${confirmation.components!["appeal"]!.response}>` |
| 253 | : "") |
| 254 | ) |
| 255 | .setStatus("Danger") |
| 256 | ], |
| 257 | components: [] |
| 258 | } |
| 259 | if (config.moderation.mute.text && config.moderation.mute.link) { |
| 260 | messageData.embeds[0]!.setFooter(LinkWarningFooter); |
| 261 | messageData.components.push(new ActionRowBuilder<Discord.ButtonBuilder>() |
| 262 | .addComponents(new ButtonBuilder() |
| 263 | .setStyle(ButtonStyle.Link) |
| 264 | .setLabel(config.moderation.mute.text) |
| 265 | .setURL(config.moderation.mute.link) |
| 266 | ) |
| 267 | ) |
| 268 | }; |
| 269 | dmMessage = await member.send(messageData); |
| 270 | status.dm = true; |
| 271 | } |
| 272 | } catch { |
| 273 | status.dm = false; |
| 274 | } |
| 275 | try { |
| 276 | if (config.moderation.mute.timeout) { |
| 277 | await member.timeout(muteTime * 1000, reason || "*No reason provided*"); |
| 278 | if (config.moderation.mute.role !== null) { |
| 279 | await member.roles.add(config.moderation.mute.role); |
| 280 | await client.database.eventScheduler.schedule("naturalUnmute", (new Date().getTime() + muteTime * 1000).toString(), { |
| 281 | guild: interaction.guild.id, |
| 282 | user: member.id, |
| 283 | expires: new Date().getTime() + muteTime * 1000 |
| 284 | }); |
| 285 | } |
| 286 | } else { |
| 287 | status.timeout = true; |
| 288 | } |
| 289 | } catch { |
| 290 | status.timeout = false; |
| 291 | } |
| 292 | try { |
| 293 | if (config.moderation.mute.role !== null) { |
| 294 | await member.roles.add(config.moderation.mute.role); |
| 295 | await client.database.eventScheduler.schedule("unmuteRole", (new Date().getTime() + muteTime * 1000).toString(), { |
| 296 | guild: interaction.guild.id, |
| 297 | user: member.id, |
| 298 | role: config.moderation.mute.role |
| 299 | }); |
| 300 | } else { |
| 301 | status.role = true; |
| 302 | } |
| 303 | } catch { |
| 304 | status.role = false; |
| 305 | } |
| 306 | const countTrue = (items: (boolean | null)[]) => items.filter(item => item === true).length; |
| 307 | const requiredPunishments = countTrue([config.moderation.mute.timeout, config.moderation.mute.role !== null]); |
| 308 | const actualPunishments = countTrue([status.timeout, status.role]); |
| 309 | |
| 310 | await client.database.history.create("mute", interaction.guild.id, member.user, interaction.user, reason); |
| 311 | if (requiredPunishments !== actualPunishments) { |
| 312 | const messages = []; |
| 313 | if (config.moderation.mute.timeout) messages.push(`The member was ${status.timeout ? "" : "not "}timed out`); |
| 314 | if (config.moderation.mute.role !== null) messages.push(`The member was ${status.role ? "" : "not "}given the mute role`); |
| 315 | messages.push(`The member was not sent a DM`); |
| 316 | if (dmMessage && actualPunishments === 0) await dmMessage.delete(); |
| 317 | await interaction.editReply({ |
| 318 | embeds: [ |
| 319 | new EmojiEmbed() |
| 320 | .setEmoji("PUNISH.MUTE." + (actualPunishments > 0 ? "YELLOW" : "RED")) |
| 321 | .setTitle("Mute") |
| 322 | .setDescription( |
| 323 | "Mute " + (actualPunishments > 0 ? "partially" : "failed") + ":\n" + |
| 324 | messages.map(message => `> ${message}`).join("\n") |
| 325 | ) |
| 326 | .setStatus(actualPunishments > 0 ? "Warning" : "Danger") |
| 327 | ], |
| 328 | components: [] |
| 329 | }); |
| 330 | } |
| 331 | const data = { |
| 332 | meta: { |
| 333 | type: "memberMute", |
| 334 | displayName: "Member Muted", |
| 335 | calculateType: "guildMemberPunish", |
| 336 | color: NucleusColors.yellow, |
| 337 | emoji: "PUNISH.WARN.YELLOW", |
| 338 | timestamp: new Date().getTime() |
| 339 | }, |
| 340 | list: { |
| 341 | memberId: entry(member.user.id, `\`${member.user.id}\``), |
| 342 | name: entry(member.user.id, renderUser(member.user)), |
| 343 | mutedUntil: entry( |
| 344 | (new Date().getTime() + muteTime * 1000).toString(), |
| 345 | renderDelta(new Date().getTime() + muteTime * 1000) |
| 346 | ), |
| 347 | muted: entry(new Date().getTime.toString(), renderDelta(new Date().getTime() - 1000)), |
| 348 | mutedBy: entry(interaction.member!.user.id, renderUser(interaction.member!.user as Discord.User)), |
| 349 | reason: entry(reason, reason ? reason : "*No reason provided*") |
| 350 | }, |
| 351 | hidden: { |
| 352 | guild: interaction.guild.id |
| 353 | } |
| 354 | }; |
| 355 | log(data); |
| 356 | const failed = !status.dm && notify; |
| 357 | await interaction.editReply({ |
| 358 | embeds: [ |
| 359 | new EmojiEmbed() |
| 360 | .setEmoji(`PUNISH.MUTE.${failed ? "YELLOW" : "GREEN"}`) |
| 361 | .setTitle("Mute") |
| 362 | .setDescription( |
| 363 | "The member was muted" + (failed ? ", but could not be notified" : "") + |
| 364 | (createAppealTicket |
| 365 | ? ` and an appeal ticket was opened in <#${confirmation.components!["appeal"]!.response}>` |
| 366 | : "") |
| 367 | ) |
| 368 | .setStatus(failed ? "Warning" : "Success") |
| 369 | ], |
| 370 | components: [] |
| 371 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 372 | }; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 373 | |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 374 | const check = (interaction: CommandInteraction) => { |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 375 | if (!interaction.guild) return; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 376 | const member = interaction.member as GuildMember; |
PineaFan | 5bea7e1 | 2023-01-05 21:20:04 +0000 | [diff] [blame^] | 377 | const me = interaction.guild.members.me!; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 378 | const apply = interaction.options.getMember("user") as GuildMember; |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 379 | const memberPos = member.roles.cache.size > 1 ? member.roles.highest.position : 0; |
| 380 | const mePos = me.roles.cache.size > 1 ? me.roles.highest.position : 0; |
| 381 | const applyPos = apply.roles.cache.size > 1 ? apply.roles.highest.position : 0; |
pineafan | c1c1879 | 2022-08-03 21:41:36 +0100 | [diff] [blame] | 382 | // Do not allow muting the owner |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 383 | if (member.id === interaction.guild.ownerId) throw new Error("You cannot mute the owner of the server"); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 384 | // Check if Nucleus can mute the member |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 385 | if (!(mePos > applyPos)) throw new Error("I do not have a role higher than that member"); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 386 | // Check if Nucleus has permission to mute |
PineaFan | 5bea7e1 | 2023-01-05 21:20:04 +0000 | [diff] [blame^] | 387 | if (!me.permissions.has("ModerateMembers")) throw new Error("I do not have the *Moderate Members* permission"); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 388 | // Do not allow muting Nucleus |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 389 | if (member.id === me.id) throw new Error("I cannot mute myself"); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 390 | // Allow the owner to mute anyone |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 391 | if (member.id === interaction.guild.ownerId) return true; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 392 | // Check if the user has moderate_members permission |
PineaFan | 5bea7e1 | 2023-01-05 21:20:04 +0000 | [diff] [blame^] | 393 | if (!member.permissions.has("ModerateMembers")) |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 394 | throw new Error("You do not have the *Moderate Members* permission"); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 395 | // Check if the user is below on the role list |
pineafan | 3a02ea3 | 2022-08-11 21:35:04 +0100 | [diff] [blame] | 396 | if (!(memberPos > applyPos)) throw new Error("You do not have a role higher than that member"); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 397 | // Allow mute |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 398 | return true; |
| 399 | }; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 400 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 401 | export { command, callback, check }; |