blob: b91f065e3e8ca8991d6ff3535ac5a9a036f996d4 [file] [log] [blame]
pineafan167bde32022-05-19 19:33:46 +01001import humanizeDuration from 'humanize-duration';
2import { CommandInteraction, GuildMember, TextChannel } from "discord.js";
pineafan4f164f32022-02-26 22:07:12 +00003import { SlashCommandSubcommandBuilder } from "@discordjs/builders";
4import { WrappedCheck } from "jshaiku";
pineafan167bde32022-05-19 19:33:46 +01005import keyValueList from "../../utils/generateKeyValueList.js";
6import confirmationMessage from "../../utils/confirmationMessage.js";
7import generateEmojiEmbed from "../../utils/generateEmojiEmbed.js";
pineafan4f164f32022-02-26 22:07:12 +00008
9const command = (builder: SlashCommandSubcommandBuilder) =>
10 builder
11 .setName("slowmode")
12 .setDescription("Manages slowmode in a channel")
pineafan167bde32022-05-19 19:33:46 +010013 .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 ]))
pineafan4f164f32022-02-26 22:07:12 +000020
pineafan167bde32022-05-19 19:33:46 +010021const 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 }
pineafan663dc472022-05-10 18:13:47 +010024 let confirmation = await new confirmationMessage(interaction)
pineafan167bde32022-05-19 19:33:46 +010025 .setEmoji("CHANNEL.SLOWMODE.RED")
pineafan663dc472022-05-10 18:13:47 +010026 .setTitle("Slowmode")
27 .setDescription(keyValueList({
pineafan167bde32022-05-19 19:33:46 +010028 "time": time ? humanizeDuration(time * 1000, { round: true }) : "No delay",
pineafan663dc472022-05-10 18:13:47 +010029 })
pineafan167bde32022-05-19 19:33:46 +010030 + `Are you sure you want to set the slowmode in this channel?`)
pineafan663dc472022-05-10 18:13:47 +010031 .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 {
pineafan167bde32022-05-19 19:33:46 +010037 (interaction.channel as TextChannel).setRateLimitPerUser(time)
38 } catch (e) {
39 await interaction.editReply({embeds: [new generateEmojiEmbed()
40 .setEmoji("CHANNEL.SLOWMODE.RED")
pineafan663dc472022-05-10 18:13:47 +010041 .setTitle(`Slowmode`)
pineafan167bde32022-05-19 19:33:46 +010042 .setDescription("An error occurred while setting the slowmode")
pineafan663dc472022-05-10 18:13:47 +010043 .setStatus("Danger")
44 ], components: []})
45 }
46 await interaction.editReply({embeds: [new generateEmojiEmbed()
pineafan167bde32022-05-19 19:33:46 +010047 .setEmoji(`CHANNEL.SLOWMODE.GREEN`)
pineafan663dc472022-05-10 18:13:47 +010048 .setTitle(`Slowmode`)
49 .setDescription("The channel slowmode was set successfully")
50 .setStatus("Success")
51 ], components: []})
52 } else {
53 await interaction.editReply({embeds: [new generateEmojiEmbed()
pineafan167bde32022-05-19 19:33:46 +010054 .setEmoji("CHANNEL.SLOWMODE.GREEN")
pineafan663dc472022-05-10 18:13:47 +010055 .setTitle(`Slowmode`)
56 .setDescription("No changes were made")
57 .setStatus("Success")
58 ], components: []})
59 }
pineafan4f164f32022-02-26 22:07:12 +000060}
61
62const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan663dc472022-05-10 18:13:47 +010063 let member = (interaction.member as GuildMember)
pineafan167bde32022-05-19 19:33:46 +010064 // 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
pineafan663dc472022-05-10 18:13:47 +010067 if (! member.permissions.has("MANAGE_CHANNELS")) throw "You do not have the `manage_channels` permission";
68 // Allow slowmode
69 return true
pineafan4f164f32022-02-26 22:07:12 +000070}
71
pineafan8b4b17f2022-02-27 20:42:52 +000072export { command, callback, check };