forked from danijelthales/yfi-faq-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
902 lines (787 loc) · 40.6 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
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
require("dotenv").config()
const Discord = require("discord.js")
const client = new Discord.Client();
const replaceString = require('replace-string');
const https = require('https');
const redis = require("redis");
let redisClient = null;
var fs = require('fs');
const clientYfiPrice = new Discord.Client();
var gasPrice = 40;
var ethPrice = 360;
var coingeckoUsd;
var coingeckoEth;
var coingeckoBtc;
var binanceUsd;
var kucoinUsd;
let gasSubscribersMap = new Map();
let gasSubscribersLastPushMap = new Map();
console.log("Redis URL:" + process.env.REDIS_URL);
if (process.env.REDIS_URL) {
redisClient = redis.createClient(process.env.REDIS_URL);
redisClient.on("error", function (error) {
console.error(error);
});
redisClient.get("gasSubscribersMap", function (err, obj) {
gasSubscribersMapRaw = obj;
console.log("gasSubscribersMapRaw:" + gasSubscribersMapRaw);
if (gasSubscribersMapRaw) {
gasSubscribersMap = new Map(JSON.parse(gasSubscribersMapRaw));
console.log("gasSubscribersMap:" + gasSubscribersMap);
}
});
redisClient.get("gasSubscribersLastPushMap", function (err, obj) {
gasSubscribersLastPushMapRaw = obj;
console.log("gasSubscribersLastPushMapRaw:" + gasSubscribersLastPushMapRaw);
if (gasSubscribersLastPushMapRaw) {
gasSubscribersLastPushMap = new Map(JSON.parse(gasSubscribersLastPushMapRaw));
console.log("gasSubscribersLastPushMap:" + gasSubscribersLastPushMap);
}
});
}
client.on("ready", () => {
console.log(`Logged in as ${client.user.tag}!`)
})
client.on("guildMemberAdd", function (member) {
const exampleEmbed = new Discord.MessageEmbed()
.setColor('#0099ff')
.setTitle('Welcome');
exampleEmbed.addField('Hello :wave:, welcome to the citadel! :japanese_castle: ',
"If you don't know anything about yearn.finance or YFI please read [this](https://www.learnyearn.finance/start-here/what-is-yearn-yfi) brief explanation first:\n" +
"\n" +
"If you are looking for information regarding Yearn, YFI, and the Y-ecosystem these places are a good starting point:\n" +
"\n" +
"[Official Community Documentation and FAQs](https://docs.yearn.finance/)\n" +
"[Learn Yearn](https://www.learnyearn.finance/)\n" +
"[Official Twitter](https://twitter.com/iearnfinance)\n" +
"[Official Medium](https://medium.com/iearn)\n" +
"\n" +
"If you are looking to get to know the community please join discussions in the appropriate channels.\n" +
"We are happy to have you here! :tada: \n" +
"\n" +
"If you need help or technical support please refer to #:question:support channel.\n" +
"\n" +
"If you want to discuss things related to speculation and price movement of YFI or any asset please refer to #:chart_with_upwards_trend:speculation channel.");
member.send(exampleEmbed);
});
client.on("message", msg => {
if (!msg.author.username.toUpperCase().includes("FAQ")) {
if (!(msg.channel.type == "dm")) {
// this is logic for channels
if (msg.content.toLowerCase().trim() == "!faq") {
msg.reply("Hi, I am yEarn FAQ bot. I will be very happy to assist you, just ask me for **help** in DM.");
} else if (msg.content.toLowerCase().trim() == "!faq help") {
msg.reply("I can only answer a predefined question by its number or by alias in a channel, e.g. **question 1**, or **gas price**. \n For more commands and options send me **help** in DM");
} else if (msg.content.toLowerCase().trim().replace(/ +(?= )/g, '').startsWith("!faq question")) {
doQuestion(msg, "!faq question", false);
} else if (msg.content.toLowerCase().trim().replace(/ +(?= )/g, '').startsWith("!faq show chart")) {
let content = msg.content.toLowerCase().trim().replace(/ +(?= )/g, '');
const args = content.slice("!faq show chart".length).split(' ');
args.shift();
const command = args.shift().trim();
doShowChart(command, msg, false);
} else if (msg.content.toLowerCase().trim().replace(/ +(?= )/g, '').startsWith("!faq ")) {
let found = checkAliasMatching(false);
if (!found) {
msg.reply("Oops, I don't know that one. You can get all aliases if you send me a DM **aliases**");
}
}
} else {
try {
// this is the logic for DM
console.log("I got sent a DM:" + msg.content);
let found = checkAliasMatching(true);
// if alias is found, just reply to it, otherwise continue
if (!found) {
if (msg.content.toLowerCase().trim().replace(/ +(?= )/g, '').startsWith("unsubscribe")) {
gasSubscribersMap.delete(msg.author.id);
gasSubscribersLastPushMap.delete(msg.author.id);
if (process.env.REDIS_URL) {
redisClient.set("gasSubscribersMap", JSON.stringify([...gasSubscribersMap]), function () {
});
redisClient.set("gasSubscribersLastPushMap", JSON.stringify([...gasSubscribersLastPushMap]), function () {
});
}
msg.reply("You are now unsubscribed from gas updates");
} else if (msg.content.toLowerCase().trim().replace(/ +(?= )/g, '').startsWith("subscribe gas")) {
const args = msg.content.toLowerCase().trim().replace(/ +(?= )/g, '').slice("subscribe gas".length).split(' ');
args.shift();
const command = args.shift().trim();
if (command && !isNaN(command)) {
gasSubscribersMap.set(msg.author.id, command);
gasSubscribersLastPushMap.delete(msg.author.id);
msg.reply(" I will send you a message once safe gas price is below " + command + " gwei , and every hour after that that it remains below that level. \nTo change the threshold level for gas price, send me a new subscribe message with the new amount.\n" +
"To unsubscribe, send me another DM **unsubscribe**.");
} else {
msg.reply(command + " is not a proper integer number.");
}
} else if (msg.content.toLowerCase().trim() == "aliases") {
showAllAliases(true);
} else if (msg.content.toLowerCase().trim() == "help") {
doFaqHelp();
} else if (msg.content.startsWith("help ")) {
const args = msg.content.slice("help".length).split(' ');
args.shift();
const command = args.shift().trim();
if (command == "question") {
msg.reply("Choose your question with ***question questionNumber***, e.g. ***question 1***\nYou can get the question number via **list** command");
} else if (command == "category") {
msg.reply("Choose your category with ***category categoryName***, e.g. ***category YFI***\nCategory name is fetched from **categories** command");
} else if (command == "search") {
msg.reply("Search for questions with ***search searchTerm***, e.g. ***search vault***");
} else {
msg.reply("I don't know that one. Try just **help** for known commands");
}
} else if (msg.content.toLowerCase().trim() == "list" || msg.content.toLowerCase().trim() == "questions") {
listQuestions();
} else if (msg.content.toLowerCase().startsWith("question ")) {
console.log("question asked:" + msg.content);
doQuestion(msg, "question", true);
} else if (msg.content == "categories") {
listCategories();
} else if (msg.content.toLowerCase().startsWith("category")) {
const args = msg.content.slice("category".length).split(' ');
args.shift();
const command = args.shift();
let rawdata = fs.readFileSync('categories/categories.json');
let categories = JSON.parse(rawdata);
const exampleEmbed = new Discord.MessageEmbed()
.setColor('#0099ff')
.setTitle('Questions in category ' + command + ':');
let found = false;
categories.forEach(function (category) {
if (category.name == command) {
found = true;
category.questions.forEach(function (question) {
rawdata = fs.readFileSync('questions/' + question + ".txt", "utf8");
exampleEmbed.addField(question, rawdata, false);
});
}
});
if (!found) {
exampleEmbed.addField('\u200b', "That doesn't look like a known category. Use a category name from **categories** command, e.g. **category YFI**");
} else {
exampleEmbed.addField('\u200b', 'Choose your question with e.g. **question 1**');
}
msg.reply(exampleEmbed);
} else if (msg.content.toLowerCase().startsWith("search ")) {
const args = msg.content.slice("search".length).split(' ').slice(1);
const searchWord = msg.content.substring("search".length + 1);
doSearch(searchWord, args);
} else if (msg.content.toLowerCase().trim().replace(/ +(?= )/g, '').startsWith("show chart")) {
let content = msg.content.toLowerCase().trim().replace(/ +(?= )/g, '');
const args = content.slice("show chart".length).split(' ');
args.shift();
const command = args.shift().trim();
doShowChart(command, msg, true);
} else {
if (!msg.author.username.toLowerCase().includes("faq")) {
if (msg.content.endsWith("?")) {
const args = msg.content.substring(0, msg.content.length - 1).split(' ');
const searchWord = msg.content;
doCustomQuestion(searchWord, args);
} else {
msg.reply("Oops, I don't know that one. Try **help** to see what I do know, or if you want to ask a custom question, make sure it ends with a question mark **?**");
}
}
}
}
} catch (e) {
msg.reply("Unknown error ocurred. Try **help** to see what I do know, or if you want to ask a custom question, make sure it ends with a question mark **?**");
}
}
}
function showAllAliases(isDM) {
let rawdata = fs.readFileSync('categories/aliases.json');
let aliases = JSON.parse(rawdata);
let questionMap = new Map();
aliases.forEach(function (alias) {
let aliasQuestion = questionMap.get(alias.number);
if (aliasQuestion) {
aliasQuestion.push(alias.alias);
questionMap.set(alias.number, aliasQuestion);
} else {
let aliasQuestion = new Array();
aliasQuestion.push(alias.alias);
questionMap.set(alias.number, aliasQuestion);
}
});
let exampleEmbed = new Discord.MessageEmbed()
.setColor('#0099ff')
.setTitle('Known aliases')
.setURL('https://github.com/dgornjakovic/yfi-faq-bot');
exampleEmbed.setDescription('Hello, here are the aliases I know:');
let counter = 0;
let pagenumber = 2;
for (let [questionNumber, questions] of questionMap) {
let questionsString = "";
questions.forEach(function (q) {
questionsString += (isDM ? "" : "!faq ") + q + "\n";
})
let rawdata = fs.readFileSync('answers/' + questionNumber + '.json');
let answer = JSON.parse(rawdata);
exampleEmbed.addField(answer.title + ' ' + answer.description, questionsString);
counter++;
if (counter == 10) {
if (isDM) {
msg.reply(exampleEmbed);
} else {
msg.channel.send(exampleEmbed);
}
exampleEmbed = new Discord.MessageEmbed()
.setColor('#0099ff')
.setTitle('Known aliases page ' + pagenumber)
.setURL('https://github.com/dgornjakovic/yfi-faq-bot');
exampleEmbed.setDescription('Hello, here are the aliases I know:');
pagenumber++;
counter = 0;
}
}
if (isDM) {
msg.reply(exampleEmbed);
} else {
msg.channel.send(exampleEmbed);
}
}
function checkAliasMatching(doReply) {
let potentialAlias = msg.content.toLowerCase().replace("!faq", "").trim();
let rawdata = fs.readFileSync('categories/aliases.json');
let aliases = JSON.parse(rawdata);
let found = false;
aliases.forEach(function (alias) {
if (alias.alias.toLowerCase().trim() == potentialAlias) {
found = true;
msg.content = "!faq question " + alias.number;
doQuestion(msg, "!faq question", doReply);
}
});
return found;
}
function doFaqHelp() {
const exampleEmbed = new Discord.MessageEmbed()
.setColor('#0099ff')
.setTitle('Switcheo Frequently Asked Questions')
.setURL('https://docs.yearn.finance/');
exampleEmbed.setDescription('Hello, here is list of commands I know:');
exampleEmbed.addField("list", "Lists all known questions");
exampleEmbed.addField("categories", "Lists all categories of known questions");
exampleEmbed.addField("category categoryName", "Lists all known questions for a given category name, e.g. ** category *YFI* **");
exampleEmbed.addField("question questionNumber", "Shows the answer to the question defined by its number, e.g. ** question *7* **");
exampleEmbed.addField("search searchTerm", "Search all known questions by given search term, e.g. ** search *YFI price* **");
exampleEmbed.addField("aliases", "List all known aliases");
exampleEmbed.addField("subscribe gas gasPrice",
"I will inform you the next time safe gas price is below your target gasPrice, e.g. **subscribe gas 30** will inform you if safe gas price is below 30 gwei");
exampleEmbed.addField("show chart [period]",
"Shows the YFI price chart for the given period, e.g. **show chart 24H**");
exampleEmbed.addField("\u200b", "*Or just ask me a question and I will do my best to find a match for you, e.g. **What is the current gas price?***");
msg.reply(exampleEmbed);
}
function listQuestions() {
let exampleEmbed = new Discord.MessageEmbed()
.setColor('#0099ff')
.setTitle('Frequently Asked Questions')
.setURL('https://docs.yearn.finance/');
fs.readdir('questions', function (err, files) {
if (err) {
console.log("Error getting directory information.")
} else {
let counter = 0;
let pagenumber = 2;
files.sort(function (a, b) {
return a.substring(0, a.lastIndexOf(".")) * 1.0 - b.substring(0, b.lastIndexOf(".")) * 1.0;
});
files.forEach(function (file) {
let rawdata = fs.readFileSync('questions/' + file, "utf8");
exampleEmbed.addField(file.substring(0, file.lastIndexOf(".")), rawdata, false)
counter++;
if (counter == 20) {
msg.reply(exampleEmbed);
exampleEmbed = new Discord.MessageEmbed()
.setColor('#0099ff')
.setTitle('Frequently Asked Questions page ' + pagenumber)
.setURL('https://docs.yearn.finance/');
pagenumber++;
counter = 0;
}
})
}
exampleEmbed.addField('\u200b', 'Choose your question with e.g. **question 1**');
msg.reply(exampleEmbed);
})
}
function listCategories() {
let rawdata = fs.readFileSync('categories/categories.json');
let categories = JSON.parse(rawdata);
const exampleEmbed = new Discord.MessageEmbed()
.setColor('#0099ff')
.setTitle('Categories');
categories.forEach(function (category) {
exampleEmbed.addField(category.name, category.desc, false);
});
exampleEmbed.addField('\u200b', "Choose the category with **category categoryName**, e.g. **category SWTH**");
msg.reply(exampleEmbed);
}
function doSearch(searchWord, args) {
const exampleEmbed = new Discord.MessageEmbed()
.setColor('#0099ff')
.setTitle('Questions found for ***' + searchWord + '***:');
const Match = class {
constructor(title, value) {
this.title = title;
this.value = value;
}
matchedCount = 0;
title;
value;
};
const fullMatches = [];
const partialMatches = [];
fs.readdir('questions', function (err, files) {
if (err) {
console.log("Error getting directory information.")
} else {
files.sort(function (a, b) {
return a.substring(0, a.lastIndexOf(".")) * 1.0 - b.substring(0, b.lastIndexOf(".")) * 1.0;
});
files.forEach(function (file) {
let rawdata = fs.readFileSync('questions/' + file, "utf8");
if (rawdata.includes(searchWord)) {
rawdata = replaceString(rawdata, searchWord, '**' + searchWord + '**');
fullMatches.push(new Match(file.substring(0, file.lastIndexOf(".")), rawdata));
} else {
let matchedCount = 0;
args.sort(function (a, b) {
return a.length - b.length;
});
args.forEach(function (arg) {
if (rawdata.toLowerCase().includes(arg.toLowerCase())) {
rawdata = replaceString(rawdata, arg, '**' + arg + '**');
rawdata = replaceString(rawdata, arg.toLowerCase(), '**' + arg.toLowerCase() + '**');
rawdata = replaceString(rawdata, arg.toUpperCase(), '**' + arg.toUpperCase() + '**');
matchedCount++;
}
});
if (matchedCount > 0) {
let match = new Match(file.substring(0, file.lastIndexOf(".")), rawdata);
match.matchedCount = matchedCount;
partialMatches.push(match);
}
}
})
}
if (fullMatches.length == 0 && partialMatches.length == 0) {
exampleEmbed.setTitle('No questions found for ***' + searchWord + '***. Please refine your search.');
} else {
let counter = 0;
fullMatches.forEach(function (match) {
counter++;
if (counter < 6) {
exampleEmbed.addField(match.title, match.value, false);
}
});
partialMatches.sort(function (a, b) {
return b.matchedCount - a.matchedCount;
});
partialMatches.forEach(function (match) {
counter++;
if (counter < 6) {
exampleEmbed.addField(match.title, match.value, false);
}
});
exampleEmbed.addField('\u200b', 'Choose your question with e.g. **question 1**');
}
msg.reply(exampleEmbed);
})
}
function doCustomQuestion(searchWord, args) {
const exampleEmbed = new Discord.MessageEmbed()
.setColor('#0099ff')
.setTitle('Looks like you asked a custom question. This is the best I could find for your query:');
const Match = class {
constructor(title, value) {
this.title = title;
this.value = value;
}
matchedCount = 0;
title;
value;
};
const fullMatches = [];
const partialMatches = [];
fs.readdir('questions', function (err, files) {
if (err) {
console.log("Error getting directory information.")
} else {
files.sort(function (a, b) {
return a.substring(0, a.lastIndexOf(".")) * 1.0 - b.substring(0, b.lastIndexOf(".")) * 1.0;
});
files.forEach(function (file) {
let rawdata = fs.readFileSync('questions/' + file, "utf8");
if (rawdata.includes(searchWord)) {
rawdata = replaceString(rawdata, searchWord, '**' + searchWord + '**');
fullMatches.push(new Match(file.substring(0, file.lastIndexOf(".")), rawdata));
} else {
args.sort(function (a, b) {
return a.length - b.length;
});
let matchedCount = 0;
args.forEach(function (arg) {
if (rawdata.toLowerCase().includes(arg.toLowerCase())) {
rawdata = replaceString(rawdata, arg, '**' + arg + '**');
rawdata = replaceString(rawdata, arg.toLowerCase(), '**' + arg.toLowerCase() + '**');
rawdata = replaceString(rawdata, arg.toUpperCase(), '**' + arg.toUpperCase() + '**');
matchedCount++;
}
});
if (matchedCount > 0) {
let match = new Match(file.substring(0, file.lastIndexOf(".")), rawdata);
match.matchedCount = matchedCount;
partialMatches.push(match);
}
}
})
}
if (fullMatches.length == 0 && partialMatches.length == 0) {
exampleEmbed.setTitle('No questions found for ***' + searchWord + '***. Please refine your search.');
} else {
let counter = 0;
fullMatches.forEach(function (match) {
counter++;
if (counter < 4) {
exampleEmbed.addField(match.title, match.value, false);
}
});
partialMatches.sort(function (a, b) {
return b.matchedCount - a.matchedCount;
});
partialMatches.forEach(function (match) {
counter++;
if (counter < 4) {
exampleEmbed.addField(match.title, match.value, false);
}
});
exampleEmbed.addField('\u200b', 'Choose your question with e.g. **question 1**');
}
msg.reply(exampleEmbed);
})
}
function doQuestion(msg, toSlice, doReply) {
const args = msg.content.slice(toSlice.length).split(' ');
args.shift();
const command = args.shift();
try {
let rawdata = fs.readFileSync('answers/' + command + '.json');
let answer = JSON.parse(rawdata);
const exampleEmbed = new Discord.MessageEmbed();
exampleEmbed.setColor(answer.color);
exampleEmbed.setTitle(answer.title);
exampleEmbed.setDescription(answer.description);
exampleEmbed.setURL(answer.url);
if (command == "19") {
https.get('https://gasprice.poa.network/', (resp) => {
let data = '';
// A chunk of data has been recieved.
resp.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received. Print out the result.
resp.on('end', () => {
let result = JSON.parse(data);
exampleEmbed.addField("Safe low gas price:", result.slow + ' gwei', false);
exampleEmbed.addField("Standard gas price:", result.standard + ' gwei', false);
exampleEmbed.addField("Fast gas price:", result.fast + ' gwei', false);
exampleEmbed.addField("Instant gas price:", result.instant + ' gwei', false);
if (doReply) {
msg.reply(exampleEmbed);
} else {
msg.channel.send(exampleEmbed);
}
});
}).on("error", (err) => {
console.log("Error: " + err.message);
});
} else if (command == "21") {
exampleEmbed.addField("USD (coingecko)", coingeckoUsd, false);
exampleEmbed.addField("ETH (coingecko):", coingeckoEth, false);
exampleEmbed.addField("BTC (coingecko):", coingeckoBtc, false);
if (doReply) {
msg.reply(exampleEmbed);
} else {
msg.channel.send(exampleEmbed);
}
} else if (command == "20") {
https.get('https://api.coingecko.com/api/v3/coins/ethereum', (resp) => {
let data = '';
// A chunk of data has been recieved.
resp.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received. Print out the result.
resp.on('end', () => {
let result = JSON.parse(data);
exampleEmbed.addField("USD", result.market_data.current_price.usd, false);
exampleEmbed.addField("BTC:", result.market_data.current_price.btc, false);
if (doReply) {
msg.reply(exampleEmbed);
} else {
msg.channel.send(exampleEmbed);
}
});
}).on("error", (err) => {
console.log("Error: " + err.message);
});
} else {
answer.fields.forEach(function (field) {
exampleEmbed.addField(field.title, field.value, field.inline);
});
if (answer.footer.title) {
exampleEmbed.setFooter(answer.footer.title, answer.footer.value);
}
if (answer.image) {
exampleEmbed.attachFiles(['images/' + answer.image])
.setImage('attachment://' + answer.image);
}
if (answer.thumbnail) {
exampleEmbed.attachFiles(['images/' + answer.thumbnail])
.setThumbnail('attachment://' + answer.thumbnail);
}
if (doReply) {
msg.reply(exampleEmbed);
} else {
msg.channel.send(exampleEmbed);
}
}
} catch (e) {
if (doReply) {
msg.reply("Oops, there seems to be something wrong there. \nChoose your question with ***question questionNumber***, e.g. **question 1**\nYou can get the question number via **list**");
} else {
msg.reply("Oops, there seems to be something wrong there. \nChoose your question with ***!FAQ question questionNumber***, e.g. **question 1**\nYou can get the question number if you send me **list** in DM");
}
}
}
}
)
setInterval(function () {
https.get('https://api.coingecko.com/api/v3/coins/ethereum', (resp) => {
let data = '';
// A chunk of data has been recieved.
resp.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received. Print out the result.
resp.on('end', () => {
let result = JSON.parse(data);
ethPrice = result.market_data.current_price.usd;
});
}).on("error", (err) => {
console.log("Error: " + err.message);
});
}, 60 * 1000);
function handleGasSubscription() {
https.get('https://gasprice.poa.network/', (resp) => {
let data = '';
// A chunk of data has been recieved.
resp.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received. Print out the result.
resp.on('end', () => {
let result = JSON.parse(data);
gasPrice = result.standard;
gasSubscribersMap.forEach(function (value, key) {
try {
if ((result.standard * 1.0) < (value * 1.0)) {
if (gasSubscribersLastPushMap.has(key)) {
var curDate = new Date();
var lastNotification = new Date(gasSubscribersLastPushMap.get(key));
var hours = Math.abs(curDate - lastNotification) / 36e5;
if (hours > 1) {
if (client.users.cache.get(key)) {
client.users.cache.get(key).send('gas price is now below your threshold. Current safe gas price is: ' + result.standard);
gasSubscribersLastPushMap.set(key, new Date().getTime());
if (process.env.REDIS_URL) {
redisClient.set("gasSubscribersMap", JSON.stringify([...gasSubscribersMap]), function () {
});
redisClient.set("gasSubscribersLastPushMap", JSON.stringify([...gasSubscribersLastPushMap]), function () {
});
}
} else {
console.log("User:" + key + " is no longer in this server");
gasSubscribersLastPushMap.delete(key);
gasSubscribersMap.delete(key);
if (process.env.REDIS_URL) {
redisClient.set("gasSubscribersMap", JSON.stringify([...gasSubscribersMap]), function () {
});
redisClient.set("gasSubscribersLastPushMap", JSON.stringify([...gasSubscribersLastPushMap]), function () {
});
}
}
} else {
console.log("Not sending a gas notification for: " + key + "because " + lastNotification + " was less than 1 h ago from current date:" + curDate);
}
} else {
if (client.users.cache.get(key)) {
client.users.cache.get(key).send('gas price is now below your threshold. Current safe gas price is: ' + result.standard);
gasSubscribersLastPushMap.set(key, new Date());
if (process.env.REDIS_URL) {
redisClient.set("gasSubscribersMap", JSON.stringify([...gasSubscribersMap]), function () {
});
redisClient.set("gasSubscribersLastPushMap", JSON.stringify([...gasSubscribersLastPushMap]), function () {
});
}
} else {
console.log("User:" + key + " is no longer in this server");
gasSubscribersLastPushMap.delete(key);
gasSubscribersMap.delete(key);
if (process.env.REDIS_URL) {
redisClient.set("gasSubscribersMap", JSON.stringify([...gasSubscribersMap]), function () {
});
redisClient.set("gasSubscribersLastPushMap", JSON.stringify([...gasSubscribersLastPushMap]), function () {
});
}
}
}
} else {
//console.log("Not sending a gas notification for: " + key + " because " + value + " is below gas " + result.standard);
}
} catch (e) {
console.log("Error occured when going through subscriptions for key: " + key + "and value " + value + " " + e);
}
});
});
}).on("error", (err) => {
console.log("Error: " + err.message);
});
}
setInterval(handleGasSubscription, 60 * 1000);
const puppeteer = require('puppeteer');
setInterval(function () {
https.get('https://api.coingecko.com/api/v3/coins/yearn-finance', (resp) => {
let data = '';
// A chunk of data has been recieved.
resp.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received. Print out the result.
resp.on('end', () => {
let result = JSON.parse(data);
coingeckoUsd = result.market_data.current_price.usd;
coingeckoEth = result.market_data.current_price.eth;
coingeckoBtc = result.market_data.current_price.btc;
});
}).on("error", (err) => {
console.log("Error: " + err.message);
});
}, 10 * 1000);
function doShowChart(type, msg, fromDM) {
try {
const exampleEmbed = new Discord.MessageEmbed()
.setColor('#0099ff')
.setTitle(type + ' YFI price chart');
exampleEmbed.addField("Possible options:", "realtime, 24H, 7D, 1M, 3M, 6M, YTD, 1Y, ALL");
exampleEmbed.attachFiles(['charts/chart' + type.toLowerCase() + '.png'])
.setImage('attachment://' + 'chart' + type.toLowerCase() + '.png');
msg.reply(exampleEmbed);
} catch (e) {
console.log("Exception happened when showing the chart");
console.log(e);
}
}
async function getChart(type) {
try {
const browser = await puppeteer.launch({
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
],
});
const page = await browser.newPage();
await page.setViewport({width: 1000, height: 926});
await page.goto("https://coincodex.com/crypto/yearn-finance/?period=" + type, {waitUntil: 'networkidle2'});
await page.waitForSelector('.chart');
const rect = await page.evaluate(() => {
const element = document.querySelector('.chart');
const {x, y, width, height} = element.getBoundingClientRect();
return {left: x, top: y, width, height, id: element.id};
});
await page.screenshot({
path: 'charts/chart' + type.toLowerCase() + '.png',
clip: {
x: rect.left - 0,
y: rect.top - 0,
width: rect.width + 0 * 2,
height: rect.height + 0 * 2
}
});
browser.close();
} catch (e) {
console.log("Error happened on getting chart.");
console.log(e);
}
}
setTimeout(function () {
getChart('realtime');
}, 1 * 1000);
setTimeout(function () {
getChart('24H');
}, 5 * 1000);
setTimeout(function () {
getChart('7D');
}, 10 * 1000);
setTimeout(function () {
getChart('1M');
}, 20 * 1000);
setTimeout(function () {
getChart('3M');
}, 30 * 1000);
setTimeout(function () {
getChart('6M');
}, 40 * 1000);
setTimeout(function () {
getChart('YTD');
}, 50 * 1000);
setTimeout(function () {
getChart('1Y');
}, 60 * 1000);
setTimeout(function () {
getChart('ALL');
}, 70 * 1000);
setInterval(function () {
getChart('realtime');
}, 60 * 3 * 1000);
setInterval(function () {
getChart('24H');
}, 60 * 7 * 1000);
setInterval(function () {
getChart('7D');
}, 60 * 10 * 1000);
setInterval(function () {
getChart('1M');
}, 60 * 20 * 1000);
setInterval(function () {
getChart('3M');
}, 60 * 25 * 1000);
setInterval(function () {
getChart('6M');
}, 60 * 50 * 1000);
setInterval(function () {
getChart('YTD');
}, 60 * 50 * 1000);
setInterval(function () {
getChart('1Y');
}, 60 * 50 * 1000);
setInterval(function () {
getChart('ALL');
}, 60 * 100 * 1000);
setInterval(function () {
clientYfiPrice.guilds.cache.forEach(function (value, key) {
try {
value.members.cache.get("752285072442261525").setNickname("$" + coingeckoUsd);
value.members.cache.get("752285072442261525").user.setActivity("eth=" + coingeckoEth + " btc=" + coingeckoBtc, {type: 'PLAYING'});
} catch (e) {
console.log(e);
}
});
}, 70 * 1000);
client.login(process.env.BOT_TOKEN);
clientYfiPrice.login(process.env.BOT_TOKEN_YFI);