-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathbot.js
244 lines (215 loc) · 7.03 KB
/
bot.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
const { Telegraf } = require("telegraf");
const requireDir = require("require-dir");
const sqlite3 = require("sqlite3");
const moment = require("moment-timezone");
//Set default timezone
process.env.TZ = "America/Sao_Paulo";
//Create and connect to DB
const db = new sqlite3.Database("./database.db", (err) => {
if (err) {
console.log(err);
}
});
//Import all models
requireDir("./src/models");
//Import controllers
const GameControler = require("./src/controllers/Game");
const Game = require("./src/controllers/Game");
//Bot token
//Put your telegram bot token here
const token = "";
//Bot instance
const bot = new Telegraf(token);
//Command /comandos
bot.command("comandos", (ctx) => {
try {
console.log(
`${ctx.message.from.username} (${ctx.message.from.first_name}) -> Comando utilizado: /comandos`
);
} catch (error) {
console.log(error);
}
return ctx.reply(`👋 Olá ${
ctx.message.from.first_name
}, abaixo você verá todos meus comandos:
👉 /comandos ➖ Mostrarei todos os comandos com exemplos de como usá-los.
👉 /hoje ➖ Mostrarei todos os jogos que aconteceram e irão acontecer hoje (${moment().format(
"DD/MM/YYYY"
)}).
👉 /amanha ➖ Mostrarei todos os jogos que irão acontecer amanhã (${moment()
.add(1, "d")
.format("DD/MM/YYYY")}).
👉 /jogo ID ➖ Eu irei disponibilizar junto a cada jogo uma identificação (ID) que se trata de um número que servirá para você pesquisar as estatísticas desse jogo utilizando este comando. No lugar de ID coloque a identificação do jogo, como por exemplo: (120) Flamengo x Fluminense, o ID do jogo é 120, logo utilizarei /jogo 120 para ver as estatísticas do mesmo.`);
});
//Command /hoje
bot.command("hoje", (ctx) => {
try {
console.log(
`${ctx.message.from.username} (${ctx.message.from.first_name}) -> Comando utilizado: /hoje`
);
} catch (error) {
console.log(error);
}
const todayDate = moment().format("YYYY-MM-DD");
GameControler.show(todayDate).then((rows) => {
if (rows.length === 0) {
return ctx.reply("😔 Infelizmente não tenho os jogos de hoje.");
}
for (
let index = 0;
index <= Math.ceil(rows.length / 10) * 10;
index += 10
) {
let newRows = rows.slice(index, index + 10);
let message = ``;
for (let index in newRows) {
let game = newRows[index];
message += `⚽️ Jogo ${game.id}\n🏠 ${game.homeName} x 🏃 ${
game.awayName
}\n🗓 ${moment(game.gameDate).format("DD/MM/YYYY HH:mm")}\n\n`;
}
if (newRows.length !== 0) {
ctx.reply(message);
}
}
});
return;
});
//Command /amanha
bot.command("amanha", (ctx) => {
try {
console.log(
`${ctx.message.from.username} (${ctx.message.from.first_name}) -> Comando utilizado: /amanha`
);
} catch (error) {
console.log(error);
}
const tomorrowDate = moment().add(1, "d").format("YYYY-MM-DD");
GameControler.show(tomorrowDate).then((rows) => {
if (rows.length === 0) {
return ctx.reply("😔 Infelizmente não tenho os jogos de amanhã.");
}
for (
let index = 0;
index <= Math.ceil(rows.length / 10) * 10;
index += 10
) {
let newRows = rows.slice(index, index + 10);
let message = ``;
for (let index in newRows) {
let game = newRows[index];
message += `⚽️ Jogo ${game.id}\n🏠 ${game.homeName} x 🏃 ${
game.awayName
}\n🗓 ${moment(game.gameDate).format("DD/MM/YYYY HH:mm")}\n\n`;
}
if (newRows.length !== 0) {
ctx.reply(message);
}
}
});
return;
});
//Command /jogo ID
bot.command("jogo", (ctx) => {
try {
console.log(
`${ctx.message.from.username} (${ctx.message.from.first_name}) -> Comando utilizado: /jogo`
);
} catch (error) {
console.log(error);
}
try {
const gameId = parseInt(
ctx.message.text.replace("/jogo ", "").split(" ")[0].trim()
);
if (Number.isInteger(gameId)) {
GameControler.index(gameId).then((row) => {
if (row === undefined) {
return ctx.reply(`🙄 O jogo ${gameId} não foi encontrado.`);
}
let message = `⚽️ ${row.id} - 🏠 ${row.homeName} x 🏃 ${
row.awayName
} 🗓 ${moment(row.gameDate).format("DD/MM/YYYY HH:mm")}
🏠 Informações do time de casa (${row.homeName}):
📊 Partidas analisadas: ${row.homeAnalyzedGames}
🥅 Gols HT: ${row.homeGoalsHt}
🥅 Gols FT: ${row.homeGoalsFt}
⛳️ Escanteios HT: ${row.homeCornersHt}
⛳️ Escanteios FT: ${row.homeCornersFt}
🥅 Média de gols HT: ${(row.homeGoalsHt / row.homeAnalyzedGames || 0).toFixed(
2
)}
🥅 Média de gols FT: ${(row.homeGoalsFt / row.homeAnalyzedGames || 0).toFixed(
2
)}
⛳️ Média de escanteios HT: ${(
row.homeCornersHt / row.homeAnalyzedGames || 0
).toFixed(2)}
⛳️ Média de escanteios FT: ${(
row.homeCornersFt / row.homeAnalyzedGames || 0
).toFixed(2)}
🏃 Informações do time de fora (${row.awayName}):
📊 Partidas analisadas: ${row.awayAnalyzedGames}
🥅 Gols HT: ${row.awayGoalsHt}
🥅 Gols FT: ${row.awayGoalsFt}
⛳️ Escanteios HT: ${row.awayCornersHt}
⛳️ Escanteios FT: ${row.awayCornersFt}
🥅 Média de gols HT: ${(row.awayGoalsHt / row.awayAnalyzedGames || 0).toFixed(
2
)}
🥅 Média de gols FT: ${(row.awayGoalsFt / row.awayAnalyzedGames || 0).toFixed(
2
)}
⛳️ Média de escanteios HT: ${(
row.awayCornersHt / row.awayAnalyzedGames || 0
).toFixed(2)}
⛳️ Média de escanteios FT: ${(
row.awayCornersFt / row.awayAnalyzedGames || 0
).toFixed(2)}
⚠️ Informações de ambos times:
🥅 Média de gols HT: ${(
parseFloat(
(row.homeGoalsHt / row.homeAnalyzedGames || 0).toFixed(2)
) +
parseFloat((row.awayGoalsHt / row.awayAnalyzedGames || 0).toFixed(2))
).toFixed(2)}
🥅 Média de gols FT: ${(
parseFloat(
(row.homeGoalsFt / row.homeAnalyzedGames || 0).toFixed(2)
) +
parseFloat((row.awayGoalsFt / row.awayAnalyzedGames || 0).toFixed(2))
).toFixed(2)}
⛳️ Média de escanteios HT: ${(
parseFloat(
(row.homeCornersHt / row.homeAnalyzedGames || 0).toFixed(2)
) +
parseFloat(
(row.awayCornersHt / row.awayAnalyzedGames || 0).toFixed(2)
)
).toFixed(2)}
⛳️ Média de escanteios FT: ${(
parseFloat(
(row.homeCornersFt / row.homeAnalyzedGames || 0).toFixed(2)
) +
parseFloat(
(row.awayCornersFt / row.awayAnalyzedGames || 0).toFixed(2)
)
).toFixed(2)}`;
return ctx.reply(message);
});
} else {
return ctx.reply("😑 Você digitou alguma coisa errada, não?");
}
} catch (error) {
console.log(error);
return ctx.reply("🥴 Oops, ocorreu um erro no meu chip.");
}
});
//Initialize bot
bot.launch();
//Close DB
db.close((err) => {
if (err) {
console.log(err);
}
});