forked from hubotio/hubot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroles.coffee
85 lines (74 loc) · 2.71 KB
/
roles.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
# Description:
# Assign roles to people you're chatting with
#
# Commands:
# hubot <user> is a badass guitarist - assign a role to a user
# hubot <user> is not a badass guitarist - remove a role from a user
# hubot who is <user> - see what roles a user has
#
# Examples:
# hubot holman is an ego surfer
# hubot holman is not an ego surfer
module.exports = (robot) ->
getAmbiguousUserText = (users) ->
"Be more specific, I know #{users.length} people named like that: #{(user.name for user in users).join(", ")}"
robot.respond /who is @?([\w .\-]+)\?*$/i, (msg) ->
joiner = ', '
name = msg.match[1].trim()
if name is "you"
msg.send "Who ain't I?"
else if name is robot.name
msg.send "The best."
else
users = robot.brain.usersForFuzzyName(name)
if users.length is 1
user = users[0]
user.roles = user.roles or [ ]
if user.roles.length > 0
if user.roles.join('').search(',') > -1
joiner = '; '
msg.send "#{name} is #{user.roles.join(joiner)}."
else
msg.send "#{name} is nothing to me."
else if users.length > 1
msg.send getAmbiguousUserText users
else
msg.send "#{name}? Never heard of 'em"
robot.respond /@?([\w .\-_]+) is (["'\w: \-_]+)[.!]*$/i, (msg) ->
name = msg.match[1].trim()
newRole = msg.match[2].trim()
unless name in ['', 'who', 'what', 'where', 'when', 'why']
unless newRole.match(/^not\s+/i)
users = robot.brain.usersForFuzzyName(name)
if users.length is 1
user = users[0]
user.roles = user.roles or [ ]
if newRole in user.roles
msg.send "I know"
else
user.roles.push(newRole)
if name.toLowerCase() is robot.name
msg.send "Ok, I am #{newRole}."
else
msg.send "Ok, #{name} is #{newRole}."
else if users.length > 1
msg.send getAmbiguousUserText users
else
msg.send "I don't know anything about #{name}."
robot.respond /@?([\w .\-_]+) is not (["'\w: \-_]+)[.!]*$/i, (msg) ->
name = msg.match[1].trim()
newRole = msg.match[2].trim()
unless name in ['', 'who', 'what', 'where', 'when', 'why']
users = robot.brain.usersForFuzzyName(name)
if users.length is 1
user = users[0]
user.roles = user.roles or [ ]
if newRole not in user.roles
msg.send "I know."
else
user.roles = (role for role in user.roles when role isnt newRole)
msg.send "Ok, #{name} is no longer #{newRole}."
else if users.length > 1
msg.send getAmbiguousUserText users
else
msg.send "I don't know anything about #{name}."