Saltearse al contenido
sern

Plugins

Esta página aún no está disponible en tu idioma.

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:

Terminal window
sern plugins
  1. Install your favorite(s) (or the ones that look the coolest). In my imaginary mind, I installed the ownerOnly plugin. (This should install to src/plugins)
  2. Add the plugin to your module in the plugins field.
src/commands/ping.ts
1
import { commandModule, CommandType } from '@sern/handler'
2
import { ownerOnly } from '../plugins'
3
4
export 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.

1
export 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.

src/plugins/inDir.ts
1
import { CommandInitPlugin } from "@sern/handler";
2
import path from "path";
3
4
export 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 plugin
15
});
16
};

Event Plugins

control plugins

  1. An event is emitted by discord.js.
  2. This event is passed to all plugins (in order!!),
  3. If all are successful, the command is executed.

Can you predict the behavior of this command?

  1. Before loading into sern, this command module will check if this module is in the correct directory: other.
  2. Before an event occurs, this command module will check if the user has the id 182326315813306368.
  3. If all plugins return controller.next(), this command replies Pong 🏓