0.6.2 ā€¢ Published 2 years ago

node-handlers v0.6.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Node Handlers

About

Node Handlers is a Node.js module that allows you to easily use Handlers in node.js!

At the moment there are only handlers for Discord Bots, more will follow.

Installation

Node.js 16.6.0 or newer is required.

npm install node-handlers

Example usage

Install all required dependencies:

npm install node-handlers

How to use the Discord Handlers:

Example Directory Structure

This is an example structure for the example below

The files named in the example structure are under the example structure
šŸ“¦ path/to/directory
 ā”£šŸ“‚ commands
 ā”ƒ ā”£šŸ“‚ help
 ā”ƒ ā”ƒ ā”—šŸ“œ ping.js
 ā”ƒ ā”—šŸ“œ pong.js
 ā”£šŸ“‚ events
 ā”ƒ ā”£šŸ“‚ interactions
 ā”ƒ ā”ƒ ā”—šŸ“œ interactionCreate.js
 ā”ƒ ā”—šŸ“œ ready.js
 ā”—šŸ“œ index.js

Commands can either be placed directly in a folder (e.g. commands) or in a subfolder (e.g. commands/help) in the folder. It's the same for the events: They can either be placed directly in a folder (e.g. events) or in a subfolder (e.g. events/interactions) in the folder.

Command Template

// Command Template 'pong.js'
const Discord = require('discord.js');
module.exports = {
    name: "pong",
    description: "Replies with Pong",
    type: 1, // 1 = Command, 2 = User, 3 = Message
    async execute(interaction){
            
            const Pinging = new Discord.MessageEmbed()
            Pinging.setTitle("> __**PONG!**__")

            interaction.reply({embeds: [ Pinging ]});
    }
}

Event Template

// Event Template (Runs when the bot is logged in, 'ready.js')
module.exports = {
	name: 'ready',
	once: true,
	execute(client) {
		console.log(`Ready! Logged in as ${client.user.tag}`);
	},
};
// Event Template (Is needed to interact with slash commands, 'InteractionCreate.js')
module.exports = {
	name: 'interactionCreate',
	once: false,
	async execute(interaction, client) {
		if (interaction.isCommand()) {
            
            const command = client.slashcmds.find(cmd => {
                return cmd.name === interaction.commandName
              })
    
            if (!command) return;
    
            try {
                await command.execute(interaction);
            } catch (error) {
                console.error(error);
                await interaction.reply({ content: 'There was an error while executing this command!', ephemeral: true });
            }
        }
	},
};

Afterwards we can create a quite simple example bot with the Event and Command Handler:

// This is the 'index.js' from the example structure
const { Discord, Client, Intents} = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MEMBERS]});
const handlers = require('node-handlers');

handlers.dc.events(__dirname + 'PATH TO EVENT FOLDER', client) // e.g. './events'
handlers.dc.commands(__dirname + 'PATH TO COMMAND FOLDER', client) // e.g. './commands'

client.login('YOUR_TOKEN') // Copy from your Application on https://discord.com/developers/applications/<ID>/bot

Links

Contributing

Before creating an issue, please ensure that it hasn't already been reported, and check the documentation.

0.6.2

2 years ago

0.6.1

2 years ago

0.5.4

2 years ago

0.5.5

2 years ago

0.6.0

2 years ago

0.5.3

2 years ago

0.5.2

3 years ago

0.5.1

3 years ago

0.5.0

3 years ago

0.4.4

3 years ago

0.4.3

3 years ago

0.4.2

3 years ago

0.4.1

3 years ago

0.3.2

3 years ago

0.3.1

3 years ago

0.2.2

3 years ago

0.2.1

3 years ago

0.1.9

3 years ago

0.1.8

3 years ago

0.1.7

3 years ago

0.1.6

3 years ago

0.1.5

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago

0.0.1

3 years ago