ns Docsns Docs
Handlers

Overview

Understanding the request-response lifecycle and handler registration in nsCore.

Introduction

Handlers are the core glue of your Discord bot architecture. They are responsible for loading, registering, and managing logic such as commands and global error handling — keeping your codebase clean, scalable, and predictable.

In nsCore, handlers sit between your client, commands, and events, ensuring everything is wired correctly at runtime.

Handlers do not contain business logic. They only load, validate, and connect logic to the Discord client.


What Are Handlers?

A handler is a centralized system that:

  • Automatically loads files from folders
  • Registers them to the client
  • Validates structure and permissions
  • Handles failures safely

This avoids:

  • Manual imports
  • Repetitive boilerplate
  • Runtime crashes due to invalid files

Handler Types in nsCore

1. Command Handler

Responsible for:

  • Loading slash commands
  • Loading message (prefix) commands
  • Validating command interfaces
  • Registering commands into collections
client.slashCommands = new Collection()
client.messageCommands = new Collection()

Commands are loaded recursively, so you can organize them into folders freely.

You can add new commands without touching any loader code. Just drop a file in the correct directory.


2. Error Handler

Responsible for:

  • Catching unhandled promise rejections
  • Catching uncaught exceptions
  • Reporting errors to a Discord channel
  • Preventing silent crashes

It listens directly to Node.js process-level events:

process.on('unhandledRejection', ...)
process.on('uncaughtException', ...)
process.on('uncaughtExceptionMonitor', ...)

Without an error handler, production bots may crash silently or lose important debugging context.


Why This Architecture?

This handler-based system gives you:

  • Scalability – works for small & large bots
  • Safety – invalid files won’t crash the bot
  • Maintainability – logic stays separated
  • Developer experience – easy onboarding

This pattern is inspired by production Discord bots and frameworks, but kept minimal and transparent for learning purposes.


Folder Structure (Conceptual)

src/
├─ handlers/
│  ├─ commandHandler.ts
│  ├─ errorHandler.ts
│  └─ index.ts

├─ commands/
│  ├─ slashCommands/
│  └─ messageCommands/

Handlers scan folders, not files explicitly.


What’s Next?

Now that you understand the role of handlers, the next documents will deep-dive into:

  • commandHandler.mdx → How commands are discovered & loaded
  • errorHandler.mdx → How runtime errors are captured & reported

Once handlers are clear, commands and events become extremely easy to understand.

Last updated on

On this page