discord-features-handler
TypeScript icon, indicating that this package has built-in type declarations

2.0.2 • Public • Published

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.

npm version npm downloads

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 github repository of mine where a discord bot is created using DiscordFeaturesHandler.

Discord Bot using DiscordFeaturesHandler

Installation

Installing DiscordFeaturesHandler

  npm install discord-features-handler

Development Build:

⚠️ This following dev build is for developers who want to experiment with new features and may encounter bugs. Refer to the roadmap page to track the additions and tests planned before the next build and release.

  npm install github:bng94/discord-features-handler#dev

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

DiscordFeaturesHandler Properties

These are the properties of the DiscordFeaturesHandler:

Property Type Default Description
client Client "" Discord Client Object
options Object {} Object that contains parameters to configure DiscordFeaturesHandler

options

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

Parameter Type Required Default Description
directories object true { main: __dirname, commands: "commands", events: "events", modules: "modules" } Contains the absolute path to the directory containing the executing main script file, and your folder names for commands, events, and modules folders. Expected Value: { main: __dirname }
config string false "./config" The path to your configuration file. Default value is path to default configuration file provided.
modulesPreloadTime number false 5000 Establish a waiting time to connect to the Discord API and load the data required for the module files. The time value is in milliseconds. You can set the time based off how many files in the module folder require access to a API call such a call to a database. Default time in milliseconds is 5000.

Commands Properties

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

Property Type Default Description
name string "" Name of your command
description string "" Description of your command
aliases Array [""] Aliases of the command. You must set `[]`
guildOnly boolean false If command is guild only (not a DM command)
permission number "" Permission level required to use command
minArgs number "" Minimum number of arguments required for command execution
maxArgs number "" Maximum number of arguments required for command execution
usage string "" Show how to use the command arguments in the command call
execute(message, args, client, level) func "" Functionality and response of the command call. Parameters are `message` object, `arguments` array, `client` object, and user's permission level to run a command
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.');
	},
};

Additional Required Command 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.

Property Type Description
data SlashCommandBuilder() DiscordJS SlashCommandBuilder
interactionReply(interaction, client, level) func Functionality and response of the slash command call. Parameters are `interaction`, `client` object, and `user's permission level`.
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:

Property Type Default Description
name string "" Discord Event Name. List of names can be found listed as enums and here with their params listed.
once boolean false If the event should run once on the first trigger or on every event trigger.
execute(client, ...params) func "" Functionality and response of the discord event trigger. Params are parameters of the event you are defining.
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

⚠️ 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.

Documentation

The official documentation can be found here: DiscordFeaturesHandler Documentation

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

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

Dependencies (1)

Dev Dependencies (2)

Package Sidebar

Install

npm i discord-features-handler

Weekly Downloads

183

Version

2.0.2

License

MIT

Unpacked Size

73.9 kB

Total Files

17

Last publish

Collaborators

  • bng94