easycommands

1.1.1 • Public • Published

EASYCOMMANDS

Easy to use command handler with discord.js. Note this command handler is for beginners.

Set up - main file

 
const Discord = require('discord.js');
const client = new Discord.Client();
 
const { EasyCommands } = require("easycommands");
const handler = new EasyCommands();
 
handler.register({
    client: client,
    cmdDirectory: __dirname + "/commands"
});
 
client.on("message", async message => {
 
    if(!message.guild) return; // return if the message was sent in DMs
    const prefix = "!"; // define prefix
    if(!message.content.startsWith(prefix)) return; // if the message content doesn't start with our prefix return.
 
    const messageArray = message.content.split(/ +/g); // split the message content at every space
    const cmd = messageArray[0].toLowerCase().slice(prefix.length); // get the command that was run
    const args = messageArray.slice(1); // define your args
 
    handler.run(cmd, client, message, args); // will run command if it is found. Will also catch errors.
 
 
    // client.handler.run(cmd, client, message, args); if your event is in a seperate folder you may still access the handler through client.handler
 
});
 
client.login("your bot token goes here.");
 

Example command inside ./commands/help.js

 
const { Command } = require("easycommands");
 
class Help extends Command {
    constructor(){
        super({
            name: "help", // required
            aliases: ['help1', 'help2'], // optional
            usage: "help", // optional
            description: "Show all commands.", // optional
            category: "General", // optional. Defaults to 'General'
            cooldown: 60000 // optional. Defaults to 0. Time is in milliseconds.
        });
    }
 
    async run(client, message, args) {
        message.reply("Hello!");
    }
 
}
 
module.exports = Help;
 

Using Event Handler - main file

 
const Discord = require('discord.js');
const client = new Discord.Client();
 
const { EasyCommands } = require("easycommands");
const handler = new EasyCommands();
 
handler.register({
    client: client,
    cmdDirectory: __dirname + "/commands",
    evtDirectory: __dirname + "/events"
});
 
 
client.login("your bot token goes here.");
 

Using Event Handler - message event in ./events/message.js

 
exports.run = async (client, message) => {
 
    if(!message.guild) return; // return if the message was sent in DMs
    const prefix = "!"; // define prefix
    if(!message.content.startsWith(prefix)) return; // if the message content doesn't start with our prefix return.
 
    const messageArray = message.content.split(/ +/g); // split the message content at every space
    const cmd = messageArray[0].toLowerCase().slice(prefix.length); // get the command that was run
    const args = messageArray.slice(1); // define your args
 
    client.handler.run(cmd, client, message, args); // will run command if it is found. Will also catch errors.
 
}
 
 

Using handler and being able to pass variables you want to the command.

In message event file with cooldowns:

 
exports.run = async (client, message) => {
 
        if(!message.guild) return; // return if the message was sent in DMs
        const prefix = "!"; // define prefix
        if(!message.content.startsWith(prefix)) return; // if the message content doesn't start with our prefix return.
     
        const messageArray = message.content.split(/ +/g); // split the message content at every space
        const cmd = messageArray[0].toLowerCase().slice(prefix.length); // get the command that was run
        const args = messageArray.slice(1); // define your args
     
       const cool = client.handler.checkCooldown(message.author.id, message.guild.id, cmd); // want to remove cooldowns? Remove this line and the following:
 
       if(cool) return message.channel.send(`You must wait ${cool.hours}${cool.minutes}${cool.seconds}s before using this command again.`); // remove this line too
 
       const command = client.handler.get(cmd);
       if(!command) return;
 
       command.run(client, message, args, "extra", "variables", "now", "work");
 
       client.handler.setCooldown(message.author.id, message.guild.id, cmd); // remove this line too
     
}
 
 

Inside ./commands/help.js for testing:

 
const { Command } = require("easycommands");
 
class Help extends Command {
    constructor(){
        super({
            name: "help", // required
            aliases: ['help1', 'help2'], // optional
            usage: "help", // optional
            description: "Show all commands.", // optional
            category: "General", // optional. Defaults to 'General'
            cooldown: 5000 // optional. Defaults to 0. Time is in milliseconds.
        });
    }
 
    async run(client, message, args, extra1, extra2, extra3, extra4) {
        message.reply(`${extra1}${extra2}${extra3}${extra4}`);
        // expected output: @author, extra, variables, now, work
    }
 
}
 
module.exports = Help;
 
 

Readme

Keywords

none

Package Sidebar

Install

npm i easycommands

Weekly Downloads

0

Version

1.1.1

License

ISC

Unpacked Size

10.8 kB

Total Files

5

Last publish

Collaborators

  • exploitz_