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 | } |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 135 | 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) { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 253 | if (reason) { |
| 254 | reason = reason |
| 255 | .split("\n") |
| 256 | .map((line) => "> " + line) |
| 257 | .join("\n"); |
| 258 | } |
PineaFan | 5bea7e1 | 2023-01-05 21:20:04 +0000 | [diff] [blame] | 259 | const messageData: { |
| 260 | embeds: EmojiEmbed[]; |
| 261 | components: ActionRowBuilder<ButtonBuilder>[]; |
| 262 | } = { |
| 263 | embeds: [ |
| 264 | new EmojiEmbed() |
| 265 | .setEmoji("PUNISH.MUTE.RED") |
| 266 | .setTitle("Muted") |
| 267 | .setDescription( |
| 268 | `You have been muted in ${interaction.guild.name}` + |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 269 | (reason ? ` for:\n${reason}` : ".\n*No reason was provided*") + |
| 270 | "\n\n" + |
| 271 | `You will be unmuted at: <t:${Math.round(Date.now() / 1000) + muteTime}:D> at ` + |
| 272 | `<t:${Math.round(Date.now() / 1000) + muteTime}:T> (<t:${ |
| 273 | Math.round(Date.now() / 1000) + muteTime |
| 274 | }:R>)` + |
| 275 | "\n\n" + |
| 276 | (createAppealTicket |
| 277 | ? `You can appeal this in the ticket created in <#${ |
| 278 | confirmation.components!["appeal"]!.response |
| 279 | }>` |
| 280 | : "") |
PineaFan | 5bea7e1 | 2023-01-05 21:20:04 +0000 | [diff] [blame] | 281 | ) |
| 282 | .setStatus("Danger") |
| 283 | ], |
| 284 | components: [] |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 285 | }; |
PineaFan | 5bea7e1 | 2023-01-05 21:20:04 +0000 | [diff] [blame] | 286 | if (config.moderation.mute.text && config.moderation.mute.link) { |
| 287 | messageData.embeds[0]!.setFooter(LinkWarningFooter); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 288 | messageData.components.push( |
| 289 | new ActionRowBuilder<Discord.ButtonBuilder>().addComponents( |
| 290 | new ButtonBuilder() |
| 291 | .setStyle(ButtonStyle.Link) |
| 292 | .setLabel(config.moderation.mute.text) |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 293 | .setURL(config.moderation.mute.link.replaceAll("{id}", member.id)) |
PineaFan | 5bea7e1 | 2023-01-05 21:20:04 +0000 | [diff] [blame] | 294 | ) |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 295 | ); |
| 296 | } |
PineaFan | 5bea7e1 | 2023-01-05 21:20:04 +0000 | [diff] [blame] | 297 | dmMessage = await member.send(messageData); |
| 298 | status.dm = true; |
| 299 | } |
| 300 | } catch { |
| 301 | status.dm = false; |
| 302 | } |
| 303 | try { |
| 304 | if (config.moderation.mute.timeout) { |
| 305 | await member.timeout(muteTime * 1000, reason || "*No reason provided*"); |
| 306 | if (config.moderation.mute.role !== null) { |
| 307 | await member.roles.add(config.moderation.mute.role); |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 308 | await client.database.eventScheduler.schedule( |
| 309 | "naturalUnmute", |
| 310 | (Date.now() + muteTime * 1000).toString(), |
| 311 | { |
| 312 | guild: interaction.guild.id, |
| 313 | user: member.id, |
| 314 | expires: Date.now() + muteTime * 1000 |
| 315 | } |
| 316 | ); |
PineaFan | 5bea7e1 | 2023-01-05 21:20:04 +0000 | [diff] [blame] | 317 | } |
| 318 | } else { |
| 319 | status.timeout = true; |
| 320 | } |
| 321 | } catch { |
| 322 | status.timeout = false; |
| 323 | } |
| 324 | try { |
| 325 | if (config.moderation.mute.role !== null) { |
| 326 | await member.roles.add(config.moderation.mute.role); |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 327 | await client.database.eventScheduler.schedule("unmuteRole", (Date.now() + muteTime * 1000).toString(), { |
PineaFan | 5bea7e1 | 2023-01-05 21:20:04 +0000 | [diff] [blame] | 328 | guild: interaction.guild.id, |
| 329 | user: member.id, |
| 330 | role: config.moderation.mute.role |
| 331 | }); |
| 332 | } else { |
| 333 | status.role = true; |
| 334 | } |
| 335 | } catch { |
| 336 | status.role = false; |
| 337 | } |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 338 | const countTrue = (items: (boolean | null)[]) => items.filter((item) => item === true).length; |
PineaFan | 5bea7e1 | 2023-01-05 21:20:04 +0000 | [diff] [blame] | 339 | const requiredPunishments = countTrue([config.moderation.mute.timeout, config.moderation.mute.role !== null]); |
| 340 | const actualPunishments = countTrue([status.timeout, status.role]); |
| 341 | |
| 342 | await client.database.history.create("mute", interaction.guild.id, member.user, interaction.user, reason); |
| 343 | if (requiredPunishments !== actualPunishments) { |
| 344 | const messages = []; |
| 345 | 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] | 346 | if (config.moderation.mute.role !== null) |
| 347 | messages.push(`The member was ${status.role ? "" : "not "}given the mute role`); |
PineaFan | 5bea7e1 | 2023-01-05 21:20:04 +0000 | [diff] [blame] | 348 | messages.push(`The member was not sent a DM`); |
| 349 | if (dmMessage && actualPunishments === 0) await dmMessage.delete(); |
| 350 | await interaction.editReply({ |
| 351 | embeds: [ |
| 352 | new EmojiEmbed() |
| 353 | .setEmoji("PUNISH.MUTE." + (actualPunishments > 0 ? "YELLOW" : "RED")) |
| 354 | .setTitle("Mute") |
| 355 | .setDescription( |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 356 | "Mute " + |
| 357 | (actualPunishments > 0 ? "partially" : "failed") + |
| 358 | ":\n" + |
| 359 | messages.map((message) => `> ${message}`).join("\n") |
PineaFan | 5bea7e1 | 2023-01-05 21:20:04 +0000 | [diff] [blame] | 360 | ) |
| 361 | .setStatus(actualPunishments > 0 ? "Warning" : "Danger") |
| 362 | ], |
| 363 | components: [] |
| 364 | }); |
| 365 | } |
| 366 | const data = { |
| 367 | meta: { |
| 368 | type: "memberMute", |
| 369 | displayName: "Member Muted", |
| 370 | calculateType: "guildMemberPunish", |
| 371 | color: NucleusColors.yellow, |
| 372 | emoji: "PUNISH.WARN.YELLOW", |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 373 | timestamp: Date.now() |
PineaFan | 5bea7e1 | 2023-01-05 21:20:04 +0000 | [diff] [blame] | 374 | }, |
| 375 | list: { |
| 376 | memberId: entry(member.user.id, `\`${member.user.id}\``), |
| 377 | name: entry(member.user.id, renderUser(member.user)), |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 378 | mutedUntil: entry((Date.now() + muteTime * 1000).toString(), renderDelta(Date.now() + muteTime * 1000)), |
TheCodedProf | 6ec331b | 2023-02-20 12:13:06 -0500 | [diff] [blame] | 379 | muted: entry(new Date().getTime.toString(), renderDelta(Date.now() - 1000)), |
PineaFan | 5bea7e1 | 2023-01-05 21:20:04 +0000 | [diff] [blame] | 380 | mutedBy: entry(interaction.member!.user.id, renderUser(interaction.member!.user as Discord.User)), |
| 381 | reason: entry(reason, reason ? reason : "*No reason provided*") |
| 382 | }, |
TheCodedProf | 94ff6de | 2023-02-22 17:47:26 -0500 | [diff] [blame] | 383 | separate: { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 384 | end: |
| 385 | getEmojiByName("ICONS.NOTIFY." + (notify ? "ON" : "OFF")) + |
| 386 | ` The user was ${notify ? "" : "not "}notified` |
TheCodedProf | 94ff6de | 2023-02-22 17:47:26 -0500 | [diff] [blame] | 387 | }, |
PineaFan | 5bea7e1 | 2023-01-05 21:20:04 +0000 | [diff] [blame] | 388 | hidden: { |
| 389 | guild: interaction.guild.id |
| 390 | } |
| 391 | }; |
| 392 | log(data); |
| 393 | const failed = !status.dm && notify; |
| 394 | await interaction.editReply({ |
| 395 | embeds: [ |
| 396 | new EmojiEmbed() |
| 397 | .setEmoji(`PUNISH.MUTE.${failed ? "YELLOW" : "GREEN"}`) |
| 398 | .setTitle("Mute") |
| 399 | .setDescription( |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 400 | "The member was muted" + |
| 401 | (failed ? ", but could not be notified" : "") + |
| 402 | (createAppealTicket |
| 403 | ? ` and an appeal ticket was opened in <#${confirmation.components!["appeal"]!.response}>` |
| 404 | : "") |
PineaFan | 5bea7e1 | 2023-01-05 21:20:04 +0000 | [diff] [blame] | 405 | ) |
| 406 | .setStatus(failed ? "Warning" : "Success") |
| 407 | ], |
| 408 | components: [] |
| 409 | }); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 410 | }; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 411 | |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 412 | const check = (interaction: CommandInteraction | ButtonInteraction, partial: boolean = false, target?: GuildMember) => { |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 413 | if (!interaction.guild) return; |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 414 | const member = interaction.member as GuildMember; |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 415 | // Check if the user has moderate_members permission |
| 416 | if (!member.permissions.has("ModerateMembers")) return "You do not have the *Moderate Members* permission"; |
| 417 | if (partial) return true; |
PineaFan | 5bea7e1 | 2023-01-05 21:20:04 +0000 | [diff] [blame] | 418 | const me = interaction.guild.members.me!; |
pineafan | 6de4da5 | 2023-03-07 20:43:44 +0000 | [diff] [blame] | 419 | let apply; |
| 420 | if (interaction.isButton()) { |
| 421 | apply = target!; |
| 422 | } else { |
| 423 | apply = interaction.options.getMember("user") as GuildMember; |
| 424 | } |
pineafan | 62ce192 | 2022-08-25 20:34:45 +0100 | [diff] [blame] | 425 | const memberPos = member.roles.cache.size > 1 ? member.roles.highest.position : 0; |
| 426 | const mePos = me.roles.cache.size > 1 ? me.roles.highest.position : 0; |
| 427 | const applyPos = apply.roles.cache.size > 1 ? apply.roles.highest.position : 0; |
pineafan | c1c1879 | 2022-08-03 21:41:36 +0100 | [diff] [blame] | 428 | // Do not allow muting the owner |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 429 | 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] | 430 | // Check if Nucleus can mute the member |
TheCodedProf | 0941da4 | 2023-02-18 20:28:04 -0500 | [diff] [blame] | 431 | 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] | 432 | // Check if Nucleus has permission to mute |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 433 | 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] | 434 | // Do not allow muting Nucleus |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 435 | if (member.id === me.id) return "I cannot mute myself"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 436 | // Allow the owner to mute anyone |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 437 | if (member.id === interaction.guild.ownerId) return true; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 438 | // Check if the user is below on the role list |
TheCodedProf | 0941da4 | 2023-02-18 20:28:04 -0500 | [diff] [blame] | 439 | 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] | 440 | // Allow mute |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 441 | return true; |
| 442 | }; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 443 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 444 | export { command, callback, check }; |
TheCodedProf | a112f61 | 2023-01-28 18:06:45 -0500 | [diff] [blame] | 445 | export const metadata = { |
Skyler Grey | da16adf | 2023-03-05 10:22:12 +0000 | [diff] [blame] | 446 | longDescription: |
| 447 | "Stops a member from being able to send messages or join voice channels for a specified amount of time.", |
| 448 | premiumOnly: true |
| 449 | }; |