-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
51 lines (44 loc) · 1.75 KB
/
index.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
/* eslint-env node */
const db = require('./src/db')
const tg = require('./src/tg')
const commands = require('./src/commands')
const adminCommands = require('./src/adminCommands')
async function main () {
const telegramBotToken = process.argv[2] || process.env.TELEGRAM_BOT_API
if (!telegramBotToken) {
throw new Error('Usage: node index.js YOUR_TELEGRAM_BOT_TOKEN')
}
await db.connect()
await tg.connect(telegramBotToken)
const adminIds = await db.getAdminIds()
process.on('unhandledRejection', (err, p) => {
console.log('Unhandled Rejection at:', p, 'reason:', err.stack)
tg.sendErrorLog(adminIds, err.stack)
})
process.on('uncaughtException', (err) => {
console.log('Uncaught Exception:', err.stack)
tg.sendErrorLog(adminIds, err.stack)
})
/* eslint-disable no-multi-spaces */
chain(tg.getBot())
.on('message', commands.onAnyMessage)
.onText(/^\/start$/, commands.onStart)
.onText(/^\/cancel$/, commands.onCancel)
.onText(/^\/help$/, commands.onHelp)
.onText(/^\/find$/, commands.onBeginFind)
.onText(/^\/find (.+)/, commands.onFind)
.onText(/^\/findex$/, commands.onBeginFindEx)
.onText(/^\/findex (.+)/, commands.onFindEx)
.onText(/^\/users$/, adminCommands.onUsers)
.onText(/^\/userinfo (\d+)/, adminCommands.onUserInfo)
.onText(/^\/usermessages (\d+)/, adminCommands.onUserMessages)
/* eslint-enable no-multi-spaces */
adminIds.forEach(id => tg.sendMessage(id, 'Bot restarted'))
}
function chain (bot) {
return ['on', 'onText'].reduce((acc, curr) => {
acc[curr] = (...args) => { bot[curr](...args); return acc }
return acc
}, {value: () => bot})
}
main().then()