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 | |
| 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") |
| 13 | .addStringOption(option => option.setName("time").setDescription("The delay between messages").setRequired(false).addChoices([ |
| 14 | ["Off", "0"], |
| 15 | ["5 seconds", "5"], ["10 seconds", "10"], ["15 seconds", "15"], ["30 seconds", "30"], |
| 16 | ["1 minute", "60"], ["2 minutes", "120"], ["5 minutes", "300"], ["10 minutes", "600"], |
| 17 | ["15 minutes", "900"], ["30 minutes", "1800"], |
| 18 | ["1 hour", "3600"], ["2 hours", "7200"], ["6 hours", "21600"] |
| 19 | ])); |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 20 | |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 21 | const callback = async (interaction: CommandInteraction): Promise<void> => { |
pineafan | 167bde3 | 2022-05-19 19:33:46 +0100 | [diff] [blame] | 22 | let time = parseInt(interaction.options.getString("time") ?? "0"); |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 23 | if (time === 0 && (interaction.channel as TextChannel).rateLimitPerUser === 0) { time = 10; } |
| 24 | const confirmation = await new confirmationMessage(interaction) |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 25 | .setEmoji("CHANNEL.SLOWMODE.OFF") |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 26 | .setTitle("Slowmode") |
| 27 | .setDescription(keyValueList({ |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 28 | "time": time ? humanizeDuration(time * 1000, { round: true }) : "No delay" |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 29 | }) |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 30 | + "Are you sure you want to set the slowmode in this channel?") |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 31 | .setColor("Danger") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 32 | .send(); |
| 33 | if (confirmation.cancelled) return; |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 34 | if (confirmation.success) { |
| 35 | try { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 36 | (interaction.channel as TextChannel).setRateLimitPerUser(time); |
pineafan | 167bde3 | 2022-05-19 19:33:46 +0100 | [diff] [blame] | 37 | } catch (e) { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 38 | await interaction.editReply({embeds: [new EmojiEmbed() |
| 39 | .setEmoji("CHANNEL.SLOWMODE.OFF") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 40 | .setTitle("Slowmode") |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame] | 41 | .setDescription("Something went wrong while setting the slowmode") |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 42 | .setStatus("Danger") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 43 | ], components: []}); |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 44 | } |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 45 | await interaction.editReply({embeds: [new EmojiEmbed() |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 46 | .setEmoji("CHANNEL.SLOWMODE.ON") |
| 47 | .setTitle("Slowmode") |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 48 | .setDescription("The channel slowmode was set successfully") |
| 49 | .setStatus("Success") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 50 | ], components: []}); |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 51 | } else { |
pineafan | 4edb776 | 2022-06-26 19:21:04 +0100 | [diff] [blame] | 52 | await interaction.editReply({embeds: [new EmojiEmbed() |
| 53 | .setEmoji("CHANNEL.SLOWMODE.ON") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 54 | .setTitle("Slowmode") |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 55 | .setDescription("No changes were made") |
| 56 | .setStatus("Success") |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 57 | ], components: []}); |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 58 | } |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 59 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 60 | |
pineafan | bd02b4a | 2022-08-05 22:01:38 +0100 | [diff] [blame] | 61 | const check = (interaction: CommandInteraction) => { |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 62 | const member = (interaction.member as GuildMember); |
pineafan | 167bde3 | 2022-05-19 19:33:46 +0100 | [diff] [blame] | 63 | // Check if Nucleus can set the slowmode |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 64 | if (! interaction.guild.me.permissions.has("MANAGE_CHANNELS")) throw "I do not have the *Manage Channels* permission"; |
pineafan | 167bde3 | 2022-05-19 19:33:46 +0100 | [diff] [blame] | 65 | // Check if the user has manage_channel permission |
pineafan | e23c4ec | 2022-07-27 21:56:27 +0100 | [diff] [blame] | 66 | 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] | 67 | // Allow slowmode |
pineafan | 63fc5e2 | 2022-08-04 22:04:10 +0100 | [diff] [blame] | 68 | return true; |
| 69 | }; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 70 | |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 71 | export { command, callback, check }; |