pineafan | 167bde3 | 2022-05-19 19:33:46 +0100 | [diff] [blame] | 1 | import humanizeDuration from 'humanize-duration'; |
| 2 | import { CommandInteraction, GuildMember, TextChannel } from "discord.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 3 | import { SlashCommandSubcommandBuilder } from "@discordjs/builders"; |
| 4 | import { WrappedCheck } from "jshaiku"; |
pineafan | 167bde3 | 2022-05-19 19:33:46 +0100 | [diff] [blame] | 5 | import keyValueList from "../../utils/generateKeyValueList.js"; |
| 6 | import confirmationMessage from "../../utils/confirmationMessage.js"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 7 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
| 8 | |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 9 | |
| 10 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 11 | builder |
| 12 | .setName("slowmode") |
| 13 | .setDescription("Manages slowmode in a channel") |
pineafan | 167bde3 | 2022-05-19 19:33:46 +0100 | [diff] [blame] | 14 | .addStringOption(option => option.setName("time").setDescription("The delay between messages").setRequired(false).addChoices([ |
| 15 | ["Off", "0"], |
| 16 | ["5 seconds", "5"], ["10 seconds", "10"], ["15 seconds", "15"], ["30 seconds", "30"], |
| 17 | ["1 minute", "60"], ["2 minutes", "120"], ["5 minutes", "300"], ["10 minutes", "600"], |
| 18 | ["15 minutes", "900"], ["30 minutes", "1800"], |
| 19 | ["1 hour", "3600"], ["2 hours", "7200"], ["6 hours", "21600"] |
| 20 | ])) |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 21 | |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 22 | const callback = async (interaction: CommandInteraction): Promise<any> => { |
pineafan | 167bde3 | 2022-05-19 19:33:46 +0100 | [diff] [blame] | 23 | let time = parseInt(interaction.options.getString("time") ?? "0"); |
| 24 | if (time === 0 && (interaction.channel as TextChannel).rateLimitPerUser === 0) { time = 10 } |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 25 | let confirmation = await new confirmationMessage(interaction) |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 26 | .setEmoji("CHANNEL.SLOWMODE.OFF") |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 27 | .setTitle("Slowmode") |
| 28 | .setDescription(keyValueList({ |
pineafan | 167bde3 | 2022-05-19 19:33:46 +0100 | [diff] [blame] | 29 | "time": time ? humanizeDuration(time * 1000, { round: true }) : "No delay", |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 30 | }) |
pineafan | 167bde3 | 2022-05-19 19:33:46 +0100 | [diff] [blame] | 31 | + `Are you sure you want to set the slowmode in this channel?`) |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 32 | .setColor("Danger") |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 33 | .send() |
pineafan | 02ba023 | 2022-07-24 22:16:15 +0100 | [diff] [blame] | 34 | if (confirmation.cancelled) return |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 35 | if (confirmation.success) { |
| 36 | try { |
pineafan | 167bde3 | 2022-05-19 19:33:46 +0100 | [diff] [blame] | 37 | (interaction.channel as TextChannel).setRateLimitPerUser(time) |
| 38 | } catch (e) { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 39 | await interaction.editReply({embeds: [new EmojiEmbed() |
| 40 | .setEmoji("CHANNEL.SLOWMODE.OFF") |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 41 | .setTitle(`Slowmode`) |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 42 | .setDescription("Something went wrong while setting the slowmode") |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 43 | .setStatus("Danger") |
| 44 | ], components: []}) |
| 45 | } |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 46 | await interaction.editReply({embeds: [new EmojiEmbed() |
| 47 | .setEmoji(`CHANNEL.SLOWMODE.ON`) |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 48 | .setTitle(`Slowmode`) |
| 49 | .setDescription("The channel slowmode was set successfully") |
| 50 | .setStatus("Success") |
| 51 | ], components: []}) |
| 52 | } else { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 53 | await interaction.editReply({embeds: [new EmojiEmbed() |
| 54 | .setEmoji("CHANNEL.SLOWMODE.ON") |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 55 | .setTitle(`Slowmode`) |
| 56 | .setDescription("No changes were made") |
| 57 | .setStatus("Success") |
| 58 | ], components: []}) |
| 59 | } |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => { |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 63 | let member = (interaction.member as GuildMember) |
pineafan | 167bde3 | 2022-05-19 19:33:46 +0100 | [diff] [blame] | 64 | // Check if Nucleus can set the slowmode |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame^] | 65 | if (! interaction.guild.me.permissions.has("MANAGE_CHANNELS")) throw "I do not have the *Manage Channels* permission"; |
pineafan | 167bde3 | 2022-05-19 19:33:46 +0100 | [diff] [blame] | 66 | // Check if the user has manage_channel permission |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame^] | 67 | if (! member.permissions.has("MANAGE_CHANNELS")) throw "You do not have the *Manage Channels* permission"; |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 68 | // Allow slowmode |
| 69 | return true |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 70 | } |
| 71 | |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 72 | export { command, callback, check }; |