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