In Seyfert middleware are functions that are called before the command is executed. You can use them to do verifications, logging, etc.
Let’s create a basic middleware that logs in the command that is being executed.
Creating a middleware
Now let’s register the middlewares on seyfert but first we should create a file to export all our middleware
Now we can use the logger middleware on any command.
Now every time the ping command is executed, the logger middleware will log forward the command.
Stop middleware
As we had said you can use middlewares to do verifications, and you can stop the execution of the command if the verification fails.
Let’s take a look adding some logic to the logger middleware.
Now every time the ping command is executed in a DM, the logger middleware will stop the execution of the command and send the error message to the handler. Learn how to handle errors here.
On the other hand we could skip the interaction (ignore the interaction and literally do nothing) by using middle.pass()
Passing data
The last thing we can do with middlewares is to pass data to the command. This can be useful to avoid repeating the same code in multiple commands for example fetching data from the database.
We’ll continue with the logger middleware and pass some data to the command.
Now let’s modify the ping command to receive the data.