-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.js
More file actions
176 lines (140 loc) · 3.59 KB
/
bot.js
File metadata and controls
176 lines (140 loc) · 3.59 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
var bot = function(){
// the id's of the elements on the page
var _buttonName = "#sayit-button";
var _inputName = "#input";
// the prefixs
var _logPrefix = "BOT ::";
var _botPrefix = "$";
// the original definitions
var _buttonSendCommand = $._data($(_buttonName).get(0), "events").click[0].handler;
var _keypressSendCommand = $._data($(_inputName).get(0), "events").keydown[1].handler;
// whether the bot is enabled or not
var _isEnabled = true;
// the command dictionary
var _commands = {};
// inital configuration for event overriding
var _init = function(){
// re-task the button click event
$(_buttonName).unbind('click');
$(_buttonName).click(_buttonChatCommand);
// now i have to handle the keydown event
$._data($(_inputName).get(0), "events").keydown[1].handler = _keydownChatCommand;
};
// so that i can turn the bot off
var _botOff = function(){
_isEnabled = false;
_status();
};
// so that i can turn the bot on
var _botOn = function(){
_isEnabled = true;
_status();
};
// Fires when the user clicks send
var _buttonChatCommand = function(){
if(_isBotCommand() && _isEnabled){
_processCommand($(_inputName).val());
return;
}
_buttonSendCommand(this);
}
// Fires when the keydown event is fired
var _keydownChatCommand = function(a){
if(_isBotCommand() && _isEnabled)
{
// get the proper keyCode as IE doesn't use a.which
var code = (a.which) ? a.which : a.keyCode;
if(code == 13 && !a.shiftKey)
{
_processCommand($(_inputName).val());
a.preventDefault();
return;
}
_keypressSendCommand(a);
}
else{
_keypressSendCommand(a);
}
}
// determine whether or not this is a bot command
var _isBotCommand = function(){
return $(_inputName).val().indexOf(_botPrefix) === 0;
}
// handle the processing of the command
var _processCommand = function(cmd){
// initial string split
var cmdAndArgs = cmd.split(" ");
// to store the args
var args = new Array();
// rebuild the args array
for(var i = 1; i < cmdAndArgs.length; i++){
args[i - 1] = cmdAndArgs[i];
}
// execute the commands
_execCommand(cmdAndArgs[0].substring(_botPrefix.length).toLowerCase(), args);
// clear the command once we are done
$(_inputName).val("");
}
var _execCommand = function(cmd, args){
// check to see if the command is in the dictionary
if(_commands[cmd])
{
// execute the command
_commands[cmd](args);
}
else{
console.log(_logPrefix + " command not found -> " + cmd);
}
return;
}
// to register a command
var _registerCommand = function(cmds, func){
var arr = cmds.toLowerCase().split(",");
for(var i = 0; i < arr.length; i++)
{
if(!_commands[arr[i]])
{
_commands[arr[i]] = func;
}
else{
console.log(_logPrefix + " cannot register command -> " + arr[i]);
}
}
return;
}
// to unregister a command
var _unregisterCommand = function(cmd){
if(_commands[cmd.toLowerCase()])
{
_commands[cmd.toLowerCase()] = null;
}
return;
}
var _enabled = function()
{
return _isEnabled;
}
// print the status to the console
var _status = function(){
if(_isEnabled){
console.log(_logPrefix + " currently active.");
}
else{
console.log(_logPrefix + " currently inactive.");
}
}
// return all the methods that we want to expose.
return {
exec : _execCommand,
init : _init,
on : _botOn,
off : _botOff,
register : _registerCommand,
status : _status,
unregister : _unregisterCommand
}
}();
// initalise the bot
bot.init();
// register a default command
bot.register("off", function() { bot.off(); });