blob: 3e883b3ecc14ed9c77af8015b3a2009835240d0b [file] [log] [blame]
pineafan3a02ea32022-08-11 21:35:04 +01001// @ts-expect-error
pineafan63fc5e22022-08-04 22:04:10 +01002import humanizeDuration from "humanize-duration";
pineafan3a02ea32022-08-11 21:35:04 +01003import type { CommandInteraction, GuildMember, TextChannel } from "discord.js";
4import type { SlashCommandSubcommandBuilder } from "@discordjs/builders";
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 +00009const command = (builder: SlashCommandSubcommandBuilder) =>
10 builder
pineafan63fc5e22022-08-04 22:04:10 +010011 .setName("slowmode")
12 .setDescription("Manages slowmode in a channel")
Skyler Grey75ea9172022-08-06 10:22:23 +010013 .addStringOption((option) =>
14 option
15 .setName("time")
16 .setDescription("The delay between messages")
17 .setRequired(false)
PineaFan64486c42022-12-28 09:21:04 +000018 .addChoices(
19 {name: "Off", value: "0"},
20 {name: "5 seconds", value: "5"},
21 {name: "10 seconds", value: "10"},
22 {name: "15 seconds", value: "15"},
23 {name: "30 seconds", value: "30"},
24 {name: "1 minute", value: "60"},
25 {name: "2 minutes", value: "120"},
26 {name: "5 minutes", value: "300"},
27 {name: "10 minutes", value: "600"},
28 {name: "15 minutes", value: "900"},
29 {name: "30 minutes", value: "1800"},
30 {name: "1 hour", value: "3600"},
31 {name: "2 hours", value: "7200"},
32 {name: "6 hours", value: "21600"}
33 )
Skyler Grey75ea9172022-08-06 10:22:23 +010034 );
pineafan4f164f32022-02-26 22:07:12 +000035
pineafanbd02b4a2022-08-05 22:01:38 +010036const callback = async (interaction: CommandInteraction): Promise<void> => {
pineafan167bde32022-05-19 19:33:46 +010037 let time = parseInt(interaction.options.getString("time") ?? "0");
Skyler Grey11236ba2022-08-08 21:13:33 +010038 if (time === 0 && (interaction.channel as TextChannel).rateLimitPerUser === 0) {
Skyler Grey75ea9172022-08-06 10:22:23 +010039 time = 10;
40 }
pineafan63fc5e22022-08-04 22:04:10 +010041 const confirmation = await new confirmationMessage(interaction)
pineafan4edb7762022-06-26 19:21:04 +010042 .setEmoji("CHANNEL.SLOWMODE.OFF")
pineafan663dc472022-05-10 18:13:47 +010043 .setTitle("Slowmode")
Skyler Grey75ea9172022-08-06 10:22:23 +010044 .setDescription(
45 keyValueList({
Skyler Grey11236ba2022-08-08 21:13:33 +010046 time: time ? humanizeDuration(time * 1000, { round: true }) : "No delay"
Skyler Grey75ea9172022-08-06 10:22:23 +010047 }) + "Are you sure you want to set the slowmode in this channel?"
48 )
pineafan663dc472022-05-10 18:13:47 +010049 .setColor("Danger")
pineafan63fc5e22022-08-04 22:04:10 +010050 .send();
51 if (confirmation.cancelled) return;
pineafan663dc472022-05-10 18:13:47 +010052 if (confirmation.success) {
53 try {
pineafan63fc5e22022-08-04 22:04:10 +010054 (interaction.channel as TextChannel).setRateLimitPerUser(time);
pineafan167bde32022-05-19 19:33:46 +010055 } catch (e) {
Skyler Grey75ea9172022-08-06 10:22:23 +010056 await interaction.editReply({
57 embeds: [
58 new EmojiEmbed()
59 .setEmoji("CHANNEL.SLOWMODE.OFF")
60 .setTitle("Slowmode")
Skyler Grey11236ba2022-08-08 21:13:33 +010061 .setDescription("Something went wrong while setting the slowmode")
Skyler Grey75ea9172022-08-06 10:22:23 +010062 .setStatus("Danger")
63 ],
64 components: []
65 });
pineafan663dc472022-05-10 18:13:47 +010066 }
Skyler Grey75ea9172022-08-06 10:22:23 +010067 await interaction.editReply({
68 embeds: [
69 new EmojiEmbed()
70 .setEmoji("CHANNEL.SLOWMODE.ON")
71 .setTitle("Slowmode")
72 .setDescription("The channel slowmode was set successfully")
73 .setStatus("Success")
74 ],
75 components: []
76 });
pineafan663dc472022-05-10 18:13:47 +010077 } else {
Skyler Grey75ea9172022-08-06 10:22:23 +010078 await interaction.editReply({
79 embeds: [
80 new EmojiEmbed()
81 .setEmoji("CHANNEL.SLOWMODE.ON")
82 .setTitle("Slowmode")
83 .setDescription("No changes were made")
84 .setStatus("Success")
85 ],
86 components: []
87 });
pineafan663dc472022-05-10 18:13:47 +010088 }
pineafan63fc5e22022-08-04 22:04:10 +010089};
pineafan4f164f32022-02-26 22:07:12 +000090
pineafanbd02b4a2022-08-05 22:01:38 +010091const check = (interaction: CommandInteraction) => {
Skyler Grey75ea9172022-08-06 10:22:23 +010092 const member = interaction.member as GuildMember;
pineafan167bde32022-05-19 19:33:46 +010093 // Check if Nucleus can set the slowmode
Skyler Grey75ea9172022-08-06 10:22:23 +010094 if (!interaction.guild.me.permissions.has("MANAGE_CHANNELS"))
pineafan3a02ea32022-08-11 21:35:04 +010095 throw new Error("I do not have the *Manage Channels* permission");
pineafan167bde32022-05-19 19:33:46 +010096 // Check if the user has manage_channel permission
pineafan3a02ea32022-08-11 21:35:04 +010097 if (!member.permissions.has("MANAGE_CHANNELS")) throw new Error("You do not have the *Manage Channels* permission");
pineafan663dc472022-05-10 18:13:47 +010098 // Allow slowmode
pineafan63fc5e22022-08-04 22:04:10 +010099 return true;
100};
pineafan4f164f32022-02-26 22:07:12 +0000101
Skyler Grey75ea9172022-08-06 10:22:23 +0100102export { command, callback, check };