Skip to Content
nyx logonyx guide

Filters

Reading time:2 min read

Filters are objects or callbacks that can restrict the execution of a given session. For example:

  • A user filter that restricts the interaction to the user who started the session
  • A permission filter that checks if the user has the required permissions to interact.

Filters receive all the information about the session, the interaction and the execution metadata, and return a boolean indicating whether the session should be executed or not.

Unlike other filterable objects, a session can have two filters: the startFilter and the updateFilter.

For more general information about filters, you must first read the general Filters guide.

Usage

In this guide we'll use a regular session, but the same applies to the rest.

Creation

Either:

  • Create a class that extends AbstractSessionStartFilter or AbstractSessionUpdateFilter and implements the check() method.
import type {  } from '@nyx-discord/djs';
import { ,  } from '@nyx-discord/plugin-sessions/djs';
import type { ,  } from '@nyx-discord/plugin-sessions/djs';

class  extends <unknown> {
  public async (: <unknown>, : ): <boolean> {
    // Do some checks
    return true; // or false if the session should not be executed
  }
}

class  extends <unknown> {
  public async (
   : <unknown>,
   : ,
   : 
 ): <boolean> {
    // Do some checks
    return true; // or false if the session should not be executed
  }
}

The type argument is the return type of your session.

  • Create a function that returns a boolean or a Promise that resolves to a boolean.
import type {
  ,
  
} from '@nyx-discord/plugin-sessions/djs';

const : <unknown> = async (, ) => {
  // Do some checks
  return true; // or false if the session should not be executed
};

const : <unknown> = async (
  ,
  ,
  ,
) => {
  // Do some checks
  return true; // or false if the session should not be executed
};

Filter usage

Override the startFilter or updateFilter property in your session:

  • If you created a class, instantiate it:
import {  } from '@nyx-discord/plugin-sessions/djs';

class  extends  {
  protected override readonly  = new ();
  
  protected override readonly  = new ();
}
  • If you created a function, assign it directly:
import {  } from '@nyx-discord/plugin-sessions/djs';

class  extends  {
  protected override readonly  = ;
  
  protected override readonly  = ;
}

Or use a simple arrow function directly:

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

class  extends  {
  protected override readonly  = () => {
    return true;
  };

  protected override readonly  = () => {
    return true;
  };
}

Examples

User Filter

A filter that restricts the interaction to the user who started the session. This is commonly used in updateFilter to prevent other users from interacting with the session.

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

class  extends <unknown> {
  public async (
    : <unknown, DjsInteractionTypes>,
    : ,
    : 
  ): <boolean> {
    const  = .();
    if (!) return true;

    // Check if the user who interacted is the same as the one who started the session
    return .. === ..;
  }
}

Permission Filter

A permission filter that checks if the user has the required permissions to interact with the session.

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

class  extends <unknown> {
  protected readonly : bigint;

  constructor(: bigint) {
    super();
    this. = ;
  }

  public async (
    : <unknown, DjsInteractionTypes>,
    : ,
    : 
  ): <boolean> {
    if (!. || !.()) {
      return false;
    }

    return ...(this.);
  }
}

On this page