Skip to content

Commit 10a5035

Browse files
authored
feat(project): refact dispatch command (#92)
1 parent f497c42 commit 10a5035

File tree

6 files changed

+30
-33
lines changed

6 files changed

+30
-33
lines changed

.github/FUNDING.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
github: [caioagiani]
12
custom: ['https://nubank.com.br/pagar/3lf5k/hgTQsUWzua']

.gitignore

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
.env
2-
32
dist
43
node_modules
5-
6-
yarn.lock
7-
4+
*.lock
85
src/config/integrantes.json
9-
106
.idea
11-
12-
.wwebjs_auth
7+
.wwebjs_auth
8+
src/data/*
9+
!src/data/.gitkeep

package.json

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@
22
"name": "whatsapp-web-bot",
33
"main": "src/index.ts",
44
"description": "WhatsApp Bot",
5-
"version": "0.6.5",
5+
"version": "0.7.0",
66
"license": "MIT",
77
"keywords": [
88
"whatsapp",
99
"whatsapp-web",
1010
"whatsapp-bot",
1111
"whatsapp-api",
12+
"whatsapp-multi-session",
1213
"whatsapp-multi-device",
1314
"api",
1415
"bot",
1516
"client",
1617
"node",
18+
"nodejs",
1719
"typescript"
1820
],
1921
"bugs": {
@@ -54,21 +56,21 @@
5456
"mobizon-node": "^0.5.0",
5557
"node-base64-image": "^2.0.1",
5658
"qrcode-terminal": "^0.12.0",
57-
"whatsapp-web.js": "^1.16.7"
59+
"whatsapp-web.js": "^1.17.1"
5860
},
5961
"devDependencies": {
60-
"@types/node": "16.11.41",
61-
"@types/qrcode": "1.4.2",
62+
"@types/node": "16.11.49",
63+
"@types/qrcode": "1.4.3",
6264
"@types/qrcode-terminal": "0.12.0",
63-
"@typescript-eslint/eslint-plugin": "5.28.0",
64-
"@typescript-eslint/parser": "5.28.0",
65+
"@typescript-eslint/eslint-plugin": "5.33.1",
66+
"@typescript-eslint/parser": "5.33.1",
6567
"dotenv": "16.0.1",
66-
"eslint": "8.18.0",
68+
"eslint": "8.22.0",
6769
"eslint-config-prettier": "8.5.0",
6870
"eslint-loader": "4.0.2",
69-
"eslint-plugin-prettier": "4.0.0",
71+
"eslint-plugin-prettier": "4.2.1",
7072
"prettier": "2.7.1",
71-
"ts-node-dev": "1.1.8",
73+
"ts-node-dev": "2.0.0",
7274
"typescript": "4.7.4"
7375
}
7476
}

src/app/utils/CommandDispatcher.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,15 @@
11
import Command from './Command';
22

33
class CommandDispatcher {
4-
private commandsHandlers: Map<string, Command<any>> = new Map();
4+
private commandsHandlers: Map<string, Command<string>> = new Map();
55

6-
async register(name: string, command: Command<any>) {
6+
async register(name: string, command: Command<string>) {
77
this.commandsHandlers.set(name, command);
88
}
99

10-
async dispatch(name: string, message: any) {
11-
for (const [key, _] of this.commandsHandlers) {
12-
if (name.includes(key)) {
13-
if (!this.commandsHandlers.has(key)) {
14-
return;
15-
}
16-
17-
const command = this.commandsHandlers.get(key);
18-
await command.execute(message);
19-
}
10+
async dispatch(name: string, message: string) {
11+
for (const [key, command] of this.commandsHandlers) {
12+
if (name.includes(key)) command.execute(message);
2013
}
2114
}
2215
}

src/data/.gitkeep

Whitespace-only changes.

src/services/whatsapp.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
import { Client, MessageMedia, LocalAuth } from 'whatsapp-web.js';
22
import * as qrcode from 'qrcode-terminal';
3+
import { resolve } from 'path';
34

45
const client = new Client({
5-
authStrategy: new LocalAuth(),
6+
authStrategy: new LocalAuth({
7+
clientId: 'wpp-bot',
8+
dataPath: resolve(__dirname, '..', 'data'),
9+
}),
610
puppeteer: {
7-
headless: true,
11+
headless: false,
812
args: ['--no-sandbox'],
913
},
1014
});
1115

1216
client.on('qr', async (qr) => qrcode.generate(qr, { small: true }));
13-
client.on('authenticated', () => console.log('authenticated'));
14-
client.on('auth_failure', () => console.log('Authentication failed.'));
17+
client.on('authenticated', () => console.log('WhatsApp authenticated.'));
18+
client.on('auth_failure', () => console.log('WhatsApp authentication failed.'));
1519
client.on('disconnected', () => console.log('WhatsApp lost connection.'));
1620
client.on('ready', async () => {
1721
await client.sendMessage(
1822
19-
`[${client.info.pushname}] - WhatsApp Online\n[x] Please, *like* project: https://github.com/caioagiani/whatsapp-bot`,
23+
`[${client.info.pushname}] - WhatsApp Online\n\n[⭐] Please *like* this project: https://github.com/caioagiani/whatsapp-bot\n\n[💝] Sponsor this SourceCode: https://github.com/sponsors/caioagiani`,
2024
);
2125

2226
console.log('WhatsApp bot successfully connected!');

0 commit comments

Comments
 (0)