-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbender.js
357 lines (305 loc) · 10.3 KB
/
bender.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
var command_handler = require("./commandHandlers").command_handler;
var util = require("./util.js").util;
/**
* Make a Bender appear
* @param {RTMClient} rtm - rtm api client
* @param {WebClient} web - web api client
* @param {Object} opts - Bender options
* @param {String} opts.bot_id - Id of the bot
* @param {Object} opts.channel_ids - Ids of channels bot is a member of
* @returns {Bender} gives a Bender
* @constructor
*/
function Bender(rtm, web, opts){
this.rtm = rtm;
this.web = web;
this.bot_id = opts.bot_id;
this.channel_ids = opts.channel_ids;
this.praises = 0;
this.bank = {};
this.heisters= {};
this.games = ["COIN","ROLL"];
this.commands = ["ROLL","JOIN","CHECKBUX","HELP","BET","COMMANDS","COIN","8BALL",
"FORTUNE","PICKIT","PRAISE"];
}
/**
* Bender's welcome handler
* @param {String} id - user id to welcome
*/
Bender.prototype.welcome = function welcome(id){
var bot_msg = "Welcome to our Slack! :congaparrot:\n" +
"Type <@" + this.bot_id + "> COMMANDS to get a list of bot commands!\n" +
"Enjoy! :bender:";
this._postMessage(id, bot_msg, this.channel_ids.general);
}
/**
* Invoke Bender's message handler
* @param {String} msg - incoming message text
* @param {String} user - incoming user
* @param {String} channel - incoming channel
*/
Bender.prototype.bend = function bend(msg, user, channel){
//ignore non messages with no text
if(typeof msg === 'undefined') return;
// console.log(msg);
var proc_msg = this._processMessage(msg.trim());
var bot_msg;
switch(proc_msg.type){
case 0: // HI command
bot_msg = "I'm a Bender. I bend girders. :partyparrot:";
this._postMessage(user, bot_msg, channel);
break;
case 1: // ROLL command
var roll_result = command_handler.rollHandler(proc_msg.die, proc_msg.times);
bot_msg = roll_result.message;
this._postMessage(user, bot_msg, channel);
break;
case 2: // JOIN command
var join_opts = {};
join_opts["channel_ids"] = this.channel_ids;
join_opts["bank"] = this.bank;
var join_result = command_handler.joinHandler(user, channel, join_opts);
bot_msg = join_result.message;
this._postMessage(user, bot_msg, channel);
break;
case 3: // CHECKBUX command
var checkbux_opts = {};
checkbux_opts["channel_ids"] = this.channel_ids;
checkbux_opts["bank"] = this.bank;
checkbux_opts["target"] = proc_msg.user;
var checkbux_result = command_handler.checkbuxHandler(user, checkbux_opts);
bot_msg = checkbux_result.message;
this._postMessage(user, bot_msg, channel);
break;
case 4: // HELP command
var help_opts = {};
help_opts["commands"] = this.commands;
help_opts["games"] = this.games;
help_opts["channel_ids"] = this.channel_ids;
help_opts["bot_id"] = this.bot_id;
var help_result = command_handler.helpHandler(proc_msg.command,
proc_msg.sub_command,
help_opts);
bot_msg = help_result.message;
this._postMessage(user, bot_msg, channel);
break;
case 5: // BET command
var bet_opts = {};
bet_opts["channel_ids"] = this.channel_ids;
bet_opts["game_data"] = proc_msg.game_data;
bet_opts["bank"] = this.bank;
bet_opts["games"] = this.games;
var bet_result = command_handler.betHandler(user, channel, bet_opts);
bot_msg = bet_result.message;
this._postMessage(user, bot_msg, channel);
break;
case 6: // COMMANDS command
bot_msg = "The current supported commands are: ";
bot_msg += this.commands.join(", ");
bot_msg += "\nUse <@" + this.bot_id + "> HELP <command-name> for details"
this._postMessage(user, bot_msg, channel);
break;
case 7: // COIN
var coin_result = command_handler.coinHandler();
bot_msg = coin_result.message;
this._postMessage(user, bot_msg, channel);
break;
case 8: // 8BALL
var eightball_result = command_handler.eightballHandler();
bot_msg = eightball_result.message;
this._postMessage(user, bot_msg, channel);
break;
case 9: // FORTUNE
var fortune_result = command_handler.fortuneHandler();
bot_msg = fortune_result.message;
this._postMessage(user, bot_msg, channel);
break;
case 10: // HEIST
var args = {
"rtm" : this.rtm,
"bank" : this.bank,
"heisters" : this.heisters,
"channel_ids" : this.channel_ids,
"channel" : channel,
"user" : user}
// do this async
//command_handler.heistHandler(args).then(function(result){});
break;
case 11: // PICKIT
var pickit_ops = proc_msg.ops;
var pickit_result = command_handler.pickitHandler(pickit_ops);
bot_msg = pickit_result.message;
this._postMessage(user, bot_msg, channel);
break;
case 12: // PRAISE
var bot_msg = "Bender has been praised " + ++this.praises +
" times since last restart";
this._postMessage(undefined, bot_msg, channel);
break;
case 13: // CHARACTER
bot_msg = command_handler.characterHandler().message;
this._postMessage(user, bot_msg, channel);
break;
default:
if(typeof proc_msg.message !== 'undefined'){
this._postMessage(user, proc_msg.message, channel);
}
return;
}
}
/**
* Process incoming message into command handler friendly objects
* @param {String} msg - incoming message to process
* @returns {Object} - object containing properties required by command handlers
* @private
*/
Bender.prototype._processMessage = function _processMessage(msg){
var result = {};
msg = msg.split(" ");
// Legacy "HI" command
if(msg[0].toUpperCase() === "HI" && msg.length > 1){
if(msg[1].toUpperCase() === "BENDER" || msg[1].includes(this.bot_id) ||
msg[1].toUpperCase() === ":B:" || msg[1].toUpperCase() === ":BENDER:") {
result["type"] = 0;
}
}
// Welcome thing
else if(msg[0].toUpperCase() === "WELCOME" && msg.length == 1){
result["type"] = 11;
}
// Start checking for real commands
else if((msg[0].includes(this.bot_id) || msg[0].toUpperCase() === ":B:" ||
msg[0].toUpperCase() === ":BENDER:") &&
msg.length > 1){
console.log("\nMention detected, checking for command..");
// ROLL command
if((msg[1].toUpperCase() === "ROLL" || msg[1].toUpperCase() === ":GAME_DIE:") &&
msg.length > 2){
console.log("Processing ROLL command...");
result["type"] = 1;
if(msg[2].toUpperCase() === "CHARACTER" || msg[2].toUpperCase() === ":DRAGON:"){
result["type"] = 13;
return result;
}
// this monstrosity hits all d cases
if(/^((:[a-z0-9_-]+:)|\d+)?d((:[a-z0-9_-]+:)|\d+)$/i.test(msg[2])){
var split_roll = util.memeSafeSplit(msg[2].toUpperCase(),"D");
//console.log(split_roll);
if(split_roll[0] === ''){ // dX roll
result["times"] = 1;
}
else{ // YdX roll
result["times"] = split_roll[0];
}
result["die"] = split_roll[1];
}
else{ // non d cases just toss it to the handler
result["die"] = msg[2];
result["times"] = "1";
}
}
// JOIN command
else if(msg[1].toUpperCase() === "JOIN"){
console.log("Processing JOIN command...");
result["type"] = 2;
}
// CHECKBUX command
else if(msg[1].toUpperCase() === "CHECKBUX"){
console.log("Processing CHECKBUX command...");
result["type"] = 3;
if(msg.length > 2){
result["user"] = msg[2];
}
}
// HELP command
else if(msg[1].toUpperCase() === "HELP"){
console.log("Processing HELP command...");
if(msg.length > 2){
result["command"] = msg[2];
}
if(msg.length > 3){
result["sub_command"] = msg[3];
}
result["type"] = 4;
}
// BET command
else if(msg[1].toUpperCase() === "BET"){
console.log("Processing BET command...");
result["type"] = 5;
result["game_data"] = {};
result.game_data["amount"] = msg[2];
result.game_data["game"] = msg[3];
result.game_data["ops"] = {};
for(let i = 4; i < msg.length; i++){
result.game_data.ops[("op" + (i-3))] = msg[i];
}
}
// COMMANDS command
else if(msg[1].toUpperCase() === "COMMANDS"){
console.log("Processing COMMANDS command...");
result["type"] = 6;
}
// COIN command
else if(msg[1].toUpperCase() === 'COIN'){
console.log("Processing COIN command...");
result["type"] = 7;
}
// 8BALL command
else if(msg[1].toUpperCase() === '8BALL' || msg[1].toUpperCase() === ':8BALL:'){
console.log("Processing 8BALL command...");
result["type"] = 8;
}
// FORTUNE command
else if(msg[1].toUpperCase() === 'FORTUNE' ||
msg[1].toUpperCase() === ':FORTUNE:'){
console.log("Processing FORTUNE command...");
result["type"] = 9;
}
// HEIST command
else if(msg[1].toUpperCase() === 'HEIST'){
console.log("Processing HEIST command...");
result["type"] = 10;
}
// PICKIT command
else if(msg[1].toUpperCase() === 'PICKIT'){
console.log("Processing PICKIT command...");
result["type"] = 11;
result["ops"] = [];
for(let i = 2; i < msg.length; i++){
result.ops[i-2] = msg[i];
}
result.ops = util.quoteSafeSplit(result.ops.join(" ")," ");
// console.log(result.ops + "<");
}
// PRAISE command
else if(msg[1].toUpperCase() === 'PRAISE' || msg[1].toUpperCase() === ':PRAY:'){
console.log("Processing PRAISE command...");
result["type"] = 12;
}
// none found
else{
console.log("No command found");
}
}
// Not a command or anything bot cares about
else{
result["type"] = -1;
}
return result;
}
/**
* Make sending messages more simple
* @param {String} user - user to notify
* @param {String} message - message to notify with
* @param {String} channel - where to post
* @private
*/
Bender.prototype._postMessage = function _postMessage(user, message, channel){
if(typeof user !== 'undefined'){
this.rtm.sendMessage("<@" + user + "> " + message, channel);
}
else{
this.rtm.sendMessage(message, channel);
}
}
module.exports.Bender = Bender;