blob: 249874639ea2edcc62f1d701392a9bd0370f8c69 [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")
pineafan663dc472022-05-10 18:13:47 +010032 .send()
33 if (confirmation.success) {
34 try {
pineafan167bde32022-05-19 19:33:46 +010035 (interaction.channel as TextChannel).setRateLimitPerUser(time)
36 } catch (e) {
37 await interaction.editReply({embeds: [new generateEmojiEmbed()
38 .setEmoji("CHANNEL.SLOWMODE.RED")
pineafan663dc472022-05-10 18:13:47 +010039 .setTitle(`Slowmode`)
pineafan6702cef2022-06-13 17:52:37 +010040 .setDescription("Something went wrong while setting the slowmode")
pineafan663dc472022-05-10 18:13:47 +010041 .setStatus("Danger")
42 ], components: []})
43 }
44 await interaction.editReply({embeds: [new generateEmojiEmbed()
pineafan167bde32022-05-19 19:33:46 +010045 .setEmoji(`CHANNEL.SLOWMODE.GREEN`)
pineafan663dc472022-05-10 18:13:47 +010046 .setTitle(`Slowmode`)
47 .setDescription("The channel slowmode was set successfully")
48 .setStatus("Success")
49 ], components: []})
50 } else {
51 await interaction.editReply({embeds: [new generateEmojiEmbed()
pineafan167bde32022-05-19 19:33:46 +010052 .setEmoji("CHANNEL.SLOWMODE.GREEN")
pineafan663dc472022-05-10 18:13:47 +010053 .setTitle(`Slowmode`)
54 .setDescription("No changes were made")
55 .setStatus("Success")
56 ], components: []})
57 }
pineafan4f164f32022-02-26 22:07:12 +000058}
59
60const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan663dc472022-05-10 18:13:47 +010061 let member = (interaction.member as GuildMember)
pineafan167bde32022-05-19 19:33:46 +010062 // 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
pineafan663dc472022-05-10 18:13:47 +010065 if (! member.permissions.has("MANAGE_CHANNELS")) throw "You do not have the `manage_channels` permission";
66 // Allow slowmode
67 return true
pineafan4f164f32022-02-26 22:07:12 +000068}
69
pineafan8b4b17f2022-02-27 20:42:52 +000070export { command, callback, check };