-
Notifications
You must be signed in to change notification settings - Fork 0
/
CryptosINFO.js
388 lines (372 loc) · 12.9 KB
/
CryptosINFO.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
// https://discordapp.com/oauth2/authorize?&client_id=222461168319594498&scope=bot&permissions=0
var Discord = require('discord.io');
var polo = require("poloniex-unofficial");
var request = require('request');
var fs = require('fs');
var util = require('util');
var data = require("./config.json");
trollboxChannel = data.trollboxchannel;
botToken = data.botToken;
// Discord channel id for the trollbox channel probably shouldnt be hardcoded
var poloPublic = new polo.PublicWrapper();
var poloPush = new polo.PushWrapper();
var bot = new Discord.Client({
token: botToken,
autorun: true
});
bot.on('ready', function() {
console.log(bot.username + " - (" + bot.id + ")");
});
bot.on('disconnect', function(errMsg, code){
console.log('---Bot disconnected with code ',code,' for reason ', errMsg,'---');
bot.connect();
});
bot.on('message', function(user, userID, channelID, message, event){
command = message.toLowerCase().split(' ');
// Just for debugging - prints peoples messages
//if (user!='CryptoBot'){
// console.log(user + " : " + userID + " : " + channelID + " : " + message);
//}
if (command[0] == "help" && command.length == 1){
bot.sendMessage({
to: channelID,
message: "These are the commands this bot supports:\nCommand\t\tDescription\nPing\t\tPong"
+"\nCurrencies\t\tReturns list of currencies on Poloniex\nPrice\t\tGets the current price of the currencies provided"
+"\nVolume\t\tGets the 24 hour volume of the currencies provided\nCap/Marketcap\t\tGets the current marketcap and rank of the cryptocurrency"
+"\nConvert\t\tConverts the currency amount to dollars eg: convert 100 xmr / convert xmr 100"
+"\nPepe\t\tOnly the rarest, images can be added with pepe add http://link.com/img.jpg"
});
}
if (command[0] == "ping" && command.length == 1){
bot.sendMessage({
to: channelID,
message: "pong"
});
}
if(command[0]=="troll" && command[0]=="trollbox"){
if(command[1]=="list"){
var message = "Trolls: " + data.polotrolls.join(',') + "\n" + "Following: " + data.polofollow.join(',');
bot.sendMessage({
to: channelID,
message: message
});
}
if(command[1]=="follow"){
if(command[2] != null){
data.polofollow.push(command[2]);
}
}
if(command[1]=="add"){
if(command[2] != null){
data.polotrolls.push(command[2]);
}
}
if(command[1]=="remove" || command[1]=="delete"){
if(command[2] != null){
if(data.polotrolls.indexOf(command[2])>-1){
data.polotrolls.splice(data.polotrolls.indexOf(command[2]),1);
}
if(data.polofollow.indexOf(command[2])>-1){
data.polofollow.splice(data.polofollow.indexOf(command[2]),1);
}
}
}
}
if (command[0] == "rip" && command[1] == "bot"){
bot.sendMessage({
to: channelID,
message: "I'm right here?"
});
}
if (command[0] == "pepe"){
if (command[1] == "add"){
if(command[2] != null){
download(command[2],'pepes');
}
}
else if(command[1] == "list"){
var pepes = fs.readdirSync('pepes');
bot.sendMessage({
to: channelID,
message: "Pepes: "+pepes.toString()
});
}
else if (command[1] == "delete" || command[1] == "remove"){
if (command[2] != null){
path = 'pepes/' + command[2].replace(/\.\.\//g,'');
if (fs.existsSync(path)){
fs.unlinkSync(path);
bot.sendMessage({
to: channelID,
message: "Bad meme "+ path + " successfully deleted"
});
} else {
bot.sendMessage({
to: channelID,
message: "File " + path + " does not exist? Try 'pepe list' to find the filename"
});
}
}
}
else{
var pepes = fs.readdirSync('pepes');
var pepefile = 'pepes/'+pepes[Math.floor(Math.random() * pepes.length)];
bot.uploadFile({
to: channelID,
file: pepefile
});
}
}
if (command[0] == "currencies" && command.length == 1){
poloCurrencies(channelID);
}
if (command[0] == "price"){
tickers = command.slice(1,command.length);
poloPrice(channelID,tickers);
}
if (command[0] == "volume"){
tickers = command.slice(1,command.length);
poloVolume(channelID,tickers);
}
if (command[0] == "cap" || command[0] == "marketcap" ){
tickers = command.slice(1,command.length);
marketCap(channelID,tickers);
}
if(command[0] == "convert" && command.length == 3){
if(isNaN(command[1])){
currency = command[1];
amount = command[2];
} else {
currency = command[2];
amount = command[1];
}
ticker = 'BTC_' + currency.toUpperCase();
poloPublic.returnTicker((err, response) => {
if (err) {
console.log("An error occurred: " + err.msg);
} else {
btc_value = response['USDT_BTC'].last;
if (currency == 'btc'){
bot.sendMessage({
to: channelID,
message: amount + ' BTC is worth $' + amount*btc_value
})
}
if(response[ticker]){
bot.sendMessage({
to: channelID,
message: amount + ' ' + currency + ' is worth $' + amount*response[ticker].last*btc_value
});
}
}
});
}
});
function updateJSON(dataobj){
fs.writeFileSync('./config.json',util.inspect(dataobj), 'utf-8');
data = require("./config.json");
}
function numberWithCommas(x) {
var parts = x.toString().split(".");
parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
return parts.join(".");
}
function download(uri,folder){
filename = folder+'/'+uri.split(/[//]+/).pop();
console.log(filename);
try{
var options = {
uri:uri,
headers:{
'User-Agent':'Mozilla/5.0 (Linux; Android 4.1.2; GT-I9100 Build/JZO54K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.117 Mobile Safari/537.36'
}
};
request.head(options, function(err, res, body){
console.log('content-type:', res.headers['content-type']);
console.log('content-length:', res.headers['content-length']);
request(uri).pipe(fs.createWriteStream(filename));
});
} catch(exception){
console.log(exception);
}
}
function marketCap(channelID, tickers){
var cmc_url = 'https://api.coinmarketcap.com/v1/ticker/';
// https://api.coinmarketcap.com/v1/ticker/
request({
url: cmc_url,
json: true
}, function (error, response, body) {
if (!error && response.statusCode === 200) {
//var cmc_object = JSON.parse(body);
var cmc_object = body;
console.log(cmc_object[0]);
message = '';
for (var key in cmc_object){
if(tickers.indexOf(cmc_object[key].symbol.toLowerCase())>-1){
var cap = numberWithCommas(cmc_object[key].market_cap_usd);
message+=cmc_object[key].symbol+'\t$'+cap+'\tRank: #'+cmc_object[key].rank+'\n';
}
}
console.log('Message: ' + message);
bot.sendMessage({
to: channelID,
message: message
});
}
});
}
function poloVolume(channelID, tickers){
var newtickers = [];
for (var ticker in tickers){
if(tickers[ticker]=='btc'){
newtickers.push("USDT_BTC");
}
else{
newtickers.push("BTC_" + tickers[ticker].toUpperCase());
}
}
tickers = newtickers;
poloPublic.returnTicker((err, response) => {
if (err) {
console.log("An error occurred: " + err.msg);
} else {
//return response;
//console.log(response);
message = '';
for (var key in response){
if(tickers.indexOf(key)>-1){
console.log(key);
message += key.replace(/^[A-Za-z]*_/,'') + '\t' + response[key].baseVolume + '\n';
}
}
console.log(message);
bot.sendMessage({
to: channelID,
message: message
});
}
});
}
function poloPrice(channelID,tickers){
var newtickers = [];
for (var ticker in tickers){
if(tickers[ticker]=='btc'){
newtickers.push("USDT_BTC");
}
else{
newtickers.push("BTC_" + tickers[ticker].toUpperCase());
}
}
tickers = newtickers;
poloPublic.returnTicker((err, response) => {
if (err) {
console.log("An error occurred: " + err.msg);
} else {
//return response;
//console.log(response);
console.log(tickers);
message = '';
btc_value = response['USDT_BTC'].last;
for (var key in response){
if(tickers.indexOf(key)>-1){
ticker_btc_value = response[key].last;
if(key=='USDT_BTC'){
ticker_btc_value = 1;
}
message += key.replace(/^[A-Za-z]*_/,'') + '\t' + ticker_btc_value + ' (' + (response[key].percentChange*100).toFixed(2) + '%)\t$' + (ticker_btc_value*btc_value).toFixed(8) + '\n';
}
}
console.log(message);
bot.sendMessage({
to: channelID,
message: message
});
}
});
}
function poloCurrencies(channelID){
poloPublic.returnCurrencies((err, response) => {
if (err) {
console.log("An error occurred: " + err.msg);
} else {
//return response;
//console.log(response);
message = '';
for (var key in response){
if(message==''){
message = "Poloniex's current currencies: "+key;
}
else{
message += ', ' + key;
}
}
console.log(message);
bot.sendMessage({
to: channelID,
message: message
});
}
});
}
poloPush.trollbox((err, response) => {
if (err) {
// Log error message
console.log("An error occurred: " + err.msg);
// Disconnect
return true;
}
// Log chat message as "[rep] username: message"
//console.log(" [" + response.reputation + "] " + response.username + ": " + response.message);
if(trollboxFilter(response.message)){
if(data.polotrolls.indexOf(response.username.toLowerCase())>-1) {
message = "```css\n" + response.username + ": " + decodeHTML(response.message) + "\n```";
} else if(data.polofollow.indexOf(response.username.toLowerCase())>-1) {
message = "```xl\n" + response.username + ": " + decodeHTML(response.message) + "\n```";
} else {
message = "__**" + response.username + "**__: " + decodeHTML(response.message);
}
bot.sendMessage({
to : trollboxChannel,
message : message
}, function(err,res){
if(res){
if(res.content!=null){
if(res.content.indexOf("```xl")>-1){
bot.pinMessage({
channelID: res.channel_id,
messageID: res.id
});
}
}
}
});
}
});
function trollboxFilter(message){
//returns true if the message is signal, false if its noise
if(message.substring(0,7)=='PRO TIP:' || message.substring(0,8)=='POLO TIP:'){
//Poloniex's Tips
return false;
}
if(message.indexOf("http") !== -1){
//Links, we want those
return true;
}
if(message.length<10){
//Messages this short are likely to be noise
return false;
}
return true;
}
function decodeHTML(message){
return message
.replace(/&/,"&")
.replace(/</,"<")
.replace(/>/,">")
.replace(/"/,"\"")
.replace(/'/,"'")
.replace(/(/,"(")
.replace(/@/,"@")
.replace(/)/,")");
}