making a command handler
Command Handler Features
Actual Part
Step #1 - importing required packages
const Enmap = require("enmap");//npm i enmap
const fs = require("fs");//npm i fs
const Discord = require("discord.js")
const client = new Discord.Client();Step #2 - Put codes together for index.js
fs.readdir("./events/", (err, files) => {
if (err) return console.error(err);
files.forEach(file => {
const event = require(`./events/${file}`);
let eventName = file.split(".")[0];
client.on(eventName, event.bind(null, client));
});
});
client.commands = new Enmap();
fs.readdir("./commands/", (err, files) => {
if (err) return console.error(err);
files.forEach(file => {
if (!file.endsWith(".js")) return;
let props = require(`./commands/${file}`);
let commandName = file.split(".")[0];
console.log(`Attempting to load command ${commandName}`);
client.commands.set(commandName, props);
});
});Step #3 - creating nessecary folders
Step #4 - add event
Step #5 - adding commands
Last updated