pineafan | 167bde3 | 2022-05-19 19:33:46 +0100 | [diff] [blame] | 1 | import humanizeDuration from 'humanize-duration'; |
| 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"; |
| 7 | import generateEmojiEmbed from "../../utils/generateEmojiEmbed.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 8 | |
| 9 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 10 | builder |
| 11 | .setName("slowmode") |
| 12 | .setDescription("Manages slowmode in a channel") |
pineafan | 167bde3 | 2022-05-19 19:33:46 +0100 | [diff] [blame] | 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 | 167bde3 | 2022-05-19 19:33:46 +0100 | [diff] [blame] | 21 | const callback = async (interaction: CommandInteraction) => { |
| 22 | let time = parseInt(interaction.options.getString("time") ?? "0"); |
| 23 | if (time === 0 && (interaction.channel as TextChannel).rateLimitPerUser === 0) { time = 10 } |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 24 | let confirmation = await new confirmationMessage(interaction) |
pineafan | 167bde3 | 2022-05-19 19:33:46 +0100 | [diff] [blame] | 25 | .setEmoji("CHANNEL.SLOWMODE.RED") |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 26 | .setTitle("Slowmode") |
| 27 | .setDescription(keyValueList({ |
pineafan | 167bde3 | 2022-05-19 19:33:46 +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 | 167bde3 | 2022-05-19 19:33:46 +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 | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 32 | .send() |
| 33 | if (confirmation.success) { |
| 34 | try { |
pineafan | 167bde3 | 2022-05-19 19:33:46 +0100 | [diff] [blame] | 35 | (interaction.channel as TextChannel).setRateLimitPerUser(time) |
| 36 | } catch (e) { |
| 37 | await interaction.editReply({embeds: [new generateEmojiEmbed() |
| 38 | .setEmoji("CHANNEL.SLOWMODE.RED") |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 39 | .setTitle(`Slowmode`) |
pineafan | 6702cef | 2022-06-13 17:52:37 +0100 | [diff] [blame^] | 40 | .setDescription("Something went wrong while setting the slowmode") |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 41 | .setStatus("Danger") |
| 42 | ], components: []}) |
| 43 | } |
| 44 | await interaction.editReply({embeds: [new generateEmojiEmbed() |
pineafan | 167bde3 | 2022-05-19 19:33:46 +0100 | [diff] [blame] | 45 | .setEmoji(`CHANNEL.SLOWMODE.GREEN`) |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 46 | .setTitle(`Slowmode`) |
| 47 | .setDescription("The channel slowmode was set successfully") |
| 48 | .setStatus("Success") |
| 49 | ], components: []}) |
| 50 | } else { |
| 51 | await interaction.editReply({embeds: [new generateEmojiEmbed() |
pineafan | 167bde3 | 2022-05-19 19:33:46 +0100 | [diff] [blame] | 52 | .setEmoji("CHANNEL.SLOWMODE.GREEN") |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 53 | .setTitle(`Slowmode`) |
| 54 | .setDescription("No changes were made") |
| 55 | .setStatus("Success") |
| 56 | ], components: []}) |
| 57 | } |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => { |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 61 | let member = (interaction.member as GuildMember) |
pineafan | 167bde3 | 2022-05-19 19:33:46 +0100 | [diff] [blame] | 62 | // Check if Nucleus can set the slowmode |
| 63 | if (! interaction.guild.me.permissions.has("MANAGE_CHANNELS")) throw "I do not have the `manage_channels` permission"; |
| 64 | // Check if the user has manage_channel permission |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 65 | if (! member.permissions.has("MANAGE_CHANNELS")) throw "You do not have the `manage_channels` permission"; |
| 66 | // Allow slowmode |
| 67 | return true |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 68 | } |
| 69 | |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 70 | export { command, callback, check }; |