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