Loading Commands from Files
Learn how to organize and load commands from external files.
Why Load Commands from Files?
As your bot grows, keeping all commands in a single file can become unwieldy. Loading commands from separate files offers several benefits:
- Better organization and maintainability
- Easier to find and modify specific commands
- Ability to enable/disable commands by adding or removing files
- Cleaner main bot file
File Structure
A common file structure for a meowcord bot with external command files looks like this:
project/
├── index.js
├── config.js
└── commands/
├── fun/
│ ├── ping.js
│ └── hello.js
├── moderation/
│ ├── kick.js
│ └── ban.js
└── utility/
├── help.js
└── info.js
Command File Format
Each command file should export an object with the command properties:
// commands/fun/ping.js
module.exports = {
cmdName: "ping",
cmdDescription: "Responds with the bot's ping",
cmdCode: async (message) => {
const ping = Date.now() - message.createdTimestamp;
message.reply(`🏓 Pong! My ping is currently ${ping}ms`);
}
};
For advanced commands, the format is similar:
// commands/utility/custom-handler.js
module.exports = {
cmdName: "custom-handler",
cmdCode: async (message) => {
// Custom command logic here
if (message.content.toLowerCase().includes("help me")) {
message.reply("I'm here to help! Try using one of my commands.");
}
}
};
Loading Commands
Meowcord provides the loadCommandFromFolder
method to load commands from a directory:
const { Meow, Intents } = require("meowcord");
const path = require("path");
const bot = new Meow({
intents: [Intents.Guilds, Intents.GuildMessages, Intents.MessageContent],
prefix: "!",
});
// Load all commands from the commands folder
bot.loadCommandFromFolder(path.join(__dirname, "commands"));
bot.start("YOUR_BOT_TOKEN");
This will load all command files from the specified directory and its subdirectories.
Loading Individual Command Files
You can also load individual command files using loadCommandFrom
:
// Load a specific command file
bot.loadCommandFrom(path.join(__dirname, "commands", "fun", "ping.js"));
Reloading Commands
As of version 0.1.3, meowcord uses a two-step process for reloading commands. This ensures that code changes are properly reflected:
// Step 1: Clear all existing commands
bot.clearCommands();
// Step 2: Reload commands from the folder
bot.loadCommandFromFolder(path.join(__dirname, "commands"));
This approach ensures that the require cache is cleared, allowing any changes to your command files to be properly loaded.
Command Parameters
As of version 0.1.3, meowcord automatically detects whether your command function needs the args
parameter:
// Command that only needs the message parameter
bot.basicCommand({
cmdName: "ping",
cmdCode: async (message) => {
// Only message parameter is passed
const ping = Date.now() - message.createdTimestamp;
message.reply(`🏓 Pong! My ping is currently ${ping}ms`);
}
});
// Command that needs both message and args parameters
bot.basicCommand({
cmdName: "say",
cmdCode: async (message, args) => {
// Both message and args parameters are passed
if (!args.length) return message.reply("Please provide something for me to say!");
message.reply(args.join(" "));
}
});
Meowcord will automatically detect the number of parameters your command function expects and pass the appropriate arguments.
Complete Example
Here's a complete example of a bot that loads commands from files:
// index.js
const { Meow, Intents } = require("meowcord");
const path = require("path");
const bot = new Meow({
intents: [Intents.Guilds, Intents.GuildMessages, Intents.MessageContent],
prefix: "!",
meowSettings: {
returnBotInfosInConsole: true
},
});
// Load all commands from the commands folder
bot.loadCommandFromFolder(path.join(__dirname, "commands"));
// Ready event
bot.on("ready", () => {
console.log(`Logged in as ${bot.discordClient.user.tag}!`);
console.log(`Loaded ${bot.commands.length} commands`);
});
// Function to reload commands (for development)
function reloadCommands() {
// Clear existing commands
bot.clearCommands();
// Load commands again
bot.loadCommandFromFolder(path.join(__dirname, "commands"));
console.log(`Reloaded ${bot.commands.length} commands`);
}
// Example command to reload commands at runtime
bot.basicCommand({
cmdName: "reload",
cmdCode: async (message) => {
reloadCommands();
message.reply("Commands reloaded successfully!");
}
});
bot.start("YOUR_BOT_TOKEN");
Example command file:
// commands/fun/ping.js
module.exports = {
cmdName: "ping",
cmdDescription: "Responds with the bot's ping",
cmdCode: async (message) => {
const ping = Date.now() - message.createdTimestamp;
message.reply(`🏓 Pong! My ping is currently ${ping}ms`);
}
};