Slash Commands Documentation

Build powerful slash commands with NoveCord's comprehensive command builder system. Create commands with options, subcommands, and advanced configurations.

SlashCommandBuilder

The main builder for creating slash commands with options, permissions, and context settings.

Basic Usage

const { SlashCommandBuilder } = require('novecord');
const pingCommand = new SlashCommandBuilder()
.setName('ping')
.setDescription('Replies with Pong!')
.build();
// Register the command
await rest.post(Routes.ApplicationCommands(clientId), pingCommand);

Command with Options

const { SlashCommandBuilder } = require('novecord');
const userCommand = new SlashCommandBuilder()
.setName('userinfo')
.setDescription('Get information about a user')
.addUserOption(option =>
option
.setName('target')
.setDescription('The user to get info about')
.setRequired(true)
)
.addBooleanOption(option =>
option
.setName('ephemeral')
.setDescription('Make the response private')
.setRequired(false)
)
.build();

SlashCommandBuilder Methods

  • .setName(name) - Set command name (lowercase, no spaces)
  • .setDescription(description) - Set command description
  • .setDefaultMemberPermissions(permissions) - Set required permissions
  • .setNSFW(nsfw) - Mark command as NSFW
  • .setIntegrationTypes(...types) - Set where command can be used
  • .setContexts(...contexts) - Set command contexts

Command Options

Add various types of options to your slash commands.

String Options with Choices

const command = new SlashCommandBuilder()
.setName('color')
.setDescription('Choose a color')
.addStringOption(option =>
option
.setName('color')
.setDescription('Pick your favorite color')
.setRequired(true)
.addChoice('Red', 'red')
.addChoice('Blue', 'blue')
.addChoice('Green', 'green')
)
.build();

Integer and Number Options

const command = new SlashCommandBuilder()
.setName('math')
.setDescription('Perform math operations')
.addIntegerOption(option =>
option
.setName('number1')
.setDescription('First number')
.setRequired(true)
)
.addNumberOption(option =>
option
.setName('number2')
.setDescription('Second number (decimal allowed)')
.setRequired(true)
)
.build();

User, Channel, and Role Options

const command = new SlashCommandBuilder()
.setName('assign')
.setDescription('Assign a role to a user in a channel')
.addUserOption(option =>
option
.setName('user')
.setDescription('User to assign role to')
.setRequired(true)
)
.addRoleOption(option =>
option
.setName('role')
.setDescription('Role to assign')
.setRequired(true)
)
.addChannelOption(option =>
option
.setName('channel')
.setDescription('Channel to announce in')
.setRequired(false)
)
.build();

Available Option Types

MethodTypeDescription
addStringOptionStringText input with optional choices
addIntegerOptionIntegerWhole numbers only
addNumberOptionNumberDecimal numbers allowed
addBooleanOptionBooleanTrue/false choice
addUserOptionUserDiscord user selection
addChannelOptionChannelDiscord channel selection
addRoleOptionRoleDiscord role selection
addMentionableOptionMentionableUser or role selection
addAttachmentOptionAttachmentFile upload

Subcommands

Create complex commands with subcommands and subcommand groups for better organization.

Basic Subcommands

const { SlashCommandBuilder } = require('novecord');
const modCommand = new SlashCommandBuilder()
.setName('mod')
.setDescription('Moderation commands')
.addSubcommand(subcommand =>
subcommand
.setName('ban')
.setDescription('Ban a user')
.addUserOption(option =>
option
.setName('user')
.setDescription('User to ban')
.setRequired(true)
)
.addStringOption(option =>
option
.setName('reason')
.setDescription('Reason for ban')
.setRequired(false)
)
)
.addSubcommand(subcommand =>
subcommand
.setName('kick')
.setDescription('Kick a user')
.addUserOption(option =>
option
.setName('user')
.setDescription('User to kick')
.setRequired(true)
)
)
.build();

Subcommand Groups

const { SlashCommandBuilder } = require('novecord');
const configCommand = new SlashCommandBuilder()
.setName('config')
.setDescription('Server configuration')
.addSubcommandGroup(group =>
group
.setName('moderation')
.setDescription('Moderation settings')
.addSubcommand(subcommand =>
subcommand
.setName('automod')
.setDescription('Configure automod')
.addBooleanOption(option =>
option
.setName('enabled')
.setDescription('Enable automod')
.setRequired(true)
)
)
.addSubcommand(subcommand =>
subcommand
.setName('logs')
.setDescription('Configure mod logs')
.addChannelOption(option =>
option
.setName('channel')
.setDescription('Log channel')
.setRequired(true)
)
)
)
.addSubcommandGroup(group =>
group
.setName('welcome')
.setDescription('Welcome settings')
.addSubcommand(subcommand =>
subcommand
.setName('message')
.setDescription('Set welcome message')
.addStringOption(option =>
option
.setName('text')
.setDescription('Welcome message text')
.setRequired(true)
)
)
)
.build();

Handling Slash Commands

Process slash command interactions in your bot.

client.on('INTERACTION_CREATE', (interaction) => {
if (interaction.type !== 2) return; // Not a slash command
const { data } = interaction;
if (data.name === 'ping') {
client.respondInteraction(interaction.id, interaction.token, {
content: 'Pong! 🏓'
});
}
if (data.name === 'userinfo') {
const targetUser = data.options.find(opt => opt.name === 'target').value;
const isEphemeral = data.options.find(opt => opt.name === 'ephemeral')?.value || false;
client.respondInteraction(interaction.id, interaction.token, {
content: `User info for <@${targetUser}>`,
flags: isEphemeral ? 64 : 0 // 64 = ephemeral
});
}
if (data.name === 'mod') {
const subcommand = data.options[0];
if (subcommand.name === 'ban') {
const user = subcommand.options.find(opt => opt.name === 'user').value;
const reason = subcommand.options.find(opt => opt.name === 'reason')?.value || 'No reason provided';
// Perform ban logic here
client.respondInteraction(interaction.id, interaction.token, {
content: `Banned <@${user}> for: ${reason}`
});
}
}
if (data.name === 'config') {
const group = data.options[0];
const subcommand = group.options[0];
if (group.name === 'moderation' && subcommand.name === 'automod') {
const enabled = subcommand.options.find(opt => opt.name === 'enabled').value;
client.respondInteraction(interaction.id, interaction.token, {
content: `Automod ${enabled ? 'enabled' : 'disabled'}`
});
}
}
});

Complete Example

A comprehensive example showing command registration and handling.

const { Client, Intents, SlashCommandBuilder, REST, Routes } = require('novecord');
const client = new Client({
token: 'YOUR_BOT_TOKEN',
intents: Intents.Guilds
});
const rest = new REST('YOUR_BOT_TOKEN');
// Create commands
const commands = [
new SlashCommandBuilder()
.setName('echo')
.setDescription('Echo a message')
.addStringOption(option =>
option
.setName('message')
.setDescription('Message to echo')
.setRequired(true)
)
.addBooleanOption(option =>
option
.setName('uppercase')
.setDescription('Convert to uppercase')
.setRequired(false)
)
.build(),
new SlashCommandBuilder()
.setName('admin')
.setDescription('Admin commands')
.setDefaultMemberPermissions('8') // Administrator permission
.addSubcommand(subcommand =>
subcommand
.setName('cleanup')
.setDescription('Clean up messages')
.addIntegerOption(option =>
option
.setName('amount')
.setDescription('Number of messages to delete')
.setRequired(true)
)
)
.build()
];
client.on('READY', async () => {
console.log(`${client.user.username} is ready!`);
// Register commands
try {
await rest.put(Routes.ApplicationCommands(client.user.id), { body: commands });
console.log('Successfully registered slash commands');
} catch (error) {
console.error('Error registering commands:', error);
}
});
client.on('INTERACTION_CREATE', (interaction) => {
if (interaction.type !== 2) return;
const { data } = interaction;
if (data.name === 'echo') {
const message = data.options.find(opt => opt.name === 'message').value;
const uppercase = data.options.find(opt => opt.name === 'uppercase')?.value || false;
const response = uppercase ? message.toUpperCase() : message;
client.respondInteraction(interaction.id, interaction.token, {
content: response
});
}
if (data.name === 'admin') {
const subcommand = data.options[0];
if (subcommand.name === 'cleanup') {
const amount = subcommand.options.find(opt => opt.name === 'amount').value;
// Perform cleanup logic here
client.respondInteraction(interaction.id, interaction.token, {
content: `Cleaned up ${amount} messages`,
flags: 64 // Ephemeral response
});
}
}
});
client.login();