-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcommand_handler.js
More file actions
94 lines (84 loc) · 3.35 KB
/
command_handler.js
File metadata and controls
94 lines (84 loc) · 3.35 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
const Discord = require('discord.js');
const Command = require('./command');
/**
* The base of all command handlers
*/
module.exports = class CommandHandlerBase {
/**
* @param {String} commandCategory String sent by user that will invoke this command handler
*/
constructor(commandCategory) {
this.commandCategory = commandCategory;
this.simpleCommands = new Map(); //String and string
this.commands = new Map(); //String and Command
this.addCommand("help", "", "", this.sendHelpList.bind(this));
}
/**
* Returns true if this class is the command handler trying to be accessed
* @param {String} id A string that is the first part of a message
*/
isCommand(id) {
return id === this.commandCategory;
}
/**
* Handles a command
* @param {Discord Text Message} message The raw message
* @param {[String]} args The "args" of the command
*/
handleCommand(message, args, client) {
const command = args[0];
if (this.simpleCommands.has(command)) {
message.channel.send(this.simpleCommands.get(command).action);
return;
}
if (this.commands.has(command)) {
args.splice(0, 1);//remove command name
const cmd = this.commands.get(command);
cmd.action(message, args, client);
}
}
/**
* Add a command which has the response of an simple message
* @param {String} commandName The ID/ name of the command that will invoke this
* @param {String} description Description of what the command does
* @param {String} example Example useage of the command
* @param {String} action The string that is sent by the bot in response
*/
addBasicCommand(commandName, description, action) {
const fullExample = `>${this.commandCategory} ${commandName}`;
this.simpleCommands.set(commandName, new Command(description, fullExample, action));
return this;
}
/**
* Add a command which has the response of a function call
* @param {String} commandName The ID/ name of the command that will invoke this
* @param {String} description Description of what the command does
* @param {String} example Example useage of the command
* @param {function(messageInfo)} action Function that is called in response the command. This must take in a 3 args, (message, args, client)
*/
addCommand(commandName, description, example, action) {
this.commands.set(commandName, new Command(description, example, action));
return this;
}
/**
* [Command] Sends help list
* @param {MessageInfo} msgInfo Information about the message
*/
sendHelpList(msgInfo) {
let output = new Discord.RichEmbed()
.setTitle("Commands for " + this.commandCategory.toUpperCase())
.setColor("#09f228");
function addOutput(m) {
m.forEach((command, commandName, _) => {
if (commandName === "help") {
return;
}
output.addField(`**__${commandName}__**`,
`Description: ${command.description}\nExample: *${command.example}*`);
});
}
addOutput(this.simpleCommands);
addOutput(this.commands);
msgInfo.channel.send(output);
}
};