discord-handler.js
Version:
A simple Discord.js command & event handler with support for slash, message, and categories.
277 lines (203 loc) β’ 7.18 kB
Markdown
# 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
```sh
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 `Command` and `Event` classes
## π 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
```js
// 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
```js
// 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
```js
// 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
```js
// 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!`);
}
});
```
```js
// 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);
}
});
```
```js
// 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.
```js
// 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](https://discord.js.org/#/docs/discord.js/stable/class/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 be `null` since 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
```js
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.
```