Skip to content

Latest commit

 

History

History
194 lines (142 loc) · 6.03 KB

README.md

File metadata and controls

194 lines (142 loc) · 6.03 KB

Publish to npm CodeQL Dependabot Updates Discord Server npm version npm downloads

Discraft

Discraft is a powerful framework for creating Discord bots, offering a robust CLI and a set of tools to streamline the development process. Think of it like Next.js but for Discord bots.

Features

  • Command Caching: Built-in LRU cache system for commands with configurable TTL and memory limits
  • Multi-step Commands: Support for commands that require follow-up responses or editing
  • Robust CLI: Comprehensive command-line interface for development and deployment
  • Modern Build System: Advanced bundling with Rollup, Babel transformations, and Terser minification
  • Hot Reload: Automatic server restart on code changes during development
  • Optimized Production Builds: Aggressive minification for production with preserved functionality

Installation

You can install Discraft via npm:

npm install discraft --save-dev # Use this to install Discraft in the current project
npm install discraft -g # May require sudo, globally installs Discraft so you can use it from anywhere

# For beta releases
npm install discraft@beta # Install the latest beta version

Usage

Discraft provides a CLI interface. You can use Discraft like this:

npx discraft [command]
# or
discraft [command]

CLI Commands

  • discraft init: Initialize a new Discraft project.
  • discraft dev: Start the development server.
  • discraft build: Build the project for production.
  • discraft start: Start the production server.
  • discraft test token: Test your bot token and client ID.
  • discraft add command: Create a new command file.
  • discraft add event: Create a new event handler file.
  • discraft help: Display available commands and help options.

Project Structure

The project is organized into several directories:

  • src/commands: Contains command files for the bot
  • src/events: Contains event handlers for Discord events
  • src/config: Configuration files for bot settings
  • src/services: Service integrations (Discord.js client setup)
  • src/utils: Utility functions including logging and caching

Auto-generated

  • src/discraft: Core framework functionality, auto-generated by Discraft

Build Output Structure

When built for production, the project is rolled up into a single file called bundle.js. This file is the entry point for the bot and includes all necessary dependencies and configurations. It's easy to deploy to production and is optimized for performance.

Development

To start the development server:

discraft dev

The development server provides:

  • Hot reloading of command and event files
  • Detailed logging with debug information
  • Automatic error handling and recovery

Dependencies

Key production dependencies (v1.5.4):

  • discord.js: ^14.16.3
  • @babel/core: ^7.26.0
  • @rollup/plugin-babel: ^6.0.4
  • commander: ^12.1.0
  • dotenv: ^16.4.5
  • rollup: ^4.27.3
  • terser: ^5.36.0

Development tools:

  • eslint: ^9.15.0
  • Various ESLint plugins for code quality

Command Caching

Discraft includes a powerful command caching system to improve performance and reduce API calls:

import { SlashCommandBuilder } from "discord.js";
import { commandCache } from "../utils/commandCache.js";

// Set command-specific cache settings
commandCache.setCommandSettings("ping", {
  ttl: 5000, // Cache ping results for 5 seconds
});

export default {
  data: new SlashCommandBuilder()
    .setName("ping")
    .setDescription("Replies with Pong"),
  cacheable: true,
  async execute(interaction) {
    const response = `Pong! Latency: ${Math.round(
      interaction.client.ws.ping
    )}ms`;
    await interaction.reply(response);
    return { content: response }; // Return in a format that can be used by interaction.reply()
  },
};

The caching system supports both simple responses and multi-step interactions. For multi-step commands, you can return an array of steps:

export default {
  data: new SlashCommandBuilder()
    .setName("complex-status")
    .setDescription("Complex status check with updates"),
  cacheable: true,
  execute: async (interaction) => {
    return {
      steps: [
        { content: "Initial response..." },
        { type: "edit", content: "Updated status..." },
        { type: "followUp", content: "Additional info..." },
      ],
    };
  },
};

The cache system automatically handles:

  • Storing command results based on command name and options
  • Multi-step responses with edits and follow-ups
  • Command-specific TTL (Time To Live) settings
  • LRU (Least Recently Used) eviction strategy
  • Memory management

Setup

  1. Install dependencies:

    npm install
  2. Create a .env file with your bot token:

    BOT_TOKEN=your_bot_token_here
    CLIENT_ID=your_client_id_here
    
  3. Start development:

    discraft dev

Beta Releases

Beta versions are available for testing new features. To install the latest beta:

npm install discraft@beta

Contributing

Contributions are welcome! Please visit the GitHub repository to report issues or submit pull requests.

License

This project is licensed under the GNU General Public License 3.0.