This repository was archived by the owner on Jun 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy patheardropping.coffee
88 lines (76 loc) · 2.83 KB
/
eardropping.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
87
88
# Description:
# Add programmable interface to hubot. Allow to run a hubot command
# whenever something came up in the conversation. Optionally, multiple
# actions can be specified, with or without ordering.
#
# Dependencies:
# None
#
# Configuration:
# None
#
# Commands:
# hubot when you hear <pattern> do <something hubot does> - Setup a ear dropping event
# hubot when you hear <pattern> do 1|<something hubot does>; 2|<some.... - Set up ear dropping with multiple actions and ordering
# hubot stop ear dropping - Stop all ear dropping
# hubot stop ear dropping on <pattern> - Remove a particular ear dropping event
# hubot show ear dropping - Show what hubot is ear dropping on
#
# Author:
# garylin
TextMessage = require('hubot').TextMessage
class EarDropping
constructor: (@robot) ->
@cache = []
@robot.brain.on 'loaded', =>
if @robot.brain.data.eardropping
@cache = @robot.brain.data.eardropping
add: (pattern, action, order) ->
task = {key: pattern, task: action, order: order}
@cache.push task
@robot.brain.data.eardropping = @cache
all: -> @cache
deleteByPattern: (pattern) ->
@cache = @cache.filter (n) -> n.key != pattern
@robot.brain.data.eardropping = @cache
deleteAll: () ->
@cache = []
@robot.brain.data.eardropping = @cache
module.exports = (robot) ->
earDropping = new EarDropping robot
robot.respond /when you hear (.+?) do (.+?)$/i, (msg) ->
key = msg.match[1]
for task_raw in msg.match[2].split ";"
task_split = task_raw.split "|"
# If it's a single task, don't add an "order" property
if not task_split[1]
earDropping.add(key, task_split[0])
else
earDropping.add(key, task_split[1], task_split[0])
msg.send "I am now ear dropping for #{key}. Hehe."
robot.respond /stop ear *dropping$/i, (msg) ->
earDropping.deleteAll()
msg.send 'Okay, fine. :( I will keep my ears shut.'
robot.respond /stop ear *dropping (for|on) (.+?)$/i, (msg) ->
pattern = msg.match[2]
earDropping.deleteByPattern(pattern)
msg.send "Okay, I will ignore #{pattern}"
robot.respond /show ear *dropping/i, (msg) ->
response = "\n"
for task in earDropping.all()
response += "#{task.key} -> #{task.task}\n"
msg.send response
robot.hear /(.+)/i, (msg) ->
robotHeard = msg.match[1]
tasks = earDropping.all()
tasks.sort (a,b) ->
return if a.order >= b.order then 1 else -1
tasksToRun = []
for task in tasks
if new RegExp(task.key, "i").test(robotHeard)
tasksToRun.push task
tasksToRun.sort (a,b) ->
return if a.order >= b.order then 1 else -1
for task in tasksToRun
if (robot.name != msg.message.user.name && !(new RegExp("^#{robot.name}", "i").test(robotHeard)))
robot.receive new TextMessage(msg.message.user, "#{robot.name}: #{task.task}")