Command Manager working

This commit is contained in:
Ben
2018-11-24 19:21:18 +00:00
parent 700b114b4e
commit d83951a8f0
4 changed files with 41 additions and 18 deletions

View File

@@ -1,3 +0,0 @@
# Specification of command manager and modules
- Ability to

View File

@@ -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);
}
}

View File

@@ -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')
}
}

View File

@@ -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();
}