blob: d9a84214b52351e73f0040147ec35f7ff5fc6018 [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";
pineafan4edb7762022-06-26 19:21:04 +01007import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
8
pineafan4f164f32022-02-26 22:07:12 +00009
10const command = (builder: SlashCommandSubcommandBuilder) =>
11 builder
12 .setName("slowmode")
13 .setDescription("Manages slowmode in a channel")
pineafan167bde32022-05-19 19:33:46 +010014 .addStringOption(option => option.setName("time").setDescription("The delay between messages").setRequired(false).addChoices([
15 ["Off", "0"],
16 ["5 seconds", "5"], ["10 seconds", "10"], ["15 seconds", "15"], ["30 seconds", "30"],
17 ["1 minute", "60"], ["2 minutes", "120"], ["5 minutes", "300"], ["10 minutes", "600"],
18 ["15 minutes", "900"], ["30 minutes", "1800"],
19 ["1 hour", "3600"], ["2 hours", "7200"], ["6 hours", "21600"]
20 ]))
pineafan4f164f32022-02-26 22:07:12 +000021
pineafan4edb7762022-06-26 19:21:04 +010022const callback = async (interaction: CommandInteraction): Promise<any> => {
pineafan167bde32022-05-19 19:33:46 +010023 let time = parseInt(interaction.options.getString("time") ?? "0");
24 if (time === 0 && (interaction.channel as TextChannel).rateLimitPerUser === 0) { time = 10 }
pineafan663dc472022-05-10 18:13:47 +010025 let confirmation = await new confirmationMessage(interaction)
pineafan4edb7762022-06-26 19:21:04 +010026 .setEmoji("CHANNEL.SLOWMODE.OFF")
pineafan663dc472022-05-10 18:13:47 +010027 .setTitle("Slowmode")
28 .setDescription(keyValueList({
pineafan167bde32022-05-19 19:33:46 +010029 "time": time ? humanizeDuration(time * 1000, { round: true }) : "No delay",
pineafan663dc472022-05-10 18:13:47 +010030 })
pineafan167bde32022-05-19 19:33:46 +010031 + `Are you sure you want to set the slowmode in this channel?`)
pineafan663dc472022-05-10 18:13:47 +010032 .setColor("Danger")
pineafan663dc472022-05-10 18:13:47 +010033 .send()
34 if (confirmation.success) {
35 try {
pineafan167bde32022-05-19 19:33:46 +010036 (interaction.channel as TextChannel).setRateLimitPerUser(time)
37 } catch (e) {
pineafan4edb7762022-06-26 19:21:04 +010038 await interaction.editReply({embeds: [new EmojiEmbed()
39 .setEmoji("CHANNEL.SLOWMODE.OFF")
pineafan663dc472022-05-10 18:13:47 +010040 .setTitle(`Slowmode`)
pineafan6702cef2022-06-13 17:52:37 +010041 .setDescription("Something went wrong while setting the slowmode")
pineafan663dc472022-05-10 18:13:47 +010042 .setStatus("Danger")
43 ], components: []})
44 }
pineafan4edb7762022-06-26 19:21:04 +010045 await interaction.editReply({embeds: [new EmojiEmbed()
46 .setEmoji(`CHANNEL.SLOWMODE.ON`)
pineafan663dc472022-05-10 18:13:47 +010047 .setTitle(`Slowmode`)
48 .setDescription("The channel slowmode was set successfully")
49 .setStatus("Success")
50 ], components: []})
51 } else {
pineafan4edb7762022-06-26 19:21:04 +010052 await interaction.editReply({embeds: [new EmojiEmbed()
53 .setEmoji("CHANNEL.SLOWMODE.ON")
pineafan663dc472022-05-10 18:13:47 +010054 .setTitle(`Slowmode`)
55 .setDescription("No changes were made")
56 .setStatus("Success")
57 ], components: []})
58 }
pineafan4f164f32022-02-26 22:07:12 +000059}
60
61const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan663dc472022-05-10 18:13:47 +010062 let member = (interaction.member as GuildMember)
pineafan167bde32022-05-19 19:33:46 +010063 // Check if Nucleus can set the slowmode
pineafan4edb7762022-06-26 19:21:04 +010064 if (! interaction.guild.me.permissions.has("MANAGE_CHANNELS")) throw "I do not have the Manage channels permission";
pineafan167bde32022-05-19 19:33:46 +010065 // Check if the user has manage_channel permission
pineafan4edb7762022-06-26 19:21:04 +010066 if (! member.permissions.has("MANAGE_CHANNELS")) throw "You do not have the Manage channels permission";
pineafan663dc472022-05-10 18:13:47 +010067 // Allow slowmode
68 return true
pineafan4f164f32022-02-26 22:07:12 +000069}
70
pineafan8b4b17f2022-02-27 20:42:52 +000071export { command, callback, check };