-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
88 lines (71 loc) · 2.13 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
require('dotenv').config()
const _ = require('lodash');
const Telebot = require('telebot')
const fs = require('fs')
const log = require('./log.json')
const bot = new Telebot(process.env.API_KEY)
const LOG_DIR = './log.json'
const ask = []
const currentLog = _.clone(log)
let chatId
let admin = []
const stringify = (data) => JSON.stringify(data)
function checkAdmin (user) {
const isAdmin = _.find(admin, ['username', user])
if (isAdmin) return isAdmin
if (!isAdmin) bot.sendMessage(chatId, `You have no power here @${user}`)
}
function writeLog () {
fs.writeFile(LOG_DIR, stringify(currentLog), err => {
if (err) throw err
})
}
// Hello world!
bot.on(['/hello'], msg => msg.reply.text('Hello World!'))
// On initialize chat
bot.on('/start', msg => {
chatId = msg.chat.id
bot.getChatAdministrators(chatId)
.then(response => response.result.forEach(people => admin.push(people.user)))
.catch(err => console.log(err))
bot.getChat(chatId)
.then(response => console.log(response.result))
.catch(err => console.log(err))
})
// Delete audio & sticker post
bot.on(['audio', 'sticker'], msg => bot.deleteMessage(msg.chat.id, msg.message_id))
// Save photo & text post to .json log
bot.on(['text', 'photo'], msg => {
if (msg.chat.id === chatId) {
currentLog.push(msg)
writeLog()
}
})
// Replace log to edited text
bot.on(['edit'], msg => {
if (msg.chat.id === chatId) {
const idx = _.findIndex(currentLog, { message_id: msg.message_id })
_.update(currentLog, idx, () => msg)
writeLog()
}
})
// Add to Ask queue
bot.on(['/ask'], msg => {
ask.push({ name: msg.from.username, done: false })
msg.reply.text(`@${msg.from.username} added to ask queue, pls wait your turn...`)
})
// Next question pls
bot.on(['/next'], msg => {
let index
const isAdmin = checkAdmin(msg.from.username)
if (isAdmin) {
const asker = _.find(ask, ['done', false])
if (asker) {
index = _.findIndex(ask, {'name': asker.name, 'done': false})
msg.reply.text(`Please state your question @${asker.name}`)
ask[index].done = true
}
if (!asker) msg.reply.text(`No asker in queue...`)
}
})
bot.start()