PineappleFan | 19b002b | 2022-05-19 11:54:01 +0100 | [diff] [blame^] | 1 | import { CommandInteraction } from "discord.js"; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 2 | import { SlashCommandSubcommandBuilder } from "@discordjs/builders"; |
| 3 | import { WrappedCheck } from "jshaiku"; |
PineappleFan | 19b002b | 2022-05-19 11:54:01 +0100 | [diff] [blame^] | 4 | import humanizeDuration from 'humanize-duration'; |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 5 | |
| 6 | const command = (builder: SlashCommandSubcommandBuilder) => |
| 7 | builder |
| 8 | .setName("slowmode") |
| 9 | .setDescription("Manages slowmode in a channel") |
PineappleFan | 19b002b | 2022-05-19 11:54:01 +0100 | [diff] [blame^] | 10 | .addIntegerOption(option => option.setName("seconds").setDescription("The seconds between messages").setRequired(false)) |
| 11 | .addIntegerOption(option => option.setName("minutes").setDescription("The minutes between messages").setRequired(false)) |
| 12 | .addIntegerOption(option => option.setName("hours").setDescription("The hours between messages").setRequired(false)) |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 13 | |
PineappleFan | 19b002b | 2022-05-19 11:54:01 +0100 | [diff] [blame^] | 14 | const callback = (interaction: CommandInteraction) => { |
| 15 | let seconds = interaction.option.getInteger("seconds") |
| 16 | let minutes = interaction.option.getInteger("minutes") |
| 17 | let hours = interaction.option.getInteger("hours") |
| 18 | let totalTime = seconds + (minutes * 60) + (hours * 60 * 60) |
| 19 | |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 20 | let confirmation = await new confirmationMessage(interaction) |
PineappleFan | 19b002b | 2022-05-19 11:54:01 +0100 | [diff] [blame^] | 21 | .setEmoji("PUNISH.SLOWMODE.RED") |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 22 | .setTitle("Slowmode") |
| 23 | .setDescription(keyValueList({ |
PineappleFan | 19b002b | 2022-05-19 11:54:01 +0100 | [diff] [blame^] | 24 | "delay": `${totalTime ? humanizeDuration(totalTime * 1000) : "*No delay*"}` |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 25 | }) |
PineappleFan | 19b002b | 2022-05-19 11:54:01 +0100 | [diff] [blame^] | 26 | + `Are you sure you want to enable slowmode in this channel?`) |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 27 | .setColor("Danger") |
| 28 | // pluralize("day", interaction.options.getInteger("delete")) |
| 29 | // const pluralize = (word: string, count: number) => { return count === 1 ? word : word + "s" } |
| 30 | .send() |
| 31 | if (confirmation.success) { |
| 32 | try { |
PineappleFan | 19b002b | 2022-05-19 11:54:01 +0100 | [diff] [blame^] | 33 | await interaction.setRateLimitPerUser(totalTime, "Nucleus slowmode") |
| 34 | } catch { |
| 35 | return await interaction.editReply({embeds: [new generateEmojiEmbed() |
| 36 | .setEmoji("PUNISH.SLOWMODE.RED") |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 37 | .setTitle(`Slowmode`) |
PineappleFan | 19b002b | 2022-05-19 11:54:01 +0100 | [diff] [blame^] | 38 | .setDescription("Something went wrong and the slowmode could not be set.") |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 39 | .setStatus("Danger") |
| 40 | ], components: []}) |
| 41 | } |
| 42 | await interaction.editReply({embeds: [new generateEmojiEmbed() |
PineappleFan | 19b002b | 2022-05-19 11:54:01 +0100 | [diff] [blame^] | 43 | .setEmoji("PUNISH.NICKNAME.GREEN") |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 44 | .setTitle(`Slowmode`) |
| 45 | .setDescription("The channel slowmode was set successfully") |
| 46 | .setStatus("Success") |
| 47 | ], components: []}) |
| 48 | } else { |
| 49 | await interaction.editReply({embeds: [new generateEmojiEmbed() |
PineappleFan | 19b002b | 2022-05-19 11:54:01 +0100 | [diff] [blame^] | 50 | .setEmoji("PUNISH.SLOWMODE.GREEN") |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 51 | .setTitle(`Slowmode`) |
| 52 | .setDescription("No changes were made") |
| 53 | .setStatus("Success") |
| 54 | ], components: []}) |
| 55 | } |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => { |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 59 | let member = (interaction.member as GuildMember) |
PineappleFan | 19b002b | 2022-05-19 11:54:01 +0100 | [diff] [blame^] | 60 | let me = (interaction.guild.me as GuildMember) |
| 61 | // Check if Nucleus can edit the channel |
| 62 | if (! interaction.guild.me.permission.has("MANAGE_CHANNELS")) throw "I do not have permission to edit this channel" |
| 63 | // Allow the owner to set any channel |
| 64 | if (member.id == interaction.guild.ownerId) return true |
| 65 | // Check if the user has manage_channels permission |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 66 | if (! member.permissions.has("MANAGE_CHANNELS")) throw "You do not have the `manage_channels` permission"; |
| 67 | // Allow slowmode |
| 68 | return true |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 69 | } |
| 70 | |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 71 | export { command, callback, check }; |