kakashi-self-handler
Version:
package handler for discord bot
55 lines (47 loc) • 1.69 kB
JavaScript
const { Collection } = require("discord.js");
const { readdirSync, existsSync } = require("fs");
const { green, red } = require("chalk");
const path = require("path");
async function MessageCommand(kakashi) {
if (!existsSync(kakashi.MessageCommands.path)) {
throw new Error(
red(`{Message Command Handler} - Path provided doesn't exist.\n'${kakashi.Message.path}'`)
);
}
const folderPath = kakashi.MessageCommands.path
const pathArray = folderPath.split("/");
const commandFolders = readdirSync(folderPath);
kakashi.scommands = new Collection();
kakashi.aliases = new Collection();
for (const folder of commandFolders) {
const commandFiles = readdirSync(`${folderPath}/${folder}`);
for (const file of commandFiles) {
const command = require(path.join(
require.main.path,
pathArray[pathArray.length - 1],
folder,
file
));
if (!command || typeof command !== "object") {
throw new Error(
red(`{Message Command Handler} - Command is missing data.\n'${folderPath}/${folder}/${file}'`)
);
}
kakashi.scommands.set(command.name, command);
if (command.aliases) {
if (Array.isArray(command.aliases)) {
command.aliases.forEach((alias) => {
kakashi.aliases.set(alias, command);
});
} else {
kakashi.aliases.set(command.aliases, command);
}
console.log(command.aliases)
}
console.log(
green(`{Message Command Handler} - "${command.name}" command registered.`)
);
}
}
}
module.exports = MessageCommand;