-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.js
357 lines (308 loc) ยท 11.9 KB
/
functions.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
'use strict';
const { HLTV } = require('hltv');
const getNews = require('hltv-api').default.getNews;
const database = require('./db');
const MONTH = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
const _MONTH = MONTH.map(x => x.slice(0, 3));
const BUTTON_REQUESTS = {
MATCHES: ['Live matches', 'List of 15 matches', 'Intresting matches', 'Results'],
EVENTS: ['This year events', 'Large tournaments'],
PLAYERS: ['Top 10 players', 'Search by name'],
TEAMS: ['HLTV top teams', 'Favorite teams']
};
const STICKERS = ['w0DL7kC/1', 'XpJZhd1/2', 'XVmsqYw/3', 'BB0YPts/4', 'gZ3Q80F/5', 'J56mp32/6', 'b7FpgTH/7', '3056g9f/8', 'vJg7FXw/9', 'NL0CX0p/10', 'J5yZJn8/11', 'FWDLkr7/12', 'ZxkZf1z/13'];
let USERS_SUBSCRIPTIONS;
let SUB_TIMER = [];
function random(min, max) {
return Math.floor(Math.random() * (max - min) + min)
}
async function LoadBot(bot) {
await database.CopyData().then(val => USERS_SUBSCRIPTIONS = val);
USERS_SUBSCRIPTIONS.forEach(user => { AddTimer(bot, user.userID) });
}
function AddTimer(bot, userID) {
SUB_TIMER.push(setInterval(() => CheckOnSub(bot, userID), 1200000));
}
function GetStickerUrl() {
const url = `https://i.ibb.co/${STICKERS[random(0, STICKERS.length)]}.webp`;
return url;
}
function ThisDay(value) {
const today = new Date().getDate();
const dayOfGame = new Date(value.date).getDate();
return today == dayOfGame;
}
function MakeInlineKeyboard(type) {
let inline_keyboard = [];
type.forEach(element => inline_keyboard.push([
{
text: element,
callback_data: element.toString(),
},
]));
const keyboard = {
reply_markup: JSON.stringify({
inline_keyboard,
}),
};
return keyboard;
}
const matchfunc = (ctx) => {
let data = "Choose which variation of list you want receive:";
ctx.reply(data, MakeInlineKeyboard(BUTTON_REQUESTS.MATCHES));
};
const eventsfunc = (ctx) => {
let data = "Choose one:";
ctx.reply(data, MakeInlineKeyboard(BUTTON_REQUESTS.EVENTS));
};
const playersfunc = (ctx) => {
let data = "Choose one:";
ctx.reply(data, MakeInlineKeyboard(BUTTON_REQUESTS.PLAYERS));
};
const teamsfunc = (ctx) => {
let data = "Choose one:";
ctx.reply(data, MakeInlineKeyboard(BUTTON_REQUESTS.TEAMS));
};
const onCallbackQuery = (ctx) => {
const data = ctx.update.callback_query.data;
switch (data) {
case 'Live matches': queryForLive(ctx); break;
case 'List of 15 matches': queryForList15(ctx); break;
case 'Intresting matches': queryForStars(ctx); break;
case 'Results': queryForResults(ctx); break;
case 'This year events': queryForYearEvents(ctx); break;
case 'Large tournaments': queryForTopEvents(ctx); break;
case 'Top 10 players': queryForTopPlayers(ctx); break;
case 'Search by name': queryForPlayer(ctx); break;
case 'HLTV top teams': queryForTopTeams(ctx); break;
case 'Favorite teams': queryForMyTeams(ctx); break;
};
};
const queryForLive = async (ctx) => {
let data = 'Here is a list of live:';
let matches = await HLTV.getMatches();
let index = 0;
while (matches[index].live) {
data += `\n๐ฎ${matches[index].team1.name} vs ${matches[index].team2.name}๐ฎ\nid: ${matches[index].id}\n`;
index++;
}
if (data == 'Here is a list of live:') ctx.reply('โ๏ธDrink cup of coffee, there are none matches now.๐งฉ');
else ctx.reply(data);
}
const queryForList15 = async (ctx) => {
let data = 'Here is a list of 15 matches: \n';
let matches = await HLTV.getMatches();
for (let i = 0; i < 15; i++) {
if (matches[i].live) data += '\nLive match: ';
else {
let date = new Date(matches[i].date);
data += `\n${date.getDate()}-${_MONTH[date.getMonth()]} [${date.getHours()} : ${date.getMinutes()}] `;
}
try {
data += `๐ฎ${matches[i].team1.name} vs ${matches[i].team2.name}๐ฎ`;
} catch {
data += `๐ข Teams not declared ๐ข`;
}
}
ctx.reply(data);
}
const queryForStars = async (ctx) => {
let data = 'Here is a list of matches:';
let matches = await HLTV.getMatches();
matches.forEach(x => {
if (x.stars > 0 && x.team1 != undefined) {
if (x.live) data += '\nLive match: ';
else {
let date = new Date(x.date);
data += `\n${date.getDate()}-${_MONTH[date.getMonth()]} [${date.getHours()} : ${date.getMinutes()}] `;
}
data += `\n๐ฎ${x.team1.name} vs ${x.team2.name}๐ฎ\n`;
}
})
if (data == 'Here is a list of matches:') ctx.reply('โ๏ธDrink cup of coffee, nothing interesting.๐งฉ');
else ctx.reply(data);
}
const queryForResults = async (ctx) => {
let data = 'Here is a list of matches:';
let results = await HLTV.getResults({ startPage: 0, endPage: 1 });
results.forEach(x => {
if (x.stars > 0) {
data += `\n๐ฎ${x.team1.name} vs ${x.team2.name} ๐ฎ RESULT: ${x.result} ๐ฎ\n`;
}
});
ctx.reply(data);
}
const queryForYearEvents = async (ctx) => {
let data = '๐T O U R N A M E N T S:๐\n\n';
let events = await HLTV.getEvents();
events.forEach(kvartal => {
data += `\n${MONTH[kvartal.month]}:\n`
const eventsArr = kvartal.events;
eventsArr.forEach(event => {
let dateS = new Date(event.dateStart);
let dateE = new Date(event.dateEnd);
data += `๐${event.name}๐ | ${_MONTH[dateS.getMonth()]} ${dateS.getDate()} - ${_MONTH[dateE.getMonth()]} ${dateE.getDate()}\n`;
});
data += '\n';
})
ctx.reply(data);
}
const queryForTopEvents = async (ctx) => {
let data = '๐T O U R N A M E N T S:๐\n\n';
let events = await HLTV.getEvents();
events.forEach(kvartal => {
data += `${MONTH[kvartal.month].toUpperCase()}:\n`
const eventsArr = kvartal.events;
eventsArr.forEach(event => {
if (event.prizePool != 'Other' && event.prizePool.length >= 8) {
let dateS = new Date(event.dateStart);
let dateE = new Date(event.dateEnd);
data += `๐${event.name}๐\n${_MONTH[dateS.getMonth()]} ${dateS.getDate()} - ${_MONTH[dateE.getMonth()]} ${dateE.getDate()} | Prize: ${event.prizePool}๐ฐ\n\n`;
}
});
data += '\n';
})
ctx.reply(data);
}
const queryForTopPlayers = async (ctx) => {
let data = 'Top 10 players this year:\n\n';
let year = new Date().getFullYear();
let dateS = `${year}-01-01`;
let dateE = `${year}-12-31`;
let players = await HLTV.getPlayerRanking({
startDate: dateS,
endDate: dateE,
minMapCount: 1,
rankingFilter: 'Top20'
});
for (let i = 0; i < 10; i++) {
data += `๐
${players[i].name} - ${players[i].teams[0].name}๐\n| [Played Maps]: ${players[i].maps} | [Rating]: ${players[i].rating} |\n\n`
}
ctx.reply(data);
}
const queryForPlayer = (ctx) => {
ctx.reply('Enter "p/" + <player name>\n\nExample: [p/s1mple]');
}
const queryInlineForPlayer = async (ctx) => {
let data;
const searchName = ctx.message.text.split('/')[1];
try {
let player = await HLTV.getPlayerByName({ name: searchName });
let playerStats = player.statistics;
data = `๐ฆ${player.name} [${player.ign}]๐ฆ\n
Team: ${player.team.name}
Country: ${player.country.name} || Age: ${player.age}
๐ซStatistcs:๐ซ
|Rating: ${playerStats.rating} || Kills/Round: ${playerStats.killsPerRound}|
|Headshots: ${playerStats.headshots} || Impact: ${playerStats.rating}|
|Maps: ${playerStats.mapsPlayed} || Death/Round: ${playerStats.deathsPerRound} |\n
https://hltv.org/player/${player.id}/${player.ign}`;
await ctx.replyWithPhoto(player.image);
ctx.reply(data);
} catch {
ctx.reply('๐ฅSorry but we can`t find this player in HLTV data-base');
}
}
const queryForTopTeams = async (ctx) => {
let data = 'Top teams for rating HLTV:\n\n';
let teams = await HLTV.getTeamRanking();
teams.forEach(team => {
let change;
if (team.change == 0) change = '๐';
else if (team.change > 0) change = '๐บ';
else change = '๐ป';
data += `${change} #${team.place} ${team.team.name} ~ Points: ${team.points}\n\n`;
})
ctx.reply(data);
}
const queryForMyTeams = async (ctx) => {
await database.CopyData().then(val => USERS_SUBSCRIPTIONS = val);
let user = await USERS_SUBSCRIPTIONS.find(user => { if (user.userID == ctx.update.callback_query.from.id) return user });
let data = '๐ฅYour favorite teams:๐ฅ\n';
if (user && user.subs.length != 0) {
for (let sub of user.subs) {
let team = await HLTV.getTeam({ id: sub });
data += `${team.name}\n`;
};
}
else data += 'Your list is empty๐ฅ๐ฅ'
data += '\n\nTo add more teams use command /subscribe';
ctx.reply(data);
}
const queryForNews = async (ctx) => {
let news = await getNews();
for (let val of news) {
ctx.reply(`๐${val.title}\n\n${val.description}\n\n๐๐${val.date.slice(5, 22)}๐๐\n${val.link}`)
}
}
const queryForStream = (ctx, message) => {
const matchID = message.slice(8, 15);
ctx.reply(`https://www.hltv.org/live?matchId=${matchID}`);
}
const queryForGit = (ctx) => {
ctx.reply(`https://github.com/gapiyka/HLTV-Bot`);
ctx.replyWithVideo('https://media.giphy.com/media/3wvHzrzgEj5y0vR9ma/giphy.gif');
}
const subFunc = async (bot, ctx, search) => {
await database.CopyData().then(val => USERS_SUBSCRIPTIONS = val);
let user = USERS_SUBSCRIPTIONS.find(user => { if (user.userID == ctx.message.from.id) return user });
let teamID = search.team.id;
if (user == undefined) {
database.AddRow(ctx.message.from.id, teamID);
AddTimer(bot, ctx.message.from.id);
ctx.reply('โ
You add this team to your list of subscriptions');
} else if (user.subs.includes(teamID.toString())) {
ctx.reply('You already subscribed on this team');
} else {
database.ChangeRow(ctx.message.from.id, teamID);
ctx.reply('โ
You add this team to your list of subscriptions');
}
}
const commandForSubscribe = async (bot, ctx, message) => {
let teams = await HLTV.getTeamRanking();
let search = teams.find(team => {
if (team.team.name.split(' ')[0].toLowerCase() == message.split(' ')[1].toLowerCase()) return team;
})
if (search == undefined) ctx.reply('We can`t find this team๐ฅ');
else subFunc(bot, ctx, search);
}
const commandForDelSubscribe = (ctx) => {
database.ClearSubs(ctx.message.from.id);
ctx.reply('Your list of subscriptions is empty . . .๐ฅ');
}
async function CheckOnSub(bot, everyID) {
await database.CopyData().then(val => USERS_SUBSCRIPTIONS = val);
let findMatches = [];
let user = USERS_SUBSCRIPTIONS.find(user => {
if (user.userID == everyID) return user;
});
if (user) {
let matches = (await HLTV.getMatches()).filter(ThisDay);
user.subs.forEach(element => {
matches.find(match => {
if (match.team1 || match.team2) {
let now = Date.parse(new Date());
let dif = Math.round((match.date - now) / 60000);
if ((match.team1.id == element || match.team2.id == element) && dif <= 30) {
bot.telegram.sendMessage(everyID, `๐ ๐ ๐\nMatch: ${match.team1.name} vs ${match.team2.name} will start in ${dif} minutes\n๐ฟ ๐ฟ ๐ฟ`);
}
}
});
});
}
}
module.exports = {
matchfunc,
eventsfunc,
playersfunc,
teamsfunc,
queryInlineForPlayer,
queryForNews,
queryForStream,
queryForGit,
commandForSubscribe,
commandForDelSubscribe,
onCallbackQuery,
GetStickerUrl,
LoadBot,
};