Filters
Filters restrict the execution of a given subscriber. For example:
- A bot filter that ignores events triggered by bots
- A guild filter that restricts the execution to a specific guild.
They receive all the information about the subscriber, the event arguments that triggered it, and the execution metadata, and return a boolean indicating whether the subscriber should be executed or not.
For more general information about filters, you must first read the general Filters guide.
Usage
Creation
Either:
- Create a class that extends
AbstractEventSubscriberFilterand implements thecheck()method.
import { } from '@nyx-discord/djs';
import type { , } from '@nyx-discord/djs';
class extends <, 'someEvent'> {
public async (
: <>,
: ,
...: ['someEvent']
): <boolean> {
// Do some checks
return true; // or false if the subscriber should not be executed
}
}- Create a function that returns a boolean or a Promise that resolves to a boolean.
import { type } from '@nyx-discord/djs';
const : <
,
'someEvent'
> = async (, , ...) => {
// Do some checks
return true; // or false if the subscriber should not be executed
};Generic filter
You can also write a filter that will work regardless of the event being handled, by omitting the event name in the type arguments:
type = {
: [string, number];
: [boolean];
};
// With a class:
class extends <> {
public async (
: <>,
: ,
...args: [keyof ] ) { /** ... */ }
}
// Or with a function:
const : <> = async (
,
...args ) => { /** ... */ };Notice how args accepts a union of the possible event arguments for any event in the MyEventsArgs type.
Filter usage
Override the filter property in your subscriber:
- If you created a class, instantiate it:
import { } from '@nyx-discord/djs';
class extends <, 'someEvent'> {
protected override readonly = new ();
}- If you created a function, assign it:
import { } from '@nyx-discord/djs';
class extends <, 'someEvent'> {
protected override readonly = ;
}Or use a simple arrow function directly:
import { } from '@nyx-discord/djs';
class extends <, 'someEvent'> {
protected override readonly = () => {
// Do some checks
return true;
};
}Examples
Bot Filter
A simple filter that ignores events triggered by bots. This example assumes the event argument is a Message.
import { } from '@nyx-discord/djs';
import type { , } from '@nyx-discord/djs';
import type { , ClientEvents } from 'discord.js';
class extends <ClientEvents, 'messageCreate'> {
public async (
: <ClientEvents, 'messageCreate'>,
: ,
:
): <boolean> {
return !..;
}
}Guild Filter
A filter that restricts the execution to a specific guild. This example assumes the event argument has a guildId property (like an Interaction or Message).
import { } from '@nyx-discord/djs';
import type { , } from '@nyx-discord/djs';
import type { ClientEvents } from 'discord.js';
class extends <ClientEvents, 'interactionCreate' | 'messageCreate'> {
protected readonly : string;
constructor(: string) {
super();
this. = ;
}
public async (
: <ClientEvents, 'interactionCreate' | 'messageCreate'>,
: ,
: { : string | null }
): <boolean> {
return . === this.;
}
}