-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtools.js
More file actions
165 lines (141 loc) · 4.56 KB
/
tools.js
File metadata and controls
165 lines (141 loc) · 4.56 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
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
process.env["NTBA_FIX_319"] = 1;
process.env["NTBA_FIX_350"] = 1;
process.on('uncaughtException', function (error) {
console.log("\x1b[31m", "Exception: ", error, "\x1b[0m");
});
process.on('unhandledRejection', function (error, p) {
console.log("\x1b[31m","Error: ", error.message, "\x1b[0m");
});
var config = require('./config.js');
var TelegramBot = require('node-telegram-bot-api');
var mysql = require('mysql');
var mysql_sync = require('sync-mysql');
var express = require('express');
var http = require('http');
var https = require('https');
var fs = require('fs');
var bodyParser = require('body-parser');
var request = require('request');
console.log('Connecting bot...');
var token = config.toolstoken;
var bot = new TelegramBot(token);
var app = express();
var path = "/tools/bot" + token;
var port = 25011;
bot.setWebHook('https://fenixweb.net:8443' + path);
app.listen(port);
app.use(bodyParser.json());
app.post(path, function(req, res) {
bot.processUpdate(req.body);
res.sendStatus(200);
});
console.log('Starting bot...');
var connection = mysql.createConnection({
host: config.dbhost,
user: config.dbuser_pinner,
password: config.dbpassword_pinner,
database: config.dbdatabase_pinner
});
connection.connect();
var connection_sync = new mysql_sync({
host: config.dbhost,
user: config.dbuser_pinner,
password: config.dbpassword_pinner,
database: config.dbdatabase_pinner
});
setInterval(function () {
connection.query('SELECT 1');
connection_sync.query('SELECT 1');
}, 60000);
process.on('SIGINT', function() {
console.log("Shutting down bot...");
connection.end();
process.exit();
});
process.on('SIGTERM', function() {
console.log("Shutting down bot...");
connection.end();
process.exit();
});
var html = {
parse_mode: "HTML"
};
bot.onText(/^\/start/i, function (message) {
bot.sendMessage(message.chat.id, "Questo è il bot personale di @fenix45!\n\nComandi disponibili:\n/rape");
});
bot.onText(/^\/rape/i, function (message) {
var options = {
'method': 'POST',
'url': 'https://api.turnip.exchange/islands/',
'headers': {
'Content-Type': 'application/json',
'User-Agent': 'Fenix Tools 2'
},
body: JSON.stringify({
"islander": "neither",
"category": "turnips",
"fee": 0
})
};
request(options, function (error, response) {
if (error) throw new Error(error);
var json = JSON.parse(response.body);
var islands = json.islands;
var maxValue = 0;
var maxIsland = null;
for (var i = 0; i < islands.length; i++) {
if ((islands[i].turnipPrice > maxValue) &&
(islands[i].queued.split("/")[0] < islands[i].queued.split("/")[1]) &&
(islands[i].patreonOnly == 0)) {
maxValue = islands[i].turnipPrice;
maxIsland = islands[i];
}
}
if (maxIsland != null) {
bot.sendMessage(message.chat.id, "<b>Nome</b>: " + maxIsland.name + "\n" +
"<b>Prezzo Rape</b>: " + maxIsland.turnipPrice + "\n" +
"<b>Descrizione</b>: " + maxIsland.description.replace("<", "").replace(">", "") + "\n" +
"<b>Coda</b>: " + maxIsland.queued + "\n" +
"<a href='https://turnip.exchange/island/" + maxIsland.turnipCode + "'>Accedi all'isola</a>", html);
console.log(maxIsland);
}
});
});
// Functions
function getNow(lang, obj) {
var d = new Date();
obj = typeof obj !== 'undefined' ? obj : false;
var datetime;
if (lang == "it") {
datetime = addZero(d.getDate()) + "/" + addZero(d.getMonth() + 1) + "/" + d.getFullYear() + " " + addZero(d.getHours()) + ':' + addZero(d.getMinutes()) + ':' + addZero(d.getSeconds());
} else if (lang == "en") {
datetime = d.getFullYear() + "-" + addZero(d.getMonth() + 1) + "-" + addZero(d.getDate()) + " " + addZero(d.getHours()) + ':' + addZero(d.getMinutes()) + ':' + addZero(d.getSeconds());
} else
datetime = "Error";
if (obj == true)
datetime = new Date(datetime);
return datetime;
}
function toDate(lang, date) {
var d = new Date(date);
if (typeof date == "object")
d = date;
var datetime = "";
if (lang == "it") {
datetime = addZero(d.getMonth() + 1) + "/" + addZero(d.getDate()) + "/" + d.getFullYear() + " " + addZero(d.getHours()) + ':' + addZero(d.getMinutes()) + ':' + addZero(d.getSeconds());
} else if (lang == "en") {
datetime = d.getFullYear() + "-" + addZero(d.getMonth() + 1) + "-" + addZero(d.getDate()) + " " + addZero(d.getHours()) + ':' + addZero(d.getMinutes()) + ':' + addZero(d.getSeconds());
} else
datetime = "Error";
return datetime;
}
function formatNumber(num) {
return ("" + num).replace(/(\d)(?=(\d\d\d)+(?!\d))/g, function ($1) {
return $1 + "."
});
}
function addZero(i) {
if (i < 10)
i = "0" + i;
return i;
}