Adding Commands
Learn how to add different types of commands to your meowcord bot.
Basic Commands
Basic commands are the simplest way to add functionality to your bot. They are triggered when a user types your bot's prefix followed by the command name.
bot.basicCommand({
cmdName: "ping",
cmdCode: async (message) => {
// Command code here
message.reply("Pong!");
}
});
The cmdName
is the name of the command that users will type after your prefix, and cmdCode
is a function that will be executed when the command is triggered.
Command Parameters
As of version 0.1.3, meowcord automatically detects whether your command function needs the args parameter. You can define your command function with either one parameter (message) or two parameters (message, args):
// Command with just message parameter
bot.basicCommand({
cmdName: "ping",
cmdCode: async (message) => {
const ping = Date.now() - message.createdTimestamp;
message.reply(`🏓 Pong! My ping is currently ${ping}ms`);
}
});
// Command with message and args parameters
bot.basicCommand({
cmdName: "greet",
cmdCode: async (message, args) => {
if (!args.length) {
return message.reply("Please provide a name to greet!");
}
message.reply(`Hello, ${args.join(" ")}! Nice to meet you!`);
}
});
In the second example, if a user types !greet John Doe
, the bot will respond with "Hello, John Doe! Nice to meet you!".
Parsing Arguments
When using the args parameter, it comes as an array of strings split by spaces:
bot.basicCommand({
cmdName: "add",
cmdCode: async (message, args) => {
if (args.length < 2) {
return message.reply("Please provide two numbers to add!");
}
const num1 = parseFloat(args[0]);
const num2 = parseFloat(args[1]);
if (isNaN(num1) || isNaN(num2)) {
return message.reply("Please provide valid numbers!");
}
const sum = num1 + num2;
message.reply(`The sum of ${num1} and ${num2} is ${sum}.`);
}
});
Command Options
Basic commands support several options to customize their behavior:
bot.basicCommand({
cmdName: "help",
cmdAliases: ["h", "assistance"], // Alternative command names
cmdDescription: "Shows a list of available commands", // Description for help commands
cmdCode: async (message) => {
// Command code here
message.reply("Here's a list of commands...");
}
});
The available options include:
cmdName
: The primary name of the command (required)cmdAliases
: An array of alternative names for the commandcmdDescription
: A description of what the command doescmdCode
: The function that will be executed (required)
Managing Commands
Meowcord provides several methods for managing commands at runtime:
// Get a list of all commands
const commands = bot.commands;
console.log(commands);
// Remove a specific command
bot.removeCommand("ping");
// Clear all commands
bot.clearCommands();
// Reload commands (as of v0.1.3)
bot.clearCommands();
bot.loadCommandFromFolder("./commands");
Listing Commands
You can create a command that lists all available commands by accessing the bot.commands
array:
bot.basicCommand({
cmdName: "commands",
cmdCode: async (message) => {
const commandList = bot.commands.map(cmd => cmd.name).join(", ");
message.reply(`Available commands: ${commandList}`);
}
});
// Or create a more detailed help command
bot.basicCommand({
cmdName: "help",
cmdCode: async (message) => {
const commandList = bot.commands
.map(cmd => `**!${cmd.name}**${cmd.description ? ` - ${cmd.description}` : ""}`)
.join("\n");
message.reply(`**Available Commands:**\n${commandList}`);
}
});
This creates a command that users can use to see all available commands in your bot. The first example shows a simple list, while the second example creates a more detailed help command with descriptions.
Command Information
Each command object in the bot.commands
array contains:
name
: The command nametype
: The command type ("basic" or "advanced")code
: The command functiondescription
: The command description (if provided)
bot.basicCommand({
cmdName: "commandinfo",
cmdCode: async (message, args) => {
if (!args.length) {
return message.reply("Please provide a command name to get info about!");
}
const commandName = args[0].toLowerCase();
const command = bot.commands.find(cmd => cmd.name === commandName);
if (!command) {
return message.reply(`Command "${commandName}" not found!`);
}
const info = [
`**Command:** ${command.name}`,
`**Type:** ${command.type}`,
`**Description:** ${command.description || "No description provided"}`
].join("\n");
message.reply(info);
}
});