Skip to content

Commit 4cc6aeb

Browse files
committed
use druplicon-style factoid.coffee instead
1 parent 73b15f5 commit 4cc6aeb

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

hubot-scripts.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
["factoid.coffee", "drupalorg.coffee", "tell.coffee", "karma.coffee", "pg-brain.coffee", "shipit.coffee", "xkcd.coffee", "github-commits.coffee", "botsnack.coffee", "hello.coffee"]
1+
["drupalorg.coffee", "tell.coffee", "karma.coffee", "pg-brain.coffee", "shipit.coffee", "xkcd.coffee", "github-commits.coffee", "botsnack.coffee", "hello.coffee"]

scripts/factoid.coffee

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
class Factoids
2+
constructor: (@robot) ->
3+
@robot.brain.on 'loaded', =>
4+
@cache = @robot.brain.data.factoids
5+
@cache = {} unless @cache
6+
7+
add: (key, val) ->
8+
@cache[key] = val
9+
@robot.brain.data.factoids = @cache
10+
"Okay."
11+
12+
get: (key) ->
13+
if @cache[key]
14+
@cache[key]
15+
16+
delete: (key) ->
17+
if @cache[key]
18+
delete @cache[key]
19+
"Okay, I've forgotten about " + key + "."
20+
21+
22+
module.exports = (robot) ->
23+
factoids = new Factoids robot
24+
25+
robot.respond /forget (about )?(.*)/i, (msg) ->
26+
result = factoids.delete msg.match[2]
27+
if result
28+
msg.reply result
29+
30+
robot.respond /(.*?) (is|are) (also )?(.*)/i, (msg) ->
31+
# We are adding to an existing factoid and not deleting one that
32+
# contains is as the key (needed for cleanup of prior match
33+
# previously being greedy
34+
if !msg.message.match(/: forget /i)
35+
if msg.match[3] and factoids.get msg.match[1]
36+
result = factoids.add msg.match[1], (factoids.get msg.match[1]) + " and " + msg.match[2] + " also " + msg.match[4]
37+
else
38+
result = factoids.add msg.match[1], msg.match[2] + " " + msg.match[4]
39+
msg.reply result
40+
41+
robot.hear /^(.+)(\?|!)$/i, (msg) ->
42+
key = msg.match[1]
43+
# Don't return a factoid if we're adding a factoid.
44+
if /(is|are) .+/.exec key
45+
return
46+
result = factoids.get key
47+
if result
48+
if reply = /(is|are) (<reply>)(.*)/.exec result
49+
msg.send reply[3]
50+
else
51+
msg.send msg.match[1] + " " + result

0 commit comments

Comments
 (0)