forked from MattNot/Milo_Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
78 lines (72 loc) · 2.09 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
const Telegraf = require('telegraf')
const request=require('request')
const { reply } = Telegraf
const express= require('express')
const expressApp=express();
const PORT = process.env.PORT || 3000;
const API_TOKEN="";
const URL = process.env.URL || ''
const Cron= require('cron').CronJob
var json=[];
var crono=[];
const bot = new Telegraf(API_TOKEN)
bot.telegram.getMe().then((bot_info)=>{
bot.options.username = bot_info.username;
})
var http = require("http");
setInterval(function() {
http.get("");
}, 300000); // every 5 minutes (300000)
expressApp.use(bot.webhookCallback(`/bot${API_TOKEN}`));
bot.start((ctx) => {
console.log('started:', ctx.from.id)
return ctx.reply('Welcome!')
})
bot.command('help', (ctx) => {
ctx.reply("To watch a round type \"Watch <battleId>\"")
})
bot.hears('Ciao', (ctx) => ctx.reply('Hey bello!'))
bot.on('sticker', (ctx) => ctx.reply('👍'));
bot.hears(/Watch (.+)/,
(match)=>
{
m=match['match'][1];
console.log(m);
if(json[m]==undefined)
{
doRequestAndCron(m, match);
}
else{match.reply('I\'m already watching this battle.')}
}
)
const doRequestAndCron= (m, match) =>
{
request('https://www.cscpro.org/suna/battle/'.concat(m).concat('.json'), (err,response,body)=>
{
json[m]=JSON.parse(body);
if(json[m].error)
{
match.reply("This battle doesn't exist!");
json[m]=undefined;
}else
{
console.log("added ".concat(m));
match.reply("Very well! I'll notice you at t10-5-2 :)")
crono[m]=new Cron('00 * * * * *',()=>{
json[m].time['minute']--;
if(json[m].time['hour']==0 && json[m].time['minute']==10 || json[m].time['minute']==5 || json[m].time['minute']==2)
match.reply('t'.concat(json[m].time['minute']).concat(" of ").concat(json[m].defender['name']).concat(" vs ").concat(json[m].attacker['name']).concat(" in ").concat(json[m].region['name']));
if(json[m].time['hour']==0 && json[m].time['minute']==0)
{
console.log("tempo finito")
crono[m]=crono[m].stop();
json[m]=undefined;
}
},null,true)
}
})
}
bot.startPolling()
expressApp.listen(PORT,()=>{
console.log(`Listening on ${PORT}`)
})