Middlewares
Explore how Sunar enhances your discord bot's functionality with middleware support.
Protectors
Protectors in Sunar act as middleware, allowing you to intercept and control the flow of commands and interactions within your Discord bot. They provide a flexible way to enforce permissions, validate inputs, or perform pre-processing before executing commands.
Create the protector logic
Create a file only-admins.js
in the directory src/protectors/
. This file defines a protector onlyAdmins
that checks if the user has administrator permissions.
When creating a Protector
instance, specify the commands, signals, or components that will use this protector. This allows precise control over the arguments received through function parameters.
Create a protected command
Create a file protected.js
in the directory src/commands/
. This file defines a Slash command named protected
that is protected by the onlyAdmins
protector. Only administrators can execute this command, and it replies with a message indicating the user is an administrator.
File structure
Here's an overview of the project's file structure to help you visualize how files are organized in this example.
Last updated on