pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 1 | import humanizeDuration from "humanize-duration"; |
pineafan | 167bde3 | 2022-05-19 19:33:46 +0100 | [diff] [blame] | 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"; |
pineafan | 167bde3 | 2022-05-19 19:33:46 +0100 | [diff] [blame] | 4 | import keyValueList from "../../utils/generateKeyValueList.js"; |
| 5 | import confirmationMessage from "../../utils/confirmationMessage.js"; |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 6 | import EmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
| 7 | |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 8 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 9 | builder |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 10 | .setName("slowmode") |
| 11 | .setDescription("Manages slowmode in a channel") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 12 | .addStringOption((option) => |
| 13 | option |
| 14 | .setName("time") |
| 15 | .setDescription("The delay between messages") |
| 16 | .setRequired(false) |
| 17 | .addChoices([ |
| 18 | ["Off", "0"], |
| 19 | ["5 seconds", "5"], |
| 20 | ["10 seconds", "10"], |
| 21 | ["15 seconds", "15"], |
| 22 | ["30 seconds", "30"], |
| 23 | ["1 minute", "60"], |
| 24 | ["2 minutes", "120"], |
| 25 | ["5 minutes", "300"], |
| 26 | ["10 minutes", "600"], |
| 27 | ["15 minutes", "900"], |
| 28 | ["30 minutes", "1800"], |
| 29 | ["1 hour", "3600"], |
| 30 | ["2 hours", "7200"], |
| 31 | ["6 hours", "21600"] |
| 32 | ]) |
| 33 | ); |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 34 | |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 35 | const callback = async (interaction: CommandInteraction): Promise<void> => { |
pineafan | 167bde3 | 2022-05-19 19:33:46 +0100 | [diff] [blame] | 36 | let time = parseInt(interaction.options.getString("time") ?? "0"); |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 37 | if ( |
| 38 | time === 0 && |
| 39 | (interaction.channel as TextChannel).rateLimitPerUser === 0 |
| 40 | ) { |
| 41 | time = 10; |
| 42 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 43 | const confirmation = await new confirmationMessage(interaction) |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 44 | .setEmoji("CHANNEL.SLOWMODE.OFF") |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 45 | .setTitle("Slowmode") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 46 | .setDescription( |
| 47 | keyValueList({ |
| 48 | time: time |
| 49 | ? humanizeDuration(time * 1000, { round: true }) |
| 50 | : "No delay" |
| 51 | }) + "Are you sure you want to set the slowmode in this channel?" |
| 52 | ) |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 53 | .setColor("Danger") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 54 | .send(); |
| 55 | if (confirmation.cancelled) return; |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 56 | if (confirmation.success) { |
| 57 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 58 | (interaction.channel as TextChannel).setRateLimitPerUser(time); |
pineafan | 167bde3 | 2022-05-19 19:33:46 +0100 | [diff] [blame] | 59 | } catch (e) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 60 | await interaction.editReply({ |
| 61 | embeds: [ |
| 62 | new EmojiEmbed() |
| 63 | .setEmoji("CHANNEL.SLOWMODE.OFF") |
| 64 | .setTitle("Slowmode") |
| 65 | .setDescription( |
| 66 | "Something went wrong while setting the slowmode" |
| 67 | ) |
| 68 | .setStatus("Danger") |
| 69 | ], |
| 70 | components: [] |
| 71 | }); |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 72 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 73 | await interaction.editReply({ |
| 74 | embeds: [ |
| 75 | new EmojiEmbed() |
| 76 | .setEmoji("CHANNEL.SLOWMODE.ON") |
| 77 | .setTitle("Slowmode") |
| 78 | .setDescription("The channel slowmode was set successfully") |
| 79 | .setStatus("Success") |
| 80 | ], |
| 81 | components: [] |
| 82 | }); |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 83 | } else { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 84 | await interaction.editReply({ |
| 85 | embeds: [ |
| 86 | new EmojiEmbed() |
| 87 | .setEmoji("CHANNEL.SLOWMODE.ON") |
| 88 | .setTitle("Slowmode") |
| 89 | .setDescription("No changes were made") |
| 90 | .setStatus("Success") |
| 91 | ], |
| 92 | components: [] |
| 93 | }); |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 94 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 95 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 96 | |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 97 | const check = (interaction: CommandInteraction) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 98 | const member = interaction.member as GuildMember; |
pineafan | 167bde3 | 2022-05-19 19:33:46 +0100 | [diff] [blame] | 99 | // Check if Nucleus can set the slowmode |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 100 | if (!interaction.guild.me.permissions.has("MANAGE_CHANNELS")) |
| 101 | throw "I do not have the *Manage Channels* permission"; |
pineafan | 167bde3 | 2022-05-19 19:33:46 +0100 | [diff] [blame] | 102 | // Check if the user has manage_channel permission |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 103 | if (!member.permissions.has("MANAGE_CHANNELS")) |
| 104 | throw "You do not have the *Manage Channels* permission"; |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 105 | // Allow slowmode |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 106 | return true; |
| 107 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 108 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 109 | export { command, callback, check }; |