A fast, lightweight, and feature-rich Discord bot handler with beautiful console UI. Built on discord.js (v14+), this framework provides seamless support for both modern Slash Commands and classic Prefix Commands, with built-in cooldowns, statistics tracking, and elegant logging.
- ⚡ Dual Command System — Full support for Slash Commands (
/) and Prefix Commands (!) - 🧩 Modular Architecture — Clean separation of
CommandHandlerandEventHandlerfor easy maintenance - ⏱️ Parallel Loading — Commands and events load simultaneously using
Promise.allfor lightning-fast startup - 🔒 Cooldown System — Built-in per-user cooldowns prevent spam and abuse
- 📊 Live Statistics — Track command usage, executions, and top users with 5-second caching
- 🎨 Beautiful Console — Custom logger with color-coded output for better visibility
- 🚀 Production Ready — Error handling, validation, and performance optimizations included
Follow these simple steps to deploy your Discord bot.
- Node.js (v18 or newer).
- A Discord Application and Bot Token.
- Basic understanding of
discord.jsv14.
Clone the repository and install the required dependencies:
git clone https://github.com/MalayaliDev/Discord-Bot-Handler-V1.git
cd Discord-Bot-Handler-V1
npm installCreate a file named .env in the root directory and add your application credentials.
| Variable | Description | Requirement |
|---|---|---|
DISCORD_TOKEN |
The secret token for your Discord Bot. | Required |
CLIENT_ID |
Your Discord Application ID (for registering Slash Commands). | Required |
GUILD_ID |
The ID of your primary testing/development server. | Highly Recommended (For instant slash command registration) |
Example .env:
DISCORD_TOKEN=your_bot_token_here
CLIENT_ID=123456789012345678
GUILD_ID=987654321098765432
Use the defined scripts in package.json to start your bot.
| Command | Description |
|---|---|
npm start |
Runs the bot using node index.js. |
npm run dev |
Runs the bot using node --watch index.js for automatic restarts during development. |
The handler automatically detects and loads commands based on their location.
Bot Handler/
├── commands/ # Prefix commands (e.g., !ping)
│ └── utility/ # Command category
│ ├── ping.js
│ └── hello.js
├── slash_command/ # Slash commands (e.g., /ping)
│ └── utility/ # Command category
│ ├── ping.js
│ └── stats.js
├── events/ # Discord event handlers
│ ├── ready.js
│ ├── interactionCreate.js
│ └── messageCreate.js
├── handlers/ # Core handlers
│ ├── commandHandler.js
│ └── eventHandler.js
├── utils/
│ └── logger.js # Beautiful console logging
├── index.js # Main bot file
├── package.json
└── .env # Configuration (create from .env.example)
Slash commands use Discord's SlashCommandBuilder for registration.
// Example: slash_command/utility/hello.js
import { SlashCommandBuilder } from 'discord.js';
export default {
type: 'slash',
cooldown: 3,
data: new SlashCommandBuilder()
.setName('hello')
.setDescription('Says hello!'),
async execute(interaction) {
await interaction.reply(`Hello ${interaction.user.username}!`);
}
};Prefix commands use a simple object structure for the command name.
// Example: commands/utility/hello.js
export default {
type: 'prefix',
cooldown: 3,
data: { name: 'hello' },
async execute(message, args) {
await message.reply(`Hello ${message.author.username}!`);
}
};Command Properties:
type— Command type ('slash'or'prefix')data— Command metadata (required)cooldown— Cooldown in seconds (default: 3)execute()— Command logic function (required)
The CommandHandler uses an in-memory Collection to store command usage timestamps (userId-commandName).
- When a command is executed,
client.commandHandler.checkCooldown()is called. - If
command.cooldownis defined, that value is used; otherwise, it defaults to 3 seconds. - If the time difference is less than the cooldown, the remaining time is calculated and returned, stopping the command execution.
This class manages the lifecycle of commands:
-
loadCommands(): Scans the file system, imports command modules, and stores them inslashCommandsandprefixCommandsCollections for$O(1)$ lookup. -
registerSlashCommands(): Extracts thedataobjects from all slash commands and usesclient.application.commands.set(commands)to register them with Discord upon bot login. -
getStats(): Efficiently calculates and caches runtime metrics every 5 seconds, including:- Uptime
- Total Executions
- Top 5 Commands (by usage count)
- Top 5 Users (by usage count)
loadEvents(): Scans theevents/directory, imports the event modules, and registers them usingclient.on()orclient.once()based on theeventModule.onceflag.
These are the main runtime processors:
- Filter/Parse: They first filter out non-relevant events (e.g., bot messages, non-command interactions).
- Lookup: They perform a fast command lookup.
- Cooldown Check: They immediately check the cooldown.
- Log & Track: They log the command execution using the custom
loggerand calltrackCommandUsage(). - Execution & Error Handling: They execute the command's logic and gracefully handle errors, replying to the user with a clean error message.
Feel free to open an issue on the GitHub repository if you encounter any bugs or have suggestions for new features!