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 | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 37 | if (time === 0 && (interaction.channel as TextChannel).rateLimitPerUser === 0) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 38 | time = 10; |
| 39 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 40 | const confirmation = await new confirmationMessage(interaction) |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 41 | .setEmoji("CHANNEL.SLOWMODE.OFF") |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 42 | .setTitle("Slowmode") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 43 | .setDescription( |
| 44 | keyValueList({ |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 45 | time: time ? humanizeDuration(time * 1000, { round: true }) : "No delay" |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 46 | }) + "Are you sure you want to set the slowmode in this channel?" |
| 47 | ) |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 48 | .setColor("Danger") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 49 | .send(); |
| 50 | if (confirmation.cancelled) return; |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 51 | if (confirmation.success) { |
| 52 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 53 | (interaction.channel as TextChannel).setRateLimitPerUser(time); |
pineafan | 167bde3 | 2022-05-19 19:33:46 +0100 | [diff] [blame] | 54 | } catch (e) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 55 | await interaction.editReply({ |
| 56 | embeds: [ |
| 57 | new EmojiEmbed() |
| 58 | .setEmoji("CHANNEL.SLOWMODE.OFF") |
| 59 | .setTitle("Slowmode") |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 60 | .setDescription("Something went wrong while setting the slowmode") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 61 | .setStatus("Danger") |
| 62 | ], |
| 63 | components: [] |
| 64 | }); |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 65 | } |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 66 | await interaction.editReply({ |
| 67 | embeds: [ |
| 68 | new EmojiEmbed() |
| 69 | .setEmoji("CHANNEL.SLOWMODE.ON") |
| 70 | .setTitle("Slowmode") |
| 71 | .setDescription("The channel slowmode was set successfully") |
| 72 | .setStatus("Success") |
| 73 | ], |
| 74 | components: [] |
| 75 | }); |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 76 | } else { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 77 | await interaction.editReply({ |
| 78 | embeds: [ |
| 79 | new EmojiEmbed() |
| 80 | .setEmoji("CHANNEL.SLOWMODE.ON") |
| 81 | .setTitle("Slowmode") |
| 82 | .setDescription("No changes were made") |
| 83 | .setStatus("Success") |
| 84 | ], |
| 85 | components: [] |
| 86 | }); |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 87 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 88 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 89 | |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 90 | const check = (interaction: CommandInteraction) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 91 | const member = interaction.member as GuildMember; |
pineafan | 167bde3 | 2022-05-19 19:33:46 +0100 | [diff] [blame] | 92 | // Check if Nucleus can set the slowmode |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 93 | if (!interaction.guild.me.permissions.has("MANAGE_CHANNELS")) |
| 94 | throw "I do not have the *Manage Channels* permission"; |
pineafan | 167bde3 | 2022-05-19 19:33:46 +0100 | [diff] [blame] | 95 | // Check if the user has manage_channel permission |
Skyler Grey | 11236ba | 2022-08-08 21:13:33 +0100 | [diff] [blame] | 96 | 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] | 97 | // Allow slowmode |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 98 | return true; |
| 99 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 100 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 101 | export { command, callback, check }; |