Plugins
Installation
Chances are, you just want your bot to work. Plugins can preprocess and create reusable conditions for modules.
To install plugins, you can use the CLI:
sern plugins
- Install your favorite(s) (or the ones that look the coolest). In my imaginary mind, I installed the
ownerOnly
plugin. (This should install tosrc/plugins
) - Add the plugin to your module in the
plugins
field.
1import { commandModule, CommandType } from '@sern/handler'2import { ownerOnly } from '../plugins'3
4export default commandModule({5 type: CommandType.Both,6 plugins: [ownerOnly(['182326315813306368'])],7 description: 'ping command',8 execute: (ctx) => {9 ctx.reply('hello, owner');10 }11})
┗|` O′|┛ perfect, your first plugin!
Creating Plugins
Plugins are essentially functions that use the controller object to determine whether to continue or stop the execution of a command.
Controller Object
The controller object is passed into every plugin. It has two methods: next
and stop
.
Plugins use these methods to control the flow of the command. For example, if a plugin fails, it can call controller.stop()
to prevent the command from executing.
1export interface Controller {2 next: () => Ok<void>;3 stop: () => Err<void>;4}
Init Plugins
Init plugins modify how commands are loaded or do preprocessing.
An instance of Controller
(as seen above) is passed into every plugin. This controls whether a module is stored into sern.
1import { CommandInitPlugin } from "@sern/handler";2import path from "path";3
4export const inDir = (dir: string) => {5 return CommandInitPlugin(({ module, absPath }) => {6 if (path.dirname(absPath) !== dir) {7 console.log(8 Date.now(),9 `${module.name} is not in the correct directory!`,10 );11 return controller.stop();12 }13 console.log(Date.now(), `${module.name} is in the correct directory!`);14 return controller.next(); // continue to next plugin15 });16};
Event Plugins
- An event is emitted by
discord.js
. - This event is passed to all plugins (in order!!),
- If all are successful, the command is executed.
Can you predict the behavior of this command?
- Before loading into sern, this command module will check if this module is in the correct directory:
other
. - Before an event occurs, this command module will check if the user has the id
182326315813306368
. - If all plugins return
controller.next()
, this command repliesPong 🏓