Slash Commands Setup
Learn how to register and manage slash commands using the SlashCommandsUpdate class.
Introduction
Slash commands need to be registered with Discord before they can be used. Meowcord v0.1.6 includes the SlashCommandsUpdate
class to handle this process.
You can register commands globally (available in all servers) or for specific guilds (servers).
Creating the SlashCommandsUpdate Instance
First, create an instance of the SlashCommandsUpdate class:
const { Meow, Intents, SlashCommandsUpdate } = require("meowcord");
const bot = new Meow({
intents: [Intents.Guilds, Intents.GuildMessages, Intents.MessageContent],
prefix: "!",
});
// For global commands
const slashUpdater = new SlashCommandsUpdate({
client: bot.discordClient,
token: "YOUR_BOT_TOKEN"
});
// For guild-specific commands
const guildSlashUpdater = new SlashCommandsUpdate({
client: bot.discordClient,
token: "YOUR_BOT_TOKEN",
guildId: "YOUR_GUILD_ID"
});
Defining Slash Commands
Slash commands are defined using Discord's application command format:
const commands = [
{
name: "ping",
description: "Replies with Pong!"
},
{
name: "echo",
description: "Echoes your message",
options: [
{
name: "message",
description: "The message to echo",
type: 3, // STRING
required: true
}
]
},
{
name: "userinfo",
description: "Get information about a user",
options: [
{
name: "user",
description: "The user to get info about",
type: 6, // USER
required: false
}
]
}
];
Registering Commands
Use the updateCommands
method to register your slash commands:
bot.on("ready", async () => {
console.log(`Logged in as ${bot.discordClient.user.tag}!`);
try {
// Register global commands (takes up to 1 hour to update)
await slashUpdater.updateCommands(commands);
console.log("Global slash commands registered successfully!");
// Or register guild commands (updates immediately)
// await guildSlashUpdater.updateCommands(commands);
// console.log("Guild slash commands registered successfully!");
} catch (error) {
console.error("Failed to register slash commands:", error);
}
});
Handling Slash Commands
After registering commands, handle them using interaction commands:
// Handle the ping command
bot.interactionCommand({
cmdName: "ping",
interactionType: "slash",
cmdCode: async (interaction) => {
const ping = Date.now() - interaction.createdTimestamp;
await interaction.reply(`🏓 Pong! Latency: ${ping}ms`);
}
});
// Handle the echo command with options
bot.interactionCommand({
cmdName: "echo",
interactionType: "slash",
cmdCode: async (interaction) => {
const message = interaction.options.getString("message");
await interaction.reply(`You said: ${message}`);
}
});
// Handle the userinfo command
bot.interactionCommand({
cmdName: "userinfo",
interactionType: "slash",
cmdCode: async (interaction) => {
const user = interaction.options.getUser("user") || interaction.user;
const member = interaction.guild.members.cache.get(user.id);
const embed = {
title: user.tag,
description: "User Information",
color: 0xff1493,
thumbnail: { url: user.displayAvatarURL({ dynamic: true }) },
fields: [
{ name: "ID", value: user.id, inline: true },
{ name: "Joined", value: member?.joinedAt?.toDateString() || "Unknown", inline: true },
{ name: "Created", value: user.createdAt.toDateString(), inline: true }
]
};
await interaction.reply({ embeds: [embed] });
}
});
Command Option Types
Discord supports various option types for slash commands:
1 - SUB_COMMAND
2 - SUB_COMMAND_GROUP
3 - STRING
4 - INTEGER
5 - BOOLEAN
6 - USER
7 - CHANNEL
8 - ROLE
9 - MENTIONABLE
10 - NUMBER
11 - ATTACHMENT
const advancedCommand = {
name: "advanced",
description: "An advanced command with multiple option types",
options: [
{
name: "text",
description: "Some text input",
type: 3, // STRING
required: true
},
{
name: "number",
description: "A number input",
type: 4, // INTEGER
required: true,
min_value: 1,
max_value: 100
},
{
name: "user",
description: "Select a user",
type: 6, // USER
required: false
},
{
name: "channel",
description: "Select a channel",
type: 7, // CHANNEL
required: false
},
{
name: "enabled",
description: "Enable or disable something",
type: 5, // BOOLEAN
required: false
}
]
};
Complete Example
Here's a complete example with slash command registration and handling:
const { Meow, Intents, SlashCommandsUpdate } = require("meowcord");
const bot = new Meow({
intents: [Intents.Guilds, Intents.GuildMessages, Intents.MessageContent],
prefix: "!",
});
const TOKEN = "YOUR_BOT_TOKEN";
// Create slash command updater
const slashUpdater = new SlashCommandsUpdate({
client: bot.discordClient,
token: TOKEN
});
// Define slash commands
const commands = [
{
name: "ping",
description: "Check the bot's latency"
},
{
name: "say",
description: "Make the bot say something",
options: [
{
name: "message",
description: "What should the bot say?",
type: 3, // STRING
required: true
}
]
}
];
bot.on("ready", async () => {
console.log(`Logged in as ${bot.discordClient.user.tag}!`);
try {
await slashUpdater.updateCommands(commands);
console.log("Slash commands registered successfully!");
} catch (error) {
console.error("Failed to register slash commands:", error);
}
});
// Handle slash commands
bot.interactionCommand({
cmdName: "ping",
interactionType: "slash",
cmdCode: async (interaction) => {
const ping = Date.now() - interaction.createdTimestamp;
await interaction.reply(`🏓 Pong! Latency: ${ping}ms`);
}
});
bot.interactionCommand({
cmdName: "say",
interactionType: "slash",
cmdCode: async (interaction) => {
const message = interaction.options.getString("message");
await interaction.reply(message);
}
});
bot.start(TOKEN);
Global vs Guild Commands
Understanding the difference between global and guild commands:
- Global Commands: Available in all servers, take up to 1 hour to update
- Guild Commands: Only available in specific servers, update immediately
Use guild commands for development and testing, then switch to global commands for production.