/
quote-grab.coffee
72 lines (60 loc) · 1.65 KB
/
quote-grab.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
# Description:
# Quotes overheard
#
# Dependencies:
# None
#
# Configuration:
# None
#
# Commands:
# hubot <user> said "<some quote>" (<context>)
# hubot quote me - show random quote
# hubot quotes - show all quotes
# hubot quotes clear - delete all quotes in the system
#
# Author:
# jmacadam
# jjasghar
sassy_confirmations = [
"Srsly? woa.",
"nice one",
"lame",
"said WHAT?",
"maybe we should call for help...",
"I'll never forget that one"
]
class Quotes
constructor: (@robot) ->
@robot.brain.on 'loaded', =>
@cache = @robot.brain.data.quotes
@cache = [] unless @cache
add: (who, what, context) ->
if context?
quote = "#{who} said #{what} (#{context})"
else
quote = "#{who} said #{what}"
@cache.push {quote: quote, who: who, what: what, context: context}
@robot.brain.data.quotes = @cache
list: ->
@cache.reduce (a, b) -> "#{a['quote']}\n#{b['quote']}"
randomQuote: ->
@cache[Math.floor(Math.random() * @cache.length)]['quote']
clear: ->
@cache = []
@robot.brain.data.quotes = @cache
module.exports = (robot) ->
quotes = new Quotes(robot)
robot.respond /(.+) said "([^"]+)"$/i, (msg) ->
quotes.add(msg.match[1], msg.match[2])
msg.send msg.random sassy_confirmations
robot.respond /(.+) said "([^"]+)"(?:\s\(([^\)]+)\))/i, (msg) ->
quotes.add(msg.match[1], msg.match[2], msg.match[3])
msg.send msg.random sassy_confirmations
robot.respond /quote me/i, (msg) ->
msg.send quotes.randomQuote()
robot.respond /quotes$/i, (msg) ->
msg.send quotes.list()
robot.respond /quotes clear$/i, (msg) ->
quotes.clear()
msg.send "What quotes?"