Skip to content

Commit e1d6f46

Browse files
authored
[DOCS] improving some files documentation
[DOCS] improving some files documentation
2 parents e331cfe + cecfd90 commit e1d6f46

File tree

8 files changed

+68
-71
lines changed

8 files changed

+68
-71
lines changed

src/Alice.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
const { Session } = require('./auth');
2-
const { Parse } = require('./utils');
32
const { Commands } = require('./build');
3+
const { Parse } = require('./utils');
44

55
const session = new Session();
66
const commands = new Commands();
77

8+
/**
9+
* @param {object[]} commandsArray
10+
*/
811
class Alice {
912
constructor(commandsArray) {
1013
this.events = [
@@ -24,12 +27,6 @@ class Alice {
2427
}
2528

2629
init() {
27-
if (session.exists) {
28-
session.load();
29-
} else {
30-
session.create();
31-
}
32-
3330
this.events.forEach((e) => {
3431
session.on(e.name, e.callback);
3532
});
@@ -49,6 +46,7 @@ class Alice {
4946

5047
static async onJoinedGroup(notification) {
5148
const contacts = await notification.getRecipients();
49+
5250
contacts.forEach(async (c) => {
5351
const chat = await c.getChat();
5452
await chat.sendMessage(

src/auth/index.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
const { Client } = require('whatsapp-web.js');
2-
const qrcode = require('qrcode-terminal');
31
const fs = require('fs');
42
const path = require('path');
5-
6-
const FILE_NAME = 'session.json';
3+
const qrcode = require('qrcode-terminal');
4+
const { Client } = require('whatsapp-web.js');
75

86
/**
97
* Starting point for interacting with the WhatsApp Web API.
10-
* @param {string} SESSION_FILE_PATH - Path to `session.json` file.
11-
* @see https://docs.wwebjs.dev/Client.html
128
* @extends {Client}
9+
* @see https://docs.wwebjs.dev/Client.html
1310
*/
1411
class Session extends Client {
1512
constructor() {
@@ -19,19 +16,20 @@ class Session extends Client {
1916
},
2017
});
2118

22-
this.SESSION_FILE_PATH = path.join(__dirname, FILE_NAME);
19+
this.SESSION_FILE_PATH = path.join(__dirname, 'session.json');
2320
}
2421

2522
/**
26-
* Checks if the `session.json` file already exists.
27-
* @returns {boolean} - `True` if exists, `False` if not.
23+
* Checks if the `session.json` file exists.
24+
* @returns {boolean} - `true` if exists, `false` if not.
2825
*/
2926
get exists() {
3027
return fs.existsSync(this.SESSION_FILE_PATH);
3128
}
3229

3330
/**
3431
* Throws the QR-Code to authenticate the session. When the QR-Code is read, the session file is written.
32+
* @private
3533
*/
3634
create() {
3735
this.on('qr', (qr) => {
@@ -42,15 +40,16 @@ class Session extends Client {
4240

4341
/**
4442
* Writes the session in a .json file (`this.SESSION_FILE_PATH`)
45-
* @param {object} session - The session file returned in the authentication.
43+
* @private
44+
* @param {object} session - The session object.
4645
*/
4746
save(session) {
4847
fs.writeFileSync(this.SESSION_FILE_PATH, JSON.stringify(session));
49-
console.log('⚠ The current session has been saved ⚠');
5048
}
5149

5250
/**
5351
* Loads the saved session file.
52+
* @private
5453
*/
5554
load() {
5655
if (!this.exists) {
@@ -60,18 +59,21 @@ class Session extends Client {
6059
const raw = fs.readFileSync(this.SESSION_FILE_PATH);
6160
const data = JSON.parse(raw);
6261
this.options.session = data;
63-
64-
console.log('⚠ The previous session was loaded ⚠');
6562
}
6663

6764
/**
68-
* Starts the session.
65+
* Starts the session (Creates it if doesn't exists).
6966
*/
7067
start() {
68+
if (this.exists) {
69+
this.load();
70+
} else {
71+
this.create();
72+
}
73+
7174
this.on('ready', () => {
7275
console.log('Client is ready!');
7376
});
74-
7577
this.initialize();
7678
}
7779
}

src/build/Commands.js

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
/**
2-
* Commands wrapper
3-
* @param {object} commands - Object that contains the commands.
4-
* @param {object} commands.* - A command. The key and value must be, respectively, the command name and a callback function for the command.
2+
* Commands wrapper.
53
*/
64
class Commands {
75
constructor() {
86
this.commands = {};
97
}
108

119
/**
12-
* Sets a command in Commands instance.
13-
* @param {string} name - Command's name.
14-
* @param {function} callback - Callback to command.
10+
* Sets an command in Commands instance.
11+
* @param {object} cmd - A command.
1512
*/
1613
register(cmd) {
1714
if (!Commands.isValid(cmd)) {
@@ -22,19 +19,19 @@ class Commands {
2219
}
2320

2421
/**
25-
* Checks if a command is set in Commands instance.
22+
* Checks if an command is set in Commands instance.
2623
* @param {string} cmdName - The command's name.
27-
* @returns {boolean} `True` if the command is set in Commands instance, `False` if not.
24+
* @returns {boolean} `true` if the command is set in Commands instance, `false` if not.
2825
*/
2926
has(cmdName) {
3027
const availableCommands = Object.keys(this.commands);
3128
return availableCommands.includes(cmdName);
3229
}
3330

3431
/**
35-
* Checks if a command is valid.
32+
* Checks if an command is valid.
3633
* @param {any} cmd - The command instance.
37-
* @returns {boolean} `True` if the command is valid, `False` if not.
34+
* @returns {boolean} `true` if the command is valid, `false` if not.
3835
*/
3936
static isValid(cmd) {
4037
if (cmd.name && cmd.execute && typeof cmd.execute === 'function') {
@@ -45,15 +42,15 @@ class Commands {
4542

4643
/**
4744
* Calls (executes) a command.
45+
* @see https://docs.wwebjs.dev/Message.html
46+
* @see https://docs.wwebjs.dev/Client.html
4847
* @param {object} data - The data extracted from the message that called the command.
49-
* @param {string} data.command - The command's name extracted from the message.
50-
* @param {string[]} data.args - The args extracted from the message.
51-
* @param {object} data.kwargs - The kwargs extracted from the message.
52-
* @param {string} data.text - The text extracted from the message. This text NOT includes command's name, args and kwargs.
48+
* @param {string} data.command - The command's name.
49+
* @param {string[]} data.args - The command's args.
50+
* @param {object} data.kwargs - The command's kwargs.
51+
* @param {string} data.text - The text. This text NOT includes command's name, args and kwargs.
5352
* @param {Message} message - The message that called the command.
5453
* @param {Session} client - The whatsapp web session.
55-
* @see https://docs.wwebjs.dev/Message.html
56-
* @see https://docs.wwebjs.dev/Client.html
5754
*/
5855
async call(data, message, client) {
5956
if (!this.has(data.command)) {

src/commands/cron.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ class Cron {
4848
}
4949

5050
async execute(data, message) {
51-
const isAdm = await chattools.isAdm(message);
51+
const isFromAdm = await chattools.isFromAdm(message);
5252

53-
if (!isAdm) {
53+
if (!isFromAdm) {
5454
message.reply('staff only.');
5555
return;
5656
}

src/commands/everyone.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ class Everyone {
2020

2121
async execute(data, message) {
2222
const { args } = data;
23-
const isAdm = await chattools.isAdm(message);
23+
const isFromAdm = await chattools.isFromAdm(message);
2424

25-
if (!isAdm) {
25+
if (!isFromAdm) {
2626
message.reply('staff only.');
2727
return;
2828
}

src/utils/Command.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Command {
2121
}
2222

2323
/**
24-
* For a given object of information about a command, returns a help message.
24+
* For a given object of information about an command, returns a help message.
2525
* @param {object} commandInfo - Information about the command.
2626
* @param {string} commandInfo.description - The command's description.
2727
* @param {string} commandInfo.usage - The command's usage.

src/utils/Parse.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class Parse {
1515
this.rawText = text.trim();
1616

1717
/**
18-
* A collection of regular expressions used in the extraction of data from the `this.rawText`.
18+
* A collection of regular expressions used in the extraction of data.
1919
* @property {string} REGEXP.command - Regular expression for commands. Ex: !command
2020
* @property {string[]} REGEXP.args - Regular expression for args. Ex: --arg1
2121
* @property {object} REGEXP.kwargs - Regular expression for kwargs. Ex: --kwarg=1
@@ -28,7 +28,7 @@ class Parse {
2828
}
2929

3030
/**
31-
* Gets the command extracted from the `this.rawText`.
31+
* Gets the command extracted.
3232
* @returns {string}
3333
*/
3434
get command() {
@@ -37,7 +37,7 @@ class Parse {
3737
}
3838

3939
/**
40-
* Gets the args extracted from `this.rawText`.
40+
* Gets the args extracted.
4141
* @returns {string[]}
4242
*/
4343
get args() {
@@ -48,7 +48,7 @@ class Parse {
4848
}
4949

5050
/**
51-
* Gets the kwargs extracted from `this.rawText`.
51+
* Gets the kwargs extracted.
5252
* @returns {object}
5353
*/
5454
get kwargs() {
@@ -64,7 +64,7 @@ class Parse {
6464
}
6565

6666
/**
67-
* Gets the text extracted from `this.rawText`.
67+
* Gets the text extracted.
6868
* @returns {string}
6969
*/
7070
get text() {

src/utils/chattools.js

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,55 @@
11
/**
22
* Get serialized phone number from a given array of users.
3-
* @param {Contact[]} users - Whatsapp users.
43
* @see https://docs.wwebjs.dev/Contact.html
4+
* @param {Contact[]} users - Whatsapp users.
55
* @returns {string[]} - Serialized phone numbers.
66
*/
7-
function getSerialList(users) {
7+
function getSerials(users) {
88
// eslint-disable-next-line no-underscore-dangle
9-
const serialList = users.map((u) => u.id._serialized);
10-
return serialList;
9+
const serials = users.map((u) => u.id._serialized);
10+
return serials;
1111
}
1212

1313
/**
1414
* Get serialized phone number of all members from a given group.
15-
* @param {Chat} chat - A whatsapp chat.
1615
* @see https://docs.wwebjs.dev/Chat.html
16+
* @param {Chat} chat - A whatsapp chat.
1717
* @returns {string[]} - Serialized phone numbers of all members.
1818
*/
19-
async function getMembersList(chat) {
19+
async function getMembers(chat) {
2020
const members = await chat.participants;
21-
const membersSerialList = getSerialList(members);
22-
return membersSerialList;
21+
const membersSerials = getSerials(members);
22+
return membersSerials;
2323
}
2424

2525
/**
2626
* Get serialized phone number of all administrators from a given group.
27-
* @param {Chat} chat - A whatsapp chat.
2827
* @see https://docs.wwebjs.dev/Chat.html
28+
* @param {Chat} chat - A whatsapp chat.
2929
* @returns {string[]} - Serialized phone numbers of all administrators.
3030
*/
31-
async function getAdmsList(chat) {
31+
function getAdms(chat) {
3232
if (!chat.isGroup) {
3333
throw new Error(`This chat isn't a group.`);
3434
}
3535

36-
const members = await chat.participants;
37-
const admsIdList = members.filter((id) => id.isAdmin);
38-
const admsSerialList = getSerialList(admsIdList);
39-
return admsSerialList;
36+
const { participants } = chat;
37+
const admsIds = participants.filter((id) => id.isAdmin);
38+
const admsSerials = getSerials(admsIds);
39+
return admsSerials;
4040
}
4141

4242
/**
43-
* Checks if a message is from an ADM.
44-
* @param {Message} message - Message to check if is from an ADM.
43+
* Checks if an message is from an ADM.
4544
* @see https://docs.wwebjs.dev/Message.html
45+
* @param {Message} message - Message to check if is from an ADM.
4646
* @returns {boolean}
4747
*/
48-
async function isAdm(message) {
48+
async function isFromAdm(message) {
4949
const chat = await message.getChat();
50-
const admList = await getAdmsList(chat);
50+
const adms = getAdms(chat);
5151
const { author } = message;
52-
return admList.includes(author);
52+
return adms.includes(author);
5353
}
5454

5555
/**
@@ -70,9 +70,9 @@ function userID(phoneNumber) {
7070
}
7171

7272
module.exports = {
73-
getAdmsList,
74-
getMembersList,
75-
getSerialList,
76-
isAdm,
73+
getAdms,
74+
getMembers,
75+
getSerials,
76+
isFromAdm,
7777
userID,
7878
};

0 commit comments

Comments
 (0)