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