pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 1 | import Discord, { CommandInteraction, GuildMember, MessageActionRow, MessageButton } from "discord.js"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 2 | import { SlashCommandSubcommandBuilder } from "@discordjs/builders"; |
| 3 | import { WrappedCheck } from "jshaiku"; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 4 | import generateEmojiEmbed 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"; |
| 8 | import humanizeDuration from "humanize-duration"; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 9 | import { create, areTicketsEnabled } from "../../automations/createModActionTicket.js"; |
| 10 | import readConfig from "../../utils/readConfig.js"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 11 | |
| 12 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 13 | builder |
| 14 | .setName("mute") |
| 15 | .setDescription("Mutes a member using Discord's \"Timeout\" feature") |
| 16 | .addUserOption(option => option.setName("user").setDescription("The user to mute").setRequired(true)) |
| 17 | .addIntegerOption(option => option.setName("days").setDescription("The number of days to mute the user for | Default 0").setMinValue(0).setMaxValue(27).setRequired(false)) |
| 18 | .addIntegerOption(option => option.setName("hours").setDescription("The number of hours to mute the user for | Default 0").setMinValue(0).setMaxValue(23).setRequired(false)) |
| 19 | .addIntegerOption(option => option.setName("minutes").setDescription("The number of minutes to mute the user for | Default 0").setMinValue(0).setMaxValue(59).setRequired(false)) |
| 20 | .addIntegerOption(option => option.setName("seconds").setDescription("The number of seconds to mute the user for | Default 0").setMinValue(0).setMaxValue(59).setRequired(false)) |
| 21 | .addStringOption(option => option.setName("reason").setDescription("The reason for the mute").setRequired(false)) |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 22 | .addStringOption(option => option.setName("notify").setDescription("If the user should get a message when they are muted | Default yes").setRequired(false) |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 23 | .addChoices([["Yes", "yes"], ["No", "no"]])) |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 24 | |
| 25 | const callback = async (interaction: CommandInteraction) => { |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 26 | // @ts-ignore |
| 27 | const { log, NucleusColors, renderUser, entry } = interaction.client.logger |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 28 | const user = interaction.options.getMember("user") as GuildMember |
| 29 | const reason = interaction.options.getString("reason") |
| 30 | const time = { |
| 31 | days: interaction.options.getInteger("days") || 0, |
| 32 | hours: interaction.options.getInteger("hours") || 0, |
| 33 | minutes: interaction.options.getInteger("minutes") || 0, |
| 34 | seconds: interaction.options.getInteger("seconds") || 0 |
| 35 | } |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 36 | let config = await readConfig(interaction.guild.id) |
| 37 | let serverSettingsDescription = (config.moderation.mute.timeout ? "given a timeout" : "") |
| 38 | if (config.moderation.mute.role) serverSettingsDescription += (serverSettingsDescription ? " and " : "") + `given the <@&${config.moderation.mute.role}> role` |
| 39 | |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 40 | let muteTime = (time.days * 24 * 60 * 60) + (time.hours * 60 * 60) + (time.minutes * 60) + time.seconds |
| 41 | if (muteTime == 0) { |
| 42 | let m = await interaction.reply({embeds: [ |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 43 | new generateEmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 44 | .setEmoji("PUNISH.MUTE.GREEN") |
| 45 | .setTitle("Mute") |
| 46 | .setDescription("How long should the user be muted") |
| 47 | .setStatus("Success") |
| 48 | ], components: [ |
| 49 | new MessageActionRow().addComponents([ |
| 50 | new Discord.MessageButton() |
| 51 | .setCustomId("1m") |
| 52 | .setLabel("1 Minute") |
| 53 | .setStyle("SECONDARY"), |
| 54 | new Discord.MessageButton() |
| 55 | .setCustomId("10m") |
| 56 | .setLabel("10 Minutes") |
| 57 | .setStyle("SECONDARY"), |
| 58 | new Discord.MessageButton() |
| 59 | .setCustomId("30m") |
| 60 | .setLabel("30 Minutes") |
| 61 | .setStyle("SECONDARY"), |
| 62 | new Discord.MessageButton() |
| 63 | .setCustomId("1h") |
| 64 | .setLabel("1 Hour") |
| 65 | .setStyle("SECONDARY") |
| 66 | ]), |
| 67 | new MessageActionRow().addComponents([ |
| 68 | new Discord.MessageButton() |
| 69 | .setCustomId("6h") |
| 70 | .setLabel("6 Hours") |
| 71 | .setStyle("SECONDARY"), |
| 72 | new Discord.MessageButton() |
| 73 | .setCustomId("12h") |
| 74 | .setLabel("12 Hours") |
| 75 | .setStyle("SECONDARY"), |
| 76 | new Discord.MessageButton() |
| 77 | .setCustomId("1d") |
| 78 | .setLabel("1 Day") |
| 79 | .setStyle("SECONDARY"), |
| 80 | new Discord.MessageButton() |
| 81 | .setCustomId("1w") |
| 82 | .setLabel("1 Week") |
| 83 | .setStyle("SECONDARY") |
| 84 | ]), |
| 85 | new MessageActionRow().addComponents([ |
| 86 | new Discord.MessageButton() |
| 87 | .setCustomId("cancel") |
| 88 | .setLabel("Cancel") |
| 89 | .setStyle("DANGER") |
| 90 | .setEmoji(getEmojiByName("CONTROL.CROSS", "id")) |
| 91 | ]) |
| 92 | ], ephemeral: true, fetchReply: true}) |
| 93 | let component; |
| 94 | try { |
| 95 | component = await (m as Discord.Message).awaitMessageComponent({filter: (m) => m.user.id === interaction.user.id, time: 2.5 * 60 * 1000}); |
| 96 | } catch { return } |
| 97 | component.deferUpdate(); |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 98 | if (component.customId == "cancel") return interaction.editReply({embeds: [new generateEmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 99 | .setEmoji("PUNISH.MUTE.RED") |
| 100 | .setTitle("Mute") |
| 101 | .setDescription("Mute cancelled") |
| 102 | .setStatus("Danger") |
| 103 | ]}) |
| 104 | switch (component.customId) { |
| 105 | case "1m": { muteTime = 60; break; } |
| 106 | case "10m": { muteTime = 60 * 10; break; } |
| 107 | case "30m": { muteTime = 60 * 30; break; } |
| 108 | case "1h": { muteTime = 60 * 60; break; } |
| 109 | case "6h": { muteTime = 60 * 60 * 6; break; } |
| 110 | case "12h": { muteTime = 60 * 60 * 12; break; } |
| 111 | case "1d": { muteTime = 60 * 60 * 24; break; } |
| 112 | case "1w": { muteTime = 60 * 60 * 24 * 7; break; } |
| 113 | } |
| 114 | } else { |
| 115 | await interaction.reply({embeds: [ |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 116 | new generateEmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 117 | .setEmoji("PUNISH.MUTE.GREEN") |
| 118 | .setTitle("Mute") |
| 119 | .setDescription("Loading...") |
| 120 | .setStatus("Success") |
| 121 | ], ephemeral: true, fetchReply: true}) |
| 122 | } |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 123 | // TODO:[Modals] Replace this with a modal |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 124 | let confirmation = await new confirmationMessage(interaction) |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 125 | .setEmoji("PUNISH.MUTE.RED") |
| 126 | .setTitle("Mute") |
| 127 | .setDescription(keyValueList({ |
| 128 | "user": `<@!${user.id}> (${user.user.username})`, |
| 129 | "time": `${humanizeDuration(muteTime * 1000, {round: true})}`, |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 130 | "reason": `\n> ${reason ? reason : "*No reason provided*"}` |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 131 | }) |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 132 | + `The user will be ` + serverSettingsDescription + "\n" |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 133 | + `The user **will${interaction.options.getString("notify") === "no" ? ' not' : ''}** be notified\n\n` |
| 134 | + `Are you sure you want to mute <@!${(interaction.options.getMember("user") as GuildMember).id}>?`) |
| 135 | .setColor("Danger") |
| 136 | // pluralize("day", interaction.options.getInteger("delete")) |
| 137 | // const pluralize = (word: string, count: number) => { return count === 1 ? word : word + "s" } |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 138 | .send(true) |
| 139 | if (confirmation.success) { |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 140 | let dmd = false |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 141 | let dm; |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 142 | let config = await readConfig(interaction.guild.id); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 143 | try { |
| 144 | if (interaction.options.getString("notify") != "no") { |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 145 | dm = await (interaction.options.getMember("user") as GuildMember).send({ |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 146 | embeds: [new generateEmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 147 | .setEmoji("PUNISH.MUTE.RED") |
| 148 | .setTitle("Muted") |
| 149 | .setDescription(`You have been muted in ${interaction.guild.name}` + |
pineafan | 1dc1572 | 2022-03-14 21:27:34 +0000 | [diff] [blame] | 150 | (interaction.options.getString("reason") ? ` for:\n> ${interaction.options.getString("reason")}` : ".\n\n" + |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 151 | `You will be unmuted at: <t:${Math.round((new Date).getTime() / 1000) + muteTime}:D> at <t:${Math.round((new Date).getTime() / 1000) + muteTime}:T> (<t:${Math.round((new Date).getTime() / 1000) + muteTime}:R>)`)) |
| 152 | .setStatus("Danger") |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 153 | ], |
| 154 | components: [new MessageActionRow().addComponents(config.moderation.mute.text ? [new MessageButton() |
| 155 | .setStyle("LINK") |
| 156 | .setLabel(config.moderation.mute.text) |
| 157 | .setURL(config.moderation.mute.link) |
| 158 | ] : [])] |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 159 | }) |
| 160 | dmd = true |
| 161 | } |
| 162 | } catch {} |
| 163 | try { |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 164 | if (config.moderation.mute.timeout) { |
| 165 | (interaction.options.getMember("user") as GuildMember).timeout(muteTime * 1000, interaction.options.getString("reason") || "No reason provided") |
| 166 | } |
| 167 | if (config.moderation.mute.role) { |
| 168 | (interaction.options.getMember("user") as GuildMember).roles.add(config.moderation.mute.role) |
| 169 | } |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 170 | } catch { |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 171 | await interaction.editReply({embeds: [new generateEmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 172 | .setEmoji("PUNISH.MUTE.RED") |
| 173 | .setTitle(`Mute`) |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 174 | .setDescription("Something went wrong and the user was not mute") |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 175 | .setStatus("Danger") |
| 176 | ], components: []}) |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 177 | if (dmd) await dm.delete() |
| 178 | return |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 179 | } |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 180 | let failed = (dmd == false && interaction.options.getString("notify") != "no") |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 181 | await interaction.editReply({embeds: [new generateEmojiEmbed() |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 182 | .setEmoji(`PUNISH.MUTE.${failed ? "YELLOW" : "GREEN"}`) |
| 183 | .setTitle(`Mute`) |
| 184 | .setDescription("The member was muted" + (failed ? ", but could not be notified" : "")) |
| 185 | .setStatus(failed ? "Warning" : "Success") |
| 186 | ], components: []}) |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 187 | let data = { |
| 188 | meta:{ |
| 189 | type: 'memberMute', |
| 190 | displayName: 'Member Muted', |
| 191 | calculateType: 'guildMemberPunish', |
| 192 | color: NucleusColors.yellow, |
| 193 | emoji: 'PUNISH.WARN.YELLOW', |
| 194 | timestamp: new Date().getTime() |
| 195 | }, |
| 196 | list: { |
| 197 | user: entry((interaction.options.getMember("user") as GuildMember).user.id, renderUser((interaction.options.getMember("user") as GuildMember).user)), |
| 198 | mutedBy: entry(interaction.member.user.id, renderUser(interaction.member.user)), |
| 199 | time: entry(muteTime, `${humanizeDuration(muteTime * 1000, {round: true})}`), |
| 200 | reason: (interaction.options.getString("reason") ? `\n> ${interaction.options.getString("reason")}` : "No reason provided") |
| 201 | }, |
| 202 | hidden: { |
| 203 | guild: interaction.guild.id |
| 204 | } |
| 205 | } |
| 206 | log(data, interaction.client); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 207 | } else { |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 208 | await interaction.editReply({embeds: [new generateEmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 209 | .setEmoji("PUNISH.MUTE.GREEN") |
| 210 | .setTitle(`Mute`) |
| 211 | .setDescription("No changes were made") |
| 212 | .setStatus("Success") |
| 213 | ], components: []}) |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => { |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 218 | let member = (interaction.member as GuildMember) |
| 219 | let me = (interaction.guild.me as GuildMember) |
| 220 | let apply = (interaction.options.getMember("user") as GuildMember) |
| 221 | if (member == null || me == null || apply == null) throw "That member is not in the server" |
| 222 | let memberPos = member.roles ? member.roles.highest.position : 0 |
| 223 | let mePos = me.roles ? me.roles.highest.position : 0 |
| 224 | let applyPos = apply.roles ? apply.roles.highest.position : 0 |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 225 | // Check if Nucleus can mute the member |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 226 | if (! (mePos > applyPos)) throw "I do not have a role higher than that member" |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 227 | // Check if Nucleus has permission to mute |
| 228 | if (! interaction.guild.me.permissions.has("MODERATE_MEMBERS")) throw "I do not have the `moderate_members` permission"; |
| 229 | // Do not allow the user to have admin or be the owner |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame^] | 230 | if (apply.permissions.has("ADMINISTRATOR") || apply.id == interaction.guild.ownerId) throw "You cannot mute an admin or the owner" |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 231 | // Do not allow muting Nucleus |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame^] | 232 | if (member.id == interaction.guild.me.id) throw "I cannot mute myself" |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 233 | // Allow the owner to mute anyone |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame^] | 234 | if (member.id == interaction.guild.ownerId) return true |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 235 | // Check if the user has moderate_members permission |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame^] | 236 | if (! member.permissions.has("MODERATE_MEMBERS")) throw "You do not have the `moderate_members` permission"; |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 237 | // Check if the user is below on the role list |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 238 | if (! (memberPos > applyPos)) throw "You do not have a role higher than that member" |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 239 | // Allow mute |
| 240 | return true |
| 241 | } |
| 242 | |
| 243 | export { command, callback, check }; |