Advertisement
Guest User

Untitled

a guest
May 18th, 2017
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 42.04 KB | None | 0 0
  1. //Init
  2. var steamBot;
  3.  
  4. //Modules
  5. var fs = require("fs");
  6. var util = require("util");
  7. var JSON5 = require("json5");
  8. var requ = require('request'); // Request
  9. var Steam = require('steam');
  10. var SteamCommunity = require('steamcommunity');
  11. var SteamTradeOffers = require('steam-tradeoffers');
  12. var TradeOfferManager = require('steam-tradeoffer-manager');
  13. var SteamTotp = require('steam-totp');
  14. var SteamWebLogOn = require('steam-weblogon'); // Steam web
  15.  
  16. //Classes
  17. var g = require("./general.js");
  18.  
  19. //Functions
  20. steamBot = (function(config, mysql, client) {
  21. //Variables steam
  22. steamBot.prototype.bots = {};
  23. steamBot.prototype.botNames = {};
  24. steamBot.prototype.steam = {};
  25. steamBot.prototype.steamCom = {};
  26. steamBot.prototype.steamClient = {};
  27. steamBot.prototype.steamUser = {};
  28. steamBot.prototype.steamFriends = {};
  29. steamBot.prototype.steamWeb = {};
  30. steamBot.prototype.steamProfiles = {};
  31.  
  32. //Variables init
  33. steamBot.prototype.config = config;
  34. steamBot.prototype.mysql = mysql;
  35. steamBot.prototype.client = client;
  36.  
  37. //Variables confirmation
  38. steamBot.prototype.confsAllowed = {};
  39.  
  40. //Variables with offers
  41. steamBot.prototype.sendedOffers = {};
  42. steamBot.prototype.sendsOffers = {};
  43. steamBot.prototype.queueOffers = {};
  44. steamBot.prototype.offers = {};
  45. steamBot.prototype.sleepOffers = {};
  46. steamBot.prototype.playerOffers = {};
  47.  
  48. //Variables with items
  49. steamBot.prototype.botItems = {};
  50. steamBot.prototype.botItemsDef = {};
  51. steamBot.prototype.itemsSum = 0;
  52. steamBot.prototype.itemsPrices = {};
  53. steamBot.prototype.itemsGets = {};
  54.  
  55. //Variables general
  56. steamBot.prototype.assignBots = false;
  57. steamBot.prototype.players = {};
  58.  
  59. //Variables with lock
  60. steamBot.prototype.lockEmpties = false;
  61.  
  62. //Variables with timers
  63. steamBot.prototype.timerSendChange = false;
  64. steamBot.prototype.timerEmpties = false;
  65. steamBot.prototype.timerEmptiesGlobal = false;
  66. steamBot.prototype.timerConfs = false;
  67.  
  68. //Init Client
  69. steamBot.prototype.clientConnect = function(uID, steamid) {
  70. //Update all informations for bot
  71. if(this.assignBots) {
  72. this.refreshInventory(uID);
  73. this.getPlayersData(uID, steamid);
  74. }
  75. }
  76.  
  77. //Init steam
  78. steamBot.prototype.getPlayers = function() {
  79. mysql.select_query("steam_accounts", "*", function(e, r) {
  80. if(!e && r && r.length > 0) {
  81. r.forEach(function(num, indx) {
  82. if(num && num.trade) {
  83. this.players[num.steamid] = num;
  84. }
  85. if(indx == (parseInt(r.length) - 1)) {
  86. this.getPlayersData();
  87. setTimeout(function() {
  88. this.getPlayers();
  89. }.bind(this), 10000);
  90. }
  91. }.bind(this));
  92. }
  93. }.bind(this));
  94. }
  95.  
  96. //Init
  97. steamBot.prototype.botInit = function(id) {
  98. //Check exist bot
  99. if(id && this.bots[id]) {
  100. this.botNames[this.bots[id].user] = id;
  101. this.setTradeURL(id);
  102. this.botOffers(id);
  103. if(this.assignBots == false) {
  104. this.assignBots = true;
  105. console.log("Find empties!");
  106. setTimeout(function() {
  107. this.findEmptiesBot();
  108. this.checkTransfers();
  109. this.getConfirmations();
  110. this.checkSendedOffers();
  111. }.bind(this), 5000);
  112. }
  113. } else {
  114. console.warn("Steam:ERROR:: Konto ID: " + id + " nie istnieje!");
  115. }
  116. }
  117.  
  118. //Assign item to bot
  119. steamBot.prototype.assignItemFor = function(item2, id) {
  120. var uid = randomInt(1000000, 9999999);
  121. if(item2 && item2.tradable && item2.marketable) {
  122. (function(item) {
  123. this.itemPrice(item.market_name, function(price) {
  124. var price = 0.03;
  125. if(!this.botItems[uid] && !this.botItemsDef[item.id]) {
  126. this.botItems[uid] = {
  127. uid: uid,
  128. id: item.id,
  129. botid: id,
  130. itemid: item.assetid + "_" + item.instanceid,
  131. assetid: item.assetid,
  132. instanceid: item.instanceid,
  133. pos: item.pos,
  134. icon: item.icon_url,
  135. name: item.name,
  136. market_name: item.market_name,
  137. price: price,
  138. type: item.type
  139. };
  140. this.botItemsDef[item.id] = uid;
  141. this.itemsSum = randomInt(10000, 9999999);
  142. if(this.timerSendChange) {
  143. clearTimeout(this.timerSendChange);
  144. }
  145. this.timerSendChange = setTimeout(function() {
  146. this.timerSendChange = false;
  147. this.refreshInventory(0);
  148. }.bind(this), 2500);
  149. }
  150. }.bind(this));
  151. }.bind(this))(item2);
  152. }
  153. }
  154.  
  155. //Wutgdraw items
  156. steamBot.prototype.withdrawClientItems = function(sid, items) {
  157. console.log("Steam -> Withdraw items for steamID -> " + sid);
  158.  
  159. if(this.players && this.players[sid]) {
  160. if(this.sendedOffers[sid]) {
  161. this.client.sendMessage(sid, "Opssss", "Twoja poprzednia oferta jest aktywna, anuluj poprzednią - aby wysłać kolejną!", "warning", 5);
  162. } else {
  163. this.sendedOffers[sid] = true;
  164.  
  165. var cash = parseFloat(this.players[sid].cash)
  166. var cashMust = 0;
  167.  
  168. var assignItems = {};
  169. var assignCash = {};
  170. var countItems = 0;
  171.  
  172. for(var iKey in items) {
  173. if(this.botItems && this.botItems[iKey]) {
  174. var item = this.botItems[iKey];
  175. var coins = this.client.getCoins(item.price, 0);
  176. cashMust += parseFloat(coins);
  177.  
  178. if(!(assignCash && assignCash[item.botid])) {
  179. assignCash[item.botid] = 0;
  180. }
  181. assignCash[item.botid] += parseFloat(coins);
  182.  
  183. if(cashMust <= cash) {
  184. if(!(assignItems && assignItems[item.botid])) {
  185. assignItems[item.botid] = {};
  186. }
  187. assignItems[item.botid][Object.keys(assignItems[item.botid]).length] = {
  188. "appid": this.config.modules[this.bots[item.botid].action].inv.appID,
  189. "contextid": this.config.modules[this.bots[item.botid].action].inv.contextID,
  190. "amount": 1,
  191. "assetid": item.id
  192. };
  193. countItems++;
  194. }
  195. } else {
  196. console.log("Item -> " + iKey + " is not exists on Bot Skins!");
  197. }
  198. }
  199.  
  200. if(cashMust > cash) {
  201. console.log("Steam -> Client -> " + sid + " dont have enought money for withdraw skins! (" + cashMust + "/" + cash + ")");
  202. this.client.sendMessage(sid, "Ooo NIE!!!", "Niestety, nie masz wystarczającej liczby coinsów ;(", "error", 5);
  203. this.sendedOffers[sid] = false;
  204. } else if(this.config.modules.market.with.min_coins_dep > this.players[sid].deposites) {
  205. var diffCoins = parseFloat(this.config.modules.market.with.min_coins_dep) - parseFloat(this.players[sid].deposites);
  206. console.log("Steam -> Client -> " + sid + " dont have enought deposited money");
  207. this.client.sendMessage(sid, "Ooo NIE!!!", "Niestety, nie masz zdeponowanej wystarczającej liczby coinsów, potrzebujesz jeszcze: " + diffCoins, "error", 5);
  208. this.sendedOffers[sid] = false;
  209. } else if(countItems > this.config.modules.market.with.items_max) {
  210. console.log("Steam -> Client -> " + sid + " too many items");
  211. this.client.sendMessage(sid, "Ooo NIE!!!", "Niestety, maksymalna liczba itemów w 1 wypłacie to: " + this.config.modules.market.with.items_max, "error", 5);
  212. this.sendedOffers[sid] = false;
  213. } else {
  214. this.client.sendMessage(sid, "Sukces!", "Twoja oferta jest przygotowywana!", "success", 3);
  215. this.makeOffers(sid, assignItems, assignCash);
  216. }
  217. }
  218. }
  219. }
  220.  
  221. //Check sended offers
  222. steamBot.prototype.checkSendedOffers = function() {
  223. //console.log("Steam -> Check Sended Offers");
  224. var confWith = parseInt(this.config.modules.market.with.autoDecline) * 60;
  225.  
  226. for(var id2 in this.bots) {
  227. (function(id) {
  228. for(var oID in this.sendsOffers) {
  229. if(id == this.sendsOffers[oID].bid) {
  230. (function(offer, oID2) {
  231. this.steam[id].getOffer({
  232. tradeofferid: offer.id
  233. }, function(err, body) {
  234. if(!err && body) {
  235. if(!err && body && body.response && body.response.offer) {
  236. var offer2 = body.response.offer;
  237. var sid = offer2.steamid_other;
  238. var ost = offer2.trade_offer_state;
  239. var cashMust = offer.coins;
  240. var createdAt = offer.created_at;
  241.  
  242. if(ost === 2 || ost === 9 || ost === 11) {
  243. //Wait for accept
  244. if(ost !== 11) {
  245. //Check is time expired
  246. if(time() > (createdAt + confWith)) {
  247. this.steam[id].cancelOffer({
  248. tradeofferid: offer.id
  249. }, function(e9,r9) {});
  250. this.client.sendMessage(sid, "Info!", "Upłynął czas oczekiwania na akceptacje, Twoja oferta została odrzucona!", "info", 3);
  251. console.log("Steam -> Offer -> " + offer.id + " >> " + sid + " -> " + ost + " [AUTO DECLINE]");
  252. }
  253. }
  254. }
  255. else if(ost === 3) {
  256. delete this.queueOffers[sid];
  257. delete this.sendedOffers[sid];
  258. delete this.sendsOffers[oID2];
  259.  
  260. this.mysql.update_noa_query("transfer", "`status` = '4', `confirmed_at` = '" + time() + "'", undefined, "`oid` = '" + offer.id + "'");
  261. this.client.sendMessage(sid, "Sukces!", "Twoja oferta zostala poprawnie zaakceptowana!", "success", 3);
  262. console.log("Steam -> Offer -> " + offer.id + " >> " + sid + " -> " + ost + " [ACCEPT]");
  263. } else {
  264. delete this.queueOffers[sid];
  265. delete this.sendedOffers[sid];
  266. delete this.sendsOffers[oID2];
  267. this.steam[id].cancelOffer({
  268. tradeofferid: offer.id
  269. }, function(e9,r9) {});
  270. if(ost !== 6) {
  271. this.client.sendMessage(sid, "Info!", "Twoja oferta zostala odrzucona!", "info", 3);
  272. console.log("Steam -> Offer -> " + offer.id + " >> " + sid + " -> " + ost + " [DECLINE]");
  273. }
  274. this.players[sid].cash += cashMust;
  275. this.mysql.update_noa_query("transfer", "`status` = '0', `canceled` = '1'", undefined, "`oid` = '" + offer.id + "'");
  276. this.mysql.update_noa_query("steam_accounts", "`cash` = cash + '" + cashMust + "'", undefined, "`steamid` = '" + sid + "'");
  277. }
  278. }
  279. }
  280. }.bind(this));
  281. }.bind(this))(this.sendsOffers[oID], oID);
  282. }
  283. }
  284. }.bind(this))(id2);
  285. }
  286.  
  287. setTimeout(function() {
  288. this.checkSendedOffers();
  289. }.bind(this), 1000 * this.config.timers.Sends);
  290. }
  291.  
  292. //Make offers
  293. steamBot.prototype.makeOffers = function(sid, assignItems, assignCash) {
  294. if(assignItems && assignCash && sid) {
  295. var iID = 0;
  296. for(var bid in assignItems) {
  297. iID++;
  298. this.makeOffer(sid, assignItems[bid], bid, assignCash[bid]);
  299. }
  300. }
  301. }
  302.  
  303. //Make offer
  304. steamBot.prototype.makeOffer = function(sid, items, id, cash) {
  305. if(items && id && this.players[sid] && this.players[sid].trade_token) {
  306. if(this.queueOffers[sid]) {
  307. setTimeout(function() {
  308. this.makeOffer(sid, items, id, cash);
  309. }.bind(this), 1500);
  310. } else {
  311. this.players[sid].cash -= cash;
  312. this.mysql.update_noa_query("steam_accounts", "`cash` = cash - '" + cash + "'", undefined, "`steamid` = '" + sid + "'");
  313. this.queueOffers[sid] = true;
  314. this.steam[id].makeOffer({
  315. partnerSteamId: sid,
  316. accessToken: this.players[sid].trade_token,
  317. itemsFromMe: items,
  318. message: this.config.modules.market.with.msg
  319. }, function(err, res) {
  320. if(!err && res) {
  321. var oid = res.tradeofferid;
  322.  
  323. this.mysql.insert_query("transfer", {
  324. "oid": oid,
  325. "items": Object.keys(items).length,
  326. "dir": 2,
  327. "created_at": time(),
  328. "coins": cash,
  329. "sid": sid,
  330. "status": 2,
  331. "botid": this.bots[id].user
  332. }, function(id, e) { }.bind(this));
  333. this.client.sendMessage(sid, "Sukces!", "Twoja oferta zostala utworzona i oczekuje na potwierdzenie mobilne!", "success", 3);
  334. this.confsAllowed[oid] = {
  335. oid: oid,
  336. type: 1,
  337. sid: sid,
  338. showConfirm: true
  339. };
  340. console.log("Success");
  341. this.getConfirmations(id);
  342. } else {
  343. console.log("############################");
  344. console.log("SEND OFFER ERROR");
  345. print_r(err);
  346. this.client.sendMessage(sid, "Opsss!", "Niestety, nie udało się wysłać oferty :(", "danger", 3);
  347. console.log("############################");
  348. this.mysql.insert_query("transfer", {
  349. "oid": 0,
  350. "items": Object.keys(items).length,
  351. "dir": 2,
  352. "created_at": time(),
  353. "coins": cash,
  354. "sid": sid,
  355. "status": 1,
  356. "botid": this.bots[id].user
  357. }, function(id, e) { }.bind(this));
  358. this.queueOffers[sid] = false;
  359. }
  360. }.bind(this));
  361. }
  362. }
  363. }
  364.  
  365. //Check transfers
  366. steamBot.prototype.checkTransfers = function() {
  367. this.mysql.select_query("transfer", "*", function(e, r) {
  368. if(!e && r && r.length > 0) {
  369. //console.log("Steam-> Transfer -> Check sended offers");
  370. var changed = false;
  371. var iid = 0;
  372.  
  373. r.forEach(function(num) {
  374. if(parseInt(num['dir']) == 2 && parseInt(num['canceled']) == 0) {
  375. //Sended offers
  376. var created = num['created_at'];
  377. var status = parseInt(num['status']);
  378. var cashMust = parseFloat(num['coins']);
  379. var sid = num['sid'];
  380.  
  381. if(status == 1 && (time() - this.config.modules.market.with.wait_creat) > created && num['oid'] == "0") {
  382. this.sendedOffers[sid] = false;
  383. console.log("Steam -> Cancel queue offer for steamID -> " + sid + "(" + num['id'] + ")");
  384. this.client.sendMessage(sid, "Informacja!", "Twoje coinsy wróciły, mozesz sprobowac wyslac kolejna oferte!", "info", 3);
  385. this.players[sid].cash += cashMust;
  386. this.mysql.update_noa_query("steam_accounts", "`cash` = cash + '" + cashMust + "'", undefined, "`steamid` = '" + sid + "'");
  387. this.mysql.update_noa_query("transfer", "`canceled` = '1'", undefined, "`id` = '" + num['id'] + "'");
  388. changed = true;
  389. } else if((status == 2 || status == 3)) {
  390. if(!num['canceled']) {
  391. if(!this.sendedOffers[sid])
  392. this.sendedOffers[sid] = true;
  393.  
  394. if(num['oid'] != "0") {
  395. var offer = num['oid'];
  396. if(!this.sendsOffers[offer]) {
  397. this.sendsOffers[offer] = {
  398. id: offer,
  399. bid: this.botNames[num['botid']],
  400. coins: cashMust,
  401. create: num['created_at']
  402. };
  403.  
  404. if(status == 2) {
  405. this.confsAllowed[offer] = {
  406. oid: offer,
  407. type: 1,
  408. sid: sid,
  409. showConfirm: true
  410. };
  411. }
  412. }
  413. }
  414. }
  415. }
  416. }
  417. iid++;
  418. if(r.length == iid) {
  419. if(changed)
  420. this.getPlayersData(0);
  421.  
  422. setTimeout(function() {
  423. this.checkTransfers();
  424. }.bind(this), 1000 * this.config.timers.RefTransfer);
  425. }
  426. }.bind(this));
  427. } else {
  428. setTimeout(function() {
  429. this.checkTransfers();
  430. }.bind(this), 1000 * this.config.timers.RefTransfer);
  431. }
  432. }.bind(this), "`canceled` = '0'");
  433. }
  434.  
  435. //Refresh inventory
  436. steamBot.prototype.refreshInventory = function(uid) {
  437. //console.log("@Refresh Inventory");
  438.  
  439. var uid = typeof uid !== "undefined" ? uid : 0;
  440. if(this.botItems && parseInt(this.itemsSum) > 0) {
  441. this.client.updateInventory(this.botItems, this.itemsSum, uid);
  442. }
  443. }
  444.  
  445. //Get total coins from steamID
  446. steamBot.prototype.getPlayersData = function(uid, steamid) {
  447. var uid = typeof uid !== "undefined" ? uid : 0;
  448. var steamid = typeof steamid !== "undefined" ? steamid : 0;
  449. if(steamid == 0) {
  450. this.client.setPlayersData(this.players);
  451. } else {
  452. this.client.setPlayerData(uid, steamid, this.players[steamid])
  453. }
  454.  
  455. }
  456.  
  457. //Get empties bot
  458. steamBot.prototype.findEmptiesBot = function() {
  459. if(!this.lockEmpties || parseInt(this.lockEmpties) < parseInt(time())) {
  460. var currentBot = -1;
  461. var itemsBot = 9999999;
  462.  
  463. this.lockEmpties = time() + parseInt(this.config.lock.FindEmpties);
  464. //console.log("Steam-> Find emptied BOT!");
  465. for(var bkey in this.bots) {
  466. var itemsTemp = {};
  467. (function(id, bot) {
  468. if(bot.connected && bot.loggedIn) {
  469. this.steamProfiles[id].getInventory(this.config.modules[bot.action].inv.appID, this.config.modules[bot.action].inv.contextID, this.config.modules[bot.action].inv.tradeableOnly, function(err, inv, curr) {
  470. if(err) {
  471. print_r(err);
  472. } else {
  473. if(inv && inv.length > 0) {
  474. for(var key in inv) {
  475. itemsTemp[inv[key].id] = true;
  476. if(!this.botItemsDef[inv[key].id]) {
  477. this.assignItemFor(inv[key], id);
  478. }
  479. }
  480. this.bots[id].items = inv.length;
  481. } else {
  482. this.bots[id].items = 0;
  483. }
  484.  
  485. if(this.bots[id].items < itemsBot) {
  486. itemsBot = this.bots[id].items;
  487. currentBot = id;
  488. }
  489. if(Object.keys(this.bots).length == (parseInt(id)+1)) {
  490. this.assignBots = currentBot;
  491. this.client.setTradeURL(this.bots[currentBot].tradeURL);
  492.  
  493. setTimeout(function() {
  494. for(var iKey in this.botItemsDef) {
  495. var item3 = this.botItemsDef[iKey];
  496. var item4 = this.botItems[item3];
  497. if(itemsTemp && !itemsTemp[iKey]) {
  498. console.log("Steam -> BOT -> DELETE ITEMID -> " + this.botItems[item3].id);
  499. delete this.botItemsDef[iKey];
  500. delete this.botItems[item3];
  501. this.itemsSum = randomInt(10000, 9999999);
  502. }
  503. }
  504. this.refreshInventory(0);
  505. }.bind(this), 500);
  506. if((Object.keys(this.bots).length - 1) == id) {
  507. if(!this.timerEmptiesGlobal) {
  508. this.timerEmptiesGlobal = setTimeout(function() {
  509. this.timerEmptiesGlobal = false;
  510. this.findEmptiesBot();
  511. }.bind(this), 1000 * this.config.timers.FindBots);
  512. }
  513. }
  514. }
  515. }
  516. }.bind(this));
  517. }
  518. }.bind(this))(bkey, this.bots[bkey]);
  519. }
  520. } else {
  521. console.log("Steam-> To fast check find empties bot!");
  522. if(!this.timerEmpties) {
  523. this.timerEmpties = setTimeout(function() {
  524. this.timerEmpties = false;
  525. this.findEmptiesBot();
  526. }.bind(this), 30000);
  527. }
  528. }
  529. }
  530.  
  531. //Offer response
  532. steamBot.prototype.setOfferResponse = function(oid, resp) {
  533. console.log("#2 Response from oid -> " + oid + " resp -> " + resp);
  534.  
  535. //Set response
  536. if(oid && this.offers[oid]) {
  537. this.offers[oid].response = resp;
  538. this.botOffer(oid, false);
  539. }
  540. }
  541.  
  542. //Set trade url
  543. steamBot.prototype.setTradeURL = function(id) {
  544. if(this.bots[id].accid && this.bots[id].steamid && this.bots[id].token) {
  545. var url = this.config.general.tradeURL;
  546. url = url.replace("[accountid]", this.bots[id].accid);
  547. url = url.replace("[token]", this.bots[id].token);
  548. this.bots[id].tradeURL = url;
  549. } else {
  550. console.log("Nie udalo sie ustawic trade URL ");
  551. }
  552. }
  553.  
  554. //Add handle to new offer
  555. steamBot.prototype.botOffer = function(oid, addTimer) {
  556. if(this.offers && this.offers[oid]) {
  557. var offerManage = this.offers[oid];
  558. var bid = offerManage.bot;
  559. var bot = this.bots[bid];
  560. var sid = offerManage.sid;
  561. addTimer = typeof addTimer !== "undefined" ? addTimer : true;
  562.  
  563. if(bot.action == "market" && offerManage && !offerManage.del) {
  564. if(offerManage.response == 0) {
  565. client.showOffer(offerManage);
  566. } else if(!this.sleepOffers[oid]) {
  567. this.sleepOffers[oid] = true;
  568. var resp = parseInt(offerManage.response);
  569.  
  570. if(resp == 1) {
  571. if(this.players[sid]) {
  572. this.steam[bid].getTradeHoldDuration({
  573. tradeOfferId: oid
  574. }, function(e2, rt) {
  575. if(!e2 && rt) {
  576. print_r(rt);
  577. if(rt && rt.their === 0 && rt.my === 0) {
  578. this.steam[bid].acceptOffer({
  579. tradeOfferId: oid,
  580. partnerSteamId: sid
  581. }, function(e, r) {
  582. if(e) {
  583. //Error with accept
  584. console.log("Steam:Error Trade Accept offerID -> " + oid + " for user -> " + sid);
  585. } else {
  586. this.offers[oid].response = 3;
  587. }
  588. delete this.sleepOffers[oid];
  589. }.bind(this));
  590. } else {
  591. this.client.sendMessage(sid, "Ooo NIE!!!", "Niestety, Twoje konto jest ograniczone!", "error", 5);
  592. delete this.playerOffers[sid];
  593. delete this.sleepOffers[oid];
  594. this.offers[oid].del = 1;
  595. }
  596. } else {
  597. delete this.sleepOffers[oid];
  598. }
  599. }.bind(this));
  600. } else {
  601. this.client.sendMessage(sid, "Ooo NIE!!!", "Niestety, Twoje konto nie ma przypisanego TradeURL!", "error", 5);
  602. delete this.sleepOffers[oid];
  603. }
  604. } else if(resp == 2) {
  605. this.steam[bid].declineOffer({
  606. tradeOfferId: oid
  607. }, function(e, r) {
  608. if(e) {
  609. //Error with accept
  610. console.log("Steam:Error Trade Decline offerID -> " + oid + " for user -> " + sid);
  611. } else {
  612. this.offers[oid].response = 3;
  613. }
  614. delete this.sleepOffers[oid];
  615. }.bind(this));
  616. } else if(resp == 3) {
  617. this.steam[bid].getOffer({
  618. tradeOfferId: oid
  619. }, function(e, r) {
  620. if(!e && r && r.response && r.response.offer) {
  621. var offer = r.response.offer;
  622. var state = offer.trade_offer_state;
  623. var resetOffer = false;
  624.  
  625. if(state === 3) {
  626. console.log("Steam:INFO Offer -> " + oid + " is accepted for steamID -> " + sid + " Add: " + this.offers[oid].coins + " coins!");
  627. this.client.sendMessage(sid, "Sukces!", "Twoja oferta zostala poprawnie zaakceptowana!", "success", 3);
  628. this.mysql.update_noa_query("steam_accounts", "`cash` = cash + '" + this.offers[oid].coins + "', `deposites` = deposites + '" + this.offers[oid].coins + "'", function(e9, r9) { }, "`steamid` = '" + sid + "'");
  629. resetOffer = true;
  630. } else if(state === 2) {
  631. console.log("Steam:INFO Offer -> " + oid + " is waiting for accept/decline for steamID -> " + sid);
  632. } else {
  633. console.log("Steam:INFO Offer -> " + oid + " is declined for steamID -> " + sid + " :: Status -> " + state);
  634. this.client.sendMessage(sid, "Sukces!", "Twoja oferta zostala poprawnie odrzucona!", "success", 3);
  635. resetOffer = true;
  636. }
  637.  
  638. if(resetOffer) {
  639. delete this.playerOffers[sid];
  640. this.offers[oid].del = 1;
  641. }
  642. delete this.sleepOffers[oid];
  643. } else {
  644. delete this.sleepOffers[oid];
  645. }
  646. }.bind(this));
  647. } else if(resp == 4) {
  648. this.steam[bid].declineOffer({
  649. tradeOfferId: oid
  650. }, function(e, r) {
  651. if(e) {
  652. //Error with accept
  653. console.log("Steam:Error Trade Decline offerID -> " + oid + " for user -> " + sid);
  654. } else {
  655. if(this.offers[oid].oErr == 1) {
  656. this.client.sendMessage(sid, "Blad!", "Minimalna liczba coinsow 1 itemu to: " + this.config.modules[bot.action].dep.min_item + "!", "error", 3);
  657. } else if(this.offers[oid].oErr == 2) {
  658. this.client.sendMessage(sid, "Blad!", "Minimalna liczba coinsow w 1 ofercie to: " + this.config.modules[bot.action].dep.min_total + "!", "error", 3);
  659. } else if(this.offers[oid].oErr == 3) {
  660. this.client.sendMessage(sid, "Blad!", "Maksymalna liczba itemow w 1 ofercie to: " + this.config.modules[bot.action].dep.items_max + "!", "error", 3);
  661. }
  662. delete this.playerOffers[sid];
  663. this.offers[oid].del = 1;
  664. }
  665. delete this.sleepOffers[oid];
  666. }.bind(this));
  667. }
  668. }
  669. if(addTimer && !offerManage.del) {
  670. setTimeout(function() {
  671. this.botOffer(oid);
  672. }.bind(this), 1000 * parseInt(this.config.timers.Offer));
  673. }
  674. } else if(offerManage && !offerManage.del) {
  675. this.botOffer(oid);
  676. }
  677. }
  678. }
  679.  
  680.  
  681. //Get item price
  682. steamBot.prototype.itemPrice = function(name, callback) {
  683. var timeUpdatePrice = 60;
  684. if(name && name !== "undefined" && name.length > 2) {
  685. var price_type = 1;
  686. if(this.config.price) {
  687. this.config.price.type = typeof this.config.price.type !== "undefined" ? this.config.price.type : 1;
  688. this.config.price.link = typeof this.config.price.link !== "undefined" ? this.config.price.link : "http://google.pl";
  689. this.config.price.key = typeof this.config.price.key !== "undefined" ? this.config.price.key : "none";
  690. this.config.price.base = typeof this.config.price.base !== "undefined" ? this.config.price.base : 0;
  691. this.config.price.update = typeof this.config.price.update !== "undefined" ? parseInt(this.config.price.update * 60 * 60) : timeUpdatePrice;
  692. } else {
  693. this.config.price.type = 1;
  694. this.config.price.link = "http://google.pl";
  695. this.config.price.key = "none";
  696. this.config.price.base = 0;
  697. this.config.price.update = timeUpdatePrice;
  698. }
  699. timeUpdatePrice = this.config.price.update;
  700.  
  701. if(!this.itemsGets[name])
  702. this.itemsGets[name] = 0;
  703.  
  704. if(!this.itemsPrices[name])
  705. this.itemsPrices[name] = 0.00;
  706.  
  707. if(this.itemsGets[name] == 0 || parseFloat(this.itemsPrices[name]) <= 0 || parseInt(this.itemsGets[name]) < parseInt(time())) {
  708. this.itemsGets[name] = parseInt(time()) + parseInt(60 * 60 * 6);
  709.  
  710. try {
  711. var link_price = "";
  712. if(this.config.price.type === 1)
  713. link_price = this.config.price.link + encodeURI(name);
  714. else {
  715. link_price = this.config.price.link;
  716. link_price = link_price.replace("[name]", encodeURI(name));
  717. link_price = link_price.replace("[key]", this.config.price.key);
  718. }
  719. var itemTimer = time();
  720.  
  721. //console.log(link_price);
  722. if(this.config.price.base == 1) {
  723. this.mysql.select_query("items_price", "*", function(err, rows) {
  724. var updatePrice = true;
  725. var priceFromBase = 0;
  726. if(!err && rows && rows.length > 0) {
  727. var num = rows[0];
  728. var lupdate = parseInt(num.last_update) + parseInt(timeUpdatePrice);
  729. if(lupdate > parseInt(time())) {
  730. priceFromBase = parseFloat(num.price);
  731. updatePrice = false;
  732. } else {
  733. updatePrice = true;
  734. }
  735. }
  736. if(updatePrice) {
  737. requ({
  738. uri: link_price,
  739. timeout: 3000
  740. }, function(err, resp, body) {
  741. if(!err && resp.statusCode == 200) {
  742. var b2 = JSON.parse(body);
  743. if(b2.success) {
  744. var priceItem = 0;
  745. if(this.config.price.type === 1) {
  746. priceItem = parseFloat(b2.lowest_price.replace("$", ""));
  747. } else {
  748. priceItem = parseFloat(b2.median_price.replace("$", ""));
  749. }
  750. if(priceItem > 0) {
  751. itemTimer = parseInt(time()) - parseInt(itemTimer);
  752. this.itemsPrices[name] = priceItem;
  753.  
  754. if(!err && rows && rows.length > 0) {
  755. this.mysql.update_query("items_price", {
  756. "price": priceItem,
  757. "last_update": time()
  758. }, function(e, r) {
  759. if(e) {
  760. console.log("Price Error -> " + e);
  761. }
  762. }, "`name` = '" + encodeURI(name) + "'");
  763. } else {
  764. this.mysql.insert_query("items_price", {
  765. "name": encodeURI(name),
  766. "price": priceItem,
  767. "last_update": time()
  768. });
  769. }
  770. }
  771. callback(priceItem);
  772. } else
  773. callback(0.00);
  774. } else {
  775. //console.log("Error with: " + name)
  776. if(err.Error == "ETIMEDOUT") {
  777. console.log("Timeout price!");
  778. }
  779. callback(0.00);
  780. }
  781. }.bind(this));
  782. } else {
  783. itemTimer = parseInt(time()) - parseInt(itemTimer);
  784. this.itemsPrices[name] = priceFromBase;
  785. callback(priceFromBase);
  786. }
  787. }.bind(this), "`name` = '" + encodeURI(name) + "'");
  788. } else {
  789. requ({
  790. uri: link_price,
  791. timeout: 3000
  792. }, function(err, resp, body) {
  793. if(!err && resp.statusCode == 200) {
  794. var b2 = JSON.parse(body);
  795. if(b2.success) {
  796. var priceItem = 0;
  797. if(this.config.price.type === 1) {
  798. priceItem = parseFloat(b2.lowest_price.replace("$", ""));
  799. } else {
  800. priceItem = parseFloat(b2.median_price.replace("$", ""));
  801. }
  802. if(priceItem > 0) {
  803. itemTimer = parseInt(time()) - parseInt(itemTimer);
  804. this.itemsPrices[name] = priceItem;
  805. }
  806. callback(priceItem);
  807. } else {
  808. callback(0.00);
  809. }
  810. } else {
  811. //console.log("Error with: " + name)
  812. if(err.Error == "ETIMEDOUT") {
  813. console.log("Timeout price!");
  814. }
  815. callback(0.00);
  816. }
  817. }.bind(this));
  818. }
  819. }
  820. catch(e) {
  821. callback(0.00);
  822. }
  823. } else {
  824. callback(parseFloat(this.itemsPrices[name]));
  825. }
  826. }
  827. }
  828.  
  829.  
  830. //Get accepted items
  831. steamBot.prototype.itemAccepted = function(item) {
  832. var allowed = false;
  833.  
  834. for(id in this.config.acceptGames) {
  835. var game = this.config.acceptGames[id];
  836.  
  837. if(parseInt(game.appid) == parseInt(item.appid) && parseInt(game.contextid) == parseInt(item.contextid)) {
  838. allowed = true;
  839. break;
  840. }
  841. }
  842.  
  843. return allowed;
  844. }
  845.  
  846.  
  847. //Get descriptions of items
  848. steamBot.prototype.decodeDesc = function(desc, c) {
  849. var itemsDesc = {};
  850.  
  851. var i = 0;
  852. if(desc.length > 0) {
  853. desc.forEach(function(item) {
  854. var name = item.classid + "_" + item.instanceid;
  855.  
  856. if(!itemsDesc[name]) {
  857. delete item.descriptions;
  858. delete item.tags;
  859. itemsDesc[name] = item;
  860. }
  861.  
  862. i++;
  863. if(i == desc.length) {
  864. c(itemsDesc);
  865. }
  866. }.bind(this));
  867. } else {
  868. c(itemsDesc);
  869. }
  870. }
  871.  
  872.  
  873. //Get all offers
  874. steamBot.prototype.botOffers = function(id, waitFor) {
  875. var timerSec = typeof waitFor === 'undefined' ? 50 : (1000 * parseInt(this.config.timers.Offers));
  876.  
  877. setTimeout(function() {
  878. if(this.bots[id].connected && this.bots[id].loggedIn) {
  879. //console.log("Get offers for : " + this.bots[id].user);
  880. this.steam[id].getOffers({
  881. get_received_offers: 1,
  882. get_descriptions: 1,
  883. active_only: 1,
  884. time_historical_cutoff: Math.round(Date.now() / 1000)
  885. }, function(error, body) {
  886. if(!error) {
  887. if(body && body.response && body.response.trade_offers_received) {
  888. var offers = body.response.trade_offers_received;
  889. var desc = body.response.descriptions;
  890.  
  891. if(offers.length > 0) {
  892. var offersId = 0;
  893. offers.forEach(function(offer) {
  894. var items = offer.items_to_receive;
  895. var oid = offer.tradeofferid;
  896. var ost = offer.trade_offer_state;
  897. var sid = offer.steamid_other;
  898. var oErr = 0;
  899. offersId++;
  900.  
  901. if(this.playerOffers[sid]) {
  902. if(offers.length == offersId) {
  903. this.botOffers(id, true);
  904. }
  905. } else {
  906. this.playerOffers[sid] = oid;
  907. if(!this.offers[oid] || this.offers[oid] && !this.offers[oid].del) {
  908. console.log("Check offer -> " + oid + " for player -> " + sid);
  909. this.client.sendMessage(sid, "Twoja oferta!", "Twoja oferta jest przetwarzana!", "info", 3);
  910. }
  911. if(this.offers[oid] && this.offers[oid].citems == items.length) {
  912. if(offers.length == offersId) {
  913. this.botOffers(id, true);
  914. }
  915. } else {
  916. this.decodeDesc(desc, function(itemsDesc) {
  917. if(items.length > 0) {
  918. var itemsAllow = 0;
  919. var itemsId = 0;
  920. var itemsTradable = 0;
  921. var itemsPrice = 0;
  922. var itemsPrices = 0;
  923. items.forEach(function(item, itemKey) {
  924. var itemDesc = itemsDesc[item.classid + "_" + item.instanceid];
  925. if(this.itemAccepted(item)) {
  926. itemsAllow++;
  927. }
  928. if(itemDesc.tradable) {
  929. itemsTradable++;
  930. }
  931.  
  932. this.itemPrice(itemDesc.market_name, function(price) {
  933. itemsId++;
  934. price = parseFloat(price);
  935. console.log("Steam Price-> " + itemDesc.name + " price-> " + price);
  936.  
  937. if(parseFloat(price) > 0) {
  938. itemsPrice++;
  939. itemsPrices += price;
  940. items[itemKey].price = price;
  941.  
  942. if(parseFloat(this.client.getCoins(price, false)) < parseFloat(this.config.modules[this.bots[id].action].dep.min_item)) {
  943. oErr = 1;
  944. }
  945. }
  946.  
  947. if(items.length == itemsId) {
  948. if(itemsAllow == items.length && itemsTradable == items.length) {
  949. if(itemsPrice == items.length) {
  950. var resp = 0;
  951. if(parseFloat(this.client.getCoins(itemsPrices)) < parseFloat(this.config.modules[this.bots[id].action].dep.min_total)) {
  952. oErr = 2;
  953. } else if(items.length > this.config.modules[this.bots[id].action].dep.items_max) {
  954. oErr = 3;
  955. }
  956. if(oErr != 0) {
  957. resp = 4;
  958. }
  959. //All allowed
  960. this.offers[oid] = {
  961. added: time(),
  962. citems: items.length,
  963. offer: offer,
  964. items: items,
  965. itemsDesc: itemsDesc,
  966. prices: itemsPrices,
  967. coins: this.client.getCoins(itemsPrices),
  968. state: ost,
  969. oid: oid,
  970. bot: id,
  971. sid: sid,
  972. response: resp,
  973. oErr: oErr
  974. };
  975. this.botOffer(oid);
  976. } else {
  977. delete this.playerOffers[sid];
  978. console.warn("Steam:ERROR:: Konto -> " + this.bots[id].user + " ofertcie: " + oid + " brak zaladowania cen wszystkich itemow!");
  979. }
  980. } else {
  981. //Any item is not allowed, decline offer!
  982. if(ost === 2) {
  983. console.warn("Steam:ERROR:: Konto -> " + this.bots[id].user + " oferta: " + oid + " posiada niedozwolone itemy!");
  984. delete this.playerOffers[sid];
  985. this.steam[id].declineOffer({tradeOfferId: oid});
  986. }
  987. }
  988.  
  989. if(offers.length == offersId) {
  990. this.botOffers(id, true);
  991. }
  992. }
  993. }.bind(this));
  994. }.bind(this));
  995. } else {
  996. delete this.playerOffers[sid];
  997. this.botOffers(id, true);
  998. }
  999. }.bind(this));
  1000. }
  1001. }
  1002. }.bind(this));
  1003. } else {
  1004. this.botOffers(id, true);
  1005. }
  1006. } else {
  1007. this.botOffers(id, true);
  1008. }
  1009. } else {
  1010. this.botOffers(id, true);
  1011. }
  1012. }.bind(this));
  1013. } else {
  1014. this.botOffers(id, true);
  1015. }
  1016. }.bind(this), timerSec);
  1017. }
  1018.  
  1019. //Confirmation key
  1020. steamBot.prototype.getConfKey = function(id, conf) {
  1021. if(parseInt(conf) == 1)
  1022. conf = "allow";
  1023. else if(parseInt(conf) == 2)
  1024. conf = "cancel";
  1025. else if(parseInt(conf) == 3)
  1026. conf = "details";
  1027. else if(parseInt(conf) == 4)
  1028. conf = "conf";
  1029.  
  1030. var time = Math.floor(Date.now() / 1000);
  1031. return SteamTotp.getConfirmationKey(this.bots[id].file.identity_secret, time, conf);
  1032. }
  1033.  
  1034. //New confirmation
  1035. steamBot.prototype.newConfirmation = function(id, conf) {
  1036. //this.confsAllowed
  1037. if(conf && conf.type == 2 && this.confsAllowed[conf.offerID]) {
  1038. this.steamCom[id].respondToConfirmation(conf.id, conf.key, time(), this.getConfKey(id, 1), true, function(e, r) {
  1039. if(!e) {
  1040. var sid = this.confsAllowed[conf.offerID].sid;
  1041. if(this.confsAllowed[conf.offerID].showConfirm) {
  1042. this.client.sendMessage(sid, "Sukces!", "Twoja oferta zostala potwierdzona. <a href='" + this.client.getOfferLink(conf.offerID) + "' target='_blank'>Przejdź do oferty</a>!", "success", 10);
  1043. }
  1044. }
  1045. }.bind(this));
  1046. }
  1047. }
  1048.  
  1049. //Get confirmations
  1050. steamBot.prototype.getConfirmations = function() {
  1051. if(this.timerConfs) {
  1052. clearTimeout(this.timerConfs);
  1053. this.timerConfs = false;
  1054. }
  1055. for(var id2 in this.bots) {
  1056. (function(id) {
  1057. this.steamCom[id].getConfirmations(time(), this.getConfKey(id, 4), function(e, r) {
  1058. if(!e && r && r.length > 0) {
  1059. r.forEach(function(conf) {
  1060. this.newConfirmation(id, conf);
  1061. }.bind(this));
  1062. }
  1063. }.bind(this));
  1064. this.timerConfs = setTimeout(function() {
  1065. this.getConfirmations(id);
  1066. }.bind(this), 1000 * this.config.timers.Confs);
  1067. }.bind(this))(id2);
  1068. }
  1069. }
  1070.  
  1071. //Connect
  1072. steamBot.prototype.connect = function(id, bot, c) {
  1073. //Add logs
  1074. console.info("Steam:INFO:: Konto -> " + bot.user + " inicializacja!");
  1075.  
  1076. if (!fs.existsSync("./" + this.config.general.steamFa + "/" + bot.user + ".2fa")) {
  1077. console.warn("Steam:WARN:: Konto -> " + bot.user + " nie posiada pliku z TwoFactoryCode!");
  1078. bot.connected = false;
  1079. } else {
  1080. bot.file = JSON5.parse(fs.readFileSync("./" + this.config.general.steamFa + "/" + bot.user + ".2fa"));
  1081. bot.code = SteamTotp.generateAuthCode(bot.file.shared_secret);
  1082.  
  1083. this.steamClient[id] = new Steam.SteamClient();
  1084. this.steamUser[id] = new Steam.SteamUser(this.steamClient[id]);
  1085. this.steamFriends[id] = new Steam.SteamFriends(this.steamClient[id]);
  1086. this.steamCom[id] = new SteamCommunity();
  1087. this.steamWeb[id] = new SteamWebLogOn(this.steamClient[id], this.steamUser[id]);
  1088. this.steam[id] = new SteamTradeOffers();
  1089.  
  1090. this.steamClient[id].connect();
  1091.  
  1092. this.steamClient[id].on('connected', function() {
  1093. bot.connected = true;
  1094. console.log("Steam:SUCCESS:: Konto -> " + bot.user + " polaczono poprawnie!");
  1095. this.steamUser[id].logOn({
  1096. account_name: bot.user,
  1097. password: bot.pass,
  1098. two_factor_code: bot.code
  1099. });
  1100. }.bind(this));
  1101.  
  1102. this.steamClient[id].on('logOnResponse', function(resp) {
  1103. if (resp.eresult == Steam.EResult.OK) {
  1104. console.log("Steam:SUCCESS:: Konto -> " + bot.user + " zalogowano poprawnie!");
  1105. this.steamFriends[id].setPersonaState(Steam.EPersonaState.Online);
  1106.  
  1107. bot.loggedIn = true;
  1108.  
  1109. this.steamWeb[id].webLogOn(function(sessionID, newCookie) {
  1110. this.steamCom[id].setCookies(newCookie);
  1111.  
  1112. this.steamCom[id].getWebApiKey(this.config.general.address, function(e, api) {
  1113. if(e) {
  1114. console.warn("Steam:ERROR:: Konto -> " + bot.user + " nie udalo sie pobrac ApiKeya!");
  1115. console.warn(e);
  1116. this.bots[id] = bot;
  1117. } else {
  1118. bot.api = api;
  1119. bot.steamid = this.steamClient[id].steamID;
  1120. bot.accid = this.steamCom[id].steamID.accountid;
  1121. this.bots[id] = bot;
  1122.  
  1123. if(api) {
  1124. this.steam[id].setup({
  1125. sessionID: sessionID,
  1126. webCookie: newCookie,
  1127. APIKey: bot.api
  1128. });
  1129.  
  1130. this.steam[id].getOfferToken(function(err6, token) {
  1131. if(err6) {
  1132. print_r(err6);
  1133. } else {
  1134. bot.token = token;
  1135. this.steamCom[id].getSteamUser(this.steamCom[id].steamID, function(e1, r1) {
  1136. if(!e1) {
  1137. bot.name = r1.name;
  1138. this.steamProfiles[id] = r1;
  1139. if(r1.isLimitedAccount) {
  1140. console.log("Steam:WARNING:: Konto -> " + bot.name + " posiada limit, ktory uniemozliwa trade!");
  1141. bot.tradable = false;
  1142. } else if(r1.tradeBanState != 'None') {
  1143. console.log("Steam:WARNING:: Konto -> " + bot.name + " posiada TradeBAN, ktory uniemozliwa trade!");
  1144. bot.tradable = false;
  1145. } else {
  1146. if(bot && bot.username && bot.username !== false && bot.username != bot.name) {
  1147. this.steamFriends[id].setPersonaName(bot.username);
  1148. console.log("Steam:INFO:: Konto -> " + bot.name + " zmieniono poprawnie nick na -> " + bot.username);
  1149. bot.name = bot.username;
  1150. }
  1151. bot.tradable = true;
  1152. this.steamCom[id].on('newConfirmation', function(conf) {
  1153. this.newConfirmation(id, conf);
  1154. }.bind(this));
  1155. }
  1156. } else {
  1157. bot.name = "[NIE WCZYTANO]";
  1158. }
  1159. this.bots[id] = bot;
  1160. (function(id2) {
  1161. setTimeout(function() {
  1162. this.botInit(id2);
  1163. }.bind(this), 1000);
  1164. }.bind(this))(id);
  1165. }.bind(this));
  1166. }
  1167. }.bind(this));
  1168. }
  1169. }
  1170. c(bot);
  1171. }.bind(this));
  1172. }.bind(this));
  1173. } else {
  1174. if(resp.eresult == 5) {
  1175. console.warn("Steam:ERROR:: Konto -> " + bot.user + " niepoprawny login lub haslo!");
  1176. } else {
  1177. console.warn("Steam:ERROR:: Konto -> " + bot.user + " powod: " + resp.eresult + "!");
  1178. }
  1179. }
  1180. }.bind(this));
  1181.  
  1182. //Messages
  1183. this.steamFriends[id].on("message", function(source, msg, type, chatter) {
  1184. if(msg.length > 1 && this.config.admins[source]) {
  1185. if(msg.indexOf("!getcode") != -1 || msg.indexOf("!gc") != -1 || msg.indexOf("!code") != -1) {
  1186. bot.code = SteamTotp.generateAuthCode(bot.file.shared_secret);
  1187. this.steamFriends[id].sendMessage(source, "Użyj kodu do zalogowania: " + bot.code, Steam.EChatEntryType.ChatMsg);
  1188. } else {
  1189. this.steamFriends[id].sendMessage(source, "Niepoprawna komenda, uzyj !help", Steam.EChatEntryType.ChatMsg);
  1190. }
  1191. } else if(msg.length > 1) {
  1192. console.log("Steam:: User -> " + source + " send message -> " + msg + " to bot -> " + bot.steamid);
  1193. this.steamFriends[id].sendMessage(source, "Nie masz uprawnien, do tej komendy!", Steam.EChatEntryType.ChatMsg);
  1194. }
  1195. }.bind(this));
  1196. }
  1197. }
  1198.  
  1199. //Excute function
  1200. this.getPlayers();
  1201. });
  1202.  
  1203. //Export
  1204. module.exports = steamBot;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement