From 500bd670445a992714144271bcb5e878486c0a5b Mon Sep 17 00:00:00 2001 From: plane000 Date: Sun, 20 May 2018 09:10:39 +0100 Subject: [PATCH] CommandManager functioning --- commandmanager.js | 34 +++++++++++++++++++++++++++++++++- commands/commands.js | 3 +-- config.js | 7 +++---- main.js | 38 ++++++++++++++++---------------------- 4 files changed, 53 insertions(+), 29 deletions(-) diff --git a/commandmanager.js b/commandmanager.js index 509e0f4..f9ddffb 100644 --- a/commandmanager.js +++ b/commandmanager.js @@ -1 +1,33 @@ -// TODO: commandmanager +const Logger = require('./logger'); +const Config = require('./config'); +const Commands = require('./commands/commands') +const CommandManager = require('./main'); +const http = require('http'); +const ping = require('ping'); +const fs = require('fs'); +const Discord = require('discord.js'); + +var commands = {}; + +module.exports.commands = commands; + +function addCommand(name, command, alt, usage, desc, requrePerms, functionReference) { + commands[name] = { + name: name, + command: command, + alt: alt, + usage: usage, + desc: desc, + requrePerms: requrePerms, + functionReference: functionReference + } +} + +/*command name, command, alt, usage, description, require permission, reference*/ +module.exports.loadCommands = function() { + //general commands + addCommand('say', 'say', undefined, 'say [input]', 'repeats the input', false, Commands.say); + addCommand('version', 'version', undefined, 'version', 'returns the version', false, Commands.version); + addCommand('ping', 'ping', undefined, 'ping', 'returns round trip to discords serves', false, Commands.ping); + //continue +} diff --git a/commands/commands.js b/commands/commands.js index 3e0d36f..287f85b 100644 --- a/commands/commands.js +++ b/commands/commands.js @@ -6,7 +6,7 @@ const Config = require('../config'); /*message object, messaage full, message args, discord client*/ module.exports.say = function(message, msg, args, discordclient) { - + message.channel.send(msg); } module.exports.version = function(message, msg, args, discordclient) { @@ -15,7 +15,6 @@ module.exports.version = function(message, msg, args, discordclient) { em.setTitle('Version:'); em.setDescription(Config.getconfig().Version); message.channel.send(em); - return; } module.exports.ping = function(message, msg, args, discordclient) { diff --git a/config.js b/config.js index 0345bf5..215083e 100644 --- a/config.js +++ b/config.js @@ -1,7 +1,7 @@ const fs = require("fs"); -var config = null; - +var config = {}; +var rules = {}; module.exports.getconfig = function() { return config; @@ -17,9 +17,8 @@ module.exports.loadDefaults = function() { NowPlaying: "RealLife.exe", Prefix: "+", Version: "2.0.1", - - Rules: ["Rule 0", "Rule 1"] }; + }; module.exports.loadFromFile = function() { diff --git a/main.js b/main.js index fbde9a3..78b8e57 100644 --- a/main.js +++ b/main.js @@ -1,5 +1,6 @@ const Logger = require('./logger'); const Config = require('./config'); +const Commands = require('./commands/commands') const CommandManager = require('./commandmanager'); const http = require('http'); const ping = require('ping'); @@ -26,11 +27,17 @@ if (!fs.existsSync('resources/config.json')) { Logger.log('Loading config...'); try { Config.loadFromFile(); - } catch (e) { Logger.failed(`Could not load the config: ${e.message}`); } +/*loads the commands*/ +try { + Logger.log("Loading commands..."); + CommandManager.loadCommands(); +} catch (e) { + Logger.log(`Could not load the commands: ${e.message}`); +} /*connects to discord*/ try { Logger.log("Starting discord client..."); @@ -43,37 +50,24 @@ client.on('ready', () => { client.user.setPresence('online'); client.user.setActivity(Config.getconfig().NowPlaying); Logger.log(`Logged in as ${client.user.tag}`); - Logger.log('Ready!') + Logger.log('Ready!'); console.log(); }); /*on message event*/ client.on('message', async (message) => { - /*if it starts with prefix*/ + /*if it starts with prefix loaded from config*/ if (message.content.startsWith(Config.getconfig().Prefix)) { Logger.logMSG(message); var msg = message.content.substring(Config.getconfig().Prefix.length); var args = msg.split(" "); - /*temp switch statement to manage commands*/ - switch (args[0].toUpperCase()) { - case 'PING': - ping.promise.probe("discordapp.com", { - timeout: 10 - }).then((output) => { - message.channel.send(`:white_check_mark: \`${output.avg}ms\``); - }); - return; - case 'VERSION': - var em = new Discord.RichEmbed(); - em.setColor('BLUE'); - em.setTitle('Version:'); - em.setDescription(Config.getconfig().Version); - message.channel.send(em); - return; - default: - message.channel.send(`:no_entry: \`The command '${args[0]}' does not exist...\``); - break; + /*command manager checks if command exists*/ + if (CommandManager.commands[args[0]]) { + /*sends command: message object, messaage full, message args, discord client*/ + CommandManager.commands[args[0]].functionReference(message, msg, args, client); + } else { + message.channel.send(`:no_entry_sign: \`The command \'${args[0]}\' does not exist\``); } } });