forked from keybase/keybase-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath-bot-es7.js
65 lines (57 loc) · 1.8 KB
/
math-bot-es7.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
#!/usr/bin/env node
const Bot = require('../../lib/index.js')
const mathjs = require('mathjs')
//
// This bot replies to any message from any user,
// starting with `/math` (in any channel)
// by actually trying to do the math. For example
// send it :
//
// /math sqrt(pi/2) * 3!`
//
const bot = new Bot()
const msgReply = s => {
let a1, a2, ans, b1, b2, eqn
try {
ans = '= ' + mathjs['eval'](s).toString()
} catch (e) {
a1 = Math.floor(Math.random() * 10)
b1 = Math.floor(Math.random() * 10)
a2 = Math.floor(Math.random() * 10)
b2 = Math.floor(Math.random() * 10)
eqn = '(' + a1 + ' + ' + b1 + 'i) * (' + a2 + ' + ' + b2 + 'i)'
ans =
"Sorry, I can't do that math. Did you know " + eqn + ' = ' + mathjs['eval'](eqn).toString() + '? True.'
}
return ans
}
async function main() {
try {
const username = process.env.KB_USERNAME
const paperkey = process.env.KB_PAPERKEY
await bot.init(username, paperkey)
console.log('I am me!', bot.myInfo().username, bot.myInfo().devicename)
console.log('Beginning watch for new messages.')
console.log(`Tell anyone to send a message to ${bot.myInfo().username} starting with '/math '`)
const onMessage = async message => {
if (message.content.type === 'text') {
const prefix = message.content.text.body.slice(0, 6)
if (prefix === '/math ') {
const reply = {body: msgReply(message.content.text.body.slice(6))}
await bot.chat.send(message.channel, reply)
}
}
}
const onError = e => console.error(e)
bot.chat.watchAllChannelsForNewMessages(onMessage, onError)
} catch (error) {
console.error(error.message)
}
}
async function shutDown() {
await bot.deinit()
process.exit()
}
process.on('SIGINT', shutDown)
process.on('SIGTERM', shutDown)
main()