Skip to content

Commit 9e8b0ce

Browse files
authored
Merge pull request #15 from carlos3g/master
[REFACTOR] Code refactor: first phrase
2 parents e23360a + 7acf53e commit 9e8b0ce

30 files changed

+4601
-986
lines changed

.eslintrc.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"env": {
3+
"browser": true,
4+
"commonjs": true,
5+
"es2021": true
6+
},
7+
"extends": ["airbnb-base", "prettier"],
8+
"parserOptions": {
9+
"ecmaVersion": 12
10+
},
11+
"rules": {
12+
"prettier/prettier": "error",
13+
"no-console": "warn",
14+
"no-plusplus": "off"
15+
},
16+
"plugins": ["prettier"]
17+
}

.prettierrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"singleQuote": true,
3+
"trailingComma": "es5",
4+
"printWidth": 80,
5+
"tabWidth": 2
6+
}

alice/index.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
// imports
2-
let src = require('./src')
3-
let build = require('./src/build')
2+
const src = require('./src');
3+
const build = require('./src/build');
44

55
// instance
6-
let path = new build.Path(__dirname)
6+
const path = new build.Path(__dirname);
77

8-
let alice = new src.Alice([
9-
path.create('scripts/bot', alias='bot'),
10-
path.create('scripts/coin', alias='coin'),
11-
path.create('scripts/commands', alias='commands'),
12-
path.create('scripts/cron', alias='cron'),
13-
path.create('scripts/dice', alias='dice'),
14-
path.create('scripts/doc', alias='doc'),
15-
path.create('scripts/doc', alias='help'),
16-
path.create('scripts/github', alias='github'),
17-
path.create('scripts/links', alias='links'),
18-
path.create('scripts/lyric', alias='lyric'),
19-
path.create('scripts/report', alias='report'),
20-
path.create('scripts/search', alias='search'),
21-
path.create('scripts/suggest', alias='suggest'),
22-
path.create('scripts/wiki', alias='wiki')
23-
])
8+
const alice = new src.Alice([
9+
path.create('scripts/bot', 'bot'),
10+
path.create('scripts/coin', 'coin'),
11+
path.create('scripts/commands', 'commands'),
12+
path.create('scripts/cron', 'cron'),
13+
path.create('scripts/dice', 'dice'),
14+
path.create('scripts/doc', 'doc'),
15+
path.create('scripts/doc', 'help'),
16+
path.create('scripts/github', 'github'),
17+
path.create('scripts/links', 'links'),
18+
path.create('scripts/lyric', 'lyric'),
19+
path.create('scripts/report', 'report'),
20+
path.create('scripts/search', 'search'),
21+
path.create('scripts/suggest', 'suggest'),
22+
path.create('scripts/wiki', 'wiki'),
23+
]);
2424

25-
alice.initialize()
25+
alice.initialize();

alice/scripts/bot.js

Lines changed: 16 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

alice/scripts/coin.js

Lines changed: 73 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,101 +1,89 @@
1-
const axios = require('axios')
2-
const cheerio = require('cheerio')
3-
1+
const axios = require('axios');
2+
const cheerio = require('cheerio');
43

54
async function loadCheerio(url) {
6-
try {
7-
let response = await axios.get(url)
8-
let html = response.data
9-
10-
return cheerio.load(html)
11-
}
12-
13-
catch (err) {
14-
console.log('error', err.response.status)
15-
16-
return null
17-
}
5+
try {
6+
const response = await axios.get(url);
7+
const html = response.data;
8+
9+
return cheerio.load(html);
10+
} catch (err) {
11+
console.log('error', err.response.status);
12+
13+
return null;
14+
}
1815
}
1916

2017
async function getData(url) {
21-
let $ = await loadCheerio(url)
22-
if (typeof $ === 'function') {
23-
let priceStatistics = $('.sc-AxhCb.gsRRvB.container___E9axz')
24-
let priceStatisticsTable = priceStatistics.find('table')
25-
let priceStatisticsTableBody = priceStatisticsTable.find('tbody')
26-
let priceStatisticsTableRow = priceStatisticsTableBody.find('tr')
27-
28-
let data = []
29-
priceStatisticsTableRow.each(function() {
30-
let elem = $(this)
31-
32-
let key = elem.find('th').text()
33-
34-
let value = elem.find('td')
35-
if (value.find('span.sc-1v2ivon-0.gClTFY').text()) {
36-
value = value.find('span').first().text()
37-
+ ' || ' +
38-
value.find('span.sc-1v2ivon-0.gClTFY').text()
39-
}
40-
41-
else {
42-
value = value.text()
43-
}
44-
45-
console.log(value)
46-
47-
if (value !== 'No Data' || value !== 'Sem Dados') {
48-
let object = Object.fromEntries([[key, value]])
49-
data.push(object)
50-
}
51-
})
52-
53-
return data
54-
}
18+
const $ = await loadCheerio(url);
19+
if (typeof $ === 'function') {
20+
const priceStatistics = $('.sc-AxhCb.gsRRvB.container___E9axz');
21+
const priceStatisticsTable = priceStatistics.find('table');
22+
const priceStatisticsTableBody = priceStatisticsTable.find('tbody');
23+
const priceStatisticsTableRow = priceStatisticsTableBody.find('tr');
24+
25+
const data = [];
26+
priceStatisticsTableRow.each(function () {
27+
const elem = $(this);
28+
29+
const key = elem.find('th').text();
30+
31+
let value = elem.find('td');
32+
if (value.find('span.sc-1v2ivon-0.gClTFY').text()) {
33+
value = `${value.find('span').first().text()} || ${value
34+
.find('span.sc-1v2ivon-0.gClTFY')
35+
.text()}`;
36+
} else {
37+
value = value.text();
38+
}
5539

56-
return null
40+
console.log(value);
41+
42+
if (value !== 'No Data' || value !== 'Sem Dados') {
43+
const object = Object.fromEntries([[key, value]]);
44+
data.push(object);
45+
}
46+
});
47+
48+
return data;
49+
}
50+
51+
return null;
5752
}
5853

59-
let _default = `
54+
const defaultMessage = `
6055
uso: *!coin* [--flag] name
6156
_--all -> mostra todas as informações disponiveis_
6257
6358
a flag _all_ pode retornar dados em excesso, sua utilização repetida será considera spam
64-
`
59+
`;
6560

66-
module.exports = async function(data) {
67-
let BASE_URL = 'https://coinmarketcap.com/currencies/'
61+
module.exports = async (data) => {
62+
let BASE_URL = 'https://coinmarketcap.com/currencies/';
6863

69-
if (data.args.includes('brl')) {
70-
BASE_URL = 'https://coinmarketcap.com/pt-br/currencies/'
71-
}
72-
73-
if (data.text) {
74-
let text = data.text.replace(/\s/g, '-').toLowerCase()
75-
let url = BASE_URL + text
76-
let coinData = await getData(url)
77-
78-
if (coinData) {
79-
if (!data.args.includes('all'))
80-
coinData = coinData.slice(0, 3)
81-
82-
let coinDataString = ''
83-
coinData.forEach(elem => {
84-
let [key, value] = Object.entries(elem)[0]
85-
86-
let string = `*_${key}_*:\n - ${value}\n\n`
87-
coinDataString += string
88-
})
89-
90-
return coinDataString.trim()
91-
}
92-
93-
else {
94-
return 'moeda não encontrada'
95-
}
96-
}
64+
if (data.args.includes('brl')) {
65+
BASE_URL = 'https://coinmarketcap.com/pt-br/currencies/';
66+
}
67+
68+
if (data.text) {
69+
const text = data.text.replace(/\s/g, '-').toLowerCase();
70+
const url = BASE_URL + text;
71+
let coinData = await getData(url);
72+
73+
if (coinData) {
74+
if (!data.args.includes('all')) coinData = coinData.slice(0, 3);
75+
76+
let coinDataString = '';
77+
coinData.forEach((elem) => {
78+
const [key, value] = Object.entries(elem)[0];
79+
80+
const string = `*_${key}_*:\n - ${value}\n\n`;
81+
coinDataString += string;
82+
});
9783

98-
else {
99-
return _default.trim()
84+
return coinDataString.trim();
10085
}
101-
}
86+
return 'moeda não encontrada';
87+
}
88+
return defaultMessage.trim();
89+
};

alice/scripts/commands.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
module.exports = function () {
2-
let string_output = `
1+
module.exports = () => {
2+
const stringOutput = `
33
Os seguintes comandos estão disponiveis:
44
- !bot
55
- !commands
@@ -13,7 +13,7 @@ module.exports = function () {
1313
- !search
1414
- !suggest
1515
- !wiki
16-
`
16+
`;
1717

18-
return string_output.trim()
19-
}
18+
return stringOutput.trim();
19+
};

0 commit comments

Comments
 (0)