discord-features-handler
v2.1.2
Published
An simple discord regular and slash commands, events and modules handler with folder structure
Downloads
18
Maintainers
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.
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!