Skip to Content
nyx logonyx guide

Overview

Reading time:1 min read

The @nyx-discord/loaders package provides directory-scanning loaders that automatically discover and instantiate your commands, event subscribers, and schedules from compiled output directories.

Instead of manually importing and registering every project file, you point a loader at a directory and it scans .js / .mjs files, instantiates them, adds them to the Bot, and returns them.

Installation

Installing

npm i @nyx-discord/loaders

Usage

Each loader is exposed as a single load() method:

import { , ,  } from '@nyx-discord/loaders';

const commands = await .({ , : './dist/commands' });
const commands: TopLevelCommand<any>[]
const events = await .({ , : './dist/events' });
const events: EventSubscriberBuckets
const schedules = await .({ , : './dist/schedules' });
const schedules: Schedule[]

These returned objects are already added by the loader, they are only returned for informational purposes.

Options

All loaders share a common options interface:

type  = {
  /** The bot to load objects for. Also passed to static create() methods. */
  : ;
  /** The directory to scan for .js / .mjs files. */
  : string;
  /**
   * Whether to auto-register loaded objects with the bot.
   * @default true
   */
  ?: boolean;
  /**
   * Optional filter to include only specific files by their absolute path.
   * Files returning false are silently skipped.
   */
  ?: (: string) => boolean;
};

register

Set to false to skip automatic registration and handle it yourself:

import {  } from '@nyx-discord/loaders';

const  = await .({
  ,
  : './dist/commands',
  : false,
});
.().(...);

filter

Filter files by their absolute path before they are imported. Useful for excluding tests, dev-only files, or selectively loading subsets:

import {  } from '@nyx-discord/loaders';

const  = await .({
  ,
  : './dist/events',
  : () => !.('.test.') && !.('.spec.'),
});

The filter is applied before any file is imported, making it efficient for excluding large numbers of files.

Edit on GitHub

Last updated on

On this page