-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactoid.coffee
86 lines (74 loc) · 2.56 KB
/
factoid.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Description:
# javabot style factoid support for your hubot. Build a factoid library
# and save yourself typing out answers to similar questions
#
# Dependencies:
# None
#
# Configuration:
# None
#
# Commands:
# ~<factoid> is <some phrase, link, whatever> - Creates a factoid
# ~<factoid> is also <some phrase, link, whatever> - Updates a factoid.
# ~<factoid> - Prints the factoid, if it exists. Otherwise tells you there is no factoid
# ~tell <user> about <factoid> - Tells the user about a factoid, if it exists
# ~~<user> <factoid> - Same as ~tell, less typing
# <factoid>? - Same as ~<factiod> except for there is no response if not found
# hubot no, <factoid> is <some phrase, link, whatever> - Replaces the full definition of a factoid
#
# Author:
# arthurkalm
class Factoids
constructor: (@robot) ->
@robot.brain.on 'loaded', =>
@cache = @robot.brain.data.factoids
@cache = {} unless @cache
add: (key, val) ->
if @cache[key]
"#{key} is already #{@cache[key]}"
else
this.setFactoid key, val
append: (key, val) ->
if @cache[key]
@cache[key] = @cache[key] + ", " + val
@robot.brain.data.factoids = @cache
"Ok. #{key} is also #{val} "
else
"No factoid for #{key}. It can't also be #{val} if it isn't already something."
setFactoid: (key, val) ->
@cache[key] = val
@robot.brain.data.factoids = @cache
"OK. #{key} is #{val} "
niceGet: (key) ->
@cache[key] or "No factoid for #{key}"
get: (key) ->
@cache[key]
tell: (person, key) ->
factoid = this.get key
if @cache[key]
"#{person}, #{key} is #{factoid}"
else
factoid
handleFactoid: (text) ->
if match = /^~(.+) is also (.+)/i.exec text
this.append match[1], match[2]
else if match = /^~(.+) is (.+)/i.exec text
this.add match[1], match[2]
else if match = (/^~tell (.+) about (.+)/i.exec text) or (/^~~(.+) (.+)/.exec text)
this.tell match[1], match[2]
else if match = /^~(.+)/i.exec text
this.niceGet match[1]
module.exports = (robot) ->
factoids = new Factoids robot
robot.hear /^~(.+)/i, (msg) ->
if match = (/^~tell (.+) about (.+)/i.exec msg.match) or (/^~~(.+) (.+)/.exec msg.match)
msg.send factoids.handleFactoid msg.message.text
else
msg.reply factoids.handleFactoid msg.message.text
robot.hear /(.+)\?/i, (msg) ->
factoid = factoids.get msg.match[1]
if factoid
msg.reply msg.match[1] + " is " + factoid
robot.respond /no, (.+) is (.+)/i, (msg) ->
msg.reply factoids.setFactoid msg.match[1], msg.match[2]