From d83951a8f078976091e4b47e639bb1f653e99a59 Mon Sep 17 00:00:00 2001 From: Ben Date: Sat, 24 Nov 2018 19:21:18 +0000 Subject: [PATCH] Command Manager working --- src/CMDManagerSpec.md | 3 --- src/commandmanager.js | 39 ++++++++++++++++++++++++++++++--------- src/commands/common.js | 12 +++++++----- src/index.js | 5 ++++- 4 files changed, 41 insertions(+), 18 deletions(-) delete mode 100644 src/CMDManagerSpec.md diff --git a/src/CMDManagerSpec.md b/src/CMDManagerSpec.md deleted file mode 100644 index 8b7f994..0000000 --- a/src/CMDManagerSpec.md +++ /dev/null @@ -1,3 +0,0 @@ -# Specification of command manager and modules - -- Ability to diff --git a/src/commandmanager.js b/src/commandmanager.js index fc74b4f..d479b35 100644 --- a/src/commandmanager.js +++ b/src/commandmanager.js @@ -3,11 +3,15 @@ import fs from 'fs'; import {Logger} from './logger'; let modules = []; -let commands = {}; +let commands = []; export class CommandManager { - static async load() { - console.log(); + static get Modules() {return modules;} + static get Commands() {return commands;} + + static async load(log) { + if (log != 1) console.log(); + Logger.info('Loading Commands and Modules'); if (!fs.existsSync('./src/commands/')) { Logger.panic('No commands folder at /src/commands'); @@ -32,7 +36,7 @@ export class CommandManager { continue; } - modules.push({name: mod.Module.Name, module: mod, exports: modExports}); + modules[mod.Module.Name] = {name: mod.Module.Name, module: mod, exports: modExports, file: file}; Logger.info(`Loaded ${mod.Module.Name} from ${file}`); for (let command of modExports) { @@ -43,12 +47,16 @@ export class CommandManager { || !current.Alias || !current.Usage || !current.Description - || !current.Module) { + || !current.Module + || !current.Init + || !current.Exec + || !current.Dispose) { Logger.warn(`Exported class ${command} in ${file} and module ${mod.Module.Name} is not a valid command`); continue; } - - + + await current.Init(); + commands[current.Command] = current; // Doesnt give support for alias commands Logger.info(`Loaded ${current.Command} from module ${mod.Module.Name}`); commandCount++; @@ -59,10 +67,23 @@ export class CommandManager { console.log(); } - static get Modules() {return modules;} - static get Commands() {return commands;} static async reload() { + console.log(); + Logger.info('Reloading Modules and Commands'); + + // Does not call dispose function + for (let command of commands) { + await command.Dispose(); + } + Logger.info('Disposed registerd commands'); + for (let mod of modules) { + await mod.module.Dispose(); + delete require.cache[require.resolve(mod.file)]; + } + Logger.info('Disposed registerd modules'); + + CommandManager.load(1); } } diff --git a/src/commands/common.js b/src/commands/common.js index dcb9143..5c458c8 100644 --- a/src/commands/common.js +++ b/src/commands/common.js @@ -1,3 +1,5 @@ +import {Logger} from "../logger"; + export class Module { static get Name() {return 'Generic module 1'} static get Author() {return 'Ben (plane000)#8618'} @@ -7,7 +9,7 @@ export class Module { } static Dispose() { - + Logger.error('module disposed') } } @@ -18,16 +20,16 @@ export class Command1 { static get Description() {return 'Echos the users input'} static get Module() {return Module;} - static Init() { + static async Init() { } - static Exec(message, client, next) { + static async Exec(message, client, next) { } - static Dispose() { - + static async Dispose() { + Logger.error('command disposed') } } diff --git a/src/index.js b/src/index.js index 3b3d383..912ec03 100644 --- a/src/index.js +++ b/src/index.js @@ -5,6 +5,7 @@ import {Config} from './config'; import {Database} from './database/database'; import {Events} from './events'; import {MessageManager} from './messagemanager'; +import {CommandManager} from './commandmanager'; let client; @@ -16,7 +17,8 @@ export async function init() { Config.load(); await Database.init(); - + + console.log(); await MessageManager.init(); client = new Discord.Client(); @@ -25,4 +27,5 @@ export async function init() { await eventHandler.init(client); eventHandler.handleEvents(); + CommandManager.reload(); }