forked from Steam-Chat-Bot/node-steam-chat-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatBot.js
More file actions
911 lines (799 loc) · 32.9 KB
/
Copy pathchatBot.js
File metadata and controls
911 lines (799 loc) · 32.9 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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
var ver = "2.1.1"; //update this before pushing to master.
var fs = require("fs");
var steam = require("steam");
var winston = require("winston");
var _ = require("underscore");
var SteamTrade = require('steam-trade');
var http = require("http");
var Express = require('express');
var expressWinston = require('express-winston');
var TriggerFactory = require("./triggerFactory.js").TriggerFactory;
var qs = require('qs');
var serversFile = "servers";
// Load latest servers from file
if (fs.existsSync(serversFile)) {
try {
winston.silly("Attempting to read servers file");
steam.servers = JSON.parse(fs.readFileSync(serversFile));
} catch(err) {
winston.error("Error reading serversFile:");
winston.error(err.stack);
}
} else {
winston.warn("No servers file found, using defaults");
}
// Bot should be usually created without options, it is a parameter mainly for testing
var ChatBot = function(username, password, options) {
this.version = ver;
this.winston = new winston.Logger;
this.options = options || {};
this.steamClient = options.client || new steam.SteamClient();
this.steamTrade = new SteamTrade();
this.name = options.name || username;
this.username = username;
this.password = password;
if(options.guardCode) {
this.guardCode = options.guardCode;
} else {
this.guardCode = false;
}
this.sentryFile = undefined;
this.games = [];
this.logFile = undefined;
this.cookie = undefined;
this.ignores = options.ignores || [];
this.startTime = process.hrtime();
this.logoffTime = process.hrtime();
this.autojoinFile = options.autojoinFile || "bot." + this.name+".autojoin"; this.autojoinRooms = undefined;
this.winston.add(winston.transports.Console,{
handleExceptions: false,
colorize: options.consoleColors||true,
timestamp: options.consoleTime||true,
level: options.consoleLogLevel||"info",
json: false
});
var that = this;
if(!options.logFile || options.logFile !== false || options.logFile === null || options.logFile === undefined) {
if(options.logFile === true) { that.logFile = "bot." + that.name + ".log"; }
else { that.logFile = options.logFile; }
that.winston.info("Chatbot "+that.name+" logging output to: " + that.logFile);
that.winston.add(
winston.transports.File, {
level: options.logLevel || "info",
colorize:false,
timestamp:true,
filename:that.logFile,
json:false
}
);
}
if(options.sentryFile) {
this.sentryFile = options.sentryFile;
if(fs.existsSync(options.sentryFile)) {
this.winston.info("Chatbot "+that.name+" using sentryfile as defined by options: "+options.sentryfile);
} else {
this.winston.warn("Chatbot "+that.name+": Sentry file defined in config does not exist. "
+ options.sentryFile + " will be created on successful login");
}
} else {
this.winston.info("Chatbot "+that.name+": Config file does not define a sentryfile. Attempting to autodetect sentry...");
var sentryFile = _.find([
"sentry."+this.name+".hash",
"sentry."+this.name,
"sentryfile."+this.name,
"sentryfile."+this.name+".hash",
"bot."+this.name+".hash",
"bot."+this.name+".sentry",
this.name+".hash",
this.name+".sentry"
], function(val) {
return fs.existsSync(val);
});
if ("undefined" !== typeof sentryFile) {
this.sentryFile = sentryFile;
this.winston.info("Chatbot "+that.name+": Sentry file "+this.sentryFile+" detected.");
} else if(fs.existsSync("sentry")) {
this.sentryFile = "sentry";
this.winston.warn("Chatbot "+that.name+": Sentry file sentry detected, but it may be incorrect as it does not claim a username. If correct, please rename to bot."
+ this.name + ".sentry or another supported sentry filename, or rename and define file name in config.");
} else {
this.sentryFile = "bot."+this.name+".sentry";
this.winston.warn("Chatbot "+that.name+": Could not detect a sentryfile, and no guardCode defined. Using default sentryFile "
+ this.sentryFile + ". I hope you have a guardCode defined or steamguard disabled. ");
}
}
this.connected = false; // Bot is connected to the steam network
this.muted = false; // Should not send any messages to a chat room when muted
this.winston.silly("Starting webserver");
if(!options.disableWebserver) {
this._startWebServer(options.webServerPort||8080)
}
this.winston.silly("Starting triggerFactory");
this.triggers = {};
if (options.triggerFactory) {
this.triggerFactory = options.triggerFactory;
} else {
this.triggerFactory = new TriggerFactory();
}
this.unmutedState = steam.EPersonaState.Online;
this.mutedState = steam.EPersonaState.Snooze;
if(options.onLogon instanceof Function) {
this.onLogon = options.onLogon;
}
// Connect relevant events
this.steamClient.on("error", function(error) { that._onError(error); });
this.steamClient.on("loggedOn", function() { that._onLoggedOn(); that.onLogon(that); that.triggerLoggedOn(); });
this.steamClient.on("loggedOff", function() { that._onDisconnected(); that.triggerLoggedOff(); });
this.steamClient.on("chatInvite", function(roomId, roomName, inviterId) { that._onChatInvite(roomId, roomName, inviterId); });
this.steamClient.on("friend", function(userId, relationship) { that._onRelationship(userId, relationship); });
this.steamClient.on("friendMsg", function(userId, message, type) { that._onFriendMsg(userId, message, type); });
this.steamClient.on("chatMsg", function(roomId, message, type, chatterId) { that._onChatMsg(roomId, message, type, chatterId); });
this.steamClient.on("chatStateChange", function(stateChange, chatterActedOn, steamChatId, actedOnBy) { that._onChatStateChange(stateChange, chatterActedOn, steamChatId, actedOnBy); });
this.steamClient.on("sentry", function(sentry) { that._onSentry(sentry); });
this.steamClient.on('webSessionID', function(sessionID) { that._onWebSessionID(sessionID); });
this.steamClient.on('tradeOffers', function(number) { that._onTradeOffers(number); });
this.steamClient.on('tradeProposed', function(tradeID, steamID) { that._onTradeProposed(tradeID, steamID, that.acceptTrade); });
this.steamClient.on('tradeResult', function(tradeID, result, steamID) { that._onTradeResult(tradeID, result, steamID) });
this.steamClient.on('sessionStart', function(steamID) { that._onSessionStart(steamID) });
this.steamClient.on('announcement', function(groupID, headline) { that._onAnnouncement(groupID, headline); });
/*
// These events exist in node-steam, but are only half-implemented in this file...
this.steamClient.on("user", function(obscureData) {that._onUser(obscureData); });
this.steamClient.on("group", function(group, clanRelationship) {that._onGroup(group, clanRelationship); });
this.steamClient.on("richPresence", function(steamId,status,obscureData) {that._onRichPresence(steamId,status,obscureData); });
*/
// Store latest servers
this.steamClient.on("servers", function(servers) {
try{
that.winston.silly("Chatbot "+that.name+": Attempting to write servers file");
fs.writeFileSync(serversFile, JSON.stringify(servers));
} catch(err) {
that.winston.error("Chatbot "+that.name+": Error writing servers file:");
that.winston.error(err.stack);
}
});
if (options.autoConnect) {
this.connect();
}
};
// Public interface
ChatBot.prototype.onLogon = function(bot) {
return;
}
ChatBot.prototype.connect = function() {
// Continuously try to reconnect if started but not connected
// If someone logs in as the bot it will be disconnected, so this allows the bot to recover automatically when it can
if (this.options.autoReconnect && !this.babysitInterval) {
var babysitTimer = this.options.babysitTimer || 5*60*1000;
var that = this;
this.babysitInterval = setInterval(function() { that.connect(); }, babysitTimer);
}
if (!this.connected) {
this.winston.info("Chatbot "+this.name+": Trying to connect");
try {
if(this.guardCode) {
this.steamClient.logOn({
accountName: this.username,
password: this.password,
authCode: this.guardCode,
shaSentryfile: (fs.existsSync(this.sentryFile) ? fs.readFileSync(this.sentryFile) : undefined)
});
} else {
this.steamClient.logOn({
accountName: this.username,
password: this.password,
shaSentryfile: (fs.existsSync(this.sentryFile) ? fs.readFileSync(this.sentryFile) : undefined)
});
}
} catch (err) {
this.winston.error("Chatbot "+this.name+": Exception trying to connect", err);
}
}
}
ChatBot.prototype.log = function(){};
ChatBot.prototype.mute = function() {
this.muted = true;
this._updatePersonaState();
}
ChatBot.prototype.unmute = function() {
this.muted = false;
this._updatePersonaState();
}
// Run the .onLoggedOff function for each trigger
ChatBot.prototype.triggerLoggedOff = function() {
if(!this.triggers || this.triggers.length === 0) {
return;
}
var that = this;
this.logoffTime = process.hrtime();
_.each(this.triggers, function(trigger) {
try {
that.winston.debug("Chatbot "+that.name+": Running onLoggedOff for " + trigger.type + " trigger " + trigger.name);
trigger.onLoggedOff();
return null;
} catch(err) {
that.winston.error("Chatbot "+that.name+": Error running onLoggedOff for " + trigger.type + " trigger " + trigger.name);
that.winston.error(err.stack);
}
});
return null;
}
// Run the .onLoggedOn function for each trigger
ChatBot.prototype.triggerLoggedOn = function() {
if(!this.triggers || this.triggers.length === 0) {
return;
}
this.logonTime = process.hrtime();
var that = this;
_.each(this.triggers, function(trigger) {
try {
that.winston.debug("Chatbot "+that.name+": Running onLoggedOn for " + trigger.type + " trigger " + trigger.name);
trigger.onLoggedOn();
return null;
} catch(err) {
that.winston.error("Chatbot "+that.name+": Error running onLoggedOn for " + trigger.type + " trigger " + trigger.name);
that.winston.error(err.stack);
}
});
return null;
}
// Add or replace a trigger - return the trigger or null
ChatBot.prototype.addTrigger = function(name, type, options) {
if (!name || !type) {
return false;
}
this.removeTrigger(name);
var trigger = this.triggerFactory.createTrigger(type, name, this, options || {}, true);
try {
this.winston.debug("Chatbot "+this.name+": Testing onLoad for " + type + " trigger " + name);
if (trigger && (!trigger.onLoad || trigger._onLoad())) {
this.winston.silly("Chatbot "+this.name+": onLoad success for " + type + " trigger " + name);
this.triggers[name] = trigger;
return trigger;
} else if (trigger) {
this.winston.error("Chatbot "+this.name+": Error loading " + type + " trigger " + name);
return null;
}
} catch(err) {
this.winston.error("Chatbot "+this.name+": Error loading " + type + " trigger " + name + ":");
this.winston.error(err.stack);
}
return null;
}
// Any duplicate names will be replaced
// triggers is of the form [{name:"",type:"",options:{}}, {name:"",type:"",options:{}}, etc]
// Returns true if all were added, false if any couldn't be added
ChatBot.prototype.addTriggers = function(triggers) {
var ok = true;
var that = this;
_.each(triggers, function(trigger) {
ok = ok && (that.addTrigger(trigger.name, trigger.type, trigger.options) != null);
});
return ok;
}
// Returns true if the trigger was removed
ChatBot.prototype.removeTrigger = function(name) {
if (name in this.triggers) {
this.winston.debug("Chatbot "+this.name+": Deleting trigger: "+name);
delete this.triggers[name];
return true;
}
return false;
}
ChatBot.prototype.clearTriggers = function(callback) {
var that = this;
if(!this.options.disableWebserver) {
//First, deinitialize the webserver
this.server.close();
delete this.io;
delete this.server;
delete this.express;
}
this.winston.debug("Chatbot "+this.name+": Clearing triggers");
this.triggers = {};
if(!this.options.disableWebserver) {
//now reinitialize it.
this._startWebServer(that.options.webServerPort||8080)
}
//allow users to redo their own webserver functions afterwards
if(callback && callback instanceof Function) {
callback();
}
}
// Returns triggers in the same form that can be used for addTriggers
// [{name:"",type:"",options:{}}, {name:"",type:"",options:{}}, etc]
ChatBot.prototype.getTriggerDetails = function() {
var triggerDetails = [];
_.each(this.triggers, function(trigger, name) {
triggerDetails.push({ name: name, type: trigger.type, options: trigger.getOptions() });
});
return triggerDetails;
}
ChatBot.prototype.sendMessage = function(steamId, message) {
this.steamClient.sendMessage(steamId, message);
var haveSeenMessage = false;
_.each(this.triggers, function(trigger) {
var seenMessageThisTrigger = trigger.onSentMessage(steamId, message, haveSeenMessage);
haveSeenMessage = haveSeenMessage || seenMessageThisTrigger;
});
}
//left this here because some configs might still be using it?
ChatBot.prototype.joinGame = function(appId) {
this.games=[appId]; //update this.games
this.steamClient.gamesPlayed([this.games]);
}
//this function will play all the games it's told to. This doesn't always show
//the first game as the one being played, so there's another function that
//plays the first game, then waits a fraction of a second to play the others
ChatBot.prototype.setGames = function(appIdArray) {
this.games=appIdArray; //update this.games
if(this.games) this.winston.info("Chatbot "+this.name+": Playing gameIDs " + this.games.toString());
else this.winston.info("Chatbot "+this.name+": Playing nothing");
this.steamClient.gamesPlayed(this.games); //play them!
}
ChatBot.prototype.setPrimaryGame = function(appId,delay) {
this.winston.info("Chatbot "+this.name+": Setting " + appId + " as primary game.");
if(!this.games || this.games === undefined){
this.games=[appId];
} else {
this.games.unshift(appId); //update this.games
}
this.winston.info("Chatbot "+this.name+": Playing gameID " + appId);
this.steamClient.gamesPlayed([appId]); //first, play only this game, so it shows up
var that = this;
setTimeout(function(){
that.winston.info("Chatbot "+that.name+": Playing gameIDs " + that.games.toString());
that.steamClient.gamesPlayed(that.games); //play them!
},(delay||1000)); //play all the games in 1 second.
}
ChatBot.prototype.joinChat = function(roomId, autoJoinAfterDisconnect) {
this.winston.info("Chat bot " + this.name + " joining room " + roomId + " with autoJoinAfterDisconnect " + autoJoinAfterDisconnect);
this.steamClient.joinChat(roomId);
if (autoJoinAfterDisconnect) {
this._addChatToAutojoin(roomId)
}
}
ChatBot.prototype.leaveChat = function(roomId) {
this.winston.info("Chat bot " + this.name + " leaving room " + roomId);
this._removeChatFromAutojoin(roomId);
this.steamClient.leaveChat(roomId);
}
ChatBot.prototype.addFriend = function(userId) {
this.winston.info("Chat bot " + this.name + " adding friend " + this._userString(userId));
this.steamClient.addFriend(userId);
}
ChatBot.prototype.removeFriend = function(userId) {
this.winston.info("Chat bot " + this.name + " removing friend " + this._userString(userId));
this.steamClient.removeFriend(userId);
}
ChatBot.prototype.setPersonaName = function(name) {
this.winston.info("Chat bot " + this.name + " changing name to " + name);
this.steamClient.setPersonaName(name);
}
ChatBot.prototype.setPersonaState = function(state) {
this.winston.info("Chat bot " + this.name + " changing state to " + state);
this.steamClient.setPersonaState(state);
}
ChatBot.prototype.lockChat = function(roomId) {
this.winston.info("Chat bot " + this.name + " locking chat " + roomId);
this.steamClient.lockChat(roomId);
}
ChatBot.prototype.unlockChat = function(roomId) {
this.winston.info("Chat bot " + this.name + " unlocking chat " + roomId);
this.steamClient.unlockChat(roomId);
}
ChatBot.prototype.setModerated = function(roomId) {
this.winston.info("Chat bot " + this.name + " moderating chat " + roomId);
this.steamClient.setModerated(roomId);
}
ChatBot.prototype.setUnmoderated = function(roomId) {
this.winston.info("Chat bot " + this.name + " unmoderating chat " + roomId);
this.steamClient.setUnmoderated(roomId);
}
ChatBot.prototype.kick = function(roomId, userId) {
this.winston.info("Chat bot " + this.name + " kicking " + this._userString(userId) + " from " + roomId);
this.steamClient.kick(roomId, userId);
}
ChatBot.prototype.ban = function(roomId, userId) {
this.winston.info("Chat bot " + this.name + " banning " + this._userString(userId) + " from " + roomId);
this.steamClient.ban(roomId, userId);
}
ChatBot.prototype.unban = function(roomId, userId) {
this.winston.info("Chat bot " + this.name + " unbanning " + this._userString(userId) + " from " + roomId);
this.steamClient.unban(roomId, userId);
}
ChatBot.prototype.users = function() {
return this.steamClient.users;
}
ChatBot.prototype.rooms = function() {
return this.steamClient.chatRooms;
}
ChatBot.prototype.friends = function() {
return this.steamClient.friends;
}
ChatBot.prototype.groups = function() {
return this.steamClient.groups;
}
ChatBot.prototype.logOff = function() {
this.winston.info('Chat bot ' + this.name + ' logging off');
this.steamClient.logOff();
}
ChatBot.prototype.chatInvite = function(chatSteamID, invitedSteamID) {
this.winston.info("Chat bot " + this.name+': Inviting ' + invitedSteamID + ' to chat ' + chatSteamID);
this.steamClient.chatInvite(chatSteamID, invitedSteamID);
}
ChatBot.prototype.getSteamLevel = function(steamids) {
this.winston.info('Chat bot ' + this.name+': Getting steam level for ' + steamids.toString());
var that = this;
this.steamClient.getSteamLevel(steamids, function(levels) {
that.winston.info(levels);
});
}
ChatBot.prototype.setIgnoreFriend = function(steamID, setIgnore) {
this.winston.info('Chat bot ' + this.name+': Setting ' + steamID + ' block to ' + setIgnore);
var that = this.
this.steamClient.setIgnoreFriend(steamID, setIgnore, function(callback) {
that.winston.info(callback); //i don't know what this callback does
});
}
ChatBot.prototype.trade = function(steamID) {
this.winston.info('Chat bot ' + this.name+': Sending trade request to ' + steamID);
this.steamClient.trade(steamID);
}
ChatBot.prototype.respondToTrade = function(tradeID, bool) {
if(bool === true) {
this.steamClient.respondToTrade(tradeID, true);
this.winston.info('Chat bot ' + this.name+': Accepting trade');
} else {
this.steamClient.respondToTrade(tradeID, false);
this.winston.info('Chat bot ' + this.name+': Denying trade');
}
}
ChatBot.prototype.cancelTrade = function(steamID) {
this.winston.info('Chat bot ' + this.name+': Cancelling trade to ' + steamID);
this.steamClient.cancelTrade(steamID);
}
// "Private" functions
ChatBot.prototype._updatePersonaState = function() {
this.steamClient.setPersonaState(this.muted ? this.mutedState : this.unmutedState);
}
ChatBot.prototype._userString = function(id) {
var result = (this.steamClient.users && id in this.steamClient.users) ? (this.steamClient.users[id].playerName + "/") : "";
result += id;
return result;
};
//same as _userString, but only the name without the steamID
ChatBot.prototype._userName = function(id) {
var result = (this.steamClient.users && id in this.steamClient.users) ? (this.steamClient.users[id].playerName) : "";
return result;
};
////
ChatBot.prototype._autojoinChatrooms = function() {
// Auto-join chat rooms that the bot was previously invited to (and not removed from)
if (fs.existsSync(this.autojoinFile)) {
this.autojoinRooms = JSON.parse(fs.readFileSync(this.autojoinFile));
var that = this;
_.each(that.autojoinRooms, function(value, roomId) {
that.winston.info("Chat bot " + that.name + " auto-joining room " + roomId);
that.steamClient.joinChat(roomId);
});
}
}
ChatBot.prototype._addChatToAutojoin = function(roomId) {
if (fs.existsSync(this.autojoinFile)) {
this.autojoinRooms = JSON.parse(fs.readFileSync(this.autojoinFile));
}
else {
this.autojoinRooms = {};
}
this.autojoinRooms[roomId] = true;
fs.writeFileSync(this.autojoinFile, JSON.stringify(this.autojoinRooms));
}
ChatBot.prototype._removeChatFromAutojoin = function(roomId) {
if (fs.existsSync(this.autojoinFile)) {
this.autojoinRooms = JSON.parse(fs.readFileSync(this.autojoinFile));
if (this.autojoinRooms[roomId]) {
delete this.autojoinRooms[roomId];
fs.writeFileSync(this.autojoinFile, JSON.stringify(this.autojoinRooms));
}
}
}
// Steam Events
ChatBot.prototype._onError = function(error) {
this.winston.error("Chatbot "+this.name+": Caught error", error);
this.winston.error(error); // don't know why parameter to winston.error isn't working
this.connected = false;
};
ChatBot.prototype._onLoggedOn = function() {
this.winston.info("ChatBot " + this.name + " logged on");
this.connected = true;
this._updatePersonaState();
this._autojoinChatrooms();
this.steamClient.gamesPlayed(this.games);
}
ChatBot.prototype._onDisconnected = function() {
this.winston.warn("ChatBot " + this.name + " disconnected");
this.connected = false;
}
ChatBot.prototype._onChatInvite = function(roomId, roomName, inviterId) {
this.winston.info("ChatBot " + this.name + " was invited to chat in " + roomName + " (" + roomId + ")" + " by " + this._userString(inviterId));
_.each(this.triggers, function(trigger) {
trigger.onChatInvite(roomId, roomName, inviterId);
});
};
ChatBot.prototype._onRelationship = function(userId, relationship) {
this.winston.info("ChatBot " + this.name + " relationship event for " + this._userString(userId) + " type " + relationship);
if (relationship === 2) {
_.each(this.triggers, function(trigger) {
trigger.onFriendRequest(userId);
});
}
};
ChatBot.prototype._onFriendMsg = function(userId, message, type) {
this.winston.info("ChatBot " + this.name + " friendMsg " + type + " <" + this._userString(userId) + ">: " + message);
if (type === steam.EChatEntryType.ChatMsg) {
var haveSentMessage = false;
_.each(this.triggers, function(trigger) {
var sentMessageThisTrigger = trigger.onFriendMessage(userId, message, haveSentMessage);
haveSentMessage = haveSentMessage || sentMessageThisTrigger;
});
}
};
ChatBot.prototype._onChatMsg = function(roomId, message, type, chatterId) {
this.winston.info("ChatBot " + this.name + " chatMsg " + type + " in " + roomId + " <" + this._userString(chatterId) + ">: " + message);
if (type === steam.EChatEntryType.ChatMsg) {
var that = this;
var haveSentMessage = false;
_.each(this.triggers, function(trigger) {
var sentMessageThisTrigger = trigger.onChatMessage(roomId, chatterId, message, haveSentMessage, that.muted);
haveSentMessage = haveSentMessage || sentMessageThisTrigger;
});
}
};
ChatBot.prototype._onChatStateChange = function(stateChange, chatterActedOn, steamChatId, chatterActedBy) {
this.winston.info("ChatBot " + this.name + " chatStateChange " + stateChange + " in " + steamChatId + " " + chatterActedOn + " acted on by " + chatterActedBy);
var muted = this.muted;
var haveSentMessage = false;
var sentMessageThisTrigger = false;
if ((stateChange & steam.EChatMemberStateChange.Kicked) > 0) {
this.winston.info("Chatbot "+this.name+":"+this._userString(chatterActedOn) + " was kicked from " + steamChatId + " by " + this._userString(chatterActedBy));
// Kicked from chat - don't autojoin
if(chatterActedOn === this.steamClient.steamId) {
this._removeChatFromAutojoin(steamChatId);
}
haveSentMessage = false;
_.each(this.triggers, function(trigger) {
sentMessageThisTrigger = trigger.onKickedChat(steamChatId, chatterActedOn, chatterActedBy, haveSentMessage, muted);
haveSentMessage = haveSentMessage || sentMessageThisTrigger;
});
}
else if ((stateChange & steam.EChatMemberStateChange.Entered) > 0) {
this.winston.info("Chatbot "+this.name+":"+this._userString(chatterActedOn) + " joined " + steamChatId);
haveSentMessage = false;
_.each(this.triggers, function(trigger) {
sentMessageThisTrigger = trigger.onEnteredChat(steamChatId, chatterActedOn, haveSentMessage, muted);
haveSentMessage = haveSentMessage || sentMessageThisTrigger;
});
}
else if ((stateChange & steam.EChatMemberStateChange.Left) > 0) {
this.winston.info("Chatbot "+this.name+":"+this._userString(chatterActedOn) + " left " + steamChatId);
haveSentMessage = false;
_.each(this.triggers, function(trigger) {
sentMessageThisTrigger = trigger.onLeftChat(steamChatId, chatterActedOn, haveSentMessage, muted);
haveSentMessage = haveSentMessage || sentMessageThisTrigger;
});
}
else if ((stateChange & steam.EChatMemberStateChange.Disconnected) > 0) {
this.winston.info("Chatbot "+this.name+":"+this._userString(chatterActedOn) + " was disconnected from " + steamChatId);
haveSentMessage = false;
_.each(this.triggers, function(trigger) {
sentMessageThisTrigger = trigger.onDisconnected(steamChatId, chatterActedOn, haveSentMessage, muted);
haveSentMessage = haveSentMessage || sentMessageThisTrigger;
});
}
else if ((stateChange & steam.EChatMemberStateChange.Banned) > 0) {
this.winston.info("Chatbot "+this.name+":"+this._userString(chatterActedOn) + " was banned from " + steamChatId);
haveSentMessage = false;
_.each(this.triggers, function(trigger) {
sentMessageThisTrigger = trigger.onBannedChat(steamChatId, chatterActedOn, chatterActedBy, haveSentMessage, muted);
haveSentMessage = haveSentMessage || sentMessageThisTrigger;
});
}
};
ChatBot.prototype._onSentry = function(sentry) {
if(this.sentryFile) {
this.winston.info("Chatbot "+this.name+":Obtained sentry. Writing to " + this.sentryFile);
fs.writeFileSync(this.sentryFile, sentry);
} else {
this.winston.info("Chatbot "+this.name+":Obtained sentry. Writing to " + this.name + ".hash", sentry);
fs.writeFileSync("bot." + this.name + ".sentry", sentry);
}
}
ChatBot.prototype._onWebSessionID = function(sessionid) {
this.steamTrade.sessionID = sessionid;
this.winston.debug('New sessionID');
this.winston.silly('New sessionID: ', sessionid);
var that = this;
this.steamClient.webLogOn(function(cookie) {
that.winston.debug('New cookie');
that.winston.silly('New cookie: ', cookie);
that.cookie = cookie;
cookie.forEach(function(part) {
that.steamTrade.setCookie(part.trim());
});
console.log(cookie);
that.winston.info('Logged into Steam Web API at ' + new Date().toString());
});
}
ChatBot.prototype._onTradeOffers = function(number) { //this function gets called when someone sends a non-interactive trade offer. There are no built-in functions for dealing with this.
this.winston.info('Chat bot ' + this.name+': New trade offer count: ' + number);
var haveEatenEvent = false;
_.each(this.triggers, function(trigger) {
var eatenEventThisTrigger = trigger.onTradeOffer(number,haveEatenEvent);
haveEatenEvent = haveEatenEvent || eatenEventThisTrigger;
});
}
ChatBot.prototype._onTradeProposed = function(tradeID, steamID) { //interactive trading session.
this.winston.info('Chat bot ' + this.name+': Received trade request from ' + steamID);
var haveEatenEvent = false;
_.each(this.triggers, function(trigger) {
var eatenEventThisTrigger = trigger.onTradeProposed(tradeID,steamID,haveEatenEvent);
haveEatenEvent = haveEatenEvent || eatenEventThisTrigger;
});
}
ChatBot.prototype._onTradeResult = function(tradeID, result, steamID) {
this.winston.info("Chatbot "+this.name+":"+result + ' from trade with ' + steamID);
}
ChatBot.prototype._onSessionStart = function(steamID) {
this.winston.info('Chat bot ' + this.name+': Trade with ' + steamID + ' initialized');
var haveEatenEvent = false;
_.each(this.triggers, function(trigger) {
var eatenEventThisTrigger = trigger.onTradeSession(steamID,haveEatenEvent);
haveEatenEvent = haveEatenEvent || eatenEventThisTrigger;
});
}
ChatBot.prototype._onAnnouncement = function(groupID, headline) {
this.winston.info("Chatbot "+this.name+":"+groupID + ' posted announcement with title ' + '"' + headline + '"');
var haveEatenEvent = false;
_.each(this.triggers, function(trigger) {
var eatenEventThisTrigger = trigger.onAnnouncement(groupID,headline,haveEatenEvent);
haveEatenEvent = haveEatenEvent || eatenEventThisTrigger;
});
}
/*
* This crap is possibly not implemented correctly/fully. Don't expect it to work right without testing it. For info, see https://github.com/seishun/node-steam
ChatBot.prototype._onLoggedOff = function() {
this.winston.warning("ChatBot " + this.name + " logged off!");
_.each(this.triggers, function(trigger) {
trigger.onLoggedOff();
});
}
ChatBot.prototype._onUser = function(obscureData) {
this.winston.info("ChatBot " + this.name + " - User event detected");
_.each(this.triggers, function(trigger) {
trigger.onUserEvent(obscureData);
});
}
ChatBot.prototype._onFriend = function(steamId,friendRelationship) {
this.winston.info("ChatBot " + this.name + " - Friend relationship event detected");
_.each(this.triggers, function(trigger) {
trigger.onFriendEvent(steamId,friendRelationship);
});
}
ChatBot.prototype._onGroup = function(steamId,clanRelationship) {
this.winston.info("ChatBot " + this.name + " - Group relationship event detected");
_.each(this.triggers, function(trigger) {
trigger.onGroupEvent(steamId,clanRelationship);
});
}
ChatBot.prototype._onRichPresence = function(steamId,status,obscureData) {
this.winston.info("ChatBot " + this.name + " - Rich presence event detected for user " + this._userString(steamId) + " - Status: " + status);
_.each(this.triggers, function(trigger) {
trigger.onRichPresence(steamId,status,obscureData);
});
}
*/
ChatBot.prototype.makeAnnouncement = function(target, head, body, source) {
var that = this;
var post_data = qs.stringify({
"sessionID" : that.steamTrade.sessionID,
"action" : "post",
"headline" : head,
"body" : body
});
var post_options = {
host: "steamcommunity.com",
port: "80",
path: "/groups/"+target+"/announcements/create",
method: "POST",
headers: {
"Content-Type" : "application/x-www-form-urlencoded",
"Content-Length" : post_data.length,
"cookie" : that.cookie
}
};
var post_req = http.request(post_options, function(res) {
res.setEncoding("utf8");
res.on("data", function(chunk) {
that.winston.info("Announcement created: " + head);
if(source) {
that.steamClient.sendMessage(source, "Announcement created: " + head);
} else {
return head;
}
});
});
post_req.write(post_data);
post_req.end();
that.winston.debug(post_data)
that.winston.debug(post_options);
}
//Webserver stuff
ChatBot.prototype._startWebServer = function(port){
var that = this;
this.Express = Express;
this.express = Express();
this.winston.debug("Initializing webserver");
this.server = http.createServer(this.express);
try {
this.server.listen(port,function(){that.winston.info('Webserver listening on '+port);});
this.winston.debug("Webserver initialized successfully!")
} catch(err) {
this.winston.error(err)
this.winston.error("ERROR starting the webserver. Most likely the port is already in use.")
}
this.express.use(expressWinston.logger({
winstonInstance: that.winston,
msg: "HTTP {{res.statusCode}} {{(req.header('x-forwarded-for') ? req.header('x-forwarded-for').split(',')[0] : req.connection.remoteAddress )}} {{req.method}} {{res.responseTime}}ms {{req.hostname+req.originalUrl}}", // customize the default logging message.
meta: (that.options.httpMetaLog===false?false:true), // optional: control whether you want to log the meta data about the request (default to true)
expressFormat: that.options.httpFormat||false, // Use the default Express/morgan request formatting, with the same colors. Enabling this will override any msg and colorStatus if true. Will only output colors on transports with colorize set to true
}));
if(this.options.favicon) {
this.express.get('/favicon.ico', function(req, res){
res.redirect(301,(that.options.favicon===true?"https://nodejs.org/favicon.ico":that.options.favicon));
});
}
this.winston.silly("Done starting webserver. Starting socket.io");
//Eventually, we might want sockets for some kind of live chat function. This would be nicer than reloading the page or using xmlhttprequests. Also, someone might want them for something else? I dunno. In any case, this is better.
this.io = require("socket.io")(this.server);
}
ChatBot.prototype._addRouter = function(path){
var router = Express.Router();
if(path) {
this.winston.debug("Adding routing path ",path);
this.express.use(path, router);
} else {
this.winston.debug("Adding routing path /");
this.express.use(router);
}
var that = this;
return router;
}
ChatBot.prototype._getSocket = function(path) {
var router = Express.Router();
if(path) {
this.winston.debug("Adding socket namespace ",path);
return this.io.of(path);
}
this.winston.debug("Adding socket namespace /");
return this.io;
}
ChatBot.prototype._getClientIp = function(req) {
var ipAddress;
// Amazon EC2 / Heroku workaround to get real client IP
var forwardedIpsStr = req.header("x-forwarded-for");
if (forwardedIpsStr) {
// "x-forwarded-for" header may return multiple IP addresses in
// the format: "client IP, proxy 1 IP, proxy 2 IP" so take the
// the first one
var forwardedIps = forwardedIpsStr.split(",");
ipAddress = forwardedIps[0];
}
if (!ipAddress) {
// Ensure getting client IP address still works in
// development environment
ipAddress = req.connection.remoteAddress;
}
return ipAddress;
};
exports.ChatBot = ChatBot;