blob: 6b33e12833170ab52e5e86e8bae126aad3555306 [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()
pineafan02ba0232022-07-24 22:16:15 +010034 if (confirmation.cancelled) return
pineafan663dc472022-05-10 18:13:47 +010035 if (confirmation.success) {
36 try {
pineafan167bde32022-05-19 19:33:46 +010037 (interaction.channel as TextChannel).setRateLimitPerUser(time)
38 } catch (e) {
pineafan4edb7762022-06-26 19:21:04 +010039 await interaction.editReply({embeds: [new EmojiEmbed()
40 .setEmoji("CHANNEL.SLOWMODE.OFF")
pineafan663dc472022-05-10 18:13:47 +010041 .setTitle(`Slowmode`)
pineafan6702cef2022-06-13 17:52:37 +010042 .setDescription("Something went wrong while setting the slowmode")
pineafan663dc472022-05-10 18:13:47 +010043 .setStatus("Danger")
44 ], components: []})
45 }
pineafan4edb7762022-06-26 19:21:04 +010046 await interaction.editReply({embeds: [new EmojiEmbed()
47 .setEmoji(`CHANNEL.SLOWMODE.ON`)
pineafan663dc472022-05-10 18:13:47 +010048 .setTitle(`Slowmode`)
49 .setDescription("The channel slowmode was set successfully")
50 .setStatus("Success")
51 ], components: []})
52 } else {
pineafan4edb7762022-06-26 19:21:04 +010053 await interaction.editReply({embeds: [new EmojiEmbed()
54 .setEmoji("CHANNEL.SLOWMODE.ON")
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
pineafane23c4ec2022-07-27 21:56:27 +010065 if (! interaction.guild.me.permissions.has("MANAGE_CHANNELS")) throw "I do not have the *Manage Channels* permission";
pineafan167bde32022-05-19 19:33:46 +010066 // Check if the user has manage_channel permission
pineafane23c4ec2022-07-27 21:56:27 +010067 if (! member.permissions.has("MANAGE_CHANNELS")) throw "You do not have the *Manage Channels* permission";
pineafan663dc472022-05-10 18:13:47 +010068 // Allow slowmode
69 return true
pineafan4f164f32022-02-26 22:07:12 +000070}
71
pineafan8b4b17f2022-02-27 20:42:52 +000072export { command, callback, check };