-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsilly.js
More file actions
60 lines (49 loc) · 1.5 KB
/
silly.js
File metadata and controls
60 lines (49 loc) · 1.5 KB
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
const {
Promise,
co
} = require('../utils')
const TYPE = '_t'
const ONLINE = 'I\'m back! How long was I gone?'
module.exports = function sillyStrategy (bot) {
const send = function (user, object) {
return bot.send({ userId: user.id, object })
}
const users = bot.users.list()
for (let id in users) {
send(users[id], ONLINE)
}
function oncreate (user) {
send(user, 'Nice to meet you!')
}
const onmessage = co(function* onmessage ({ user, object }) {
const type = object[TYPE]
switch (type) {
case 'tradle.SelfIntroduction':
case 'tradle.IdentityPublishRequest':
if (!object.profile) break
let name = object.profile.firstName
let oldName = user.profile && user.profile.firstName
user.profile = object.profile
bot.users.save(user)
if (name !== oldName) {
yield send(user, `${name}, eh? Hot name!`)
}
break
case 'tradle.SimpleMessage':
yield send(user, `tell me more about this "${object.message}," it sounds interesting`)
break
case 'tradle.CustomerWaiting':
yield send(user, 'Buahahaha! ...I mean welcome to my super safe world')
break
default:
yield send(user, `Huh? What's a ${type}? I only understand simple messages. One day, when I'm a real boy...`)
break
}
})
const removeHandler = bot.addReceiveHandler(onmessage)
bot.users.on('create', oncreate)
return function disable () {
removeHandler()
bot.users.removeListener('create', oncreate)
}
}