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 🙏

© 2024 – Pkg Stats / Ryan Hefner

discord-features-handler

v2.1.2

Published

An simple discord regular and slash commands, events and modules handler with folder structure

Downloads

18

Readme

discord-features-handler

Discord-features-handler is a handler for discord commands, slash commands and discord events that allows you to easily start creating command and events files to interact with discord.js and connect to discord api to easily create your own discord.js bot without the worrying of how to setup and run the commands and events files.

Features

  • Command Handler
  • Slash Command Handler
  • Events Handler
  • Modules Handler
  • Pre-Made Reload Command
  • Pre-made Help Command
  • Unhandled Rejection Handler
  • String.prototype.toProperCase()
  • Array.prototype.Random()
  • Supports TypeScript Natively

Demo

Here is a demo bot where I created a discord bot is created using DiscordFeaturesHandler in JavaScript and in TypeScript in the TypeScript Branch.

Demo Bot

Installation

Installing DiscordFeaturesHandler

  npm install discord-features-handler

Usage

Here is a basic example of how to setup discord-features-handler. A simple example with only the essentials to get a bot up and running:

const { Client, Intents } = require("discord.js");
const { DiscordFeaturesHandler } = require("discord-features-handler");
const client = new Client({
  intents: [...],
  partials: [...],
});

DiscordFeaturesHandler(client,  {
  config: "./config.js",
  directories: {
    main: __dirname,
  },
}); 

The intents are gateway intents are what discord gives for bot developers access to events based on what data it need for their function. You can find the list of intents here. You can read more about intents in the discordjs.guide docs.You should enable all partials for your use cases, as missing one then the event does not get emitted. You can read more about partials in the discordjs.guide docs.

Folder Structure
discord-bot/
├── commands/
│   ├── miscellaneous/   //this can be any name you want
│   │   └── ping.js
├── events/
│   └── ready.js
├── modules/
├── node_modules/
├── .env
├── config.js
├── index.js
├── package-lock.json
└── package.json

Documentation

The official documentation can be found here: DiscordFeaturesHandler Documentation

You can read all the version and changes history here: ChangeLog

DiscordFeaturesHandler Properties

These are the properties of the DiscordFeaturesHandler:

DiscordFeaturesHandlerOptions

Here are the some parameters of options Object. For a full list please check out the documentation.

Commands Properties

The properties that are required to have when creating a command file

Example Command:
module.exports = {
	name: 'ping',
	description: 'Ping Pong Command!',
	aliases: ['p'],
	guildOnly: true,
	permissions: 0,
	minArgs: 0, 
	usage: '',
    /** 
    * @param {message} message The discord message object
    * @param {Array<string>} args The arguments following the command call
    * @param {Client} client The discord client object
    * @param {number} level The permission level of the user who made the command call
    */
	execute(message, args, client, level) { 
		return message.channel.send('Pong.');
	},
};

Properties for Slash commands

The properties that are required when creating a command file for slash commands are listed above, and they include the following additional properties.

Example Slash Command:
const { SlashCommandBuilder } = require("discord.js");
const name = "ping";
const description = "Ping Pong Command";

module.exports = {
	name,
	description,
	aliases: ['p'],
	guildOnly: true,
	permissions: 0,
	minArgs: 0, 
	usage: '',
	/**
	* This is required and set as true. Otherwise would not recognize as a slash command
	*/
	data: new SlashBuilderCommand().setName(name)
	    .setDescription(description),
    /** 
    * @param {message} message The discord message object
    * @param {Array<string>} args The arguments following the command call
    * @param {Client} client The discord client object
    * @param {number} level The permission level of the user who made the command call
    */
	execute(message, args, client, level) { 
      return message.channel.send({ content: 'Pong.'});
    },
    /** 
    * @param {interaction} interaction The discord interaction object
    * @param {Client} client The discord client object
    * @param {number} level The permission level of the user who made the command call
    */
    async interactionReply(interaction, client, level) {
      await interaction.reply({
	      content: 'Pong!'
      });
    }
};

Discord Event File

When creating a discord event file in your events folder, will required the following properties:

Example Ready Event
module.exports = {
  name: "ready",
  once: true,
  async execute(client) {
      console.log('Bot just started!');
  },
};

Modules Files

You can create a plain module.exports file in your modules folder. The only parameter being passed is an optional parameter, the client object. If using TypeScript make sure this is an default exports instead.

  module.exports = (client) => {
    // do something
  }; 

Built-in functions

String.prototype.toProperCase()

This add a new function to a String constructor object where you can make all the first letter of a word in that object, capitalize.

const str = "A quick brown fox jumps over the lazy dog";

console.log(str.toProperCase());
//expected output: "A Quick Brown Fox Jumps Over The Lazy Dog"

Array.prototype.random()

This add a new function to a Array constructor object where in returns a random element in the array.

const arr = ['a', 'b', 'c', 'd', 'e'];

console.log(arr.random());
//expected output is either: a, b, c, d, or e

unhandledRejection

:warning: Catches unhandled promise rejections

This handles and console.log any unhandled errors. Which are methods that are missing .catch(e) that causes to crashes the bot. This function prevent the crash and handles it by console logging it.

process.on("unhandledRejection", (e) => {
  console.log(e);
});

You can read more about these and other built-in functions in the official Documentation.

Bug and Issues

If you found and bug and issues please report the issue and provide steps to reproducible bugs/issues.

Notes

This handler also allows you to follow the Discord.js guide with a few changes, such as we are using a JavaScript file instead of a JSON file for the config file, using the property interactionReply instead of execute property for slash commands, and without creating your own handler for loading commands and events file. Also loading your modules files that contains features of your bot.

Support and New Features

This package is looking for feedback and ideas to help cover more use cases. If you have any ideas feel free to share them or even contribute to this package! Please first discuss the add-on or change you wish to make, in the repository. If you like this package, and want to see more add-on, please don't forget to give a star to the repository and provide some feedbacks!

License

MIT