aboutsummaryrefslogtreecommitdiff
path: root/bot.js
diff options
context:
space:
mode:
authorUnknown <jtsshieh@outlook.com>2018-02-24 14:59:35 -0500
committerUnknown <jtsshieh@outlook.com>2018-02-24 14:59:35 -0500
commitc484753ee15dbe2a5ce9c6297f22fabb52ce3b7e (patch)
tree93fd74f30abbd3c8206321b9d9c20b2571ed84ca /bot.js
parent3d36bcf0591f2694c3a2472a157ecca0de8d35fc (diff)
downloadPokeBot-c484753ee15dbe2a5ce9c6297f22fabb52ce3b7e.tar.gz
PokeBot-c484753ee15dbe2a5ce9c6297f22fabb52ce3b7e.tar.bz2
PokeBot-c484753ee15dbe2a5ce9c6297f22fabb52ce3b7e.zip
New Command system
Diffstat (limited to 'bot.js')
-rw-r--r--bot.js61
1 files changed, 42 insertions, 19 deletions
diff --git a/bot.js b/bot.js
index 9f5e6de..1d64af0 100644
--- a/bot.js
+++ b/bot.js
@@ -5,27 +5,50 @@ const fs = require('fs');
bot.commands = new Discord.Collection();
bot.aliases = new Discord.Collection();
+bot.categories = new Discord.Collection();
-fs.readdir('./commands', (err, files) => {
- if (err) console.error(err);
- console.log(`Attempting to load a total of ${files.length} commands into the memory.`);
- files.forEach(file => {
- try {
- const command = require(`./commands/${file}`);
- console.log(`Attempting to load the command "${command.help.name}".`);
- bot.commands.set(command.help.name, command);
- command.conf.aliases.forEach(alias => {
- bot.aliases.set(alias, command.help.name);
- console.log(`Attempting to load "${alias}" as an alias for "${command.help.name}"`);
- });
- }
- catch (err) {
- console.log('An error has occured trying to load a command. Here is the error.');
- console.log(err.stack);
- }
+cmdLoader();
+
+async function cmdLoader() {
+ const categories = await fs.readdirSync('./commands');
+ console.log(`Loading ${categories.length} categories(s) into memory\n`);
+ categories.forEach(x => {
+ loadGroup(x);
});
- console.log('Command Loading complete!');
-});
+}
+async function loadGroup(name) {
+ const files = await fs.readdirSync(`./commands/${name}`);
+
+ console.log(`Loading the category '${name}' into memory with a total of ${files.length} command(s)`);
+
+ bot.commands.set(name, new Map());
+ bot.aliases.set(name, new Map());
+
+ const commands = [];
+ files.forEach(x => {
+ loadCmd(name, x);
+ commands.push(x.split('.')[0]);
+ });
+
+ bot.categories.set(name, commands);
+ console.log(`The category ${name} has been loaded.\n`);
+}
+
+async function loadCmd(category, cmd) {
+ try {
+ console.log(`Loading the Command ${cmd.split('.')[0]}`);
+ const command = require(`./commands/${category}/${cmd}`);
+ bot.commands.get(category).set(command.help.name, command);
+ command.conf.aliases.forEach(alias => {
+ bot.aliases.get(category).set(alias, command.help.name);
+ });
+ }
+ catch (err) {
+ console.log(`An error has occured trying to load the command '${cmd.split('.')[0]}'`);
+ console.log(err.stack);
+ }
+}
+
fs.readdir('./events', (err, files) => {
if (err) console.error(err);