Skip to Content
nyx logonyx guide

Events

Reading time:2 min read

It's recommended to read the Events Overview and Event Subscribers pages first.

The EventLoader docs scans a directory for event subscriber classes and returns them grouped into six typed arrays.

Folder structure

There is no enforced folder structure, any file in any subdirectory at any depth will be classified by the subscriber class it extends:

A recommended convention:

ready.js
command-add.js
service-start.js
plugin-add.js
subscriber-add.js
schedule-add.js

Usage

By default, events are automatically subscribed to the appropriate buses:

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

await .({ , : './dist/events' });

Pass register: false to handle registration yourself:

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

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

await .(....);
await .().(....);
await .().(....);
await .().(....);
await .().(....);

EventBus subscribers (events.bus) cannot be registered automatically, so you need to attach them manually

Writing subscribers for the loader

Any class extending one of the six abstract subscriber base classes will be detected:

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

export class  extends <'ready'> {
  protected readonly  = 'ready' as ;

  public (: , : <true>) {
    .(`Bot logged in as ${..}`);
  }
}

Top-level subscribers are instantiated with new Class() (no arguments). Subscribers don't receive a parent instance.

Alternatively, you can specify a static create(bot) factory:

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

export class  extends <'ready'> {
  protected readonly  = 'ready' as ;

  public static (: ) {
    return new ();
  }

  // ...
}

The loader will call this method instead of the constructor if it exists. This allows you to access the bot instance, or add logic, for example to inject services or other dependencies with a DI container.

Edit on GitHub

Last updated on

On this page