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()
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 is a development branch is still in development of new features and there may be bugs. Check out the road map page to see what is added and tested before 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, {
mainDirectory: __dirname,
config: "./config.js",
BOT_TOKEN: "YOUR_BOT_TOKEN",
});
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
- node_modules
- commands
- sub-folder(s) (named category folders to categories your command files)
- command files
- sub-folder(s) (named category folders to categories your command files)
- events
- your discord.js event files
- modules
- module files
- config.js (optional: configuration file)
- index.js (your bot start file)
DiscordFeaturesHandler Properties
Thees 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 |
---|---|---|---|---|
mainDirectory | string | true | "" | The absolute path to the directory containing the executing main script file. Expected Value: __dirname |
config | string | false | "./config" | The path to your configuration file. Default value is path to default configuration file provided. |
BOT_TOKEN | string | true | "" | This is your bot token that you can find in your Discord Developer Portal. This is required to login to your discord bot. |
commandDir | string | false | "commands" | Folder name of your command folder that contains sub-folders which contains the command files. The sub-folders are the category names of the command inside those folders. Default folder name is: commands. |
eventDir | string | false | "events" | Folder name of your event folder containing discord event files. Default folder name is: events. |
modulesDir | string | false | "modules" | Folder name of your module folder that contains your module files. Default folder name is: modules. |
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 requires access to the API. 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 | bool | 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.');
},
};
Slash Command Properties
The properties that are required to have when creating a slash command file The properties of all command listed above and the following:
Property | Type | Default | Description |
---|---|---|---|
slash | bool | false | State if this command is a slash command |
slashOptions | JSON object | "" | OPTIONAL: Options properties of a slash command, documentation can be found here. Discord Developer Doc |
interactionReply(interaction, client, level) | func | "" | Functionality and response of the slash command call. Parameters are interaction and client Object , and user's permission level
|
Example Slash Command:
module.exports = {
name: 'ping',
description: 'Ping Pong Command!',
aliases: ['p'],
guildOnly: true,
permissions: 0,
minArgs: 0,
usage: '',
/**
* This is required and set as true. Otherwise would not recognize as a slash command
*/
slash: true,
/**
* @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.');
},
/**
* @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 here. |
once | bool | false | if the event should run once on 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 in is the client object. No properties are required to be defined.
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);
});
The following functions can be overwritten by re-defining
If you create a new client.<functionName>
you can override then existing function with the new function.
client.getPermissionsLevel ( parameter )
⚠️ Please do not override unless you are creating your own permission level configuration with a different approach then this handler uses!
This parameter is either an interaction object or message object, based off the command type and which type of command was called. This function returns a permission level based off the config.js
file.
client.loadCommand
❌ When overriding this will override the handler and commands will not load!
This function handles load the command
client.unloadCommand
❌ When overriding this will override the handler and commands may be able to unload to reload the new command file!
Unload the command, by clearing the cache so you can reload the command with a new command file.
client.commands and client.aliases
❌ DO NOT OVERRIDE: This saves all the properties of the command files so that we can load the commands for you
These are Discord.Collection object that contains the command and aliases information to handle loading commands and executing them based off their properties
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
discord-features-handler allows you to create the command and event files by using the pre-define properties with the respective command name or event name (event name associated with the event, such as ready, messageCreate, messageUpdate, or interactionCreate as listed on discord.js documentation. As a flexible handler, this help the developer focus on what required for their discord bot without worrying about how to connect to the Discord API using discord.js and focus on the main aspect of the bot which is the functionality and features for their bot.
This is my first npm package that I created due to having three bots that I have created for different purposes, but using the same formats to start-up the bot. Please bear with me as I improving the package for those interesting in supporting and using it!
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!