Overview
The @nyx-discord/plugin-sessions package is an official plugin that provides and manages user interaction sessions.
These are objects that represent an open interaction "session" with a user. You can consider them as a more advanced, opinionated, OOP version of DJS' Interaction Collectors.
They can be used as an easy way to maintain a "conversation" with a user, and respond to interactions that the user makes to it.
Some examples of sessions are:
- A paginated message that shows a user's moderations, allowing advanced options like filtering by type.
- An interactive message that guides a user step by step to accomplish something, like creating a ticket category.
- A detailed settings message that allows a user to change their server's settings with buttons, modals or select menus.
- In essence, any kind of situation where continuous interaction with a user is needed.
Quick example
This example features many components used in the same session. Some required component properties are omitted for brevity.
import { , , , } from 'discord.js';
import type { , , } from 'discord.js';
import { } from '@nyx-discord/plugin-sessions/djs';
class extends {
protected override readonly = 5_000; // End 5 seconds after the last interaction
public async () {
// A select menu that updates this session
const = this.('select');
const = new ().();
const = new <>().();
// A button that updates this session
const = this.('button');
const = new ().();
const = new <>().();
await this..({ : [, ] });
}
public async () {
await this..({
: 'Bye from session!',
: [],
});
}
protected async (: ) {
const = this.('modal');
const = new ().();
await .();
return true;
}
protected async (: ) {
await .('You selected: ' + .[0]);
return true;
}
protected async (: ) {
const = ..('name');
await .(`Your name is ${}`);
return true;
}
}Then somewhere in your code, like inside a command:
// Can be a command, component or modal interaction
declare const : ;
const = 'mySessionId'; // Should be short and randomly generated, nanoid is good for this
const = new ({ , , });
await .();
// Wait for the session to end
await .();
// Wait 3 seconds
await new ( => (, 3000));
await .('Bye from command!');After filling the modal and components with values, this will look like this:
Notice how:
- The plugin routes interactions belonging to the session to the appropiate handler.
- The session finishes automatically after the
ttlhas passed since the last interaction. - The session starter is able to wait for the session to end, and then do what it needs to, like reply further.
We'll dive deeper on what each of the methods do later, but this should be enough to give you an idea of how sessions work.
Installation
Installing
npm i @nyx-discord/plugin-sessionsRegistering
Instantiate and register it to your bot's plugin manager:
import { } from '@nyx-discord/plugin-sessions/djs';
await .().(.());And you're done! Check the session types guides to learn how to make your own Session.
