-
Notifications
You must be signed in to change notification settings - Fork 0
/
commandFactory.js
41 lines (30 loc) · 959 Bytes
/
commandFactory.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const _ = require('lodash');
const commands = require('./commands');
const commandsData = require('./commands/commands.json');
class CommandFactory {
constructor(io) {
this.io = io;
}
create (input) {
if (!input) {
return new commands.EmptyInputCommand(this.io);
}
let cleanInput = input.trim().toLowerCase();
const commandData = _.find(
commandsData,
commandDescription => _.some(commandDescription.respondTo, r => r.toLowerCase() === cleanInput)
);
if (!commandData) {
return new commands.UnknownCommand(this.io);
}
const command = new commands[commandData.command](this.io);
if (!commandData) {
return new commands.UnknownCommand(this.io);
}
return command;
}
getHelpCommand() {
return new commands.HelpCommand(this.io);
}
}
module.exports = CommandFactory;