Skip to content

Commit 6d2be0f

Browse files
committed
[REFACTOR][BREAKING][WIP] Re-do commands with classes
1 parent 3c33408 commit 6d2be0f

File tree

15 files changed

+270
-187
lines changed

15 files changed

+270
-187
lines changed

index.js

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,29 @@
11
require('dotenv').config();
22
const { Alice } = require('./src/Alice');
3-
const build = require('./src/build');
4-
5-
const path = new build.Path(__dirname);
3+
const {
4+
Bot,
5+
Coin,
6+
Commands,
7+
Doc,
8+
Links,
9+
Lyric,
10+
Report,
11+
Search,
12+
Suggest,
13+
Wiki,
14+
} = require('./src/commands');
615

716
const alice = new Alice([
8-
path.create('src/commands/bot', 'bot'),
9-
path.create('src/commands/coin', 'coin'),
10-
path.create('src/commands/commands', 'commands'),
11-
path.create('src/commands/cron', 'cron'),
12-
path.create('src/commands/dice', 'dice'),
13-
path.create('src/commands/doc', 'doc'),
14-
path.create('src/commands/links', 'links'),
15-
path.create('src/commands/lyric', 'lyric'),
16-
path.create('src/commands/report', 'report'),
17-
path.create('src/commands/search', 'search'),
18-
path.create('src/commands/suggest', 'suggest'),
19-
path.create('src/commands/wiki', 'wiki'),
17+
new Bot(),
18+
new Coin(),
19+
new Commands(),
20+
new Doc(),
21+
new Links(),
22+
new Lyric(),
23+
new Report(),
24+
new Search(),
25+
new Suggest(),
26+
new Wiki(),
2027
]);
2128

2229
alice.initialize();

src/Alice.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class Alice {
1212
};
1313

1414
commandsArray.forEach((cmd) => {
15-
commands.set(...cmd);
15+
commands.register(cmd);
1616
});
1717
}
1818

src/commands/bot.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,13 @@
1-
module.exports = () =>
2-
`Olá, eu sou a Alice. Quer saber mais sobre mim? use o comando !doc`;
1+
class Bot {
2+
constructor() {
3+
this.name = 'bot';
4+
this.defaultMessage =
5+
'Olá, eu sou a Alice. Quer saber mais sobre mim? use o comando !doc';
6+
}
7+
8+
execute(_, message) {
9+
message.reply(this.defaultMessage);
10+
}
11+
}
12+
13+
module.exports = Bot;

src/commands/coin.js

Lines changed: 36 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
const axios = require('axios');
22
const 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-
114
async function loadCheerio(url) {
125
try {
136
const { data } = await axios.get(url);
@@ -63,30 +56,46 @@ function getUrl(args, text) {
6356
return baseURL + path;
6457
}
6558

66-
module.exports = async (data) => {
67-
const { args, text } = data;
59+
class Coin {
60+
constructor() {
61+
this.name = 'coin';
62+
this.defaultMessage = `
63+
uso: *!coin* [--flag] name
64+
_--all -> mostra todas as informações disponiveis_
6865
69-
if (!text) {
70-
return defaultMessage.trim();
66+
a flag _all_ pode retornar dados em excesso, sua utilização repetida será considera spam
67+
`.trim();
7168
}
7269

73-
const url = getUrl(args, text);
74-
let coinStats = await getData(url);
70+
async execute(data, message) {
71+
const { args, text } = data;
7572

76-
if (!coinStats) {
77-
return 'moeda não encontrada';
78-
}
79-
if (!args.includes('all')) {
80-
coinStats = coinStats.slice(0, 3);
81-
}
73+
if (!text) {
74+
message.reply(this.defaultMessage.trim());
75+
return;
76+
}
8277

83-
let output = '';
78+
const url = getUrl(args, text);
79+
let coinStats = await getData(url);
8480

85-
coinStats.forEach((s) => {
86-
const [key, value] = Object.entries(s)[0];
87-
const string = `*_${key}_*:\n - ${value}\n\n`;
88-
output += string;
89-
});
81+
if (!coinStats) {
82+
message.reply('moeda não encontrada');
83+
return;
84+
}
85+
if (!args.includes('all')) {
86+
coinStats = coinStats.slice(0, 3);
87+
}
88+
89+
let output = '';
90+
91+
coinStats.forEach((s) => {
92+
const [key, value] = Object.entries(s)[0];
93+
const string = `*_${key}_*:\n - ${value}\n\n`;
94+
output += string;
95+
});
96+
97+
message.reply(output.trim());
98+
}
99+
}
90100

91-
return output.trim();
92-
};
101+
module.exports = Coin;

src/commands/commands.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
1-
module.exports = () =>
2-
`Os seguintes comandos estão disponiveis:
1+
class Commands {
2+
constructor() {
3+
this.name = 'commands';
4+
this.defaultMessage = `
5+
Os seguintes comandos estão disponiveis:
36
- !bot
47
- !coin
58
- !commands
69
- !cron
7-
- !dice
810
- !doc
9-
- !github
1011
- !links
1112
- !lyric
1213
- !report
1314
- !search
1415
- !suggest
15-
- !wiki`.trim();
16+
- !wiki
17+
`.trim();
18+
}
19+
20+
execute(_, message) {
21+
message.reply(this.defaultMessage);
22+
}
23+
}
24+
25+
module.exports = Commands;

src/commands/cron.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ function toPositiveNumber(value) {
3434

3535
class Cron {
3636
constructor(data, message) {
37+
this.name = 'cron';
3738
this.data = data;
3839
this.text = data.text;
3940
this.message = message;
@@ -46,15 +47,16 @@ class Cron {
4647
this.timer = time.timer(seconds, minutes, hours, days);
4748
}
4849

49-
async init() {
50+
async execute(_, message) {
5051
const { args } = this.data;
5152
const isAdm = await chattools.isAdm(this.message);
5253

5354
if (isAdm) {
54-
return this.runsArg(args);
55+
message.reply(this.runsArg(args));
56+
return;
5557
}
5658

57-
return 'staff only.';
59+
message.reply('staff only.');
5860
}
5961

6062
create() {
@@ -166,7 +168,4 @@ _--d -> define um intervalor de dias_
166168
}
167169
}
168170

169-
module.exports = async (data, message) => {
170-
const cron = new Cron(data, message);
171-
return cron.init();
172-
};
171+
module.exports = Cron;

src/commands/dice.js

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/commands/doc.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@ comando: *!cron*
1919
args: --create, --destroy, --start, --stop
2020
kwargs: --s, --m, --h, --d
2121
descrição: repete uma mensagem a cada determinado periodo de tempo
22-
`,
23-
dice: `
24-
comando: *!dice*
25-
descrição: lanca um dado de rpg e retorna seu valor
2622
`,
2723
doc: `
2824
comando: *!doc*
@@ -56,12 +52,22 @@ descrição: retorna o primeiro resultado de uma pesquisa na wikipedia
5652
`,
5753
};
5854

59-
module.exports = (data) => {
60-
const { args } = data;
55+
class Doc {
56+
constructor() {
57+
this.name = 'doc';
58+
this.strings = strings;
59+
}
60+
61+
execute(data, message) {
62+
const { args } = data;
6163

62-
if (strings[args[0]]) {
63-
return strings[args[0]].trim();
64+
if (strings[args[0]]) {
65+
message.reply(strings[args[0]].trim());
66+
return;
67+
}
68+
69+
message.reply(this.strings.defaultMessage.trim());
6470
}
71+
}
6572

66-
return strings.defaultMessage.trim();
67-
};
73+
module.exports = Doc;

src/commands/index.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* eslint-disable global-require */
2+
3+
module.exports = {
4+
Bot: require('./bot'),
5+
Coin: require('./coin'),
6+
Commands: require('./commands'),
7+
Cron: require('./cron'),
8+
Doc: require('./doc'),
9+
Links: require('./links'),
10+
Lyric: require('./lyric'),
11+
Report: require('./report'),
12+
Search: require('./search'),
13+
Suggest: require('./suggest'),
14+
Wiki: require('./wiki'),
15+
};

src/commands/links.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
module.exports = () =>
2-
`Coding in python:
1+
class Links {
2+
constructor() {
3+
this.name = 'links';
4+
this.defaultMessage = `
5+
Coding in python:
36
https://chat.whatsapp.com/I4IpHC0YFPQLUcGHJeqYdF
47
58
Coding in C/C++:
@@ -22,4 +25,12 @@ https://chat.whatsapp.com/GOXnIXSXEFH7wHvO9aTuFs
2225
2326
Speaking in English:
2427
https://chat.whatsapp.com/EOirNapuFe3CVunBqbwj1Z
25-
`.trim();
28+
`.trim();
29+
}
30+
31+
execute(_, message) {
32+
message.reply(this.defaultMessage);
33+
}
34+
}
35+
36+
module.exports = Links;

0 commit comments

Comments
 (0)