blob: 556553446fd33badacfeacd112e3c7d4d94f6142 [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 Grey11236ba2022-08-08 21:13:33 +010037 if (time === 0 && (interaction.channel as TextChannel).rateLimitPerUser === 0) {
Skyler Grey75ea9172022-08-06 10:22:23 +010038 time = 10;
39 }
pineafan63fc5e22022-08-04 22:04:10 +010040 const confirmation = await new confirmationMessage(interaction)
pineafan4edb7762022-06-26 19:21:04 +010041 .setEmoji("CHANNEL.SLOWMODE.OFF")
pineafan663dc472022-05-10 18:13:47 +010042 .setTitle("Slowmode")
Skyler Grey75ea9172022-08-06 10:22:23 +010043 .setDescription(
44 keyValueList({
Skyler Grey11236ba2022-08-08 21:13:33 +010045 time: time ? humanizeDuration(time * 1000, { round: true }) : "No delay"
Skyler Grey75ea9172022-08-06 10:22:23 +010046 }) + "Are you sure you want to set the slowmode in this channel?"
47 )
pineafan663dc472022-05-10 18:13:47 +010048 .setColor("Danger")
pineafan63fc5e22022-08-04 22:04:10 +010049 .send();
50 if (confirmation.cancelled) return;
pineafan663dc472022-05-10 18:13:47 +010051 if (confirmation.success) {
52 try {
pineafan63fc5e22022-08-04 22:04:10 +010053 (interaction.channel as TextChannel).setRateLimitPerUser(time);
pineafan167bde32022-05-19 19:33:46 +010054 } catch (e) {
Skyler Grey75ea9172022-08-06 10:22:23 +010055 await interaction.editReply({
56 embeds: [
57 new EmojiEmbed()
58 .setEmoji("CHANNEL.SLOWMODE.OFF")
59 .setTitle("Slowmode")
Skyler Grey11236ba2022-08-08 21:13:33 +010060 .setDescription("Something went wrong while setting the slowmode")
Skyler Grey75ea9172022-08-06 10:22:23 +010061 .setStatus("Danger")
62 ],
63 components: []
64 });
pineafan663dc472022-05-10 18:13:47 +010065 }
Skyler Grey75ea9172022-08-06 10:22:23 +010066 await interaction.editReply({
67 embeds: [
68 new EmojiEmbed()
69 .setEmoji("CHANNEL.SLOWMODE.ON")
70 .setTitle("Slowmode")
71 .setDescription("The channel slowmode was set successfully")
72 .setStatus("Success")
73 ],
74 components: []
75 });
pineafan663dc472022-05-10 18:13:47 +010076 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +010077 await interaction.editReply({
78 embeds: [
79 new EmojiEmbed()
80 .setEmoji("CHANNEL.SLOWMODE.ON")
81 .setTitle("Slowmode")
82 .setDescription("No changes were made")
83 .setStatus("Success")
84 ],
85 components: []
86 });
pineafan663dc472022-05-10 18:13:47 +010087 }
pineafan63fc5e22022-08-04 22:04:10 +010088};
pineafan4f164f32022-02-26 22:07:12 +000089
pineafanbd02b4a2022-08-05 22:01:38 +010090const check = (interaction: CommandInteraction) => {
Skyler Grey75ea9172022-08-06 10:22:23 +010091 const member = interaction.member as GuildMember;
pineafan167bde32022-05-19 19:33:46 +010092 // Check if Nucleus can set the slowmode
Skyler Grey75ea9172022-08-06 10:22:23 +010093 if (!interaction.guild.me.permissions.has("MANAGE_CHANNELS"))
94 throw "I do not have the *Manage Channels* permission";
pineafan167bde32022-05-19 19:33:46 +010095 // Check if the user has manage_channel permission
Skyler Grey11236ba2022-08-08 21:13:33 +010096 if (!member.permissions.has("MANAGE_CHANNELS")) throw "You do not have the *Manage Channels* permission";
pineafan663dc472022-05-10 18:13:47 +010097 // Allow slowmode
pineafan63fc5e22022-08-04 22:04:10 +010098 return true;
99};
pineafan4f164f32022-02-26 22:07:12 +0000100
Skyler Grey75ea9172022-08-06 10:22:23 +0100101export { command, callback, check };