From fa000a6c371cc01d828734ca51d5f95ae3bb1f1c Mon Sep 17 00:00:00 2001 From: ahoZiorce Date: Sun, 1 Jul 2018 22:15:42 +0200 Subject: [PATCH] Command Handler working --- index.js | 18 ++++++++++-------- src/botClient.js | 3 +++ src/cmd/debug.js | 13 ++++++++++--- src/commandHandler.js | 2 +- src/commandLoader.js | 2 +- 5 files changed, 25 insertions(+), 13 deletions(-) create mode 100644 src/botClient.js diff --git a/index.js b/index.js index 75f5970..953db89 100644 --- a/index.js +++ b/index.js @@ -1,8 +1,6 @@ -const Eris = require('eris'); const configM = require('./src/configManager'); const logger = require('./src/logger'); -const commandL = require('./src/commandLoader'); - +const commandH = require('./src/commandHandler'); // TODO: botClient.js if (!configM.loadConfig('./config.json')) { @@ -10,10 +8,11 @@ if (!configM.loadConfig('./config.json')) { } logger.log('Config loaded'); -commandL.load(); +const bot = require('./src/botClient').bot; + +require('./src/commandLoader').load(); logger.log('Commands loaded'); -var bot = new Eris(configM.config.token); bot.on('ready', () => { logger.log(bot.user.username + '#' + bot.user.discriminator); bot.editStatus('online', { @@ -24,9 +23,12 @@ bot.on('ready', () => { }); bot.on("messageCreate", (msg) => { - if(msg.content === "!ping") { - bot.createMessage(msg.channel.id, "Pong!"); - } + let content = msg.content.replace(new RegExp(`^(?:<@${bot.user.id}> +|\\*)\\b`), ''); + if (content === msg.content) return; + if (msg.author.bot) return; + if (msg.author === bot.user) return; + let trimmedContent = content.trim(); + commandH.apply(trimmedContent, msg); }); bot.connect(); diff --git a/src/botClient.js b/src/botClient.js new file mode 100644 index 0000000..00b6a67 --- /dev/null +++ b/src/botClient.js @@ -0,0 +1,3 @@ +const Eris = require('eris'); +const configM = require('./configManager'); +exports.bot = new Eris(configM.config.token); \ No newline at end of file diff --git a/src/cmd/debug.js b/src/cmd/debug.js index f029c94..1e6e11f 100644 --- a/src/cmd/debug.js +++ b/src/cmd/debug.js @@ -1,4 +1,11 @@ -exports.loadModule = function loadModule (commandH) { - console.log('In debug.js'); - console.log(commandH); +const commandH = require('../commandHandler'); +const bot = require('../botClient').bot; + +exports.loadModule = function loadModule () { + commandH.endpoint('^ping test$', (match, message) => { + bot.createMessage(message.channel.id, 'hey ho'); + }); + commandH.endpoint('^ping test (.*)$', (match, message) => { + bot.createMessage(message.channel.id, 'hey ho ' + match[1]); + }); } \ No newline at end of file diff --git a/src/commandHandler.js b/src/commandHandler.js index 558221e..6089a57 100644 --- a/src/commandHandler.js +++ b/src/commandHandler.js @@ -7,7 +7,7 @@ exports.endpoint = function endpoint (match, handler) { exports.apply = function apply (command, context) { let keys = Object.keys(commands); for (let i = 0; i < keys.length; i++) { - let match = command.match(keys[i]); + let match = command.match(new RegExp(keys[i])); if (match) { commands[keys[i]](match, context); return true; diff --git a/src/commandLoader.js b/src/commandLoader.js index d12bc3e..c0447e9 100644 --- a/src/commandLoader.js +++ b/src/commandLoader.js @@ -7,7 +7,7 @@ module.exports.load = function load() { if (file.endsWith('.js')) { let plugin = require(`./cmd/${file}`); if (plugin.loadModule) { - plugin.loadModule(commandH); + plugin.loadModule(); logger.log(`${file} loaded`); } }