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