Skip to content

Commit 1bb8587

Browse files
committed
[REFACTOR] Makes some changes to commands uses classes correctly
1 parent 9dd3632 commit 1bb8587

File tree

5 files changed

+27
-32
lines changed

5 files changed

+27
-32
lines changed

src/commands/coin.js

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ async function loadCheerio(url) {
1010
}
1111
}
1212

13-
async function getData(url) {
13+
async function getCoinStats(url) {
1414
const $ = await loadCheerio(url);
1515

1616
if (!(typeof $ === 'function')) {
@@ -23,8 +23,8 @@ async function getData(url) {
2323
.find('tr');
2424
const statsArray = [];
2525

26-
priceStatistics.each(function () {
27-
const tr = $(this);
26+
priceStatistics.each((_, pS) => {
27+
const tr = $(pS);
2828
const key = tr.find('th').text();
2929
let value = tr.find('td');
3030

@@ -36,7 +36,7 @@ async function getData(url) {
3636
value = value.text();
3737
}
3838

39-
if (value !== 'No Data' || value !== 'Sem Dados') {
39+
if (value !== 'No Data') {
4040
const object = Object.fromEntries([[key, value]]);
4141
statsArray.push(object);
4242
}
@@ -45,20 +45,10 @@ async function getData(url) {
4545
return statsArray;
4646
}
4747

48-
function getUrl(args, text) {
49-
let baseURL = 'https://coinmarketcap.com/currencies/';
50-
const path = text.replace(/\s/g, '-').toLowerCase();
51-
52-
if (args.includes('brl')) {
53-
baseURL = 'https://coinmarketcap.com/pt-br/currencies/';
54-
}
55-
56-
return baseURL + path;
57-
}
58-
5948
class Coin {
6049
constructor() {
6150
this.name = 'coin';
51+
this.BASE_URL = 'https://coinmarketcap.com/currencies/';
6252
this.defaultMessage = `
6353
uso: *!coin* [--flag] name
6454
_--all -> mostra todas as informações disponiveis_
@@ -71,17 +61,18 @@ a flag _all_ pode retornar dados em excesso, sua utilização repetida será con
7161
const { args, text } = data;
7262

7363
if (!text) {
74-
message.reply(this.defaultMessage.trim());
64+
message.reply(this.defaultMessage);
7565
return;
7666
}
7767

78-
const url = getUrl(args, text);
79-
let coinStats = await getData(url);
68+
const url = this.getUrl(text);
69+
let coinStats = await getCoinStats(url);
8070

8171
if (!coinStats) {
82-
message.reply('moeda não encontrada');
72+
message.reply('Moeda não encontrada.');
8373
return;
8474
}
75+
8576
if (!args.includes('all')) {
8677
coinStats = coinStats.slice(0, 3);
8778
}
@@ -96,6 +87,11 @@ a flag _all_ pode retornar dados em excesso, sua utilização repetida será con
9687

9788
message.reply(output.trim());
9889
}
90+
91+
getUrl(text) {
92+
const path = text.replace(/\s/g, '-').toLowerCase();
93+
return this.BASE_URL + path;
94+
}
9995
}
10096

10197
module.exports = Coin;

src/commands/lyric.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@ class Lyric {
3434
const { link } = results[0];
3535
const soup = await makeSoup(link);
3636
const title = soup.find('div', { class: 'cnt-head_title' }).find('h1');
37-
const lyricsDiv = soup.find('div', { class: 'cnt-letra' });
38-
const pArray = lyricsDiv.findAll('p');
39-
const lyric = pArray.map((p) => removeBr(p).text).join('');
40-
const output = `*${title.text}*\n\n${lyric}\n\n_${link}_`;
37+
const lyrics = soup
38+
.find('div', { class: 'cnt-letra' })
39+
.findAll('p')
40+
.map((p) => removeBr(p).text)
41+
.join('');
42+
const output = `*${title.text}*\n\n${lyrics}\n\n_${link}_`;
4143

4244
message.reply(output);
4345
}

src/commands/report.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const { chattools } = require('../utils');
22

3-
const myID = chattools.userID(process.env.REPORT_NUMBER);
43
const strings = {
54
defaultMessage: `
65
uso: _!report [--flag] [descrição]_
@@ -17,12 +16,12 @@ argumentos:
1716
class Report {
1817
constructor() {
1918
this.name = 'report';
19+
this.reportID = chattools.userID(process.env.REPORT_NUMBER);
2020
this.strings = strings;
2121
}
2222

2323
execute(data, message, client) {
2424
const { args, text } = data;
25-
2625
const reportMsg = `⚠️ *${args[0]} report* ⚠️\n\n${text}`;
2726

2827
if (args.length === 0 && text) {
@@ -33,7 +32,7 @@ class Report {
3332
}
3433

3534
if (args.includes('bug') || args.includes('user')) {
36-
client.sendMessage(myID, reportMsg);
35+
client.sendMessage(this.reportID, reportMsg);
3736
message.reply(this.strings[args[0]]);
3837
return;
3938
}

src/commands/search.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ class Search {
2626
message.reply(`Nenhum resultado foi encontrado para: _${text}_`);
2727
}
2828

29-
static formatResult(object) {
30-
const { title, link, snippet } = object;
29+
static formatResult(result) {
30+
const { title, link, snippet } = result;
3131
return `*${title}*\n\n${snippet}\n\n_${link}_`;
3232
}
3333
}

src/commands/suggest.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
const { chattools } = require('../utils');
22

3-
const myID = chattools.userID(process.env.REPORT_NUMBER);
4-
53
class Suggest {
64
constructor() {
75
this.name = 'suggest';
6+
this.reportID = chattools.userID(process.env.REPORT_NUMBER);
87
this.defaultMessage = `
98
uso: _!suggest [--flag] [sugestão]_
109
@@ -19,7 +18,6 @@ argumentos:
1918

2019
execute(data, message, client) {
2120
const { args, text } = data;
22-
2321
const reportMsg = `⚠️ *${args[0]} suggestion* ⚠️\n\n${text}`;
2422

2523
if (args.length === 0 && text) {
@@ -34,7 +32,7 @@ argumentos:
3432
args.includes('remove') ||
3533
args.includes('change')
3634
) {
37-
client.sendMessage(myID, reportMsg);
35+
client.sendMessage(this.reportID, reportMsg);
3836
message.reply('Obrigado pela colaboração!');
3937
return;
4038
}

0 commit comments

Comments
 (0)