pineafan | 663dc47 | 2022-05-10 18:13:47 +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 | 663dc47 | 2022-05-10 18:13:47 +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 | 663dc47 | 2022-05-10 18:13:47 +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 | 663dc47 | 2022-05-10 18:13:47 +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 } |
| 24 | let confirmation = await new confirmationMessage(interaction) |
| 25 | .setEmoji("CHANNEL.SLOWMODE.RED") |
| 26 | .setTitle("Slowmode") |
| 27 | .setDescription(keyValueList({ |
| 28 | "time": time ? humanizeDuration(time * 1000, { round: true }) : "No delay", |
| 29 | }) |
| 30 | + `Are you sure you want to set the slowmode in this channel?`) |
| 31 | .setColor("Danger") |
| 32 | // pluralize("day", interaction.options.getInteger("delete")) |
| 33 | // const pluralize = (word: string, count: number) => { return count === 1 ? word : word + "s" } |
| 34 | .send() |
| 35 | if (confirmation.success) { |
| 36 | try { |
| 37 | (interaction.channel as TextChannel).setRateLimitPerUser(time) |
| 38 | } catch (e) { |
| 39 | await interaction.editReply({embeds: [new generateEmojiEmbed() |
| 40 | .setEmoji("CHANNEL.SLOWMODE.RED") |
| 41 | .setTitle(`Slowmode`) |
| 42 | .setDescription("An error occurred while setting the slowmode") |
| 43 | .setStatus("Danger") |
| 44 | ], components: []}) |
| 45 | } |
| 46 | await interaction.editReply({embeds: [new generateEmojiEmbed() |
| 47 | .setEmoji(`CHANNEL.SLOWMODE.GREEN`) |
| 48 | .setTitle(`Slowmode`) |
| 49 | .setDescription("The channel slowmode was set successfully") |
| 50 | .setStatus("Success") |
| 51 | ], components: []}) |
| 52 | } else { |
| 53 | await interaction.editReply({embeds: [new generateEmojiEmbed() |
| 54 | .setEmoji("CHANNEL.SLOWMODE.GREEN") |
| 55 | .setTitle(`Slowmode`) |
| 56 | .setDescription("No changes were made") |
| 57 | .setStatus("Success") |
| 58 | ], components: []}) |
| 59 | } |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => { |
pineafan | 663dc47 | 2022-05-10 18:13:47 +0100 | [diff] [blame] | 63 | let member = (interaction.member as GuildMember) |
| 64 | // Check if Nucleus can set the slowmode |
| 65 | if (! interaction.guild.me.permissions.has("MANAGE_CHANNELS")) throw "I do not have the `manage_channels` permission"; |
| 66 | // Check if the user has manage_channel permission |
| 67 | if (! member.permissions.has("MANAGE_CHANNELS")) throw "You do not have the `manage_channels` permission"; |
| 68 | // Allow slowmode |
| 69 | return true |
pineafan | 4f164f3 | 2022-02-26 22:07:12 +0000 | [diff] [blame] | 70 | } |
| 71 | |
pineafan | 8b4b17f | 2022-02-27 20:42:52 +0000 | [diff] [blame] | 72 | export { command, callback, check }; |