Middlewares
Reading time:1 min read
Middlewares are objects or callbacks that run before any session is executed. They can do some pre-processing logic and store it in the metadata, or even prevent the subscriber from being executed.
Specifically, sessions have two types of middlewares: one for starting sessions, and one for updating them.
For general information about middlewares, you must first read the general Middlewares guide.
Usage
Creation
Either:
- Create a class that extends
BaseSessionStartMiddlewareorBaseSessionUpdateMiddlewareand implements thecheck()method.
import {
,
} from '@nyx-discord/plugin-sessions/djs';
import type {
DjsInteractionTypes,
,
,
} from '@nyx-discord/djs';
import type { , } from '@nyx-discord/plugin-sessions/djs';
class extends {
public async (
: <unknown, DjsInteractionTypes>,
:
): <> {
// Do some pre-processing logic
return this.();
}
}
class extends {
public async (
: <unknown, DjsInteractionTypes>,
: ,
:
): <> {
return this.();
}
}- Create a function that returns a
MiddlewareResponse.
import type { DjsInteractionTypes, } from '@nyx-discord/djs';
import type {
,
,
,
} from '@nyx-discord/plugin-sessions/djs';
const : = async (
: <unknown, DjsInteractionTypes>,
:
) => {
// Do some pre-processing logic
return {
: true,
: true,
};
};
const : = async (
: <unknown, DjsInteractionTypes>,
: ,
:
) => {
return {
: true,
: true,
};
};Registration
Register it to the executor's MiddlewareList:
const = .();
.().(new (), );
.().(new (), );