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) |
PineaFan | 64486c4 | 2022-12-28 09:21:04 +0000 | [diff] [blame] | 18 | .addChoices( |
| 19 | {name: "Off", value: "0"}, |
| 20 | {name: "5 seconds", value: "5"}, |
| 21 | {name: "10 seconds", value: "10"}, |
| 22 | {name: "15 seconds", value: "15"}, |
| 23 | {name: "30 seconds", value: "30"}, |
| 24 | {name: "1 minute", value: "60"}, |
| 25 | {name: "2 minutes", value: "120"}, |
| 26 | {name: "5 minutes", value: "300"}, |
| 27 | {name: "10 minutes", value: "600"}, |
| 28 | {name: "15 minutes", value: "900"}, |
| 29 | {name: "30 minutes", value: "1800"}, |
| 30 | {name: "1 hour", value: "3600"}, |
| 31 | {name: "2 hours", value: "7200"}, |
| 32 | {name: "6 hours", value: "21600"} |
| 33 | ) |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 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 | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 37 | let time = parseInt(interaction.options.get("time")?.value as string || "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 |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 94 | if (!interaction.guild!.members.me!.permissions.has("ManageChannels")) throw new Error("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 |
PineaFan | a00db1b | 2023-01-02 15:32:54 +0000 | [diff] [blame] | 96 | if (!member.permissions.has("ManageChannels")) throw new Error("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 }; |