blob: 951dc53eabc278f0635adb1530a16f152411cb57 [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";
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
pineafan63fc5e22022-08-04 22:04:10 +010012 .setName("slowmode")
13 .setDescription("Manages slowmode in a channel")
14 .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");
pineafan63fc5e22022-08-04 22:04:10 +010024 if (time === 0 && (interaction.channel as TextChannel).rateLimitPerUser === 0) { time = 10; }
25 const 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({
pineafan63fc5e22022-08-04 22:04:10 +010029 "time": time ? humanizeDuration(time * 1000, { round: true }) : "No delay"
pineafan663dc472022-05-10 18:13:47 +010030 })
pineafan63fc5e22022-08-04 22:04:10 +010031 + "Are you sure you want to set the slowmode in this channel?")
pineafan663dc472022-05-10 18:13:47 +010032 .setColor("Danger")
pineafan63fc5e22022-08-04 22:04:10 +010033 .send();
34 if (confirmation.cancelled) return;
pineafan663dc472022-05-10 18:13:47 +010035 if (confirmation.success) {
36 try {
pineafan63fc5e22022-08-04 22:04:10 +010037 (interaction.channel as TextChannel).setRateLimitPerUser(time);
pineafan167bde32022-05-19 19:33:46 +010038 } catch (e) {
pineafan4edb7762022-06-26 19:21:04 +010039 await interaction.editReply({embeds: [new EmojiEmbed()
40 .setEmoji("CHANNEL.SLOWMODE.OFF")
pineafan63fc5e22022-08-04 22:04:10 +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")
pineafan63fc5e22022-08-04 22:04:10 +010044 ], components: []});
pineafan663dc472022-05-10 18:13:47 +010045 }
pineafan4edb7762022-06-26 19:21:04 +010046 await interaction.editReply({embeds: [new EmojiEmbed()
pineafan63fc5e22022-08-04 22:04:10 +010047 .setEmoji("CHANNEL.SLOWMODE.ON")
48 .setTitle("Slowmode")
pineafan663dc472022-05-10 18:13:47 +010049 .setDescription("The channel slowmode was set successfully")
50 .setStatus("Success")
pineafan63fc5e22022-08-04 22:04:10 +010051 ], components: []});
pineafan663dc472022-05-10 18:13:47 +010052 } else {
pineafan4edb7762022-06-26 19:21:04 +010053 await interaction.editReply({embeds: [new EmojiEmbed()
54 .setEmoji("CHANNEL.SLOWMODE.ON")
pineafan63fc5e22022-08-04 22:04:10 +010055 .setTitle("Slowmode")
pineafan663dc472022-05-10 18:13:47 +010056 .setDescription("No changes were made")
57 .setStatus("Success")
pineafan63fc5e22022-08-04 22:04:10 +010058 ], components: []});
pineafan663dc472022-05-10 18:13:47 +010059 }
pineafan63fc5e22022-08-04 22:04:10 +010060};
pineafan4f164f32022-02-26 22:07:12 +000061
62const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan63fc5e22022-08-04 22:04:10 +010063 const 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
pineafan63fc5e22022-08-04 22:04:10 +010069 return true;
70};
pineafan4f164f32022-02-26 22:07:12 +000071
pineafan8b4b17f2022-02-27 20:42:52 +000072export { command, callback, check };