Command Handler working

This commit is contained in:
ahoZiorce
2018-07-01 22:15:42 +02:00
parent c6c9dbf1c5
commit fa000a6c37
5 changed files with 25 additions and 13 deletions

View File

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

3
src/botClient.js Normal file
View File

@@ -0,0 +1,3 @@
const Eris = require('eris');
const configM = require('./configManager');
exports.bot = new Eris(configM.config.token);

View File

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

View File

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

View File

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