1
1
const axios = require ( 'axios' ) ;
2
2
const cheerio = require ( 'cheerio' ) ;
3
3
4
- const defaultMessage = `
5
- uso: *!coin* [--flag] name
6
- _--all -> mostra todas as informações disponiveis_
7
-
8
- a flag _all_ pode retornar dados em excesso, sua utilização repetida será considera spam
9
- ` ;
10
-
11
4
async function loadCheerio ( url ) {
12
5
try {
13
6
const { data } = await axios . get ( url ) ;
@@ -17,7 +10,7 @@ async function loadCheerio(url) {
17
10
}
18
11
}
19
12
20
- async function getData ( url ) {
13
+ async function getCoinStats ( url ) {
21
14
const $ = await loadCheerio ( url ) ;
22
15
23
16
if ( ! ( typeof $ === 'function' ) ) {
@@ -30,8 +23,8 @@ async function getData(url) {
30
23
. find ( 'tr' ) ;
31
24
const statsArray = [ ] ;
32
25
33
- priceStatistics . each ( function ( ) {
34
- const tr = $ ( this ) ;
26
+ priceStatistics . each ( ( _ , pS ) => {
27
+ const tr = $ ( pS ) ;
35
28
const key = tr . find ( 'th' ) . text ( ) ;
36
29
let value = tr . find ( 'td' ) ;
37
30
@@ -43,7 +36,7 @@ async function getData(url) {
43
36
value = value . text ( ) ;
44
37
}
45
38
46
- if ( value !== 'No Data' || value !== 'Sem Dados' ) {
39
+ if ( value !== 'No Data' ) {
47
40
const object = Object . fromEntries ( [ [ key , value ] ] ) ;
48
41
statsArray . push ( object ) ;
49
42
}
@@ -52,41 +45,53 @@ async function getData(url) {
52
45
return statsArray ;
53
46
}
54
47
55
- function getUrl ( args , text ) {
56
- let baseURL = 'https://coinmarketcap.com/currencies/' ;
57
- const path = text . replace ( / \s / g, '-' ) . toLowerCase ( ) ;
48
+ class Coin {
49
+ constructor ( ) {
50
+ this . name = 'coin' ;
51
+ this . BASE_URL = 'https://coinmarketcap.com/currencies/' ;
52
+ this . defaultMessage = `
53
+ uso: *!coin* [--flag] name
54
+ _--all -> mostra todas as informações disponiveis_
58
55
59
- if ( args . includes ( 'brl' ) ) {
60
- baseURL = 'https://coinmarketcap.com/pt-br/currencies/' ;
56
+ a flag _all_ pode retornar dados em excesso, sua utilização repetida será considera spam
57
+ ` . trim ( ) ;
61
58
}
62
59
63
- return baseURL + path ;
64
- }
60
+ async execute ( data , message ) {
61
+ const { args , text } = data ;
65
62
66
- module . exports = async ( data ) => {
67
- const { args, text } = data ;
63
+ if ( ! text ) {
64
+ message . reply ( this . defaultMessage ) ;
65
+ return ;
66
+ }
68
67
69
- if ( ! text ) {
70
- return defaultMessage . trim ( ) ;
71
- }
68
+ const url = this . getUrl ( text ) ;
69
+ let coinStats = await getCoinStats ( url ) ;
72
70
73
- const url = getUrl ( args , text ) ;
74
- let coinStats = await getData ( url ) ;
71
+ if ( ! coinStats ) {
72
+ message . reply ( 'Moeda não encontrada.' ) ;
73
+ return ;
74
+ }
75
75
76
- if ( ! coinStats ) {
77
- return 'moeda não encontrada' ;
78
- }
79
- if ( ! args . includes ( 'all' ) ) {
80
- coinStats = coinStats . slice ( 0 , 3 ) ;
81
- }
76
+ if ( ! args . includes ( 'all' ) ) {
77
+ coinStats = coinStats . slice ( 0 , 3 ) ;
78
+ }
82
79
83
- let output = '' ;
80
+ let output = '' ;
84
81
85
- coinStats . forEach ( ( s ) => {
86
- const [ key , value ] = Object . entries ( s ) [ 0 ] ;
87
- const string = `*_${ key } _*:\n - ${ value } \n\n` ;
88
- output += string ;
89
- } ) ;
82
+ coinStats . forEach ( ( s ) => {
83
+ const [ key , value ] = Object . entries ( s ) [ 0 ] ;
84
+ const string = `*_${ key } _*:\n - ${ value } \n\n` ;
85
+ output += string ;
86
+ } ) ;
87
+
88
+ message . reply ( output . trim ( ) ) ;
89
+ }
90
+
91
+ getUrl ( text ) {
92
+ const path = text . replace ( / \s / g, '-' ) . toLowerCase ( ) ;
93
+ return this . BASE_URL + path ;
94
+ }
95
+ }
90
96
91
- return output . trim ( ) ;
92
- } ;
97
+ module . exports = Coin ;
0 commit comments