ezcmds

Easy command and event handler for discord.js without classes. If you would like to use the version with classes visit: https://www.npmjs.com/package/easycommands

Usage no npm install needed!

<script type="module">
  import ezcmds from 'https://cdn.skypack.dev/ezcmds';
</script>

README

EZCMDS

Easy command and event handler for discord.js without classes.

If you would like to use the version with classes visit: https://www.npmjs.com/package/easycommands

Set up - main file (not with event handler)


const Discord = require("discord.js");
const client = new Discord.Client();

const { Handler } = require("ezcmds");
const handler = new Handler();

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("bot token goes here");

Example Command inside ./commands/test.js


exports.run = async (client, message, args) => {

    // code goes here.


    message.channel.send("Test done!");


}

exports.help = {
    name: "test", // required
    aliases: ['test1', 'test2'], // optional. 
    cooldown: 60000 // optional. in milliseconds
}


Example using event handler. Main file:


const Discord = require("discord.js");
const client = new Discord.Client();

const { Handler } = require("ezcmds");
const handler = new Handler();

handler.register({
    client: client,
    cmdDirectory: __dirname + "/commands",
    evtDirectory: __dirname + "/events"
});


client.login("bot token goes here.");


Inside ./events/message.js



exports.run = async (client, message) => {

    if(!message.guild) return; 
    const prefix = "!"; 
    if(!message.content.startsWith(prefix)) return; 
 
    const messageArray = message.content.split(/ +/g); 
    const cmd = messageArray[0].toLowerCase().slice(prefix.length); 
    const args = messageArray.slice(1); 
 
    client.handler.run(cmd, client, message, args);



}


The command example will remain the same as above.

Bonus: Using the handler and being able to pass additional variables.

Inside ./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
     
       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}h ${cool.minutes}m ${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
     
}


Then inside ./commands/test.js



exports.run = async (client, message, args, extra1, extra2, extra3, extra4) => {

    message.channel.send(`${extra1} ${extra2} ${extra3} ${extra4}`);

    // expected output: "extra variables now work"


}


exports.help = {
    name: "test"
}