First I should point out that I'm new at both JS and node.js. I'm trying to write a node.js chat bot, currently working at commands.
My problem is with calling a method of a method .. if that's even possible. This is the relevant part :
var text; // received message from user
var userId; // Id of the user, number.
if (cmd.hasOwnProperty(text.split(' ')[0])) { // Message will be a string, it should be ignore if the first word is not a command name.
console.log("[Inc]" + userId + ": " + text)
if (text.split(' ').length === 1) { // If the command name is not followed by any arguments then the only argument passed should be the userID(used to send replies to the user).
cmd[text.split(' ')[0]](userId)
} else {
var args = [] // Placing all args in an array because the commands will take different number of arguments, a calculator command for example could take a lot of args.
for (var i = 1; i < text.split(' ').length; i++) {
args.push(text.split(' ')[i])
}
console.log(args)
cmd[text.split(' ')[0]](userId, args)
}
} else {
console.log("Command not found, try again");
}
var cmd = {
help : function () {
cookies : function () { //
console.log('Cookies recipe')
}
if (arguments.length === 1) {
console.log('General help text');
} else if (help.hasOwnProperty(arguments[1])) {
this[arguments[0]](); // Seems my problem was here, this was creating the reference error.
} else {
console.log('Requested topic not found')
}
},
invite : function(id) {
send_PRIVGRP_INVITE(id)
}
}
Any ideas how I could make this work, or is there some better way, cleaner way to do this. Also I should mention I chose to use commands as objects and methods because some commands will be more complex and I was planing to giving them their on file.js and exporting it to the main.js file and it would be a lot easier to add commands w/o editing the main file all the time.
Keep in mind I'm quite new at this, a detailed explanation would go a long way, thank you.