discord-handler.js
v2.0.0
Published
A simple Discord.js command & event handler with support for slash, message, and categories.
Maintainers
Readme
discord-handler.js
A lightweight and flexible command & event handler for Discord.js bots.
Supports slash commands, message commands, and event handling out of the box.
Perfect for quickly bootstrapping Discord bots with clean structure.
📦 Installation
npm install discord-handler.js✨ Features
- ⚡ Simple & fast setup
- 🧩 Supports slash & message commands
- 📂 Organized folder-based structure with optional categories
- 🔔 Event handling (client + custom events)
- 🌍 Global & guild slash command deployment
- 🛠️ Ready-to-use
CommandandEventclasses
📂 Example Project Structure
my-bot/
├── commands/
│ ├── slash/
│ │ └── info/
│ │ └── ping.js
│ └── message/
│ └── info/
│ └── ping.js
├── events/
│ ├── clientReady.js
│ ├── interactionCreate.js
│ └── messageCreate.js
├── deploy.js
├── index.js
└── package.json🚀 Usage
1️⃣ Initialize Handler
// index.js
const { Client, GatewayIntentBits } = require("discord.js");
const { loadCommands, loadEvents } = require("discord-handler.js");
const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent],
});
// Initialize handler
createBot(client, {
slash: true,
message: true,
category: true,
events: true,
});
client.login("YOUR_BOT_TOKEN");
2️⃣ Creating Commands
📌 Slash Command
// commands/slash/info/ping.js
const { Command } = require("discord-handler.js");
module.exports = new Command({
name: "ping",
description: "Replies with Pong!",
type: "slash",
category: "info",
run: async (client, interaction) => {
await interaction.reply("🏓 Pong! (Slash Command)");
}
});
📌 Message Command
// commands/message/info/ping.js
const { Command } = require("discord-handler.js");
module.exports = new Command({
name: "ping",
description: "Replies with Pong!",
type: "message",
category: "info",
run: async (client, message) => {
await message.reply("🏓 Pong! (Message Command)");
}
});
3️⃣ Creating Events
// events/clientReady.js
const { Event } = require("discord-handler.js");
module.exports = new Event({
name: "clientReady",
once: true,
run: (client) => {
console.log(`${client.user.tag} is online!`);
}
});// events/interactionCreate.js
const { Event } = require("discord-handler.js");
module.exports = new Event({
name: "interactionCreate",
run: async (client, interaction) => {
if (!interaction.isChatInputCommand()) return;
const command = client.commands.slash.get(interaction.commandName);
if (command) command.run(client, interaction);
}
});
// events/messageCreate.js
const { Event } = require("discord-handler.js");
module.exports = new Event({
name: "messageCreate",
run: async (client, message) => {
if (message.author.bot || !message.guild) return;
const prefix = "!"; // you can make this configurable later
if (!message.content.startsWith(prefix)) return;
const args = message.content.slice(prefix.length).trim().split(/ +/);
const cmdName = args.shift().toLowerCase();
const command = client.commands.message.get(cmdName);
if (command) {
try {
await command.run(client, message, args);
} catch (err) {
console.error(err);
message.reply("❌ Something went wrong while executing this command.");
}
}
}
});
4️⃣ Deploying Slash Commands
Create a deploy.js file to register slash commands.
// deploy.js
require("dotenv").config();
const { deployCommands } = require("discord-handler.js");
deployCommands(null, {
clientId: process.env.clientId,
guildId: process.env.guildId, //if you want to deploy globally, remove this line
token: process.env.token,
commandsPath: "./commands/slash",
category: true,
});⚙️ API Reference
🔹 createBot(client, options)
Initialize the bot by automatically loading commands and events.
Parameters
client (
Object)
The Discord.js Client instance.options (
Object, optional)
Configuration options for enabling/disabling features.| Option | Type | Default | Description | |----------------|----------|---------|-------------| |
slash| boolean |true| Load slash commands | |message| boolean |true| Load message commands (prefix-based) | |category| boolean |true| Organize commands by category | |events| boolean |true| Load events |
🔹 deployCommands(client, options)
Deploys slash commands to Discord, either globally or to a specific guild.
This function scans your commandsPath folder for command files and registers them via Discord’s REST API.
Parameters
client (
Object | null)
The Discord.js client instance.
Can benullsince deployment only requires REST, not a running client.options (
Object, required)
Configuration options for deploying commands.| Option | Type | Default | Required | Description | |-----------------|---------|--------------------------------------|----------|-------------| |
clientId| string | — | ✅ | Your bot’s Application (Client) ID | |token| string | — | ✅ | Your bot token | |guildId| string | — | ❌ | Guild ID where commands should be deployed (omit for global) | |commandsPath| string |./commands/slash| ❌ | Path to your slash commands directory | |category| boolean |true| ❌ | Organize commands by category subfolders |
📝 Example Bot
const { Client, GatewayIntentBits } = require("discord.js");
const { createBot } = require("discord-handler.js");
const client = new Client({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent],
});
// Initialize handler
createBot(client, {
slash: true,
message: true,
category: true,
events: true,
});
client.login("YOUR_BOT_TOKEN");📜 License
MIT License © 2025 Developed with ❤️ for Discord.js bots.
