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:
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 modalsmodules/**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.
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)
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)
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:
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:
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 (
srcin development,distin 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
Getting started
Learn how to create your first bot using Sunar in just a few steps.
Working with signals
Learn how to effectively use signals in Sunar to handle various events and interactions in your discord bot. This guide covers the basics of creating, managing, and responding to signals for a more interactive and responsive bot experience.