Filters
Reading time:1 min read
Filters restrict the execution of a given schedule. For example:
- A maintenance filter that checks if the system is in maintenance mode
- A production filter that restricts the execution to the production environment.
They receive all the information about the schedule and the execution metadata, and return a boolean indicating whether the schedule 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
AbstractScheduleFilterand implements thecheck()method.
import { } from '@nyx-discord/djs';
import type { Schedule, } from '@nyx-discord/djs';
class extends {
public async (: Schedule, : ): <boolean> {
// Do some checks
return true; // or false if the schedule 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 : = async (, ) => {
// Do some checks
return true; // or false if the schedule should not be executed
};Filter usage
Override the filter property in your schedule:
- If you created a class, instantiate it:
import { } from '@nyx-discord/djs';
class extends {
protected override readonly = new ();
}- If you created a function, assign it directly:
import { } from '@nyx-discord/djs';
class extends {
protected override readonly = ;
}Or use a simple arrow function directly:
import { } from '@nyx-discord/djs';
class extends {
protected override readonly = () => {
// Do some checks
return true;
};
}Examples
Maintenance Filter
A filter that checks if the system is in maintenance mode, and prevents the schedule from running if it is.
import { } from '@nyx-discord/djs';
import type { Schedule, } from '@nyx-discord/djs';
class extends {
public async (: Schedule, : ): <boolean> {
// Check if the system is in maintenance mode, e.g. from an env variable or database
return .. !== 'true';
}
}Production Filter
A filter that restricts the execution to the production environment.
import { } from '@nyx-discord/djs';
import type { Schedule, } from '@nyx-discord/djs';
class extends {
public async (: Schedule, : ): <boolean> {
return .. === 'production';
}
}