Skip to Content
nyx logonyx guide

Pagination Session

See in:
AbstractPaginationSessionGitHub
Reading time:4 min read

It's recommended to read the Session page first, all of its features apply to this.

A PaginationSession is a special type of session made to be paginated (have pages).

From a manager's perspective, a pagination session is just a regular session. The pagination logic happens inside the session itself.

Page switch

A page switch is triggered when the user sends an interaction to the pagination session, and a page can be extracted from it:

  1. It attempts to extract a referred page, depending on the interaction type:
    • Buttons: Extracts from the customId.
    • Select Menu: Extracts from the customId. If no page is present there, and it's a StringSelectMenu, and only one value was selected, it extracts from the selected value.
    • Modal: Extracts from the customId. If no page is present there, it iterates through the modal's components, searching for a component whose customId is the session's customId. If there is one, the page is extracted from said component's value.
  2. If there's no referred page, it behaves like a regular session, passing the interaction to the appropiate handler.
  3. If there's a referred page, it updates its currentPage and calls #updatePage().

Overriding page resolving

You can override this behavior by overriding extractPageFromInteraction(), or the more concrete methods:

  • extractPageFromButton() for buttons
  • extractPageFromSelectMenu() for select menus
  • extractPageFromModal() for modals

Creation

Creation

Extend the AbstractPaginationSession class:

import {  } from '@nyx-discord/plugin-sessions/djs';

class  extends  {}

Implementation

Apart from the base Session implementation, also implement the updatePage() method:

import { , type  } from '@nyx-discord/plugin-sessions/djs';

  protected async (: ): <boolean> {
    if ('update' in ) {
      await .({ : `Page: ${this.}` });
    }
    return true;
  }
}

Similarly to update(), it must return whether the session's TTL should be renewed or not.

Initialization

Instantiate and start your session the same way as a regular one:

const  = new ({ , : , :  });
await .();

Pagination customId

The page to switch to is usually extracted from the customId. This means you'll need to change the way you build it in case you want to make a component that switches to a specific page.

// For a specific page
const  = this.(3);
const  = new ()
  .()
  .('Go to page 3');

// For the next page
const  = this.(this. + 1);
const  = new ()
  .()
  .('Next page');

You can add more ways by overriding extractPageFromButton().

Page switch row

The AbstractPaginationSession has a utility method for building an action row containing two buttons that switch to the previous and next page:

const  = this.();
await this..({ : [] });

This row uses emoji arrows to avoid issues with localized text.

On this page