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