-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
59 lines (45 loc) · 1.36 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
require('dotenv').config();
const Telegraf = require("telegraf");
const {init: initDb, writeLeave, getLastLeave, getDaysWithoutLeaving} = require('./db');
const {getImgUrl} = require('./utils');
const runCron = require('./cron');
const token = process.env.BOT_TOKEN;
const bot = new Telegraf(token);
bot.on('left_chat_member', async (ctx) => {
if (ctx.message.left_chat_member.is_bot) {
return;
}
const {
chat: {
id: chatId,
},
left_chat_member: {
id: userId,
first_name: userFirstName,
username,
}
} = ctx.message;
await writeLeave(chatId, userId, userFirstName, username);
return ctx.replyWithPhoto({url: getImgUrl()});
});
bot.command('who', async ctx => {
const lastLeave = await getLastLeave(ctx.chat.id);
if (!lastLeave) {
return ctx.reply('Никто не ливал');
}
return ctx.reply(`Последний ливнувший – ${lastLeave.userFirstName} (@${lastLeave.username})`);
});
bot.command('leave_stats', async (ctx) => {
const daysWithoutLeaving = await getDaysWithoutLeaving(ctx.chat.id);
const imgUrl = getImgUrl(daysWithoutLeaving);
return ctx.replyWithPhoto({url: imgUrl});
});
(async () => {
await initDb();
await runCron(bot.telegram);
const botInfo = await bot.telegram.getMe();
if (botInfo) {
bot.options.username = botInfo.username;
}
bot.startPolling();
})();