Middleware
A guide for using middleware in ShadowCore.
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. The beforeExecution
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 the ping
command:
In this example:
- The
name
is"ping"
, so this middleware will only apply to theping
command. - The
beforeExecution
function is executed before the command runs. - The
afterExecution
function 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.
In this example:
- The
name
is"global"
, so this middleware will apply to all commands. - The
beforeExecution
andafterExecution
functions will be run for every command.
🔄 Execution Flow
The middleware’s beforeExecution
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
afterExecution
is executed. - Specific command middleware’s
afterExecution
is executed last.
This allows for layered logic and flexibility.
🎯 Example: Logging Middleware
You can use middleware to log the execution of each command:
In this case, the middleware logs when a command starts and finishes.