UNPKG

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
# 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. ```