Skip to content

Commit fbce136

Browse files
committed
[FEAT] adds and uses Commands.isValid
1 parent 8ad1c47 commit fbce136

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

src/build/Commands.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,38 @@ class Commands {
1414
* @param {function} callback - Callback to command.
1515
*/
1616
register(cmd) {
17+
if (!Commands.isValid(cmd)) {
18+
throw new Error(`${cmd} isn't a valid Command!`);
19+
}
20+
1721
this.commands[cmd.name] = cmd;
1822
}
1923

2024
/**
2125
* Checks if a command is set in Commands instance.
22-
* @param {string} cmd - The command's name.
26+
* @param {string} cmdName - The command's name.
2327
* @returns {boolean} `True` if the command is set in Commands instance, `False` if not.
2428
*/
25-
has(cmd) {
29+
has(cmdName) {
2630
const availableCommands = Object.keys(this.commands);
27-
return availableCommands.includes(cmd);
31+
return availableCommands.includes(cmdName);
32+
}
33+
34+
/**
35+
* Checks if a command is valid.
36+
* @param {any} cmd - The command instance.
37+
* @returns {boolean} `True` if the command is valid, `False` if not.
38+
*/
39+
static isValid(cmd) {
40+
if (cmd.name && cmd.execute && typeof cmd.execute === 'function') {
41+
return true;
42+
}
43+
return false;
2844
}
2945

3046
/**
3147
* Calls (executes) a command.
32-
* @param {string} cmd - The command's name to be called.
48+
* @param {string} cmdName - The command's name to be called.
3349
* @param {object} data - The data extracted from the message that called the command.
3450
* @param {string} data.command - The command's name extracted from the message.
3551
* @param {string[]} data.args - The args extracted from the message.

0 commit comments

Comments
 (0)