11const axios = require ( 'axios' ) ;
22const cheerio = require ( 'cheerio' ) ;
33
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-
114async function loadCheerio ( url ) {
125 try {
136 const { data } = await axios . get ( url ) ;
@@ -17,7 +10,7 @@ async function loadCheerio(url) {
1710 }
1811}
1912
20- async function getData ( url ) {
13+ async function getCoinStats ( url ) {
2114 const $ = await loadCheerio ( url ) ;
2215
2316 if ( ! ( typeof $ === 'function' ) ) {
@@ -30,8 +23,8 @@ async function getData(url) {
3023 . find ( 'tr' ) ;
3124 const statsArray = [ ] ;
3225
33- priceStatistics . each ( function ( ) {
34- const tr = $ ( this ) ;
26+ priceStatistics . each ( ( _ , pS ) => {
27+ const tr = $ ( pS ) ;
3528 const key = tr . find ( 'th' ) . text ( ) ;
3629 let value = tr . find ( 'td' ) ;
3730
@@ -43,7 +36,7 @@ async function getData(url) {
4336 value = value . text ( ) ;
4437 }
4538
46- if ( value !== 'No Data' || value !== 'Sem Dados' ) {
39+ if ( value !== 'No Data' ) {
4740 const object = Object . fromEntries ( [ [ key , value ] ] ) ;
4841 statsArray . push ( object ) ;
4942 }
@@ -52,41 +45,53 @@ async function getData(url) {
5245 return statsArray ;
5346}
5447
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_
5855
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 ( ) ;
6158 }
6259
63- return baseURL + path ;
64- }
60+ async execute ( data , message ) {
61+ const { args , text } = data ;
6562
66- module . exports = async ( data ) => {
67- const { args, text } = data ;
63+ if ( ! text ) {
64+ message . reply ( this . defaultMessage ) ;
65+ return ;
66+ }
6867
69- if ( ! text ) {
70- return defaultMessage . trim ( ) ;
71- }
68+ const url = this . getUrl ( text ) ;
69+ let coinStats = await getCoinStats ( url ) ;
7270
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+ }
7575
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+ }
8279
83- let output = '' ;
80+ let output = '' ;
8481
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+ }
9096
91- return output . trim ( ) ;
92- } ;
97+ module . exports = Coin ;
0 commit comments