mirror of
https://github.com/Brandon-Rozek/Rozbot.git
synced 2024-09-12 23:52:28 -04:00
56 lines
1.5 KiB
JavaScript
56 lines
1.5 KiB
JavaScript
|
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) {
|
||
|
//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) {
|
||
|
return cmd.condition(message) === true;
|
||
|
});
|
||
|
//Run the command using user's contextual data (app by app basis)
|
||
|
if (command !== undefined) {
|
||
|
command.respond.apply(command, args.concat(user.getData(command.name)));
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|