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"; |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 4 | import type { SlashCommandSubcommandBuilder } from "discord.js"; |
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 | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 50 | .setFailedMessage("No changes were made", "Danger", "CHANNEL.SLOWMODE.ON") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 51 | .send(); |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 52 | if (confirmation.cancelled || !confirmation.success) return; |
| 53 | try { |
| 54 | (interaction.channel as TextChannel).setRateLimitPerUser(time); |
| 55 | } catch (e) { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 56 | await interaction.editReply({ |
| 57 | embeds: [ |
| 58 | new EmojiEmbed() |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 59 | .setEmoji("CHANNEL.SLOWMODE.OFF") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 60 | .setTitle("Slowmode") |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [diff] [blame] | 61 | .setDescription("Something went wrong while setting the slowmode") |
| 62 | .setStatus("Danger") |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 63 | ], |
| 64 | components: [] |
| 65 | }); |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 66 | } |
PineaFan | 1dee28f | 2023-01-16 22:09:07 +0000 | [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 | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 77 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 78 | |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 79 | const check = (interaction: CommandInteraction, partial: boolean = false) => { |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 80 | const member = interaction.member as GuildMember; |
pineafan | 167bde3 | 2022-05-19 19:33:46 +0100 | [diff] [blame] | 81 | // Check if the user has manage_channel permission |
PineaFan | 0d06edc | 2023-01-17 22:10:31 +0000 | [diff] [blame] | 82 | if (!member.permissions.has("ManageChannels")) return "You do not have the *Manage Channels* permission"; |
TheCodedProf | f86ba09 | 2023-01-27 17:10:07 -0500 | [diff] [blame] | 83 | if (partial) return true; |
| 84 | // Check if Nucleus can set the slowmode |
| 85 | if (!interaction.guild!.members.me!.permissions.has("ManageChannels")) return "I do not have the *Manage Channels* permission"; |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 86 | // Allow slowmode |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 87 | return true; |
| 88 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 89 | |
Skyler Grey | 75ea917 | 2022-08-06 10:22:23 +0100 | [diff] [blame] | 90 | export { command, callback, check }; |
TheCodedProf | a112f61 | 2023-01-28 18:06:45 -0500 | [diff] [blame^] | 91 | export const metadata = { |
| 92 | longDescription: "Stops members from being able to send messages without waiting a certain amount of time between messages.", |
| 93 | premiumOnly: true, |
| 94 | } |