Skip to content

Commit

Permalink
can remove phrases
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam Breed committed Jun 3, 2011
1 parent f3a1fa0 commit 81da011
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 3 deletions.
7 changes: 6 additions & 1 deletion hack-bot.coffee
Expand Up @@ -42,14 +42,19 @@ quack = ( room ) ->

Reminder.poller( msg, room )

if /^tell (\w+\s\w+|\@\w+)\s(.+)$/.test( msg.body )
if /^tell (\w+\s\w+|\@\w+)\s(.+)$/.test( msg.body )
Reminder.save msg, ->
room.speak "sure", logger

Phrases.listen( msg, room )

setter = /^([^=]+)\s\=\s(.+)$/

if /^destroy (.+)$/.test( msg.body )
match = /^destroy (.+)$/.exec(msg.body)[1]
Phrase.remove match, ->
room.speak "#{match} removed"

if setter.test( msg.body )
params = setter.exec( msg.body )
Phrases.store params[1], params[2], ->
Expand Down
8 changes: 7 additions & 1 deletion hack-bot.js
Expand Up @@ -30,7 +30,7 @@ quack = function(room) {
room.speak("hai guys", logger);
}
return room.listen(function(msg) {
var g_exp, mdc_exp, params, setter, yt_exp;
var g_exp, match, mdc_exp, params, setter, yt_exp;
if (msg.user_id === parseInt(bot_id)) {
return;
}
Expand All @@ -43,6 +43,12 @@ quack = function(room) {
}
Phrases.listen(msg, room);
setter = /^([^=]+)\s\=\s(.+)$/;
if (/^destroy (.+)$/.test(msg.body)) {
match = /^destroy (.+)$/.exec(msg.body)[1];
Phrase.remove(match, function() {
return room.speak("" + match + " removed");
});
}
if (setter.test(msg.body)) {
params = setter.exec(msg.body);
Phrases.store(params[1], params[2], function() {
Expand Down
6 changes: 6 additions & 0 deletions lib/phrases.coffee
Expand Up @@ -26,6 +26,12 @@ api =
console.log("#{msg} saved")
callback(doc) if _.isFunction( callback )

remove: ( match, callback ) ->
p = Phrase.find { regex: match }, ( err, doc ) ->
doc.remove ->
console.log("#{match} destroyed")
callback() if _.isFunction( callback )

register: ( match, msg, callback ) ->

phrases.push { regex: match, msg: msg ? false, callback: callback ? false }
Expand Down
13 changes: 13 additions & 0 deletions lib/phrases.js
Expand Up @@ -50,6 +50,19 @@ api = {
}
});
},
remove: function(match, callback) {
var p;
return p = Phrase.find({
regex: match
}, function(err, doc) {
return doc.remove(function() {
console.log("" + match + " destroyed");
if (_.isFunction(callback)) {
return callback();
}
});
});
},
register: function(match, msg, callback) {
return phrases.push({
regex: match,
Expand Down
2 changes: 1 addition & 1 deletion spec/PhrasesSpec.coffee
Expand Up @@ -13,7 +13,7 @@ describe 'Phrases', ->
it 'should match and speak static phrases', ->
spyOn room, 'speak'

# phrases are currently hard coded, so let's test one that's in ther
# phrases are currently hard coded, so let's test o`ne that's in ther
# TODO change once db support added
Phrases.listen( { body: "wet" }, room )

Expand Down

0 comments on commit 81da011

Please sign in to comment.