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