blob: 1a06db18f2b55b28662cc24a4c1978d3966b879d [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 +00008const command = (builder: SlashCommandSubcommandBuilder) =>
9 builder
pineafan63fc5e22022-08-04 22:04:10 +010010 .setName("slowmode")
11 .setDescription("Manages slowmode in a channel")
Skyler Grey75ea9172022-08-06 10:22:23 +010012 .addStringOption((option) =>
13 option
14 .setName("time")
15 .setDescription("The delay between messages")
16 .setRequired(false)
17 .addChoices([
18 ["Off", "0"],
19 ["5 seconds", "5"],
20 ["10 seconds", "10"],
21 ["15 seconds", "15"],
22 ["30 seconds", "30"],
23 ["1 minute", "60"],
24 ["2 minutes", "120"],
25 ["5 minutes", "300"],
26 ["10 minutes", "600"],
27 ["15 minutes", "900"],
28 ["30 minutes", "1800"],
29 ["1 hour", "3600"],
30 ["2 hours", "7200"],
31 ["6 hours", "21600"]
32 ])
33 );
pineafan4f164f32022-02-26 22:07:12 +000034
pineafanbd02b4a2022-08-05 22:01:38 +010035const callback = async (interaction: CommandInteraction): Promise<void> => {
pineafan167bde32022-05-19 19:33:46 +010036 let time = parseInt(interaction.options.getString("time") ?? "0");
Skyler Grey75ea9172022-08-06 10:22:23 +010037 if (
38 time === 0 &&
39 (interaction.channel as TextChannel).rateLimitPerUser === 0
40 ) {
41 time = 10;
42 }
pineafan63fc5e22022-08-04 22:04:10 +010043 const confirmation = await new confirmationMessage(interaction)
pineafan4edb7762022-06-26 19:21:04 +010044 .setEmoji("CHANNEL.SLOWMODE.OFF")
pineafan663dc472022-05-10 18:13:47 +010045 .setTitle("Slowmode")
Skyler Grey75ea9172022-08-06 10:22:23 +010046 .setDescription(
47 keyValueList({
48 time: time
49 ? humanizeDuration(time * 1000, { round: true })
50 : "No delay"
51 }) + "Are you sure you want to set the slowmode in this channel?"
52 )
pineafan663dc472022-05-10 18:13:47 +010053 .setColor("Danger")
pineafan63fc5e22022-08-04 22:04:10 +010054 .send();
55 if (confirmation.cancelled) return;
pineafan663dc472022-05-10 18:13:47 +010056 if (confirmation.success) {
57 try {
pineafan63fc5e22022-08-04 22:04:10 +010058 (interaction.channel as TextChannel).setRateLimitPerUser(time);
pineafan167bde32022-05-19 19:33:46 +010059 } catch (e) {
Skyler Grey75ea9172022-08-06 10:22:23 +010060 await interaction.editReply({
61 embeds: [
62 new EmojiEmbed()
63 .setEmoji("CHANNEL.SLOWMODE.OFF")
64 .setTitle("Slowmode")
65 .setDescription(
66 "Something went wrong while setting the slowmode"
67 )
68 .setStatus("Danger")
69 ],
70 components: []
71 });
pineafan663dc472022-05-10 18:13:47 +010072 }
Skyler Grey75ea9172022-08-06 10:22:23 +010073 await interaction.editReply({
74 embeds: [
75 new EmojiEmbed()
76 .setEmoji("CHANNEL.SLOWMODE.ON")
77 .setTitle("Slowmode")
78 .setDescription("The channel slowmode was set successfully")
79 .setStatus("Success")
80 ],
81 components: []
82 });
pineafan663dc472022-05-10 18:13:47 +010083 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +010084 await interaction.editReply({
85 embeds: [
86 new EmojiEmbed()
87 .setEmoji("CHANNEL.SLOWMODE.ON")
88 .setTitle("Slowmode")
89 .setDescription("No changes were made")
90 .setStatus("Success")
91 ],
92 components: []
93 });
pineafan663dc472022-05-10 18:13:47 +010094 }
pineafan63fc5e22022-08-04 22:04:10 +010095};
pineafan4f164f32022-02-26 22:07:12 +000096
pineafanbd02b4a2022-08-05 22:01:38 +010097const check = (interaction: CommandInteraction) => {
Skyler Grey75ea9172022-08-06 10:22:23 +010098 const member = interaction.member as GuildMember;
pineafan167bde32022-05-19 19:33:46 +010099 // Check if Nucleus can set the slowmode
Skyler Grey75ea9172022-08-06 10:22:23 +0100100 if (!interaction.guild.me.permissions.has("MANAGE_CHANNELS"))
101 throw "I do not have the *Manage Channels* permission";
pineafan167bde32022-05-19 19:33:46 +0100102 // Check if the user has manage_channel permission
Skyler Grey75ea9172022-08-06 10:22:23 +0100103 if (!member.permissions.has("MANAGE_CHANNELS"))
104 throw "You do not have the *Manage Channels* permission";
pineafan663dc472022-05-10 18:13:47 +0100105 // Allow slowmode
pineafan63fc5e22022-08-04 22:04:10 +0100106 return true;
107};
pineafan4f164f32022-02-26 22:07:12 +0000108
Skyler Grey75ea9172022-08-06 10:22:23 +0100109export { command, callback, check };