Skip to content

Commit e236a1f

Browse files
committed
list of key holders
1 parent b42c985 commit e236a1f

File tree

1 file changed

+99
-43
lines changed

1 file changed

+99
-43
lines changed

scripts/keys.coffee

Lines changed: 99 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -23,53 +23,72 @@
2323
module.exports = (robot)->
2424

2525
key = ()->
26-
Key = robot.brain.get("key") or ""
26+
Key = robot.brain.get("key") or []
2727
robot.brain.set("key" ,Key)
2828
Key
29-
3029

31-
robot.respond /i have (a key|the key|key|keys)/i, (msg)->
30+
31+
robot.respond /i have (a key|the key|key|keys) of (.+)/i, (msg)->
3232
name = msg.message.user.name
33+
ownerName = msg.match[2]
3334
user = robot.brain.userForName name
34-
k = key()
35-
if typeof user is 'object'
36-
msg.send "Okay #{name} has keys"
37-
k = user.name
38-
robot.brain.set("key",k)
35+
try
36+
kh = key()
37+
if typeof user is 'object'
38+
msg.send "Okay #{name} has keys"
39+
kh.push holder:name,owner:ownerName
40+
robot.brain.set("key",kh)
41+
catch e
42+
console.log e
43+
3944

4045

4146
robot.respond /i (don\'t|dont|do not) (has|have) (the key|key|keys|a key)/i , (msg)->
4247
name = msg.message.user.name
43-
user = robot.brain.userForName name
44-
k = key()
45-
if k is name
46-
k = ""
48+
kh = key()
49+
check =0
50+
for x in kh
51+
if x.holder is name
52+
index=kh.indexOf(x)
53+
owner = x.owner
54+
kh.splice(index,1)
55+
check=1
56+
msg.send "Okay #{name} doesn't have keys.Then, Who got the keys of #{owner}?"
4757
if typeof user is 'object'
48-
if k is ""
49-
msg.send "Okay #{name} doesn't have keys. Who got the keys then?"
50-
else
51-
msg.send "Yes , i know buddy, its because #{k} has got the keys"
52-
robot.brain.set("key",k)
58+
if check is 0
59+
msg.send "Yes , i know buddy"
60+
robot.brain.set("key",kh)
5361

5462

55-
robot.respond /(.+) (has|have) (the key|key|keys|a key)/i , (msg)->
63+
robot.respond /(.+) (has|have) (the key|key|keys|a key) of (.+)/i , (msg)->
5664
othername = msg.match[1]
65+
ownerName = msg.match[4]
5766
name = msg.message.user.name
5867
k = key()
59-
unless othername in ["who", "who all","Who", "Who all" , "i" , "I" , "i don't" , "i dont" , "i do not" , "I don't" , "I dont" , "I do not"]
60-
if othername is 'you'
61-
msg.send "How am I supposed to take those keys? #{name} is a liar!"
62-
else if othername is robot.name
63-
msg.send "How am I supposed to take those keys? #{name} is a liar!"
64-
else
65-
users = robot.brain.usersForFuzzyName othername
66-
if users.length is 1
67-
robot.brain.set "key", users[0].name
68-
msg.send "Okay, so now the keys are with #{users[0].name}"
69-
else if users.length > 1
70-
msg.send getAmbiguousUserText users
68+
if ownerName is ""
69+
msg.send "okay, but whose key"
70+
else
71+
unless othername in ["who", "who all","Who", "Who all" , "i" , "I" , "i don't" , "i dont" , "i do not" , "I don't" , "I dont" , "I do not"]
72+
if othername is 'you'
73+
msg.send "How am I supposed to take those keys? #{name} is a liar!"
74+
else if othername is robot.name
75+
msg.send "How am I supposed to take those keys? #{name} is a liar!"
7176
else
72-
msg.send "#{othername}? Never heard of 'em"
77+
users = robot.brain.usersForFuzzyName othername
78+
userso = robot.brain.usersForFuzzyName ownerName
79+
if users.length is 1
80+
if userso.length is 1
81+
k.push holder:users[0].name,owner:userso[0].name
82+
robot.brain.set("key", k)
83+
msg.send "Okay, so now the key of #{userso[0].name} are with #{users[0].name}"
84+
else if users.length > 1
85+
msg.send getAmbiguousUserText users
86+
else
87+
msg.send "#{ownerName}? Never heard of 'em"
88+
else if users.length > 1
89+
msg.send getAmbiguousUserText users
90+
else
91+
msg.send "#{othername}? Never heard of 'em"
7392

7493
robot.respond /(i|I) (have given|gave|had given) (the key|key|keys|a key|the keys) to (.+)/i , (msg)->
7594
othername = msg.match[4]
@@ -80,24 +99,61 @@ module.exports = (robot)->
8099
else if othername is robot.name
81100
msg.send "That's utter lies! How can you blame a bot to have the keys?"
82101
else
83-
users = robot.brain.userForName othername
102+
users = robot.brain.usersForFuzzyName othername
84103
if users is null
85104
msg.send "I don't know anyone by the name #{othername}"
86105
else
87-
k = users.name
88-
msg.send "Okay, so now the keys are with #{users.name}"
89-
106+
check =0;
107+
for x in k
108+
if x.holder is name
109+
x.holder = users[0].name
110+
msg.send "Okay, so now the keys of #{x.owner} are with #{users[0].name}"
111+
check=1
112+
if check is 0
113+
msg.send "Liar, you don't have the keys"
90114
robot.brain.set("key",k)
91115

92116
robot.respond /(who|who all) (has|have) (the key|key|keys|a key)/i , (msg)->
93-
k = key()
94-
msgText = k
95-
if msgText is ""
96-
msg.send "Ah! Nobody informed me about the keys. Don't hold me responsible for this :expressionless:"
97-
else
98-
msgText+=" has keys"
99-
msg.send msgText
100-
robot.brain.set("key" ,k)
117+
try
118+
kh = key()
119+
if kh is []
120+
msg.send "Ah! Nobody informed me about the keys. Don't hold me responsible for this :expressionless:"
121+
else
122+
list = []
123+
msgText=[]
124+
for x in kh
125+
list.push "#{x.owner}'s key are with #{x.holder}"
126+
msgText += list.join '\n'
127+
msgText+=""
128+
if msgText is ""
129+
msg.send "Ah! Nobody informed me about the keys. Don't hold me responsible for this :expressionless:"
130+
else
131+
msg.send msgText
132+
robot.brain.set("key" ,kh)
133+
catch e
134+
console.log e
135+
136+
robot.respond /who (has|have) (.+) ('s key|'s keys)/i , (msg)->
137+
ownerName = msg.match[2];
138+
users = robot.brain.usersForFuzzyName ownerName
139+
s = ""
140+
try
141+
kh = key()
142+
if users.length is 1
143+
for x in kh
144+
if x.owner is users[0].name
145+
s = "#{x.owner} keys are with #{x.holder}"
146+
msg.send s
147+
else if users.length > 1
148+
msg.send getAmbiguousUserText users
149+
else
150+
msg.send "#{ownerName}? Never heard of 'em"
151+
robot.brain.set("key" ,kh)
152+
catch e
153+
console.log e
154+
155+
156+
101157

102158
getAmbiguousUserText = (users) ->
103159
"Be more specific, I know #{users.length} people named like that: #{(user.name for user in users).join(", ")}"

0 commit comments

Comments
 (0)