Rozbot/Rozbot.js

66 lines
2 KiB
JavaScript
Raw Normal View History

2016-07-11 23:24:15 -04:00
var promise = require('promise-polyfill');
var prototypes = require('./lib/helpers/additionalPrototypes.js');
var User = require('./User.js');
module.exports = function() {
//Store possible commands
this.commandList = [];
//Store users
this.userList = [];
//Add commands to the commandList
this.extend = function(command) {
if (typeof(command) === 'object') {
this.commandList.push(command);
} else {
throw new Error("Extend must take a Command Object");
}
};
//Add user to userlist
this.addUser = function(username, sendMethod) {
var user = new User(username, sendMethod);
this.userList.push(user);
return user;
}
//Get's user from userlist by username
this.getUser = function(username) {
for (var i = 0; i < this.userList.length; i++) {
if (this.userList[i].username === username) {
return this.userList[i];
}
}
//User not found
return null;
}
//Gives Rozbot's response
this.respond = function(message, user, extra) {
extra = extra || {};
extra.privateMessage = extra.privateMessage || false;
2016-07-11 23:24:15 -04:00
//Store whether or not an app wants to listen to the next message
var inAppScope = user.inAppScope
//Used to allow apps to get the next message
user.listener.emit('message', message);
if (!inAppScope) {
var args = [message, user.send];
//Find the right command to run
var command = this.commandList.find(function(cmd) {
var userData = user.getData(cmd.name);
//Attach whether or not this is a private message
userData.privateMessage = extra.privateMessage;
return cmd.condition(message, userData ) === true;
2016-07-11 23:24:15 -04:00
});
//Run the command using user's contextual data (app by app basis)
if (command !== undefined) {
var userData = user.getData(command.name);
//Attach whether or not this is a private message
userData.privateMessage = extra.privateMessage;
//Attach the username
userData.username = user.username;
command.respond.apply(command, args.concat(userData));
2016-07-11 23:24:15 -04:00
}
}
}
}