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