Sunar

Load modules

Modules in Sunar encapsulate related commands, signals and components, enabling efficient organization and management of your bot's capabilities. Learn how to leverage Sunar's modular architecture to streamline development and scalability.

Example

All you have to do is use the load function and use a tinyglobby pattern:

src/index.js
import { Client, load } from 'sunar';

const client = new Client(/* your options */);

// you can add more directories to load by
// passing them with a comma after "signals"
await load('src/{commands,signals}/**/*.{js,ts}');

client.login('YOUR_DISCORD_BOT_TOKEN');

The {commands,signals} part is a glob pattern that matches both commands and signals directories. You can add more directories by extending this pattern, for example:

  • {commands,signals,events} to include an events directory
  • {commands,signals,buttons,modals} for buttons and modals
  • modules/** to include all subdirectories inside a modules folder

Replace 'YOUR_DISCORD_BOT_TOKEN' with your actual bot token obtained from the Discord Developer Portal.

File structure

Here's an overview of the project's file structure to help you visualize how files are organized within Sunar.

ping.js
interaction-create.js
ready.js
index.js

Usage in JavaScript and TypeScript

Sunar supports both JavaScript and TypeScript projects. Below are examples for each environment and recommendations to ensure your glob pattern works in both development and production.

JavaScript (src)

src/index.js
import { Client, load } from 'sunar';

const client = new Client(/* your options */);

await load('src/{commands,signals}/**/*.js');

client.login('YOUR_DISCORD_BOT_TOKEN');

TypeScript (development)

src/index.ts
import { Client, load } from 'sunar';

const client = new Client(/* your options */);

await load('src/{commands,signals}/**/*.{js,ts}');

client.login('YOUR_DISCORD_BOT_TOKEN');

TypeScript (production, after build)

If you build your bot (for example, using tsup, tsc, or similar), your code will be in the dist folder (or whichever you configure). Adjust the glob to target the compiled files:

dist/index.js
import { Client, load } from 'sunar';

const client = new Client(/* your options */);

await load('dist/{commands,signals}/**/*.js');

client.login('YOUR_DISCORD_BOT_TOKEN');

Using dirname for absolute paths

To make your code work the same in both development and production, use Sunar's dirname function with import.meta.url:

src/index.ts
import { Client, load, dirname } from 'sunar';

const client = new Client(/* your options */);

// This gets the base path (src or dist) depending on the environment
const base = dirname(import.meta.url);

await load(`${base}/{commands,signals}/**/*.{js,ts}`);

client.login('YOUR_DISCORD_BOT_TOKEN');

Tips and best practices

  • Always use absolute or project-root-relative paths to avoid issues after building.
  • If you use TypeScript, make sure your build copies all necessary files (not just .js, but also .json, etc.).
  • The glob pattern must match your actual build structure (src in development, dist in production).
  • If you have issues loading modules after building, check that the path and file extension are correct.

How is this guide?

Last updated on