-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclasses_Bot.js.html
More file actions
561 lines (490 loc) · 21.6 KB
/
Copy pathclasses_Bot.js.html
File metadata and controls
561 lines (490 loc) · 21.6 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: classes/Bot.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: classes/Bot.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>
Bot.prototype.__proto__ = require('events').EventEmitter.prototype;
const Auth = require('./Auth.js');
const Trade = require('./Trade.js');
const Request = require('./Request.js');
const Friends = require('./Friends.js');
const Profile = require('./Profile.js');
const Community = require('./Community.js');
const TaskManager = require('../lib/TaskManager.js');
const SteamCommunity = require('steamcommunity');
const SteamUser = require('steam-user');
const SteamStore = require('steamstore');
const TradeOfferManager = require('steam-tradeoffer-manager');
const SteamID = TradeOfferManager.SteamID;
/**
* Create a new bot instance
*
* @param username
* @param password
* @param details
* @param settings
* @param logger
* @constructor
*/
function Bot(username, password, details, settings, logger) {
// Ensure account values are valid
var self = this;
// Init all required variables
if (typeof username != "string" || typeof password != "string")
if (!details.hasOwnProperty("steamguard") || !(details.hasOwnProperty("oAuthToken")))
throw Error("Invalid username/password or missing oAuthToken/Steamguard code");
if (typeof details == "object") {
if (details.hasOwnProperty("displayName"))
self.displayName = details.displayName;
if (details.hasOwnProperty("apiKey"))
self.apiKey = details.apiKey;
}
self.username = username;
self.password = password;
self.settings = settings || {
api_key: "",
tradeCancelTime: 60 * 60 * 24 * 1000,
tradePendingCancelTime: 60 * 60 * 24 * 1000,
language: "en",
tradePollInterval: 5000,
tradeCancelOfferCount: 30,
tradeCancelOfferCountMinAge: 60 * 60 * 1000,
cancelTradeOnOverflow: true
};
self.logger = logger;
self.community = new SteamCommunity();
self.client = new SteamUser();
self.TradeOfferManager = new TradeOfferManager({
"steam": self.client,
"community": self.community,
"cancelTime": self.settings.tradeCancelTime, // Keep offers upto 1 day, and then just cancel them.
"pendingCancelTime": self.settings.tradePendingCancelTime, // Keep offers upto 30 mins, and then cancel them if they still need confirmation
"cancelOfferCount": self.settings.tradeCancelOfferCount,// Cancel offers once we hit 7 day threshold
"cancelOfferCountMinAge": self.settings.tradeCancelOfferCountMinAge,// Keep offers until 7 days old
"language": self.settings.language, // We want English item descriptions
"pollInterval": self.settings.tradePollInterval // We want to poll every 5 seconds since we don't have Steam notifying us of offers
});
self.request = self.community.request;
self.store = new SteamStore();
self.loggedIn = false;
self.rateLimited = true;
self.Tasks = new TaskManager(logger);
self.Auth = new Auth(self, details, logger);
self.Request = new Request(self.request, logger);
self.Auth.on('updatedAccountDetails', function (accountDetails) {
self.emit('updatedAccountDetails', accountDetails);
});
self.Auth.on('loggedInNodeSteam', function(){
self.emit('loggedInNodeSteam');
});
self.Profile = new Profile(self.Tasks, self.community, self.Auth, logger);
self.Profile.displayName = self.displayName;
self.Friends = new Friends(self, self.Request, logger);
self.Trade = new Trade(self.TradeOfferManager, self.Auth, self.Tasks, self.settings, logger);
self.Community = new Community(self.community, self.Auth, logger);
}
/**
* @callback callbackErrorOnly
* @param {Error} error - An error message if the process failed, undefined if successful
*/
/**
* @callback callbackSteamID
* @param {Error} error - An error message if the process failed, undefined if successful
* @param {SteamID} steamid - steamid if successful, null if error.
*/
/**
* This error callback is expected only to return an error, and no other information.
* @callback errorOnlyCallback
* @param {Error} error - An error message if the process failed, undefined if successful
*/
/**
* Get the account's username, used to login to Steam
* @returns {String} username
*/
Bot.prototype.getAccountName = function () {
var self = this;
return self.username;
};
/**
* Get if the API/account is rate limited by SteamAPI
* @returns {Boolean} rateLimited
*/
Bot.prototype.getRateLimited = function () {
var self = this;
return self.rateLimited;
};
/**
* Get if the API/account is rate limited by SteamAPI
* @returns {Boolean} rateLimited
*/
Bot.prototype.setRateLimited = function (rateLimited) {
var self = this;
return self.rateLimited = rateLimited;
};
/**
* Set the user we are chatting with
* @param {*|{username: *, sid: *}} chattingUserInfo
*/
Bot.prototype.setChatting = function (chattingUserInfo) {
var self = this;
self.currentChatting = chattingUserInfo;
};
/**
* Fetch SteamID Object from the Individual Account ID (i.e 46143802)
* @returns {Error | String}
*/
Bot.prototype.getUserFromAccountID = function (id) {
return SteamID.fromIndividualAccountID(id);
};
/**
* Fetch SteamID Object from the Individual Account ID (i.e 46143802)
* @returns {Error | String}
* @deprecated
*/
Bot.prototype.fromIndividualAccountID = function (id) {
var self = this;
return self.getUserFromAccountID(id);
};
/**
* Fetch SteamID Object from the SteamID2, SteamID3, SteamID64 or Tradeurl.
* @returns {Error | SteamID}
*/
Bot.prototype.getUser = function (steamid) {
return new SteamID(steamid);
};
/**
* Get the display name of the account
* @returns {String|undefined} displayName - Display name of the account
* @deprecated
*/
Bot.prototype.getDisplayName = function () {
var self = this;
return self.Profile.getDisplayName();
};
/**
* Change the display name of the account (with prefix)
* @param {String} newName - The new display name
* @param {String} namePrefix - The prefix if there is one (Nullable)
* @param {callbackErrorOnly} callbackErrorOnly - A callback returned with possible errors
* @deprecated
*/
Bot.prototype.changeName = function (newName, namePrefix, callbackErrorOnly) {
var self = this;
self.Profile.changeDisplayName(newName, namePrefix, callbackErrorOnly);
};
/**
* Retrieve account inventory based on filters
* @param {Integer} appid - appid by-which to fetch inventory based on.
* @param {Integer} contextid - contextid of lookup (1 - Gifts, 2 - In-game Items, 3 - Coupons, 6 - Game Cards, Profile Backgrounds & Emoticons)
* @param {Boolean} tradableOnly - Items retrieved must be tradable
* @param {inventoryCallback} inventoryCallback - Inventory details (refer to inventoryCallback for more info.)
* @deprecated
*/
Bot.prototype.getInventory = function (appid, contextid, tradableOnly, inventoryCallback) {
var self = this;
self.Trade.getInventory(appid, contextid, tradableOnly, inventoryCallback);
};
/**
* Retrieve account inventory based on filters and provided steamID
* @param {SteamID} steamID - SteamID to use for lookup of inventory
* @param {Integer} appid - appid by-which to fetch inventory based on.
* @param {Integer} contextid - contextid of lookup (1 - Gifts, 2 - In-game Items, 3 - Coupons, 6 - Game Cards, Profile Backgrounds & Emoticons)
* @param {Boolean} tradableOnly - Items retrieved must be tradableOnly
* @param {inventoryCallback} inventoryCallback - Inventory details (refer to inventoryCallback for more info.)
* @deprecated
*/
Bot.prototype.getUserInventory = function (steamID, appid, contextid, tradableOnly, inventoryCallback) {
var self = this;
if (!self.loggedIn) {
self.Tasks.addToQueue('login', self, self.Trade.getUserInventory, [steamID, appid, contextid, tradableOnly, inventoryCallback]);
}
else
self.Trade.getUserInventory(steamID, appid, contextid, tradableOnly, inventoryCallback);
};
/**
* Add a phone-number to the account (For example before setting up 2-factor authentication)
* @param phoneNumber - Certain format must be followed
* @param {callbackErrorOnly} callbackErrorOnly - A callback returned with possible errors
*/
Bot.prototype.addPhoneNumber = function (phoneNumber, callbackErrorOnly) {
var self = this;
self.store.addPhoneNumber(phoneNumber, true, function (err) {
callbackErrorOnly(err);
});
};
/**
* Enter the code to verify the phone number.
* @param code
* @param {callbackErrorOnly} callbackErrorOnly - A callback returned with possible errors
*/
Bot.prototype.verifyPhoneNumber = function (code, callbackErrorOnly) {
var self = this;
self.store.verifyPhoneNumber(code, function (err) {
if (err) {
callbackErrorOnly(err);
}
else {
callbackErrorOnly(undefined);
}
});
};
/**
* This is a private method - but incase you would like to edit it for your own usage...
* @param cookies - Cookies sent by Steam when logged in
* @param sessionID - Session ID as sent by Steam
* @param {callbackSteamID} callbackSteamID - If encountered error (optional), and return SteamID Object
*/
Bot.prototype.loggedInAccount = function (cookies, sessionID, callbackSteamID) {
var self = this;
self.Friends.login(500, 'web');
self.logger.log('debug', 'Logged into %j', self.getAccountName());
if (self.sessionID != sessionID || self.cookies != cookies) {
self.sessionID = sessionID;
self.cookies = cookies;
}
if (self.cookies) {
self.community.setCookies(cookies);
self.store.setCookies(cookies);
self.TradeOfferManager.setCookies(cookies, function (err) {
self.SteamID = self.TradeOfferManager.steamID;
if (err) {
self.logger.log("debug", "Failed to get API Key - TradeOverflowChecking disabled for %j & getOffers call disabled due to %j", self.getAccountName(), err);
if (err.Error == "Access Denied") {
self.api_access = false;
self.logger.log("debug", "%j is a limited account. Check here for more info: https://support.steampowered.com/kb_article.php?ref=3330-IAGK-7663", self.getAccountName());
}
}
else {
self.api_access = true;
// We will always fetch the apiKey on login and ensure it is the same as recorded, otherwise we save it.
if (!self.hasOwnProperty("apiKey")) {
self.Community.getWebApiKey('localhost', function (err, apiKey) {
if (err) {
self.logger.log("debug", "Failed to get apiKey for %j due to %s", self.username, err);
} else {
self.logger.log("debug", "Updated apiKey for %s", self.username);
self.apiKey = apiKey;
self.Auth._updateAccountDetails({apiKey: self.apiKey});
}
});
}
self.Profile.getSteamUser(self.SteamID, function (err, user) {
if (err) {
self.logger.log("debug", "Failed to get display name for ", self.username, err);
} else {
self.logger.log("debug", "Loaded account details for %s", self.username);
self.community_details = user;
self.Profile.displayName = user.name;
self.displayName = user.name;
self.Auth._updateAccountDetails({displayName: user.name});
}
});
}
self.Trade.setAPIAccess(self.api_access);
self.loggedIn = true;
self.emit('loggedIn', self);
self.Tasks.processQueue('login', function (err) {
if (err) {
self.logger.log('error', err);
if (callbackSteamID)
return callbackSteamID(err, self.SteamID);
}
self.community.on('chatTyping', function (senderID) {
self.emit('chatTyping', senderID);
});
self.community.on('chatLoggedOn', function () {
self.emit('chatLoggedOn');
});
self.community.on('chatLogOnFailed', function (err, fatal) {
self.emit('chatLogOnFailed', err, fatal);
});
self.community.on('chatMessage', function (senderID, message) {
if (self.currentChatting != undefined && senderID == self.currentChatting.sid) {
console.log(("\n" + self.currentChatting.username + ": " + message));
}
self.logger.log("debug", "Received message from %j: %s", senderID, message);
/**
* Emitted when a friend message or chat room message is received.
*
* @event Bot#chatMessage
* @type {object}
* @property {SteamID} senderID - The message sender, as a SteamID object
* @property {String} message - The message text
*/
self.emit('chatMessage', senderID, message);
});
self.community.on('sessionExpired', function (err) {
self.logger.log('debug', "Login session expired due to " + err);
self.emit('sessionExpired', err);
});
self.TradeOfferManager.on('sentOfferChanged', function (offer, oldState) {
/**
* Emitted when a trade offer changes state (Ex. accepted, pending, escrow, etc...)
*
* @event Bot#offerChanged
* @type {object}
* @property {TradeOffer} offer - The new offer's details
* @property {TradeOffer} oldState - The old offer's details
* @deprecated
*/
self.emit('offerChanged', offer, oldState);
});
self.TradeOfferManager.on('sentOfferChanged', function (offer, oldState) {
/**
* Emitted when a trade offer changes state (Ex. accepted, pending, escrow, etc...)
*
* @event Bot#sentOfferChanged
* @type {object}
* @property {TradeOffer} offer - The new offer's details
* @property {TradeOffer} oldState - The old offer's details
*/
self.emit('sentOfferChanged', offer, oldState);
});
self.TradeOfferManager.on('receivedOfferChanged', function (offer, oldState) {
self.emit('receivedOfferChanged', offer, oldState);
});
self.TradeOfferManager.on('offerList', function (filter, sent, received) {
/**
* Emitted when we fetch the offerList
*
* @event Bot#offerList
* @type {object}
*/
self.emit('offerList', filter, sent, received);
});
self.TradeOfferManager.on('newOffer', function (offer) {
/**
* Emitted when we receive a new trade offer
*
* @event Bot#newOffer
* @type {object}
* @property {TradeOffer} offer - The offer's details
*/
self.emit('newOffer', offer);
});
self.TradeOfferManager.on('realTimeTradeConfirmationRequired', function (offer) {
/**
* Emitted when a trade offer is cancelled
*
* @event Bot#tradeOffers
* @type {object}
* @property {Integer} count - The amount of active trade offers (can be 0).
*/
self.emit('realTimeTradeConfirmationRequired', offer);
});
self.TradeOfferManager.on('realTimeTradeCompleted', function (offer) {
/**
* Emitted when a trade offer is cancelled
*
* @event Bot#tradeOffers
* @type {object}
* @property {Integer} count - The amount of active trade offers (can be 0).
*/
self.emit('realTimeTradeCompleted', offer);
});
self.TradeOfferManager.on('sentOfferCanceled', function (offer) {
/**
* Emitted when a trade offer is cancelled
*
* @event Bot#tradeOffers
* @type {object}
* @property {Integer} count - The amount of active trade offers (can be 0).
*/
self.emit('sentOfferCanceled', offer);
});
/**
* Emitted when we fully sign into Steam and all functions are usable.
*
* @event Bot#loggedIn
*/
if (callbackSteamID)
return callbackSteamID(undefined, self.SteamID);
});
});
}
else
return callbackSteamID(new Error("Invalid cookies supplied."), null);
};
Bot.prototype.hasPhone = function (callback) {
var self = this;
self.store.hasPhone(function (err, hasPhone, lastDigits) {
callback(err, hasPhone, lastDigits);
});
};
/**
* Create a new account using this account as the maker.
* @param username
* @param password
* @param email
* @param callback
*/
Bot.prototype.createAccount = function (username, password, email, callback) {
var self = this;
self.client.createAccount(username, password, email, function(EResult, steamid){
callback(EResult, steamid);
})
};
Bot.prototype.setSetting = function (settingName, tempSettingValue) {
var self = this;
self.settings[settingName] = tempSettingValue;
};
Bot.prototype.getSetting = function (tempSetting) {
var self = this;
if (self.settings.hasOwnProperty(tempSetting))
return self.settings[tempSetting];
return undefined;
};
Bot.prototype.deleteSetting = function (tempSetting) {
var self = this;
if (self.settings.hasOwnProperty(tempSetting))
delete self.settings[tempSetting];
};
Bot.prototype.logoutAccount = function () {
var self = this;
self.Friends.logout();
self.community = new SteamCommunity();
self.client = new SteamUser();
self.TradeOfferManager = new TradeOfferManager({
"steam": self.client,
"community": self.community,
"cancelTime": self.settings.tradeCancelTime, // Keep offers upto 1 day, and then just cancel them.
"pendingCancelTime": self.settings.tradePendingCancelTime, // Keep offers upto 30 mins, and then cancel them if they still need confirmation
"cancelOfferCount": self.settings.tradeCancelOfferCount,// Cancel offers once we hit 7 day threshold
"cancelOfferCountMinAge": self.settings.tradeCancelOfferCountMinAge,// Keep offers until 7 days old
"language": self.settings.language, // We want English item descriptions
"pollInterval": 5000 // We want to poll every 5 seconds since we don't have Steam notifying us of offers
});
self.request = self.community.request;
self.store = new SteamStore();
self.loggedIn = false;
};
module.exports = Bot;
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="Auth.html">Auth</a></li><li><a href="Bot.html">Bot</a></li><li><a href="global.html#BotManager">BotManager</a></li><li><a href="Community.html">Community</a></li><li><a href="Friends.html">Friends</a></li><li><a href="GUI_Handler.html">GUI_Handler</a></li><li><a href="Profile.html">Profile</a></li><li><a href="Request.html">Request</a></li><li><a href="Trade.html">Trade</a></li></ul><h3>Events</h3><ul><li><a href="Bot.html#event:chatMessage">chatMessage</a></li><li><a href="Bot.html#event:loggedIn">loggedIn</a></li><li><a href="Bot.html#event:newOffer">newOffer</a></li><li><a href="Bot.html#event:offerChanged">offerChanged</a></li><li><a href="Bot.html#event:offerList">offerList</a></li><li><a href="Bot.html#event:sentOfferChanged">sentOfferChanged</a></li><li><a href="Bot.html#event:tradeOffers">tradeOffers</a></li></ul><h3>Global</h3><ul><li><a href="global.html#webserver">webserver</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.4.0</a> on Sat Oct 28 2017 11:52:44 GMT-0400 (Eastern Daylight Time)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>