Middleware is currently only available for commands. More middleware types will be added in future releases.
Command Middleware
Middleware in ShadowCore allows you to execute logic before and after a command’s execution. It is useful for tasks like logging, validation, permission checks, or modifying the command’s behavior. You can define middleware for specific commands or globally for all commands.📂 Middleware Structure
Middleware in ShadowCore are organized within the/middleware directory and categorized into subfolders.
Example Folder Structure:
⚙️ Properties
The CommandMiddleware component allows you to specify the following properties:🧩 Usage
You can create middleware for specific commands or for all commands globally. ThebeforeExecution function runs before the command executes, and the afterExecution function runs after the command has finished executing.
Specific Command Middleware
This middleware will only apply to a specific command. For example, you can create middleware for theping command:
- The
nameis"ping", so this middleware will only apply to thepingcommand. - The
beforeExecutionfunction is executed before the command runs. - The
afterExecutionfunction is executed after the command completes.
Global Command Middleware
Global middleware applies to all commands. It is useful for tasks like logging or permission checks that should be applied universally.- The
nameis"global", so this middleware will apply to all commands. - The
beforeExecutionandafterExecutionfunctions will be run for every command.
🔄 Execution Flow
The middleware’sbeforeExecution function is called before the command executes. If beforeExecution returns false, the command will be blocked and will not execute.
After the command runs, the afterExecution function is called. This happens regardless of whether the command was successful or not. afterExecution only runs if beforeExecution returned true.
🔧 Middleware Order
ShadowCore executes middleware in the order they are registered. For example:- Global middleware is executed first.
- Specific command middleware is executed second.
- The command itself is executed.
- Global middleware’s
afterExecutionis executed. - Specific command middleware’s
afterExecutionis executed last.