diff --git a/bot.js b/bot.js index 78152e3..da32fda 100644 --- a/bot.js +++ b/bot.js @@ -28,7 +28,7 @@ const startBot = async () => { await require('./src/handle/startup/run/login')(client); //log in to discord await require('./src/db/connect')(client); //connect to database - await require('./src/handle/startup/run/collect')(client); + await require('./src/handle/startup/run/collect')(client); //load in commands and events }; startBot().catch(e => errorhandler(e)); // TODO add a .catch() and flag to recover the process // feels like there isn't a function name to do this justice :joy: diff --git a/src/handle/startup/collect/commands.js b/src/handle/startup/collect/commands.js index 1231a23..d7b2ee2 100644 --- a/src/handle/startup/collect/commands.js +++ b/src/handle/startup/collect/commands.js @@ -1,5 +1,52 @@ const fs = require('fs'); +const chalk = require('chalk'); +const {Collection} = require('discord.js'); + +const commandsDirName = './src/handle/runtime/commands'; module.exports = async client => { + client.aliases = new Collection(); + let dirErrors = []; let fileErrors = []; //collect error objects to output them all at the end + let readDirs = []; //list of dirs to print at the end + let conflictingAliases = {}; + client.log('Loading commands...', {source: 'boot', color: 'blue'}, 0, 1); + const readDir = dir => { //when you're trying to comment your code but realize you have good variable naming for once and don't need to :D + let dirRead; + try {dirRead = fs.readdirSync(dir);} + catch (e) { + client.error(`Failed to read directory ${chalk.white(dir)}`); + return dirErrors.push([`Unable to read directory ${chalk.white(dir)}. Error:`, e]); + } + let files = dirRead.filter(item => item.endsWith('.js')); + let folders = dirRead.filter(item => fs.lstatSync(`${dir}/${item}`).isDirectory()); + files.forEach(file => { + try { + const command = require(`../../../../${dir.slice(2)}/${file}`); + client.commands.set(command.name, command); + if (command.aliases) {command.aliases.forEach(alias => { + if (client.aliases.has(alias)) { + if (conflictingAliases[alias]) {conflictingAliases[alias].push(command.name);} //object of alias conflictions keyed by alias -> by array[command names] + else {conflictingAliases[alias] = [command.name];} + } + client.aliases.set(alias, command.name); + });} + client.log(`Loaded ${chalk.white(command.name)} with ${chalk.white(`${command.aliases ? command.aliases.length : 0}`)} alias${command.aliases && command.aliases.length === 1 ? '' : 'es'}.`, {color: 'blueBright', source: 'boot', sourceColor: 'blue'}); + } + catch (e) { + client.error(`Failed to read file ${chalk.white(file)}`); + return fileErrors.push([`Unable to read file ${chalk.white(file)}. Error:`, e]); + } + }); + readDirs.push(`${dir.split('/').slice(4).join('/')}/`); // "commands/..." + return folders.forEach(folder => readDir(`${dir}/${folder}`)); //recurse infinitely + }; + readDir(commandsDirName); + console.log(""); + dirErrors.forEach(error => client.error(error[0], 0, 0, 1, error[1])); + fileErrors.forEach(error => client.error(error[0], 0, 0, 1, error[1])); + readDirs.forEach(dir => client.log(`Read from directory ${chalk.green(dir)}`, {source: 'proc'})); + console.log(''); + Object.keys(conflictingAliases).forEach(alias => client.warn(`Alias ${chalk.white(alias)} appears on ${client.utils.as(conflictingAliases[alias].length, 'command')} ${chalk.white(conflictingAliases[alias].join(chalk.yellow(', ')))}`)); + client.log(`Loaded ${chalk.white(client.commands.size)} command${client.utils.s(client.commands.size)}!`, {color: 'blue', source: 'boot'}, 1, 1); }; \ No newline at end of file