blob: 0000844807690ed1f2954e28a8f2bbfb8221c01e [file] [log] [blame]
PineappleFan19b002b2022-05-19 11:54:01 +01001import { CommandInteraction } from "discord.js";
pineafan4f164f32022-02-26 22:07:12 +00002import { SlashCommandSubcommandBuilder } from "@discordjs/builders";
3import { WrappedCheck } from "jshaiku";
PineappleFan19b002b2022-05-19 11:54:01 +01004import humanizeDuration from 'humanize-duration';
pineafan4f164f32022-02-26 22:07:12 +00005
6const command = (builder: SlashCommandSubcommandBuilder) =>
7 builder
8 .setName("slowmode")
9 .setDescription("Manages slowmode in a channel")
PineappleFan19b002b2022-05-19 11:54:01 +010010 .addIntegerOption(option => option.setName("seconds").setDescription("The seconds between messages").setRequired(false))
11 .addIntegerOption(option => option.setName("minutes").setDescription("The minutes between messages").setRequired(false))
12 .addIntegerOption(option => option.setName("hours").setDescription("The hours between messages").setRequired(false))
pineafan4f164f32022-02-26 22:07:12 +000013
PineappleFan19b002b2022-05-19 11:54:01 +010014const callback = (interaction: CommandInteraction) => {
15 let seconds = interaction.option.getInteger("seconds")
16 let minutes = interaction.option.getInteger("minutes")
17 let hours = interaction.option.getInteger("hours")
18 let totalTime = seconds + (minutes * 60) + (hours * 60 * 60)
19
pineafan663dc472022-05-10 18:13:47 +010020 let confirmation = await new confirmationMessage(interaction)
PineappleFan19b002b2022-05-19 11:54:01 +010021 .setEmoji("PUNISH.SLOWMODE.RED")
pineafan663dc472022-05-10 18:13:47 +010022 .setTitle("Slowmode")
23 .setDescription(keyValueList({
PineappleFan19b002b2022-05-19 11:54:01 +010024 "delay": `${totalTime ? humanizeDuration(totalTime * 1000) : "*No delay*"}`
pineafan663dc472022-05-10 18:13:47 +010025 })
PineappleFan19b002b2022-05-19 11:54:01 +010026 + `Are you sure you want to enable slowmode in this channel?`)
pineafan663dc472022-05-10 18:13:47 +010027 .setColor("Danger")
28// pluralize("day", interaction.options.getInteger("delete"))
29// const pluralize = (word: string, count: number) => { return count === 1 ? word : word + "s" }
30 .send()
31 if (confirmation.success) {
32 try {
PineappleFan19b002b2022-05-19 11:54:01 +010033 await interaction.setRateLimitPerUser(totalTime, "Nucleus slowmode")
34 } catch {
35 return await interaction.editReply({embeds: [new generateEmojiEmbed()
36 .setEmoji("PUNISH.SLOWMODE.RED")
pineafan663dc472022-05-10 18:13:47 +010037 .setTitle(`Slowmode`)
PineappleFan19b002b2022-05-19 11:54:01 +010038 .setDescription("Something went wrong and the slowmode could not be set.")
pineafan663dc472022-05-10 18:13:47 +010039 .setStatus("Danger")
40 ], components: []})
41 }
42 await interaction.editReply({embeds: [new generateEmojiEmbed()
PineappleFan19b002b2022-05-19 11:54:01 +010043 .setEmoji("PUNISH.NICKNAME.GREEN")
pineafan663dc472022-05-10 18:13:47 +010044 .setTitle(`Slowmode`)
45 .setDescription("The channel slowmode was set successfully")
46 .setStatus("Success")
47 ], components: []})
48 } else {
49 await interaction.editReply({embeds: [new generateEmojiEmbed()
PineappleFan19b002b2022-05-19 11:54:01 +010050 .setEmoji("PUNISH.SLOWMODE.GREEN")
pineafan663dc472022-05-10 18:13:47 +010051 .setTitle(`Slowmode`)
52 .setDescription("No changes were made")
53 .setStatus("Success")
54 ], components: []})
55 }
pineafan4f164f32022-02-26 22:07:12 +000056}
57
58const check = (interaction: CommandInteraction, defaultCheck: WrappedCheck) => {
pineafan663dc472022-05-10 18:13:47 +010059 let member = (interaction.member as GuildMember)
PineappleFan19b002b2022-05-19 11:54:01 +010060 let me = (interaction.guild.me as GuildMember)
61 // Check if Nucleus can edit the channel
PineappleFan5fe720d2022-05-19 12:01:49 +010062 if (! me.permission.has("MANAGE_CHANNELS")) throw "I do not have permission to edit this channel"
PineappleFan19b002b2022-05-19 11:54:01 +010063 // Allow the owner to set any channel
64 if (member.id == interaction.guild.ownerId) return true
65 // Check if the user has manage_channels permission
pineafan663dc472022-05-10 18:13:47 +010066 if (! member.permissions.has("MANAGE_CHANNELS")) throw "You do not have the `manage_channels` permission";
67 // Allow slowmode
68 return true
pineafan4f164f32022-02-26 22:07:12 +000069}
70
pineafan8b4b17f2022-02-27 20:42:52 +000071export { command, callback, check };