-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbot.js
More file actions
113 lines (93 loc) · 3.67 KB
/
bot.js
File metadata and controls
113 lines (93 loc) · 3.67 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
const rp = require('request-promise');
const dotenv = require('dotenv');
const TeleBot = require('telebot');
const userService = require('./services/users');
const rankingService = require('./services/rankings');
dotenv.config();
const bot = new TeleBot(process.env.TELEGRAM_BOT_TOKEN);
bot.on(/^\/register (.+)$/, async (msg, props) => {
const username = props.match[1];
try {
const telegramId = msg.from.id;
const chatId = msg.chat.id;
let user = await userService.get({ username, chat_id: chatId });
if (user) {
return bot.sendMessage(msg.chat.id, `${username} already registered`);
}
user = await userService.create({ username: username, telegram_id: telegramId, chat_id: chatId });
return bot.sendMessage(msg.chat.id, `${user.username} registered`);
} catch (err) {
console.err(err);
return bot.sendMessage(msg.chat.id, `Could not register ${username}`);
}
});
bot.on(/^\/unregister (.+)$/, async (msg, props) => {
const username = props.match[1];
try {
const telegramId = msg.from.id;
const chatId = msg.chat.id;
let user = await userService.get({ username, chat_id: chatId });
if (user) {
await userService.removeUser(user.id);
return bot.sendMessage(msg.chat.id, `${user.username} unregistered`);
} else {
return bot.sendMessage(msg.chat.id, `Could not unregister ${username}`);
}
} catch (err) {
console.err(err);
return bot.sendMessage(msg.chat.id, `Could not unregister ${username}`);
}
});
bot.on('/stats', async (msg) => {
try {
const chatId = msg.chat.id;
const users = await userService.list({ chat_id: chatId });
if (!users) {
return bot.sendMessage(msg.chat.id, `No user registered yet. User /register to register new usernames`);
}
const stats = ['=========================\n ===== Live Chess (Blitz) ===== \n========================='];
let users_live_ratings = [];
for (let user of users) {
let url = `https://api.chess.com/pub/player/${user.username}/stats`;
let res = await rp(url, { json: true });
let rating = res.chess_blitz && res.chess_blitz.last && res.chess_blitz.last.rating || 'No score';
users_live_ratings.push({ username: user.username, rating: rating });
}
users_live_ratings.sort((a, b) => b.rating - a.rating);
users_live_ratings.forEach((ur, i) => (
stats.push(`${i+1} - ${ur.username} - ${ur.rating} - ${rankingService.getLiveRating(ur.rating)}`)
));
stats.push('\n========================\n ===== Tactics Ranking ===== \n========================')
let users_tactics_ratings = [];
for (let user of users) {
let url = `https://www.chess.com/callback/member/stats/${user.username}`;
let res = await rp(url, { json: true });
let rating = 'No Score';
if (res.stats) {
res.stats.forEach(function(statsType) {
if (statsType.key === 'tactics') {
rating = statsType.stats.rating;
}
});
}
users_tactics_ratings.push({ username: user.username, rating: rating });
}
users_tactics_ratings.sort((a, b) => b.rating - a.rating);
users_tactics_ratings.forEach((ur, i) => (
stats.push(`${i+1} - ${ur.username} - ${ur.rating} - ${rankingService.getTacticsRating(ur.rating)}`)
));
return bot.sendMessage(msg.chat.id, stats.join('\n'));
} catch (err) {
console.err(err);
return bot.sendMessage(msg.chat.id, `Could not list stats`);
}
});
bot.on('/rankings', async (msg) => {
try {
return bot.sendMessage(msg.chat.id, rankingService.getAllRankings());
} catch (err) {
console.err(err);
return bot.sendMessage(msg.chat.id, `Could not list all rankings`);
}
});
module.exports = bot;