Events

Load Discord.js events automatically from a folder.

Register Events Folder

Call .events('events') once during setup. Files inside that folder are loaded automatically, and event run functions receive ctx as the first argument.

1import { App, Intents } from 'discordjs-nextgen';
2
3const app = new App({ intents: Intents.ALL });
4
5app
6 .events('events')
7 .login(process.env.TOKEN!);

ctx in Event Files

For events registered through .events(), the run signature starts with ctx. Then the native Discord event arguments follow after it.

1import { AppEvent } from 'discordjs-nextgen';
2
3const readyEvent: AppEvent<'ready'> = {
4 name: 'ready',
5 run: async (ctx, user) => {
6 console.log(`Logged in as ${user.tag}`);
7 await ctx.log('Bot is ready');
8 },
9};
10
11export default readyEvent;

Example Usage

Create it inside events/ready.ts.

1import { AppEvent } from 'discordjs-nextgen';
2
3const readyEvent: AppEvent<'ready'> = {
4 name: 'ready',
5 run: (ctx, user) => {
6 console.log(`${user.tag} is ready!`);
7 console.log(ctx.user?.id);
8 },
9};
10
11export default readyEvent;

Interaction Event Example

If you are not using built-in .button() or .slash() handlers, you can still use ctx manually inside interactionCreate.

1import { AppEvent } from 'discordjs-nextgen';
2
3const interactionEvent: AppEvent<'interactionCreate'> = {
4 name: 'interactionCreate',
5 run: async (ctx, interaction) => {
6 if (interaction.isButton() && interaction.customId === 'open_form') {
7 await ctx.showModal('feedback_form');
8 }
9 },
10};
11
12export default interactionEvent;

Features

  • Automatic event loading from folders.
  • ctx is injected first for files loaded with .events().
  • Strong TypeScript support for event parameters.
  • Centralized event organization.