Skip to content

Commit

Permalink
Initial support for multiple intents - #32
Browse files Browse the repository at this point in the history
  • Loading branch information
imbcmdth committed Feb 13, 2014
1 parent 6a10a53 commit 033eadc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
3 changes: 3 additions & 0 deletions lib/irc/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,9 @@ Client.Channel.prototype.self_command = function(command, message, options) {
Client.Channel.prototype.send_reply = function(user, message, options) {
if (user === this) {
this.send (message, options);
} else if (Array.isArray(user)){
var names = user.map(function(user) { return user.name; });
this.send (names.join(', ') + ": " + message, options);
} else {
this.send (user.name + ": " + message, options);
}
Expand Down
23 changes: 15 additions & 8 deletions lib/irc/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,22 @@ Bot.prototype.compile_command_listener = function() {


Bot.prototype.parse_intent = function(str) {
var parsed = str.match(/^(.*?)\s*@\s*([A-Z\x5B-\x60\x7B-\x7D][-0-9A-Z\x5B-\x60\x7B-\x7D]{0,16})$/i);
if (!parsed) return false;
var intentRegex = /@\s*([A-Z\x5B-\x60\x7B-\x7D][-0-9A-Z\x5B-\x60\x7B-\x7D]{0,16})(?=([\s,]*@\s*([A-Z\x5B-\x60\x7B-\x7D][-0-9A-Z\x5B-\x60\x7B-\x7D]){0,16})|\s*$)/gi;
var nicks = [], nick;

while (nick = intentRegex.exec(str)) nicks.push(nick[1]);

if (!nicks.length) return false;

var resultRegex = new RegExp("^(.*?)\\s*@\\s*" + nicks[0], 'i');
var result = resultRegex.exec(str)[1];

return {
intent: parsed[2],
result: parsed[1]
intent: nicks,
result: result
};
};


Bot.prototype.get_commands = function() {
var array = [];
for (var i in this.__commands) {
Expand Down Expand Up @@ -322,15 +329,15 @@ Bot.prototype.listeners = {
var intent_obj = Bot.prototype.parse_intent.call(this, parameters);
if (intent_obj) {
parameters = intent_obj.result;
context.intent = channel.client.get_user(intent_obj.intent);
context.intent = intent_obj.intent.map(channel.client.get_user.bind(channel.client));
}
}
this.__commands[command].callback.call(this, context, parameters, command);
} else {
var intent_obj = Bot.prototype.parse_intent.call(this, full);
if (intent_obj) {
full = intent_obj.result;
context.intent = channel.client.get_user(intent_obj.intent);
context.intent = intent_obj.intent.map(channel.client.get_user.bind(channel.client));
}
this.emit('command_not_found', context, full);
}
Expand All @@ -346,7 +353,7 @@ Bot.prototype.listeners = {
var intent_obj = Bot.prototype.parse_intent.call(this, trimmed);
if (intent_obj) {
trimmed = intent_obj.result;
context.intent = channel.client.get_user(intent_obj.intent);
context.intent = intent_obj.intent.map(channel.client.get_user.bind(channel.client));
}
}

Expand Down

0 comments on commit 033eadc

Please sign in to comment.