I've been working on a Slowmode Command recently for my personal Discord Bot, and I'm looking to split my slowmode command into two subcommands (one to set, one to clear). I've never implemented subcommands into any projects before (this is my first time), and I'm having a bit of trouble working it out.
The main problem is I don't know how to call a subcommand's options.
Any advice would do! Also to note...
I use reconlx's DJS Base Handler, however, I tweaked a few lines to make it functional in DJS v14.
Secondly, I used reconlx's Slash Commands Guide as a sort of handbook, but it didn't really help out much.
Here's the source code.
const { Command } = require("reconlx");
const { EmbedBuilder, ApplicationCommandOptionType, ApplicationCommandType, PermissionsBitField } = require("discord.js");
const ms = require("ms");
module.exports = new Command({
name: "slowmode2",
description: "Sets a custom channel slowmode, version 2",
userPermissions: PermissionsBitField.Flags.ManageGuild,
type: ApplicationCommandType.ChatInput,
options: [
{
name: "set",
description: "Set a channel rate limit",
type: ApplicationCommandOptionType.Subcommand,
options: [
{
name: "rate-limit",
description: "Duration of channel slowmode [s|m|h]",
type: ApplicationCommandOptionType.String,
required: true
},
{
name: "channel",
description: "Channel to rate-limit",
type: ApplicationCommandOptionType.Channel,
required: false
},
]
},
{
name: "clear",
description: "Clears the rate limit for a specific channel",
type: ApplicationCommandOptionType.Subcommand,
options: [
{
name: "channel",
description: "the channel to set the welcome message to.",
type: ApplicationCommandOptionType.Channel,
required: false
},
]
},
],
run: async ({ interaction }) => {
const [ subcommand ] = args;
if (interaction.options.getSubcommand() === 'set') {
const rateLimit = interaction.options.getString('rate-limit');
const rateLimitChannel = interaction.options.getChannel('channel');
}
const timeInMs = ms(rateLimit);
if (interaction.options.getSubcommand() === 'clear') {
const clearChannel = interaction.options.getChannel("channel")
}
const slowmodeSuccess = new EmbedBuilder()
.setColor("#2F3136")
.setTitle(` SLOWING DOWN...`)
.addFields(
{
name: "Slowmode has been enabled for everyone in this channel!",
value: `To clear your slowmode, use \`/slowmode clear\``,
inline: false
},
{
name: "Rate Limit",
value: `${rateLimit}`,
inline: true
},
{
name: "Channel",
value: `${rateLimitChannel}`,
inline: true
}
)
const slowmodeClear = new EmbedBuilder()
.setColor("#2F3136")
.setTitle(`♂️ SLOWMODE CLEARED!`)
.addFields(
{
name: "Slowmode has been cleared for everyone in this channel!",
value: `Everyone may now talk freely at a normal pace!`,
inline: false
},
{
name: "Channel",
value: `${clearChannel}`,
inline: true
}
)
const invalidFormat = new EmbedBuilder()
.setColor("#2F3136")
.setTitle(`❌ ERROR`)
.addFields(
{
name: "Invalid Format!",
value: `Please provide your duration in [s|m|h] format!`,
inline: true
},
)
const error = new EmbedBuilder()
.setColor("#2F3136")
.setTitle(`❌ ERROR`)
.addFields(
{
name: "Unknown Error: Oops!",
value: `Something seems to have gone wrong!`,
inline: true
},
)
if (subcommand === "set") {
if (!timeInMs || timeInMs === false) {
interaction.followUp({ embeds: [invalidFormat] })
} else {
interaction.channel.setRateLimitPerUser(timeInMs/1000).then( () => {
interaction.followUp({ embeds: [slowmodeSuccess] })
}).catch( () => {
interaction.followUp({ embeds: [error] })
})
}
}
if (subcommand === "clear") {
interaction.channel.setRateLimitPerUser(false)
interaction.followUp({ embeds: [slowmodeClear] })
}
}
});