Extensions
Extensions are a very powerful resource in DDBKit that allows you to incorporate middleware functionality into your bot interactions.
There are two ways to use extensions:
- Registering a
DDBKitExtension
type to yourBotInstance
. - Using modifiers exposed by the extension to intercept commands. And you can combine both to create a powerful extension that can do a lot of things.
Registering an extension
This is important to all end-users of extensions. If you need to use an extension that acts on a global level, you need to add some code to your bot app that runs on startup.
When your bot launches, the extension will be registered and will be able to intercept commands, as well as adding and removing commands and events.
Using extension modifiers
This is important to all end-users of extensions. This is for using an extension-provided modifier that acts on a command-scope.
Extensions can expose modifiers that can intercept actions of the command they’re attached to.
Check out this example of a hypothetical extension modifier:
In this hypothetical example, the levelRequirement
modifier is provided by an extension and will only allow the command to be executed if the user has an XP level of 20 or higher.
The modifier can throw early if the requirement is not met, plus they could add their own error handling too, but that wouldn’t be cool.
Maybe the modifier requires its extension to be registered for storing and retrieving XP levels.
Example extension ideas
- XP System: An extension that tracks user XP and levels, from messages and command interactions.
- Moderation: An extension that layers a permission system for moderation commands where your server consists of different rankings rather than moderator and admin.
- Logging: An extension that logs all events, commands and interactions to a database for later review.
- Custom Commands: An extension that aggregates some data and creates a command to view that data.
- Ratelimiting: An extension that limits the number of commands a user can run in a certain time frame or bucket.
A Discord bot could encapsulate functionality and behaviour to be more modular, or just be a drop-in system. It depends on your preference and goals.
Creating an extension
Registerable Extension
To create an extension, you need to create an actor that conforms to the DDBKitExtension
protocol.
From in this onBoot(_:)
function, you can interact with BotInstance
, which is the main class behind the user’s Discord bot.
It contains a bunch of useful properties and functions that you can use to interact with the bot.
The onBoot(_:)
function is called when BotInstance
finishes setting up the environment and is ready to start the bot.
It then calls the function of all registered extensions in the order they were registered.
Here’s the interface of BotInstance
:
If you didn’t already notice inout
, it means you’ve been passed a reference to the BotInstance
,
and you can modify it directly and your changes will be reflected in the original instance.
Here’s an example to print all registered commands.
You can edit the commands from here too, such as adding new commands, removing commands, or modifying existing commands.
DDBKitExtension
has two more methods called at other times.
Modifiers
To create a modifier, you just extend ExtensibleCommand
with new methods that return type Self
.
Here’s some of the interface for ExtensibleCommand
:
You can combine these modifiers inside of your own modifier to create a chain of actions that will be executed as part of an interaction.
The boot
modifier runs your closure immediately after extension registering finishes
so you can do initial setup if needed, allowing you to work with a global extension instance.
Here’s an example of a modifier that prints something before and after a command is run:
This modifier will print the command name when the bot boots, when the command is started, when the command is completed, and when the command errors.
In this example, this command will fail, and the modifier will print the command name, the user who started the command, and the error it threw. The postAction will not run in this case.
You can then combine all of the above with your existing skills to make a powerful extension that’ll help make people’s bots better.
Modifier utilities
You may need to interact with your global extension from within a modifier, but you can’t store a reference to it anywhere that you can access easily.
To solve this, DDBKit exposes some utilities, one of which can pull a reference from the BotInstance
for you quickly and easily.
InteractionExtras
Extensions
InteractionExtras
is the structure passed into all interaction closures in DDBKit,
which exposes it’s wrapped Interaction
structure and a reference to the BotInstance
.
It’s extended with computed properties to expose the gateway and cache objects, and
you can extend it with your own properties and methods for your extension.
You can now expose methods and properties from InteractionExtras
to your extension for users.