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 | 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 | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 34 | let config = await client.database.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 | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 41 | new generateEmojiEmbed() |
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 | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 96 | if (component.customId == "cancel") return interaction.editReply({embeds: [new generateEmojiEmbed() |
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 | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 114 | new generateEmojiEmbed() |
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({ |
| 126 | "user": `<@!${user.id}> (${user.user.username})`, |
| 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 | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 138 | let config = await client.database.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 | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 142 | embeds: [new generateEmojiEmbed() |
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 {} |
| 159 | try { |
pineafan | e625d78 | 2022-05-09 18:04:32 +0100 | [diff] [blame] | 160 | if (config.moderation.mute.timeout) { |
| 161 | (interaction.options.getMember("user") as GuildMember).timeout(muteTime * 1000, interaction.options.getString("reason") || "No reason provided") |
| 162 | } |
| 163 | if (config.moderation.mute.role) { |
| 164 | (interaction.options.getMember("user") as GuildMember).roles.add(config.moderation.mute.role) |
| 165 | } |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 166 | } catch { |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 167 | await interaction.editReply({embeds: [new generateEmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 168 | .setEmoji("PUNISH.MUTE.RED") |
| 169 | .setTitle(`Mute`) |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 170 | .setDescription("Something went wrong and the user was not mute") |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 171 | .setStatus("Danger") |
| 172 | ], components: []}) |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 173 | if (dmd) await dm.delete() |
| 174 | return |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 175 | } |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 176 | let failed = (dmd == false && interaction.options.getString("notify") != "no") |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 177 | await interaction.editReply({embeds: [new generateEmojiEmbed() |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 178 | .setEmoji(`PUNISH.MUTE.${failed ? "YELLOW" : "GREEN"}`) |
| 179 | .setTitle(`Mute`) |
| 180 | .setDescription("The member was muted" + (failed ? ", but could not be notified" : "")) |
| 181 | .setStatus(failed ? "Warning" : "Success") |
| 182 | ], components: []}) |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 183 | let data = { |
| 184 | meta:{ |
| 185 | type: 'memberMute', |
| 186 | displayName: 'Member Muted', |
| 187 | calculateType: 'guildMemberPunish', |
| 188 | color: NucleusColors.yellow, |
| 189 | emoji: 'PUNISH.WARN.YELLOW', |
| 190 | timestamp: new Date().getTime() |
| 191 | }, |
| 192 | list: { |
| 193 | user: entry((interaction.options.getMember("user") as GuildMember).user.id, renderUser((interaction.options.getMember("user") as GuildMember).user)), |
| 194 | mutedBy: entry(interaction.member.user.id, renderUser(interaction.member.user)), |
| 195 | time: entry(muteTime, `${humanizeDuration(muteTime * 1000, {round: true})}`), |
| 196 | reason: (interaction.options.getString("reason") ? `\n> ${interaction.options.getString("reason")}` : "No reason provided") |
| 197 | }, |
| 198 | hidden: { |
| 199 | guild: interaction.guild.id |
| 200 | } |
| 201 | } |
PineappleFan | b3dd83c | 2022-06-17 10:53:48 +0100 | [diff] [blame] | 202 | log(data, client); |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 203 | } else { |
pineafan | 377794f | 2022-04-18 19:01:01 +0100 | [diff] [blame] | 204 | await interaction.editReply({embeds: [new generateEmojiEmbed() |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 205 | .setEmoji("PUNISH.MUTE.GREEN") |
| 206 | .setTitle(`Mute`) |
| 207 | .setDescription("No changes were made") |
| 208 | .setStatus("Success") |
| 209 | ], components: []}) |
| 210 | } |
| 211 | } |
| 212 | |
| 213 | const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => { |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 214 | let member = (interaction.member as GuildMember) |
| 215 | let me = (interaction.guild.me as GuildMember) |
| 216 | let apply = (interaction.options.getMember("user") as GuildMember) |
| 217 | if (member == null || me == null || apply == null) throw "That member is not in the server" |
| 218 | let memberPos = member.roles ? member.roles.highest.position : 0 |
| 219 | let mePos = me.roles ? me.roles.highest.position : 0 |
| 220 | let applyPos = apply.roles ? apply.roles.highest.position : 0 |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 221 | // Check if Nucleus can mute the member |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 222 | 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] | 223 | // Check if Nucleus has permission to mute |
PineappleFan | 5fe720d | 2022-05-19 12:01:49 +0100 | [diff] [blame] | 224 | 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] | 225 | // Do not allow the user to have admin or be the owner |
PineappleFan | 5fe720d | 2022-05-19 12:01:49 +0100 | [diff] [blame] | 226 | 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] | 227 | // Do not allow muting Nucleus |
PineappleFan | 5fe720d | 2022-05-19 12:01:49 +0100 | [diff] [blame] | 228 | if (member.id == me.id) throw "I cannot mute myself" |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 229 | // Allow the owner to mute anyone |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 230 | if (member.id == interaction.guild.ownerId) return true |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 231 | // Check if the user has moderate_members permission |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 232 | 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] | 233 | // Check if the user is below on the role list |
pineafan | 5d1908e | 2022-02-28 21:34:47 +0000 | [diff] [blame] | 234 | 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] | 235 | // Allow mute |
| 236 | return true |
| 237 | } |
| 238 | |
| 239 | export { command, callback, check }; |