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