A modular, extensible Discord app framework for Node.js, with built-in support for slash commands, localization, and event-driven architecture.
- Simple, opinionated Discord app setup for Node.js
- Slash command registration and handler auto-loading
- Event handler auto-loading for all Discord Gateway events
- Built-in localization system with easy locale file management
- TypeScript type definitions included
- Dependency injection and testability for all major components
- Logging and error handling hooks
- Extensible and modular directory structure
npm install @purinton/discord
import 'dotenv/config';
import log from '@purinton/log';
import path from '@purinton/path';
import { createDiscord } from '@purinton/discord';
try {
await createDiscord({
log,
rootDir: path(import.meta),
intents: { MessageContent: true }
});
} catch (err) {
log.error('Failed to start app:', err);
}
require('dotenv/config');
const log = require('@purinton/log').default;
const path = require('@purinton/path').default;
const { createDiscord } = require('@purinton/discord');
(async () => {
try {
await createDiscord({
log,
rootDir: path(__filename),
intents: { MessageContent: true }
});
} catch (err) {
log.error('Failed to start app:', err);
}
})();
Creates and logs in a Discord client, auto-registers commands, loads event handlers, and sets up localization.
Options:
client_id
(string): Discord application client ID (required)token
(string): Discord bot token (required)log
(Logger): Logger instance (optional)rootDir
(string): Root directory for events, commands, and locales (default: autodetect)localesDir
(string): Directory for locale files (default:<rootDir>/locales
)commandsDir
(string): Directory for command definitions and handlers (default:<rootDir>/commands
)eventsDir
(string): Directory for event handlers (default:<rootDir>/events
)intents
(object): Discord Gateway Intents (default: Guilds and GuildMessages enabled)partials
(array): Discord.js partials (default:['MESSAGE', 'CHANNEL', 'REACTION']
)clientOptions
(object): Additional Discord.js client optionsClientClass
(constructor): Custom Discord.js Client class (for testing)setupEventsFn
,setupCommandsFn
,registerCommandsFn
,setupLocalesFn
: Dependency injection for advanced use/testingcontext
(object): Additional arbitrary data to be injected into all event and command handlers
Returns: A logged-in Discord.js Client
instance.
Splits a message into chunks of up to maxLength
characters, attempting to split at newlines or periods for readability.
msg
(string): The message to splitmaxLength
(number, optional): The maximum length of each chunk (default: 2000)- Returns: An array of message chunks, each no longer than
maxLength
.
- Commands:
- Place
.json
files in thecommands/
directory for each command definition (seecommands/help.json
for structure). - Place a
.mjs
file with the same name for the command handler (seecommands/help.mjs
).
- Place
- Events:
- Place
.mjs
files in theevents/
directory, named after Discord Gateway events (e.g.,ready.mjs
,messageCreate.mjs
).
- Place
- Place locale files in the
locales/
directory (e.g.,en-US.json
,es-ES.json
). - Each file should be a flat key-value JSON object for that locale.
Type definitions are included and cover all public APIs and options:
import type { CreateDiscordOptions } from '@purinton/discord';
declare function createDiscord(options?: CreateDiscordOptions): Promise<Client>;
For help, questions, or to chat with the author and community, visit: