blob: 7e8b8a20e575a261392443326e4429aa7efcee67 [file] [log] [blame]
pineafan63fc5e22022-08-04 22:04:10 +01001import humanizeDuration from "humanize-duration";
pineafan167bde32022-05-19 19:33:46 +01002import { CommandInteraction, GuildMember, TextChannel } from "discord.js";
pineafan4f164f32022-02-26 22:07:12 +00003import { SlashCommandSubcommandBuilder } from "@discordjs/builders";
pineafan167bde32022-05-19 19:33:46 +01004import keyValueList from "../../utils/generateKeyValueList.js";
5import confirmationMessage from "../../utils/confirmationMessage.js";
pineafan4edb7762022-06-26 19:21:04 +01006import EmojiEmbed from "../../utils/generateEmojiEmbed.js";
7
pineafan4f164f32022-02-26 22:07:12 +00008
9const command = (builder: SlashCommandSubcommandBuilder) =>
10 builder
pineafan63fc5e22022-08-04 22:04:10 +010011 .setName("slowmode")
12 .setDescription("Manages slowmode in a channel")
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 ]));
pineafan4f164f32022-02-26 22:07:12 +000020
pineafanbd02b4a2022-08-05 22:01:38 +010021const callback = async (interaction: CommandInteraction): Promise<void> => {
pineafan167bde32022-05-19 19:33:46 +010022 let time = parseInt(interaction.options.getString("time") ?? "0");
pineafan63fc5e22022-08-04 22:04:10 +010023 if (time === 0 && (interaction.channel as TextChannel).rateLimitPerUser === 0) { time = 10; }
24 const confirmation = await new confirmationMessage(interaction)
pineafan4edb7762022-06-26 19:21:04 +010025 .setEmoji("CHANNEL.SLOWMODE.OFF")
pineafan663dc472022-05-10 18:13:47 +010026 .setTitle("Slowmode")
27 .setDescription(keyValueList({
pineafan63fc5e22022-08-04 22:04:10 +010028 "time": time ? humanizeDuration(time * 1000, { round: true }) : "No delay"
pineafan663dc472022-05-10 18:13:47 +010029 })
pineafan63fc5e22022-08-04 22:04:10 +010030 + "Are you sure you want to set the slowmode in this channel?")
pineafan663dc472022-05-10 18:13:47 +010031 .setColor("Danger")
pineafan63fc5e22022-08-04 22:04:10 +010032 .send();
33 if (confirmation.cancelled) return;
pineafan663dc472022-05-10 18:13:47 +010034 if (confirmation.success) {
35 try {
pineafan63fc5e22022-08-04 22:04:10 +010036 (interaction.channel as TextChannel).setRateLimitPerUser(time);
pineafan167bde32022-05-19 19:33:46 +010037 } catch (e) {
pineafan4edb7762022-06-26 19:21:04 +010038 await interaction.editReply({embeds: [new EmojiEmbed()
39 .setEmoji("CHANNEL.SLOWMODE.OFF")
pineafan63fc5e22022-08-04 22:04:10 +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")
pineafan63fc5e22022-08-04 22:04:10 +010043 ], components: []});
pineafan663dc472022-05-10 18:13:47 +010044 }
pineafan4edb7762022-06-26 19:21:04 +010045 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan63fc5e22022-08-04 22:04:10 +010046 .setEmoji("CHANNEL.SLOWMODE.ON")
47 .setTitle("Slowmode")
pineafan663dc472022-05-10 18:13:47 +010048 .setDescription("The channel slowmode was set successfully")
49 .setStatus("Success")
pineafan63fc5e22022-08-04 22:04:10 +010050 ], components: []});
pineafan663dc472022-05-10 18:13:47 +010051 } else {
pineafan4edb7762022-06-26 19:21:04 +010052 await interaction.editReply({embeds: [new EmojiEmbed()
53 .setEmoji("CHANNEL.SLOWMODE.ON")
pineafan63fc5e22022-08-04 22:04:10 +010054 .setTitle("Slowmode")
pineafan663dc472022-05-10 18:13:47 +010055 .setDescription("No changes were made")
56 .setStatus("Success")
pineafan63fc5e22022-08-04 22:04:10 +010057 ], components: []});
pineafan663dc472022-05-10 18:13:47 +010058 }
pineafan63fc5e22022-08-04 22:04:10 +010059};
pineafan4f164f32022-02-26 22:07:12 +000060
pineafanbd02b4a2022-08-05 22:01:38 +010061const check = (interaction: CommandInteraction) => {
pineafan63fc5e22022-08-04 22:04:10 +010062 const member = (interaction.member as GuildMember);
pineafan167bde32022-05-19 19:33:46 +010063 // Check if Nucleus can set the slowmode
pineafane23c4ec2022-07-27 21:56:27 +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
pineafane23c4ec2022-07-27 21:56:27 +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
pineafan63fc5e22022-08-04 22:04:10 +010068 return true;
69};
pineafan4f164f32022-02-26 22:07:12 +000070
pineafan8b4b17f2022-02-27 20:42:52 +000071export { command, callback, check };