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.
- 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
Here is a demo bot where I created a discord bot is created using DiscordFeaturesHandler in JavaScript and in TypeScript in the TypeScript Branch.
Installing DiscordFeaturesHandler
npm install discord-features-handler
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.
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
The official documentation can be found here: DiscordFeaturesHandler Documentation
You can read all the version and changes history here: ChangeLog
These are the properties of the DiscordFeaturesHandler:
Property | Type | Default | Description |
---|---|---|---|
client | Client | client | The DiscordJS BaseClient Object |
options | DiscordFeaturesHandlerOptions | { ... } | Options that contains parameters to configure DiscordFeaturesHandler |
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 | { ... } | Contains a parameter called main that is 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. |
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 by writing an example of how to execute the command using the command argument(s) in the command call |
execute(message, args, client, level) | func | "" | This is a function that is invoked when the command is called to be executed. Parameters are `message` object, `arguments` array, `client` object, and user's permission level to run a 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.');
},
};
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 | This is a function that is invoked when the slash command is called to be executed Parameters are `interaction`, `client` object, and `user's permission level`. |
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!'
});
}
};
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 | "" | This is the method used to execute and handle the Discord event trigger. Params are parameters of the event you are defining. |
module.exports = {
name: "ready",
once: true,
async execute(client) {
console.log('Bot just started!');
},
};
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
};
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"
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
⚠️ 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.
If you found and bug and issues please report the issue and provide steps to reproducible bugs/issues.
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.
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!