Filters
Filters restrict the execution of a given command. You may be familiar with them from other usages, like:
- A cooldown filter that prevents a command from being executed too frequently
- A permission filter that checks if a member has some required permissions.
They receive all the information about the command, the interaction that triggered it, and the execution metadata, and return a boolean indicating whether the command should be executed or not.
For more general information about filters, you must first read the general Filters guide.
Given only these are executable, you can only use this in Standalone Commands, SubCommands and Context Menu Commands.
Usage
In this example we'll use a standalone command, but the same applies to the rest.
Creation
'Filter Denied' replies
The command will simply not be called if the filter returns false, so if you want some kind of reply, you'll need to do it yourself in the filter.
Either:
- Create a class that extends
AbstractCommandFilterand implements thecheck()method.
import { } from '@nyx-discord/djs';
import type { , , } from '@nyx-discord/djs';
class extends {
public async (
: ,
: ,
:
): <boolean> {
// Do some checks
return true; // or false if the command 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 command should not be executed
};Filter usage
Override the filter property in your command:
- 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
Cooldown Filter
A simple cooldown filter that prevents a command from being executed too frequently, while also cleaning up expired cooldowns every minute:
import { } from '@nyx-discord/djs';
import type { , , } from '@nyx-discord/djs';
class extends {
protected readonly = new <string, number>();
protected readonly : NodeJS.Timeout;
protected readonly : number;
constructor(: number = 5000) {
super();
this. = ;
this. = (() => {
const = .();
for (const [, ] of this.) {
if ( >= ) {
this..();
}
}
}, 60_000).();
}
public async (
: ,
: ,
:
): <boolean> {
const = ..;
const = .();
const = this..() ?? 0;
if ( < ) {
// Ideally reply to the interaction, telling the user they are on cooldown
return false;
}
this..(, + this.);
return true;
}
}Permission Filter
A permission filter that checks if a member has the required permissions to execute a command:
import { } from '@nyx-discord/djs';
import type { , , } from '@nyx-discord/djs';
import { } from 'discord.js';
class extends {
protected readonly : bigint;
constructor(: bigint) {
super();
this. = ;
}
public async (
: ,
: ,
:
): <boolean> {
if (!. || !.()) {
// This will depend on whether you want to allow outside of cached guilds or not
return false;
}
if (!...(this.)) {
// Ideally reply to the interaction, telling the user they lack permissions
return false;
}
return true;
}
}