Middleware
Your bot supports optional middleware, similar to how it works in popular web frameworks like Express.js. There are two types of middleware you can use to intercept traffic and add pre-execution hooks.
1. General Middleware (bot.Use())
Section titled “1. General Middleware (bot.Use())”This is the main middleware that runs on every raw incoming message, checking it before the bot even decides if it’s a valid command or just a regular conversation.
- Middlewares are executed in the exact order they are added.
- Each middleware receives the full context of the incoming message and decides whether to continue the chain by calling
next().
Use this for features that need to inspect all traffic, such as:
- Global logging and analytics
- Spam protection / Rate limiting
- Blocking users across the entire bot
const bot = new WhatsbotCord({ commandPrefix: ["!"], credentialsFolder: "./auth",});
// Add a general middleware for loggingbot.Use((bot, senderId_LID, senderId_PN, chatId, rawMsg, msgType, senderType, next) => { // ✅ This code runs for EVERY incoming message console.log(`Incoming message from ${senderId_LID} in ${chatId}`);
// Continue to the next middleware (or to command parsing) next();});2. Command-Specific Middleware (bot.Use_OnCommandFound())
Section titled “2. Command-Specific Middleware (bot.Use_OnCommandFound())”This is targeted middleware that runs only after a valid command is found, but before that command’s run() method is executed. This applies to both regular Commands and Tags.
This makes it the perfect place for logic that should only apply to intentional bot interactions, such as:
- Permission Checks: Is the user an admin? Do they have a specific role?
- Command-Specific Logging: Tracking who uses what command.
- Cooldowns: Preventing spam on intensive commands.
The function signature includes an extra commandFound object carrying the command’s profile.
// Register an admin-only commandbot.Commands.Add( { name: "ban", async run(ctx) { await ctx.SendText("Banning user..."); }, }, CommandType.Normal);
// Add middleware for permission checksbot.Use_OnCommandFound(async (bot, senderId_LID, senderId_PN, chatId, rawMsg, msgType, senderType, commandFound, next) => { const admins = ["admin1@s.whatsapp.net", "admin2@s.whatsapp.net"];
// ✅ This code ONLY runs if a valid !command is triggered
// Check if command is 'ban' and if the user is an admin if (commandFound.name === "ban" && !admins.includes(senderId_LID)) { // Block the command and send feedback await bot.SendMsg.Text(chatId, "❌ You don't have permission to use the 'ban' command.");
// IMPORTANT: Return without calling next() to halt execution return; }
// Allow execution to continue and actually trigger the run() method await next();});Important Notes
Section titled “Important Notes”If you don’t call next(), the middleware chain stops dead in its tracks.
- In a general middleware, the message will not be evaluated for commands.
- In a command-specific middleware, the command’s
run()method will not be executed.