-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
182 lines (149 loc) · 4.88 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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
const express = require('express');
const app = express();
// Telegraf's imports
const Telegraf = require('telegraf');
const Extra = require('telegraf/extra');
// Tools
const api = require('./config/requests');
const logger = require('./config/logger');
const messages = require('./config/messages');
const strings = require('./config/strings');
const { name, version } = require('./package.json');
require('dotenv').config();
// Init app
app.set('port', (process.env.PORT || 5000));
// Init bot
const bot = new Telegraf(process.env.BOT_TOKEN);
bot.start((ctx) => {
logger.addEntryToUserLog(ctx.update);
api.getGeneralStatus()
.then((res) => {
const { data } = res;
const message = messages.getGeneralStatusMessage(data);
return ctx.replyWithHTML(message, Extra.HTML().markup((m) => m.inlineKeyboard(
[
[m.callbackButton('📊 Check servers status', '/status')],
[m.callbackButton('⏰ Game Time', '/time')],
],
)));
})
.catch((error) => {
logger.addEntryToErrorLog(error);
});
});
bot.command('status', (ctx) => {
api.getServersStatus()
.then((response) => {
const data = response.data.response;
const buttons = messages.getStatusButtons(data);
return ctx.reply('<b>Choose server for detailed information:</b>',
Extra.HTML().markup((m) => m.inlineKeyboard(
buttons.map(
(i) => [m.callbackButton(`${i.online} ${i.game} ${i.name}`, i.id)],
),
)));
})
.catch((error) => {
logger.addEntryToErrorLog(error);
});
});
bot.command('time', (ctx) => {
api.getGameTime()
.then((response) => response.json())
.then((res) => {
const time = res.response.calculated_game_time;
const message = messages.getTimeMessage(time);
return ctx.replyWithHTML(message,
Extra.HTML().markup((m) => m.inlineKeyboard(
[m.callbackButton('\uD83D\uDD19 Back', '/backLink')],
)));
})
.catch((err) => {
logger.addEntryToErrorLog(err);
});
});
bot.on('callback_query', (ctx) => {
if (ctx.callbackQuery.data === '/backLink') {
api.getGeneralStatus()
.then((res) => {
const { data } = res;
const message = messages.getBackLinkMessage(data);
return ctx.replyWithHTML(message,
Extra.HTML().markup((m) => m.inlineKeyboard(
[
[m.callbackButton('📊 Check servers status', '/status')],
[m.callbackButton('⏰ Game Time', '/time')],
],
)));
})
.catch((error) => {
logger.addEntryToErrorLog(error);
});
ctx.answerCbQuery();
}
if (ctx.callbackQuery.data === '/time') {
api.getGameTime()
.then((response) => response.json())
.then((res) => {
const time = res.response.calculated_game_time;
const message = messages.getTimeMessage(time);
return ctx.replyWithHTML(message,
Extra.HTML().markup((m) => m.inlineKeyboard(
[m.callbackButton('\uD83D\uDD19 Back', '/backLink')],
)));
})
.catch((err) => {
logger.addEntryToErrorLog(err);
});
ctx.answerCbQuery();
}
if (ctx.callbackQuery.data === '/status') {
api.getServersStatus()
.then((response) => {
const data = response.data.response;
const buttons = messages.getStatusButtons(data);
return ctx.reply('<b>Choose server for detailed information:</b>',
Extra.HTML().markup((m) => m.inlineKeyboard(
buttons.map(
(i) => [
m.callbackButton(`${i.online} ${i.game} ${i.name}`,
i.id)],
),
)));
})
.catch((error) => {
logger.addEntryToErrorLog(error);
});
ctx.answerCbQuery();
}
api.getServersStatus()
.then((response) => {
const data = response.data.response;
const serverInfo = data.filter((i) => Number(ctx.callbackQuery.data)
=== i.id);
const isValid = !!serverInfo[0];
if (!isValid) {
return false;
}
const message = messages.getServerStatusMessage(serverInfo);
return ctx.replyWithHTML(message,
Extra.HTML().markup((m) => m.inlineKeyboard(
[m.callbackButton('\uD83D\uDD19 Back to servers list', '/status')],
)));
})
.catch((error) => {
logger.addEntryToErrorLog(error);
});
ctx.answerCbQuery();
});
bot.command('about', (ctx) => ctx.replyWithHTML(strings.aboutString, Extra.webPreview(false)));
bot.command('help', (ctx) => ctx.replyWithHTML(strings.helpString));
// Launch bot
bot.launch();
app.get('/mp', (request, response) => {
const result = `<pre>🚀 ${name} is running.\n\nversion ${version}</pre>`;
response.send(result);
}).listen(app.get('port'), () => {
// eslint-disable-next-line no-console
console.log(`🚀 ${name} running, server is listening on port`, app.get('port'));
});