Namespace

Namespaces are simply files in which you can export stuff that you will use in your commands, like utils.

Namespaces are utility modules that allow you to organize and share code across your bot. They are TypeScript files where you can export functions, classes, constants, middlewares, and any other reusable code.

Why use namespaces?

  • Code organization: Keep related utilities together

  • Reusability: Share code between commands, listeners, and other components

  • Type safety: Full TypeScript support with auto-imports

  • Clean imports: Access via the #namespaces/* alias

Create a namespace

You must use the CLIarrow-up-right to create your namespaces. The CLI automatically registers the namespace in package.json imports.

CLI pattern

bot add namespace

The CLI will prompt you for:

  1. The namespace name (e.g., utils, middlewares, constants)

  2. Whether to import the core module

Namespace structure

A namespace file is a standard TypeScript module. Here's a basic example:

Using namespaces

Import your namespace using the #namespaces/* alias:

In a command

In a listener

Common use cases

1. Middlewares

Create reusable command middlewares:

Usage in a command:

2. Constants and configuration

3. API wrappers

4. Database helpers

File location

Namespaces are stored in the src/namespaces/ directory:

Best practices

  1. Single responsibility: Each namespace should have a clear purpose

  2. Descriptive names: Use clear, descriptive namespace names

  3. Export explicitly: Export only what needs to be shared

  4. Type everything: Add proper TypeScript types to all exports

  5. Document complex functions: Add JSDoc comments for complex utilities

  6. Avoid circular dependencies: Be careful not to create circular imports between namespaces

Last updated