Skip to content

Commit

Permalink
feat(project): refactored function typing (#49)
Browse files Browse the repository at this point in the history
* chore(typing): added mobizon package
* chore(package): updated version
* feat(project): refactored function typing
  • Loading branch information
caioagiani authored Jul 16, 2021
1 parent c82ac1d commit fa56ec9
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 67 deletions.
1 change: 1 addition & 0 deletions ambient.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
declare module 'qrcode-terminal';
declare module 'mobizon-node';
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,20 @@
"mobizon-node": "^0.3.1",
"node-base64-image": "^2.0.1",
"qrcode-terminal": "^0.12.0",
"whatsapp-web.js": "^1.12.6"
"whatsapp-web.js": "^1.13.2"
},
"devDependencies": {
"@types/node": "^15.0.1",
"@types/node": "15.14.2",
"@types/axios": "0.14.0",
"@typescript-eslint/eslint-plugin": "4.28.1",
"@typescript-eslint/parser": "4.28.1",
"@typescript-eslint/eslint-plugin": "4.28.3",
"@typescript-eslint/parser": "4.28.3",
"dotenv": "10.0.0",
"eslint": "7.29.0",
"eslint": "7.30.0",
"eslint-config-prettier": "8.3.0",
"eslint-loader": "4.0.2",
"eslint-plugin-prettier": "3.4.0",
"prettier": "2.3.2",
"ts-node-dev": "1.1.7",
"typescript": "4.3.4"
"ts-node-dev": "1.1.8",
"typescript": "4.3.5"
}
}
2 changes: 1 addition & 1 deletion src/app/commands/CepCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default class EconomyCommand {
this.cep = cep;
}

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

const chat = await msg.getChat();
Expand Down
8 changes: 4 additions & 4 deletions src/app/commands/EconomyCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import axios from 'axios';
import type { ICurrency } from '../interfaces/Currency';
import type { Message } from 'whatsapp-web.js';

export default class EconomyCommand {
async execute(msg: Message) {
export const EconomyCommand = {
async execute(msg: Message): Promise<Message> {
const chat = await msg.getChat();

await chat.sendStateTyping();
Expand All @@ -21,5 +21,5 @@ export default class EconomyCommand {
data.BTC,
)}`,
);
}
}
},
};
8 changes: 4 additions & 4 deletions src/app/commands/ProfileCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default class ProfileCommand {
this.mention = mention;
}

async execute(msg: Message) {
async execute(msg: Message): Promise<Message> {
const chat = await msg.getChat();

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

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

const url_i = await client.getProfilePicUrl(contact.number);
const uri = await client.getProfilePicUrl(contact.number);

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

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

if (typeof imageProfile === 'string') {
const media = new MessageMedia(
Expand Down
48 changes: 23 additions & 25 deletions src/app/commands/QuoteCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,39 @@ import { client } from '../../services/whatsapp';
import { company } from '../../config/integrantes.json';
import type { Message, GroupChat } from 'whatsapp-web.js';

export default class QuoteCommand {
async execute(msg: Message) {
const chat: GroupChat = (await msg.getChat()) as any;
const user = await msg.getContact();

const { user: contato } = user.id;
export const QuoteCommand = {
async execute(msg: Message): Promise<Message> {
const chat: Partial<GroupChat> = await msg.getChat();
const {
id: { user: contato },
} = await msg.getContact();

await chat.sendStateTyping();

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

company.map(async ({ numero, admin }) => {
if (numero == contato) {
if (!admin) {
return msg.reply(
'Ops, você não tem permissão para executar este comando!',
);
}
company.forEach(async ({ numero, admin }): Promise<Message> => {
if (numero !== contato) return;

let text = '';
const mentions = [];
if (!admin) {
return msg.reply(
'Ops, você não tem permissão para executar este comando!',
);
}

for (const participant of chat.participants) {
const contact = await client.getContactById(
participant.id._serialized,
);
let text = '';
const mentions = [];

mentions.push(contact);
text += `@${participant.id.user} `;
}
for (const participant of chat.participants) {
const contact = await client.getContactById(participant.id._serialized);

return chat.sendMessage(text, { mentions });
mentions.push(contact);
text += `@${participant.id.user} `;
}

return chat.sendMessage(text, { mentions });
});
}
}
},
};
2 changes: 1 addition & 1 deletion src/app/commands/SmsCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default class ProfileCommand {
this.mention = mention;
}

async execute(msg: Message) {
async execute(msg: Message): Promise<Message> {
const chat = await msg.getChat();

const [contact] = await msg.getMentions();
Expand Down
23 changes: 10 additions & 13 deletions src/app/commands/index.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
import { commandDispatcher } from '../utils/CommandDispatcher';
import { Message } from 'whatsapp-web.js';

import EconomyCommand from './EconomyCommand';
import QuoteCommand from './QuoteCommand';
import { EconomyCommand } from './EconomyCommand';
import { QuoteCommand } from './QuoteCommand';
import CepCommand from './CepCommand';
import ProfileCommand from './ProfileCommand';
import SmsCommand from './SmsCommand';

export default async (message) => {
const economyCommand = new EconomyCommand();
const quoteCommand = new QuoteCommand();
const cepCommand = new CepCommand(message.body);
const profileCommand = new ProfileCommand(message.body);
const smsCommand = new SmsCommand(message.body);
export const CommandHandler = async (message: Message): Promise<void> => {
if (!message.body.startsWith('!')) return;

await commandDispatcher.register('cotacao', economyCommand);
await commandDispatcher.register('mencionar', quoteCommand);
await commandDispatcher.register('cep', cepCommand);
await commandDispatcher.register('perfil', profileCommand);
await commandDispatcher.register('sms', smsCommand);
await commandDispatcher.register('cotacao', EconomyCommand);
await commandDispatcher.register('mencionar', QuoteCommand);
await commandDispatcher.register('cep', new CepCommand(message.body));
await commandDispatcher.register('perfil', new ProfileCommand(message.body));
await commandDispatcher.register('sms', new SmsCommand(message.body));

return commandDispatcher.dispatch(message.body.slice(1), message);
};
7 changes: 2 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import { client } from './services/whatsapp';
import type { Message } from 'whatsapp-web.js';
import dispatchCommand from './app/commands';
import { CommandHandler } from './app/commands';

client.on('message', (message: Message) => {
if (message.body.startsWith('!')) return dispatchCommand(message);
});
client.on('message_create', CommandHandler);
20 changes: 13 additions & 7 deletions src/services/whatsapp.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
import { Client, MessageMedia } from 'whatsapp-web.js';
import {
Client,
ClientOptions,
ClientSession,
MessageMedia,
} from 'whatsapp-web.js';
import { generate } from 'qrcode-terminal';
import { existsSync, writeFile, unlink } from 'fs';
import { resolve } from 'path';

const sessionFile = resolve('src', 'data', 'session.json');
const session = existsSync(sessionFile) ? require(sessionFile) : null;

const client = new Client({
const session: ClientSession = existsSync(sessionFile) && require(sessionFile);
const optionsClient: ClientOptions = {
puppeteer: {
headless: true,
args: ['--no-sandbox'],
},
session,
} as any);
};

const client: Client = new Client(optionsClient);

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

client.on('authenticated', (session: any) => {
writeFile(sessionFile, JSON.stringify(session), (err) => {
client.on('authenticated', (dataSession: ClientSession) => {
writeFile(sessionFile, JSON.stringify(dataSession), (err) => {
if (err) console.log(err);
});
});
Expand Down

0 comments on commit fa56ec9

Please sign in to comment.