forked from keybase/keybase-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflippy.js
executable file
·69 lines (60 loc) · 1.95 KB
/
flippy.js
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
#!/usr/bin/env node
const Bot = require('../../lib/index.js')
/*
* Taken from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random#Getting_a_random_integer_between_two_values_inclusive
*/
function getRandomIntInclusive(min, max) {
min = Math.ceil(min)
max = Math.floor(max)
return Math.floor(Math.random() * (max - min + 1)) + min
}
const bot = new Bot()
async function main() {
const channel = {
name: process.env.KB_TEAM,
public: false,
topicType: 'chat',
membersType: 'team',
topicName: 'general',
}
try {
const username = process.env.KB_USERNAME
const paperkey = process.env.KB_PAPERKEY
await bot.init(username, paperkey)
const info = bot.myInfo()
console.log(`Flippy initialized with username ${info.username}.`)
const onMessage = async message => {
if (message.content.type === 'text') {
// TODO: could probably normalize the body a little more before comparisons
const messageBody = message.content.text.body.toLowerCase()
switch (messageBody) {
case 'new flip':
console.log('Starting a new flip!')
const messageToSend = {body: 'Starting a new flip...'}
await bot.chat.send(channel, messageToSend)
break
case 'coin flip':
case 'coyne flip':
const flip = getRandomIntInclusive(0, 1) ? ':+1:' : ':-1:'
console.log('Making a flip:', flip)
await bot.chat.react(channel, message.id, flip)
break
default:
break
}
}
}
const onError = e => console.error(e)
console.log(`Listening in the general channel of ${channel.name}...`)
await bot.chat.watchChannelForNewMessages(channel, onMessage, onError)
} catch (error) {
console.error(error)
}
}
async function shutDown() {
await bot.deinit()
process.exit()
}
process.on('SIGINT', shutDown)
process.on('SIGTERM', shutDown)
main()