@@ -2,11 +2,21 @@ function isFunction(object) {
2
2
return typeof object === 'function' ;
3
3
}
4
4
5
+ /**
6
+ * Commands wrapper
7
+ * @param {object } commands - Object that contains the commands.
8
+ * @param {object } commands.* - A command. The key and value must be, respectively, the command name and a callback function for the command.
9
+ */
5
10
class Commands {
6
11
constructor ( ) {
7
12
this . commands = { } ;
8
13
}
9
14
15
+ /**
16
+ * Sets a command in Commands instance.
17
+ * @param {string } name - Command's name.
18
+ * @param {function } callback - Callback to command.
19
+ */
10
20
set ( name , callback ) {
11
21
if ( ! isFunction ( callback ) ) {
12
22
throw new Error ( `${ callback } must be a function` ) ;
@@ -15,11 +25,29 @@ class Commands {
15
25
this . commands [ name ] = callback ;
16
26
}
17
27
28
+ /**
29
+ * Checks if a command is set in Commands instance.
30
+ * @param {string } cmd - The command's name.
31
+ * @returns {boolean } `True` if the command is set in Commands instance, `False` if not.
32
+ */
18
33
has ( cmd ) {
19
34
const availableCommands = Object . keys ( this . commands ) ;
20
35
return availableCommands . includes ( cmd ) ;
21
36
}
22
37
38
+ /**
39
+ * Calls (executes) a command.
40
+ * @param {string } cmd - The command's name to be called.
41
+ * @param {object } data - The data extracted from the message that called the command.
42
+ * @param {string } data.command - The command's name extracted from the message.
43
+ * @param {string[] } data.args - The args extracted from the message.
44
+ * @param {object } data.kwargs - The kwargs extracted from the message.
45
+ * @param {string } data.text - The text extracted from the message. This text NOT includes command's name, args and kwargs.
46
+ * @param {Message } message - The message that called the command.
47
+ * @param {Session } client - The whatsapp web session.
48
+ * @see https://docs.wwebjs.dev/Message.html
49
+ * @see https://docs.wwebjs.dev/Client.html
50
+ */
23
51
async call ( cmd , data , message , client ) {
24
52
if ( ! this . has ( cmd ) ) {
25
53
throw new Error ( `${ cmd } is not registered` ) ;
0 commit comments