Skip to content

Commit fa56ec9

Browse files
authored
feat(project): refactored function typing (#49)
* chore(typing): added mobizon package * chore(package): updated version * feat(project): refactored function typing
1 parent c82ac1d commit fa56ec9

File tree

10 files changed

+66
-67
lines changed

10 files changed

+66
-67
lines changed

ambient.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
declare module 'qrcode-terminal';
2+
declare module 'mobizon-node';

package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,20 @@
5252
"mobizon-node": "^0.3.1",
5353
"node-base64-image": "^2.0.1",
5454
"qrcode-terminal": "^0.12.0",
55-
"whatsapp-web.js": "^1.12.6"
55+
"whatsapp-web.js": "^1.13.2"
5656
},
5757
"devDependencies": {
58-
"@types/node": "^15.0.1",
58+
"@types/node": "15.14.2",
5959
"@types/axios": "0.14.0",
60-
"@typescript-eslint/eslint-plugin": "4.28.1",
61-
"@typescript-eslint/parser": "4.28.1",
60+
"@typescript-eslint/eslint-plugin": "4.28.3",
61+
"@typescript-eslint/parser": "4.28.3",
6262
"dotenv": "10.0.0",
63-
"eslint": "7.29.0",
63+
"eslint": "7.30.0",
6464
"eslint-config-prettier": "8.3.0",
6565
"eslint-loader": "4.0.2",
6666
"eslint-plugin-prettier": "3.4.0",
6767
"prettier": "2.3.2",
68-
"ts-node-dev": "1.1.7",
69-
"typescript": "4.3.4"
68+
"ts-node-dev": "1.1.8",
69+
"typescript": "4.3.5"
7070
}
7171
}

src/app/commands/CepCommand.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default class EconomyCommand {
88
this.cep = cep;
99
}
1010

11-
async execute(msg: Message) {
11+
async execute(msg: Message): Promise<Message> {
1212
const [_, setCep] = this.cep.split(' ');
1313

1414
const chat = await msg.getChat();

src/app/commands/EconomyCommand.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import axios from 'axios';
22
import type { ICurrency } from '../interfaces/Currency';
33
import type { Message } from 'whatsapp-web.js';
44

5-
export default class EconomyCommand {
6-
async execute(msg: Message) {
5+
export const EconomyCommand = {
6+
async execute(msg: Message): Promise<Message> {
77
const chat = await msg.getChat();
88

99
await chat.sendStateTyping();
@@ -21,5 +21,5 @@ export default class EconomyCommand {
2121
data.BTC,
2222
)}`,
2323
);
24-
}
25-
}
24+
},
25+
};

src/app/commands/ProfileCommand.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export default class ProfileCommand {
88
this.mention = mention;
99
}
1010

11-
async execute(msg: Message) {
11+
async execute(msg: Message): Promise<Message> {
1212
const chat = await msg.getChat();
1313

1414
const [contact] = await msg.getMentions();
@@ -23,11 +23,11 @@ export default class ProfileCommand {
2323

2424
await msg.reply('Stalkeando este contato...');
2525

26-
const url_i = await client.getProfilePicUrl(contact.number);
26+
const uri = await client.getProfilePicUrl(contact.number);
2727

28-
if (!url_i) return msg.reply('Imagem não foi localizada.');
28+
if (!uri) return msg.reply('Imagem não foi localizada.');
2929

30-
const imageProfile = await encode(url_i, { string: true });
30+
const imageProfile = await encode(uri, { string: true });
3131

3232
if (typeof imageProfile === 'string') {
3333
const media = new MessageMedia(

src/app/commands/QuoteCommand.ts

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,39 @@ import { client } from '../../services/whatsapp';
22
import { company } from '../../config/integrantes.json';
33
import type { Message, GroupChat } from 'whatsapp-web.js';
44

5-
export default class QuoteCommand {
6-
async execute(msg: Message) {
7-
const chat: GroupChat = (await msg.getChat()) as any;
8-
const user = await msg.getContact();
9-
10-
const { user: contato } = user.id;
5+
export const QuoteCommand = {
6+
async execute(msg: Message): Promise<Message> {
7+
const chat: Partial<GroupChat> = await msg.getChat();
8+
const {
9+
id: { user: contato },
10+
} = await msg.getContact();
1111

1212
await chat.sendStateTyping();
1313

1414
if (!chat.isGroup) {
1515
return msg.reply('Comando apenas para grupos!');
1616
}
1717

18-
company.map(async ({ numero, admin }) => {
19-
if (numero == contato) {
20-
if (!admin) {
21-
return msg.reply(
22-
'Ops, você não tem permissão para executar este comando!',
23-
);
24-
}
18+
company.forEach(async ({ numero, admin }): Promise<Message> => {
19+
if (numero !== contato) return;
2520

26-
let text = '';
27-
const mentions = [];
21+
if (!admin) {
22+
return msg.reply(
23+
'Ops, você não tem permissão para executar este comando!',
24+
);
25+
}
2826

29-
for (const participant of chat.participants) {
30-
const contact = await client.getContactById(
31-
participant.id._serialized,
32-
);
27+
let text = '';
28+
const mentions = [];
3329

34-
mentions.push(contact);
35-
text += `@${participant.id.user} `;
36-
}
30+
for (const participant of chat.participants) {
31+
const contact = await client.getContactById(participant.id._serialized);
3732

38-
return chat.sendMessage(text, { mentions });
33+
mentions.push(contact);
34+
text += `@${participant.id.user} `;
3935
}
36+
37+
return chat.sendMessage(text, { mentions });
4038
});
41-
}
42-
}
39+
},
40+
};

src/app/commands/SmsCommand.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default class ProfileCommand {
77
this.mention = mention;
88
}
99

10-
async execute(msg: Message) {
10+
async execute(msg: Message): Promise<Message> {
1111
const chat = await msg.getChat();
1212

1313
const [contact] = await msg.getMentions();

src/app/commands/index.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
11
import { commandDispatcher } from '../utils/CommandDispatcher';
2+
import { Message } from 'whatsapp-web.js';
23

3-
import EconomyCommand from './EconomyCommand';
4-
import QuoteCommand from './QuoteCommand';
4+
import { EconomyCommand } from './EconomyCommand';
5+
import { QuoteCommand } from './QuoteCommand';
56
import CepCommand from './CepCommand';
67
import ProfileCommand from './ProfileCommand';
78
import SmsCommand from './SmsCommand';
89

9-
export default async (message) => {
10-
const economyCommand = new EconomyCommand();
11-
const quoteCommand = new QuoteCommand();
12-
const cepCommand = new CepCommand(message.body);
13-
const profileCommand = new ProfileCommand(message.body);
14-
const smsCommand = new SmsCommand(message.body);
10+
export const CommandHandler = async (message: Message): Promise<void> => {
11+
if (!message.body.startsWith('!')) return;
1512

16-
await commandDispatcher.register('cotacao', economyCommand);
17-
await commandDispatcher.register('mencionar', quoteCommand);
18-
await commandDispatcher.register('cep', cepCommand);
19-
await commandDispatcher.register('perfil', profileCommand);
20-
await commandDispatcher.register('sms', smsCommand);
13+
await commandDispatcher.register('cotacao', EconomyCommand);
14+
await commandDispatcher.register('mencionar', QuoteCommand);
15+
await commandDispatcher.register('cep', new CepCommand(message.body));
16+
await commandDispatcher.register('perfil', new ProfileCommand(message.body));
17+
await commandDispatcher.register('sms', new SmsCommand(message.body));
2118

2219
return commandDispatcher.dispatch(message.body.slice(1), message);
2320
};

src/index.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
import { client } from './services/whatsapp';
2-
import type { Message } from 'whatsapp-web.js';
3-
import dispatchCommand from './app/commands';
2+
import { CommandHandler } from './app/commands';
43

5-
client.on('message', (message: Message) => {
6-
if (message.body.startsWith('!')) return dispatchCommand(message);
7-
});
4+
client.on('message_create', CommandHandler);

src/services/whatsapp.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,31 @@
1-
import { Client, MessageMedia } from 'whatsapp-web.js';
1+
import {
2+
Client,
3+
ClientOptions,
4+
ClientSession,
5+
MessageMedia,
6+
} from 'whatsapp-web.js';
27
import { generate } from 'qrcode-terminal';
38
import { existsSync, writeFile, unlink } from 'fs';
49
import { resolve } from 'path';
510

611
const sessionFile = resolve('src', 'data', 'session.json');
7-
const session = existsSync(sessionFile) ? require(sessionFile) : null;
8-
9-
const client = new Client({
12+
const session: ClientSession = existsSync(sessionFile) && require(sessionFile);
13+
const optionsClient: ClientOptions = {
1014
puppeteer: {
1115
headless: true,
1216
args: ['--no-sandbox'],
1317
},
1418
session,
15-
} as any);
19+
};
20+
21+
const client: Client = new Client(optionsClient);
1622

1723
client.on('qr', (qr: string) => {
1824
generate(qr, { small: true });
1925
});
2026

21-
client.on('authenticated', (session: any) => {
22-
writeFile(sessionFile, JSON.stringify(session), (err) => {
27+
client.on('authenticated', (dataSession: ClientSession) => {
28+
writeFile(sessionFile, JSON.stringify(dataSession), (err) => {
2329
if (err) console.log(err);
2430
});
2531
});

0 commit comments

Comments
 (0)