npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

discord-handler.js

v2.0.0

Published

A simple Discord.js command & event handler with support for slash, message, and categories.

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 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


// 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 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

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.