Skip to content

Latest commit

Β 

History

8 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Discord.js Starter Template with CommandKit

A modern Discord.js starter template featuring CommandKit for command handling, database integration, and a clean project structure.

πŸš€ Features

  • CommandKit Integration: Modern command and event handling system
  • Database Support: Built-in SQLite and JSON database support using st.db
  • Sequelize ORM: Advanced database operations with Sequelize
  • YAML Configuration: Easy configuration management
  • Event System: Organized event handling structure
  • Validation System: Built-in user role validation with Components V2
  • TypeScript Support: Full TypeScript definitions for CommandKit
  • Modular Architecture: Clean separation of concerns
  • Automatic Database Sync: Database tables are automatically synchronized on startup

πŸ“ Project Structure

β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ Commands/          # Slash commands
β”‚   β”‚   └── Ping.js       # Example ping command with TypeScript annotations
β”‚   β”œβ”€β”€ Events/           # Discord events
β”‚   β”‚   β”œβ”€β”€ clientReady/  # Bot ready events
β”‚   β”‚   β”‚   └── ready.js  # Bot ready event handler
β”‚   β”‚   └── messageCreate/ # Message events
β”‚   β”‚       └── hi.js     # Message event handler example
β”‚   β”œβ”€β”€ Database/         # Database configuration
β”‚   β”‚   β”œβ”€β”€ Config/       # Database configs
β”‚   β”‚   β”‚   β”œβ”€β”€ index.js  # Database exports
β”‚   β”‚   β”‚   β”œβ”€β”€ Example.js # Example database config
β”‚   β”‚   β”‚   β”œβ”€β”€ Sequelize.js # Sequelize configuration
β”‚   β”‚   β”‚   └── YamlConfig.js # YAML configuration
β”‚   β”‚   β”œβ”€β”€ Data/         # Database files
β”‚   β”‚   β”‚   └── Example.json # Example database data
β”‚   β”‚   β”œβ”€β”€ Helpers/      # Database helper functions
β”‚   β”‚   β”‚   └── index.js  # Helper exports
β”‚   β”‚   └── Models/       # Database models
β”‚   β”‚       └── User.js   # User model with Sequelize
β”‚   β”œβ”€β”€ Validations/      # Command validations
β”‚   β”‚   └── userRoles.js  # User role validation with Components V2
β”‚   └── Utils/           # Utility functions
β”œβ”€β”€ Config.yaml          # Bot configuration
β”œβ”€β”€ index.js            # Application entry point
└── package.json        # Dependencies

πŸ› οΈ Installation

  1. Clone the repository

    git clone https://github.com/AboMeezO/D.js-Starter-Files
    cd D.js-Starter-Files
  2. Install dependencies

    npm install
  3. Configure your bot

    • Open Config.yaml
    • Add your Discord bot token:
      Token: YOUR_BOT_TOKEN_HERE
  4. Start the bot

    npm start

βš™οΈ Configuration

Bot Token Setup

Edit the Config.yaml file and add your Discord bot token:

Token: your_discord_bot_token_here

Database Configuration

The template includes multiple database systems:

  • YAML Config: For bot configuration (Config.yaml)
  • Example Database: Additional database for examples (src/Database/Data/Example.json)

πŸ“ Creating Commands

Commands are located in src/Commands/. Here's the structure:

export default {
  /**
   * @type {import('commandkit').CommandData}
   */
  data: {
    name: "commandname",
    description: "Command description",
  },
  /**
   *
   * @param {import('commandkit').SlashCommandProps} param0
   */
  run: async ({ interaction, client, handler }) => {
    await interaction.reply("Command response!");
  },
  /**
   * @type {import('commandkit').CommandOptions}
   */
  options: {
    botPermissions: [],
    userPermissions: [],
    deleted: false,
    devOnly: false,
    guildOnly: false,
  },
};

Command Options

  • botPermissions: Required bot permissions
  • userPermissions: Required user permissions
  • deleted: Whether the command is deleted
  • devOnly: Restrict to developers only
  • guildOnly: Restrict to guilds only

🎯 Creating Events

Events are located in src/Events/. Create folders for event types and add your event files:

Client Ready Event

export default (client) => {
  console.log(`Bot is ready! Logged in as ${client.user.tag}`);
};

Message Create Event

/**
 * @param {import('discord.js').Message} message
 * @param {import('discord.js').Client} client
 */
export default async (message, client) => {
  if (message.content !== "hi" || message.author.bot) return;
  await message.reply("hey");
};

πŸ” Validations

The template includes a user role validation system in src/Validations/userRoles.js using Discord.js Components V2. Add it to your command options:

options: {
  userRoles: ["role_id_1", "role_id_2"], // Required role IDs
}

Validation Structure

The validation system uses Components V2 for enhanced user experience:

import {
  ContainerBuilder,
  MessageFlags,
  SeparatorBuilder,
  SeparatorSpacingSize,
  TextDisplayBuilder,
} from "discord.js";

/**
 * @param {import('commandkit').ValidationFunctionProps} param0
 */
export default function ({ interaction, commandObj }) {
  const requiredRoles = new Set(commandObj.options?.userRoles);
  if (!requiredRoles.size) return false;

  const hasRequiredRole = interaction.member.roles.cache.some((role) =>
    requiredRoles.has(role.id)
  );

  if (!hasRequiredRole) {
    interaction.reply({
      flags: [MessageFlags.Ephemeral, MessageFlags.IsComponentsV2],
      components: [
        new ContainerBuilder()
          .addTextDisplayComponents(
            new TextDisplayBuilder().setContent(
              `## β›” **Insufficient Permissions**`
            )
          )
          .addSeparatorComponents(
            new SeparatorBuilder().setSpacing(SeparatorSpacingSize.Large)
          )
          .addTextDisplayComponents(
            new TextDisplayBuilder().setContent(
              `You need the following roles:\n\n${[...requiredRoles]
                .map((role) => `β€’ <@&${role}>`)
                .join("\n")}`
            )
          ),
      ],
    });
    return true;
  }
  return false;
}

πŸ—„οΈ Database Usage

The template includes multiple database systems for different use cases:

YAML Configuration

import { YamlConfig } from "./Database/index.js";

// Get configuration
const token = await YamlConfig.get("Token");

// Set configuration
await YamlConfig.set("Key", "Value");

JSON Database (st.db)

import { Example } from "./Database/index.js";

// Example Database
await Example.set("exampleKey", "exampleValue");
const exampleData = await Example.get("exampleKey");

Sequelize ORM with SQLite

The template includes Sequelize for advanced database operations:

import { sequelize, Users } from "./Database/index.js";

// Database initialization (handled automatically in app.js)
await sequelize.sync();

// Create a new user
const newUser = await Users.create({
  id: "123456789",
  username: "example_user",
  level: "1",
});

// Find a user
const user = await Users.findOne({
  where: { id: "123456789" },
});

// Update a user
await Users.update({ level: "2" }, { where: { id: "123456789" } });

// Delete a user
await Users.destroy({
  where: { id: "123456789" },
});

User Model Structure

The included User model provides a foundation for user management:

import { DataTypes } from "sequelize";
import { sequelize } from "../Config/index.js";

const Users = sequelize.define("users", {
  id: {
    primaryKey: true,
    unique: true,
    type: DataTypes.STRING,
  },
  username: {
    type: DataTypes.STRING,
    allowNull: false,
  },
  level: {
    type: DataTypes.STRING,
    defaultValue: "0",
    allowNull: false,
  },
});

πŸ—οΈ Application Structure

The template includes a modular app class for easy bot management:

App Class Usage

import app from "./src/app.js";

// Initialize with existing client
const bot = new app({ client: yourClient });

// Initialize with token (creates new client)
const bot = new app({ token: "your_bot_token" });

// Start the bot
await bot.Start();

Automatic Database Initialization

The app class automatically handles database synchronization:

async initDatabase() {
  await sequelize.sync();
}

This ensures all database tables are created and synchronized when the bot starts.

πŸ“¦ Dependencies

  • discord.js: Discord API wrapper
  • commandkit: Command and event handler
  • st.db: Database management
  • sequelize: SQL ORM
  • sqlite3: SQLite database driver
  • zod: Schema validation

πŸš€ Development

Development Mode

The template includes nodemon for development:

npm run dev

File Watching

Nodemon is configured to ignore database files during development to prevent unnecessary restarts.

πŸ“‹ Available Scripts

  • npm start: Start the bot
  • npm run dev: Start in development mode with auto-restart
  • npm test: Run tests (placeholder)

🀝 Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Submit a pull request

πŸ“„ License

This project is licensed under the ISC License.

πŸ‘¨β€πŸ’» Author

Created by @AboTasneem

πŸ”— Links


Note: Remember to keep your bot token secure and never commit it to version control!

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages