Advertisement
Guest User

Untitled

a guest
Dec 19th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 40.03 KB | None | 0 0
  1. function shuffle(array) {
  2. var currentIndex = array.length, temporaryValue, randomIndex;
  3.  
  4. // While there remain elements to shuffle...
  5. while (0 !== currentIndex) {
  6.  
  7. // Pick a remaining element...
  8. randomIndex = Math.floor(Math.random() * currentIndex);
  9. currentIndex -= 1;
  10.  
  11. // And swap it with the current element.
  12. temporaryValue = array[currentIndex];
  13. array[currentIndex] = array[randomIndex];
  14. array[randomIndex] = temporaryValue;
  15. }
  16.  
  17. return array;
  18. }
  19.  
  20. let SteamUser = require('steam-user'),
  21. SteamTotp = require('steam-totp'),
  22. TradeOfferManager = require('steam-tradeoffer-manager'),
  23. SteamCommunity = require('steamcommunity'),
  24. Utils = require('./utils.js'),
  25. CONFIG = require('./SETTINGS/config.js'),
  26. allCards = {},
  27. botSets = {},
  28. fs = require('fs'),
  29. users = {},
  30. userMsgs = {},
  31. SID64REGEX = new RegExp(/^[0-9]{17}$/),
  32. chatLogs = '',
  33. userLogs = {};
  34.  
  35.  
  36. let client = new SteamUser(),
  37. manager = new TradeOfferManager({
  38. 'steam': client,
  39. 'language': 'en',
  40. 'pollInterval': '10000',
  41. 'cancelTime': '7200000' // 2 hours in ms
  42. }),
  43. community = new SteamCommunity();
  44.  
  45. fs.readFile('./UserData/Users.json', (ERR, DATA) => {
  46. if (ERR) {
  47. console.log('## An error occurred while getting Users: ' + ERR);
  48. } else {
  49. users = JSON.parse(DATA);
  50. }
  51. });
  52.  
  53. Utils.getCardsInSets((ERR, DATA) => {
  54. if (!ERR) {
  55. allCards = DATA;
  56. console.log('Card data loaded. [' + Object.keys(DATA).length + ']');
  57. } else {
  58. console.log('An error occurred while getting cards: ' + ERR);
  59. }
  60. });
  61.  
  62. setInterval(() => {
  63. for (let i = 0; i < Object.keys(users).length; i++) {
  64. if (users[Object.keys(users)[i]].idleforhours >= CONFIG.MAXHOURSADDED) {
  65. client.chatMessage(Object.keys(users)[i], 'Hi, you have been inactive on my friends list for too long. If you wish to use this bot again re-add it.');
  66. client.removeFriend(Object.keys(users)[i]);
  67. delete users[Object.keys(users)[i]];
  68. fs.writeFile('./UserData/Users.json', JSON.stringify(users), (ERR) => {
  69. if (ERR) {
  70. console.log('## An error occurred while writing UserData file: ' + ERR);
  71. }
  72. });
  73. } else {
  74. users[Object.keys(users)[i]].idleforhours += 1;
  75. fs.writeFile('./UserData/Users.json', JSON.stringify(users), (ERR) => {
  76. if (ERR) {
  77. console.log('## An error occurred while writing UserData file: ' + ERR);
  78. }
  79. });
  80. }
  81. }
  82. }, 1000 * 60 * 60);
  83.  
  84. setInterval(() => {
  85. for (let i = 0; i < Object.keys(userMsgs).length; i++) {
  86. if (userMsgs[Object.keys(userMsgs)[i]] > CONFIG.MAXMSGPERSEC) {
  87. client.chatMessage(Object.keys(userMsgs)[i], 'You have been removed for spamming. Another offense will get you blocked.');
  88. client.removeFriend(Object.keys(userMsgs)[i]);
  89. for (let j = 0; j < CONFIG.ADMINS.length; j++) {
  90. client.chatMessage(CONFIG.ADMINS[j], 'User #' + Object.keys(userMsgs)[i] + ' has been removed for spamming. To block him use !block [STEAMID64]');
  91. }
  92. }
  93. }
  94. userMsgs = {};
  95. }, 1000);
  96.  
  97. client.logOn({
  98. accountName: CONFIG.USERNAME,
  99. password: CONFIG.PASSWORD,
  100. twoFactorCode: SteamTotp.getAuthCode(CONFIG.SHAREDSECRET)
  101. });
  102.  
  103. client.on('loggedOn', (details, parental) => {
  104. client.getPersonas([client.steamID], (personas) => {
  105. console.log('## Logged in as #' + client.steamID + ' (' + personas[client.steamID].player_name + ')');
  106. });
  107. client.setPersona(1);
  108. client.gamesPlayed(CONFIG.PLAYGAMES);
  109. });
  110.  
  111. client.on('webSession', (sessionID, cookies) => {
  112. manager.setCookies(cookies, (ERR) => {
  113. if (ERR) {
  114. console.log('## An error occurred while setting cookies.');
  115. } else {
  116. console.log('## Websession created and cookies set.');
  117. }
  118. });
  119. community.setCookies(cookies);
  120. community.startConfirmationChecker(10000, CONFIG.IDENTITYSECRET);
  121. Utils.getInventory(client.steamID.getSteamID64(), community, (ERR, DATA) => {
  122. console.log('DEBUG#INVLOADED');
  123. if (!ERR) {
  124. let s = DATA;
  125. Utils.getSets(s, allCards, (ERR, DATA) => {
  126. console.log('DEBUG#SETSLOADED');
  127. if (!ERR) {
  128. botSets = DATA;
  129. console.log('## Bot\'s sets loaded.');
  130. } else {
  131. console.log('## An error occurred while getting bot sets: ' + ERR);
  132. process.exit();
  133. }
  134. });
  135. } else {
  136. console.log('## An error occurred while getting bot inventory: ' + ERR);
  137. }
  138. });
  139. });
  140.  
  141. community.on('sessionExpired', (ERR) => {
  142. console.log('## Session Expired. Relogging.');
  143. client.webLogOn();
  144. });
  145.  
  146. client.on('friendMessage', (SENDER, MSG) => {
  147. if (userLogs[SENDER.getSteamID64()]) {
  148. userLogs[SENDER.getSteamID64()].push(MSG);
  149. } else {
  150. userLogs[SENDER.getSteamID64()] = [];
  151. userLogs[SENDER.getSteamID64()].push(MSG);
  152. }
  153. fs.writeFile('./ChatLogs/UserLogs/' + SENDER.getSteamID64() + '-log-' + new Date().getDate() + '-' + new Date().getMonth() + '-' + new Date().getFullYear() + '.json', JSON.stringify({ logs: userLogs[SENDER.getSteamID64()] }), (ERR) => {
  154. if (ERR) {
  155. console.log('## An error occurred while writing UserLogs file: ' + ERR);
  156. }
  157. });
  158. chatLogs += SENDER.getSteamID64() + ' : ' + MSG + '\n';
  159. fs.writeFile('./ChatLogs/FullLogs/log-' + new Date().getDate() + '-' + new Date().getMonth() + '-' + new Date().getFullYear() + '.txt', chatLogs, (ERR) => {
  160. if (ERR) {
  161. console.log('## An error occurred while writing FullLogs file: ' + ERR);
  162. }
  163. });
  164. if (Object.keys(users).indexOf(SENDER.getSteamID64()) < 0) {
  165. users[SENDER.getSteamID64()] = {};
  166. users[SENDER.getSteamID64()].idleforhours = 0;
  167. fs.writeFile('./UserData/Users.json', JSON.stringify(users), (ERR) => {
  168. if (ERR) {
  169. console.log('## An error occurred while writing UserData file: ' + ERR);
  170. }
  171. });
  172. } else {
  173. users[SENDER.getSteamID64()].idleforhours = 0;
  174. }
  175. if (userMsgs[SENDER.getSteamID64()]) {
  176. userMsgs[SENDER.getSteamID64()]++;
  177. } else {
  178. userMsgs[SENDER.getSteamID64()] = 1;
  179. }
  180. /*if (MSG.toUpperCase() == "!STOCK") {
  181. for (let i = 0; i < botSets.length; i++) {
  182. //
  183. }
  184. } else */
  185. if (MSG.toUpperCase().indexOf('!LEVEL') >= 0) {
  186. let n = parseInt(MSG.toUpperCase().replace('!LEVEL ', ''));
  187. if (!isNaN(n)) {
  188. if (n <= CONFIG.MESSAGES.MAXLEVEL) {
  189. Utils.getBadges(SENDER.getSteamID64(), (ERR, DATA, CURRENTLEVEL, XPNEEDED) => {
  190. if (!ERR) {
  191. if (DATA) {
  192. if (n > CURRENTLEVEL) {
  193. let s = 0,
  194. l = 0;
  195. for (let i = 0; i < (n - CURRENTLEVEL); i++) {
  196. s += parseInt((CURRENTLEVEL + l) / 10) + 1;
  197. l++;
  198. }
  199. client.chatMessage(SENDER, 'To get to level ' + n + ' you will need ' + (s - Math.floor(XPNEEDED / 100)) + ' sets. That would cost ' + parseInt((s - Math.floor(XPNEEDED / 100)) / CONFIG.CARDS.BUY1KEYFORAMOUNTOFSETS * 100) / 100 + ' keys.');
  200. } else {
  201. client.chatMessage(SENDER, 'Please provide a valid level.');
  202. }
  203. } else {
  204. for (let i = 0; i < (n - 0); i++) {
  205. s += parseInt((0 + l) / 10) + 1;
  206. l++;
  207. }
  208. client.chatMessage(SENDER, 'To get to level ' + n + ' you will need ' + (s - Math.floor(XPNEEDED / 100)) + ' sets. That would cost ' + parseInt((s - Math.floor(XPNEEDED / 100)) / CONFIG.CARDS.BUY1KEYFORAMOUNTOFSETS * 100) / 100 + ' keys.');
  209. }
  210. } else {
  211. console.log('## An error occurred while getting badge data: ' + ERR);
  212. client.chatMessage(SENDER, 'An error occurred while loading your badges. Please try again later.');
  213. }
  214. });
  215. } else {
  216. client.chatMessage(SENDER, 'Please try a lower level.');
  217. }
  218. } else {
  219. client.chatMessage(SENDER, 'Please provide a valid level.');
  220. }
  221. } else if (MSG.toUpperCase() === '!HELP') {
  222. client.chatMessage(SENDER, CONFIG.MESSAGES.HELP);
  223. if (CONFIG.CARDS.PEOPLETHATCANSELL.indexOf(SENDER.getSteamID64()) >= 0) {
  224. client.chatMessage(SENDER, CONFIG.MESSAGES.SELLHELP);
  225. }
  226. } else if (MSG.toUpperCase().indexOf('!SELLCHECK') >= 0) {
  227. let n = parseInt(MSG.toUpperCase().replace('!SELLCHECK ', ''));
  228. client.chatMessage(SENDER, 'Loading inventory...');
  229.  
  230. Utils.getInventory(SENDER.getSteamID64(), community, (ERR, DATA) => {
  231. console.log('DEBUG#INVLOADED');
  232. if (!ERR) {
  233. let s = DATA;
  234. Utils.getSets(s, allCards, (ERR, DATA) => {
  235. console.log('DEBUG#SETSLOADED');
  236. if (!ERR) {
  237.  
  238. // console.log(b);
  239. // TODO: COUNT AMOUNT OF SETS BOT CAN GIVE HIM
  240. // 1: GET BOTS CARDS. DONE
  241. // 2: GET PLAYER's BADGES. DONE
  242. // 3: MAGIC
  243. let hisMaxSets = 0,
  244. botNSets = 0;
  245. // Loop for sets he has partially completed
  246. // Loop for sets he has never crafted
  247. for (let i = 0; i < Object.keys(DATA).length; i++) {
  248. if (DATA[Object.keys(DATA)[i]].length >= 5) {
  249. hisMaxSets += 5;
  250. } else {
  251. hisMaxSets += DATA[Object.keys(DATA)[i]].length;
  252. }
  253. botNSets += DATA[Object.keys(DATA)[i]].length;
  254. }
  255. client.chatMessage(SENDER, 'You currently have ' + botNSets + ' sets available which the bot can buy. For all of them the bot will pay you ' + parseInt(botNSets / CONFIG.CARDS.GIVE1KEYPERAMOUNTOFSETS * 100) / 100 + ' keys.');
  256. } else {
  257. console.log('## An error occurred while getting user sets: ' + ERR);
  258. }
  259. });
  260. } else {
  261. console.log('## An error occurred while getting user inventory: ' + ERR);
  262. }
  263. });
  264. } else if (MSG.toUpperCase().indexOf('!CHECK') >= 0) {
  265. let n = parseInt(MSG.toUpperCase().replace('!CHECK ', ''));
  266. if (!isNaN(n)) {
  267. client.chatMessage(SENDER, 'With ' + n + ' keys you can get ' + n * CONFIG.CARDS.BUY1KEYFORAMOUNTOFSETS + ' sets.');
  268. } else {
  269. if (Object.keys(botSets).length > 0) {
  270. client.chatMessage(SENDER, 'Loading badges...');
  271. Utils.getBadges(SENDER.getSteamID64(), (ERR, DATA) => {
  272. if (!ERR) {
  273. let b = {}; // List with badges that CAN still be crafted
  274. if (DATA) {
  275. for (let i = 0; i < Object.keys(DATA).length; i++) {
  276. if (DATA[Object.keys(DATA)[i]] < 6) {
  277. b[Object.keys(DATA)[i]] = 5 - DATA[Object.keys(DATA)[i]];
  278. }
  279. }
  280. } else {
  281. client.chatMessage(SENDER.getSteamID64(), 'Your badges are empty, sending an offer without checking badges.');
  282. }
  283. // console.log(b);
  284. // TODO: COUNT AMOUNT OF SETS BOT CAN GIVE HIM
  285. // 1: GET BOTS CARDS. DONE
  286. // 2: GET PLAYER's BADGES. DONE
  287. // 3: MAGIC
  288. let hisMaxSets = 0,
  289. botNSets = 0;
  290. // Loop for sets he has partially completed
  291. for (let i = 0; i < Object.keys(b).length; i++) {
  292. if (botSets[Object.keys(b)[i]] && botSets[Object.keys(b)[i]].length >= 5 - b[Object.keys(b)[i]].length) {
  293. hisMaxSets += 5 - b[Object.keys(b)[i]].length;
  294. }
  295. }
  296. // Loop for sets he has never crafted
  297. for (let i = 0; i < Object.keys(botSets).length; i++) {
  298. if (Object.keys(b).indexOf(Object.keys(botSets)[i]) < 0) {
  299. if (botSets[Object.keys(botSets)[i]].length >= 5) {
  300. hisMaxSets += 5;
  301. } else {
  302. hisMaxSets += botSets[Object.keys(botSets)[i]].length;
  303. }
  304. }
  305. botNSets += botSets[Object.keys(botSets)[i]].length;
  306. }
  307. client.chatMessage(SENDER, 'There are currently ' + hisMaxSets + '/' + botNSets + ' sets available which you have not fully crafted yet. Buying all of them will cost you ' + parseInt(hisMaxSets / CONFIG.CARDS.BUY1KEYFORAMOUNTOFSETS * 100) / 100 + ' keys.');
  308. } else {
  309. client.chatMessage(SENDER, 'An error occurred while getting your badges. Please try again.');
  310. console.log('An error occurred while getting badges: ' + ERR);
  311. }
  312. });
  313. } else {
  314. client.chatMessage(SENDER, 'Please try again later.');
  315. }
  316. }
  317. } else if (MSG.toUpperCase().indexOf('!SELL') >= 0) {
  318. /*if (botSets) {
  319. //if (CONFIG.CARDS.PEOPLETHATCANSELL.indexOf(SENDER.getSteamID64().toString()) >= 0 || CONFIG.CARDS.PEOPLETHATCANSELL.indexOf(parseInt(SENDER.getSteamID64())) >= 0) {
  320. if(1 == 1) {
  321. let n = parseInt(MSG.toUpperCase().replace('!SELL ', '')),
  322. amountofsets = n * CONFIG.CARDS.GIVE1KEYPERAMOUNTOFSETS;
  323. if (!isNaN(n)) {
  324. if (n <= CONFIG.MESSAGES.MAXSELL) {
  325. client.chatMessage(SENDER, 'Processing your request.');
  326. let botKeys = [],
  327. t = manager.createOffer(SENDER.getSteamID64());
  328. t.getUserDetails((ERR, ME, THEM) => {
  329. if (ERR) {
  330. console.log('## An error occurred while getting trade holds: ' + ERR);
  331. client.chatMessage(SENDER, 'An error occurred while getting your trade holds. Please try again');
  332. } else if (ME.escrowDays == 0 && THEM.escrowDays == 0) {
  333. manager.getUserInventoryContents(client.steamID.getSteamID64(), CONFIG.KEYSFROMGAME, 2, true, (ERR, INV, CURR) => {
  334. if (ERR) {
  335. console.log('## An error occurred while getting bot inventory: ' + ERR);
  336. client.chatMessage(SENDER, 'An error occurred while loading the bot\'s inventory. Please try again.');
  337. } else {
  338. for (let i = 0; i < INV.length; i++) {
  339. if (botKeys.length < n && CONFIG.ACCEPTEDKEYS.indexOf(INV[i].market_hash_name) >= 0) {
  340. botKeys.push(INV[i]);
  341. }
  342. }
  343. if (botKeys.length != n) {
  344. client.chatMessage(SENDER, 'The bot does not have enough keys.');
  345. } else {
  346. let amountofB = amountofsets;
  347. Utils.getInventory(SENDER.getSteamID64(), community, (ERR, DATA) => {
  348. if (!ERR) {
  349. let s = DATA;
  350. Utils.getSets(s, allCards, (ERR, DDATA) => {
  351. if (!ERR) {
  352. sortSetsByAmountB(s, (DATA) => {
  353. let setsSent = {};
  354. firsttLoop: for (let i = 0; i < DATA.length; i++) {
  355. console.log(setsSent);
  356. console.log(DATA[i]);
  357. if (DDATA[DATA[i]]) {
  358. for (let j = 0; j < DDATA[DATA[i]].length; j++) {
  359. if (amountofB > 0) {
  360. if ((setsSent[DATA[i]] && setsSent[DATA[i]] < CONFIG.CARDS.MAXSETSELL) || !setsSent[DATA[i]]) {
  361. t.addTheirItems(DDATA[DATA[i]][j]);
  362. console.log('DEBUG#LOOP #2 CONTINUE: ITEM ADD');
  363. amountofB--;
  364. if (!setsSent[DATA[i]]) {
  365. setsSent[DATA[i]] = 1;
  366. } else {
  367. setsSent[DATA[i]] += 1;
  368. }
  369. } else {
  370. console.log('DEBUG#LOOP #2 CONTINUE: RETURN');
  371. continue firsttLoop;
  372. }
  373. } else {
  374. console.log('DEBUG#LOOP #2 CONTINUE: RETURN');
  375. continue firsttLoop;
  376. }
  377. }
  378. } else {
  379. console.log('DEBUG#LOOP #2 CONTINUE: RETURN 2');
  380. continue firsttLoop;
  381. }
  382. }
  383. });
  384. if (amountofB > 0) {
  385. client.chatMessage(SENDER, 'You do not have enough sets, (this bot only accepts ' + CONFIG.CARDS.MAXSETSELL + ' sets per set type at a time). Please try again later.');
  386. } else {
  387. console.log('DEBUG#SENDING');
  388. t.addMyItems(botKeys);
  389. t.data('commandused', 'Sell');
  390. t.data('amountofsets', amountofsets.toString());
  391. t.data('amountofkeys', n);
  392. t.send((ERR, STATUS) => {
  393. if (ERR) {
  394. client.chatMessage(SENDER, 'An error occurred while sending your trade. Steam Trades could be down. Please try again later.');
  395. console.log('## An error occurred while sending trade: ' + ERR);
  396. } else {
  397. client.chatMessage(SENDER, 'Trade Sent! Click here to accept: https://steamcommunity.com/tradeoffer/' + t.id);
  398. console.log('## Trade offer sent!');
  399. }
  400. });
  401. }
  402. } else {
  403. console.log('## An error occurred while getting bot sets: ' + ERR);
  404. }
  405. });
  406. } else {
  407. console.log('## An error occurred while getting user inventory: ' + ERR);
  408. }
  409. });
  410. }
  411. }
  412. });
  413. } else {
  414. client.chatMessage(SENDER, 'Please make sure you don\'t have a trade hold!');
  415. }
  416. });
  417. } else {
  418. client.chatMessage(SENDER, 'Please try a lower amount of keys.');
  419. }
  420. } else {
  421. client.chatMessage(SENDER, 'Please enter a valid amount of keys!');
  422. }
  423. } else {
  424. client.chatMessage(SENDER, 'You are not able to sell sets.');
  425. }
  426. } else {
  427. client.chatMessage(SENDER, 'Please try again later.');
  428. }*/
  429. let n = parseInt(MSG.toUpperCase().replace('!SELL ', ''));
  430.  
  431. manager.getUserInventoryContents(client.steamID.getSteamID64(), CONFIG.KEYSFROMGAME, 2, true, (ERR, INV, CURR) => {
  432. if (ERR) {
  433. console.log('## An error occurred while getting bot inventory: ' + ERR);
  434. client.chatMessage(SENDER, 'An error occurred while loading the bot\'s inventory. Please try again.');
  435. } else {
  436. INV = shuffle(INV);
  437. let botKeys = [];
  438. for (let i = 0; i < INV.length; i++) {
  439. if (botKeys.length < n && CONFIG.ACCEPTEDKEYS.indexOf(INV[i].market_hash_name) >= 0) {
  440. botKeys.push(INV[i]);
  441. }
  442. }
  443. if (botKeys.length != n) {
  444. client.chatMessage(SENDER, 'The bot does not have enough keys.');
  445. } else {
  446.  
  447. manager.getUserInventoryContents(SENDER.getSteamID64(), 753, 6, true, (ERR, INV, CURR) => {
  448. if (ERR) {
  449. console.log('## An error occurred while getting inventory: ' + ERR);
  450. client.chatMessage(SENDER, 'An error occurred while loading inventory. Please try again.');
  451. } else {
  452. let cardsNeeded = n * CONFIG.MKSO.COUNT * CONFIG.CARDS.GIVE1KEYPERAMOUNTOFSETS - 1;
  453. let itemsAdded = 0;
  454. let cardsAdded = [];
  455. for(let i in INV) {
  456. //console.log('loop '+i, INV[i].type);
  457. let item = INV[i];
  458. if(item.type == CONFIG.MKSO.NAME+' Trading Card' && itemsAdded <= cardsNeeded) {
  459. console.log('add');
  460. itemsAdded++;
  461. cardsAdded.push(item);
  462. } else if(itemsAdded == cardsNeeded) break;
  463. }
  464. if(itemsAdded < cardsNeeded) {
  465. console.log('a', cardsAdded, 'n', cardsNeeded);
  466. client.chatMessage(SENDER, 'You don\'t have enough cards!');
  467. return;
  468. }
  469.  
  470. let t = manager.createOffer(SENDER.getSteamID64());
  471.  
  472. t.addMyItems(botKeys);
  473. t.addTheirItems(cardsAdded);
  474. t.data('commandused', 'Sell');
  475. t.data('amountofsets', itemsAdded.toString());
  476. t.data('amountofkeys', n);
  477.  
  478. t.send((ERR, STATUS) => {
  479. if (ERR) {
  480. client.chatMessage(SENDER, 'An error occurred while sending your trade. Steam Trades could be down. Please try again later.');
  481. console.log('## An error occurred while sending trade: ' + ERR);
  482. } else {
  483. client.chatMessage(SENDER, 'Trade Sent! Click here to accept: https://steamcommunity.com/tradeoffer/' + t.id);
  484. console.log('## Trade offer sent!');
  485. }
  486. });
  487. }
  488. });
  489. }
  490. }
  491. });
  492. } else if (MSG.toUpperCase().indexOf('!BUYANY') >= 0) {
  493. if (botSets) {
  494. let n = MSG.toUpperCase().replace('!BUYANY ', ''),
  495. amountofsets = parseInt(n) * CONFIG.CARDS.BUY1KEYFORAMOUNTOFSETS;
  496. if (!isNaN(n)) {
  497. if (n <= CONFIG.MESSAGES.MAXBUY) {
  498. let t = manager.createOffer(SENDER.getSteamID64());
  499. n = parseInt(n);
  500. let theirKeys = [];
  501. t.getUserDetails((ERR, ME, THEM) => {
  502. if (ERR) {
  503. console.log('## An error occurred while getting trade holds: ' + ERR);
  504. client.chatMessage(SENDER, 'An error occurred while getting your trade holds. Please try again');
  505. } else if (ME.escrowDays == 0 && THEM.escrowDays == 0) {
  506. client.chatMessage(SENDER, 'Processing your request.');
  507. manager.getUserInventoryContents(SENDER.getSteamID64(), CONFIG.KEYSFROMGAME, 2, true, (ERR, INV, CURR) => {
  508. if (ERR) {
  509. console.log('## An error occurred while getting inventory: ' + ERR);
  510. client.chatMessage(SENDER, 'An error occurred while loading your inventory. Please try later');
  511. } else {
  512. amountofB = amountofsets;
  513. for (let i = 0; i < INV.length; i++) {
  514. if (theirKeys.length < n && CONFIG.ACCEPTEDKEYS.indexOf(INV[i].market_hash_name) >= 0) {
  515. theirKeys.push(INV[i]);
  516. }
  517. }
  518. if (theirKeys.length != n) {
  519. client.chatMessage(SENDER, 'You do not have enough keys.');
  520. } else {
  521. sortSetsByAmount(botSets, (DATA) => {
  522. let setsSent = {};
  523. firstLoop: for (let i = 0; i < DATA.length; i++) {
  524. console.log(setsSent);
  525. console.log(DATA[i]);
  526. if (botSets[DATA[i]]) {
  527. for (let j = 0; j < botSets[DATA[i]].length; j++) {
  528. if (amountofB > 0) {
  529. if ((setsSent[DATA[i]] && setsSent[DATA[i]] < 5) || !setsSent[DATA[i]]) {
  530. t.addMyItems(botSets[DATA[i]][j]);
  531. console.log('DEBUG#LOOP #2 CONTINUE: ITEM ADD');
  532. amountofB--;
  533. if (!setsSent[DATA[i]]) {
  534. setsSent[DATA[i]] = 1;
  535. } else {
  536. setsSent[DATA[i]] += 1;
  537. }
  538. } else {
  539. console.log('DEBUG#LOOP #2 CONTINUE: RETURN');
  540. continue firstLoop;
  541. }
  542. } else {
  543. console.log('DEBUG#LOOP #2 CONTINUE: RETURN');
  544. continue firstLoop;
  545. }
  546. }
  547. } else {
  548. console.log('DEBUG#LOOP #2 CONTINUE: RETURN 2');
  549. continue firstLoop;
  550. }
  551. }
  552. });
  553. }
  554. }
  555. if (amountofB > 0) {
  556. client.chatMessage(SENDER, 'There are not enough sets. Please try again later.');
  557. } else {
  558. console.log('DEBUG#SENDING');
  559. t.addTheirItems(theirKeys);
  560. t.data('commandused', 'BuyAny');
  561. t.data('amountofsets', amountofsets.toString());
  562. t.data('amountofkeys', n);
  563. t.send((ERR, STATUS) => {
  564. if (ERR) {
  565. client.chatMessage(SENDER, 'An error occurred while sending your trade. Steam Trades could be down. Please try again later.');
  566. console.log('## An error occurred while sending trade: ' + ERR);
  567. } else {
  568. client.chatMessage(SENDER, 'Trade Sent! Click here to accept: https://steamcommunity.com/tradeoffer/' + t.id);
  569. console.log('## Trade offer sent!');
  570. }
  571. });
  572. }
  573. });
  574. } else {
  575. client.chatMessage(SENDER, 'Please make sure you don\'t have a trade hold!');
  576. }
  577. });
  578. } else {
  579. client.chatMessage(SENDER, 'Please try a lower amount of keys');
  580. }
  581. } else {
  582. client.chatMessage(SENDER, 'Please provide a valid amount of keys.');
  583. }
  584. } else {
  585. client.chatMessage(SENDER, 'Please try again later.');
  586. }
  587. } else if (MSG.toUpperCase().indexOf('!BUY') >= 0) {
  588. if (botSets) {
  589. let n = MSG.toUpperCase().replace('!BUY ', ''),
  590. amountofsets = parseInt(n) * CONFIG.CARDS.BUY1KEYFORAMOUNTOFSETS;
  591. if (!isNaN(n)) {
  592. if (n <= CONFIG.MESSAGES.MAXBUY) {
  593. let t = manager.createOffer(SENDER.getSteamID64());
  594. t.getUserDetails((ERR, ME, THEM) => {
  595. if (ERR) {
  596. console.log('## An error occurred while getting trade holds: ' + ERR);
  597. client.chatMessage(SENDER, 'An error occurred while getting your trade holds. Please try again');
  598. } else if (ME.escrowDays == 0 && THEM.escrowDays == 0) {
  599. n = parseInt(n);
  600. let theirKeys = [];
  601. client.chatMessage(SENDER, 'Processing your request.');
  602. manager.getUserInventoryContents(SENDER.getSteamID64(), CONFIG.KEYSFROMGAME, 2, true, (ERR, INV, CURR) => {
  603. if (ERR) {
  604. console.log('## An error occurred while getting inventory: ' + ERR);
  605. client.chatMessage(SENDER, 'An error occurred while loading your inventory. Please try later');
  606. } else {
  607. console.log('DEBUG#INV LOADED');
  608. if (!ERR) {
  609. console.log('DEBUG#INV LOADED NOERR');
  610. for (let i = 0; i < INV.length; i++) {
  611. if (theirKeys.length < n && CONFIG.ACCEPTEDKEYS.indexOf(INV[i].market_hash_name) >= 0) {
  612. theirKeys.push(INV[i]);
  613. }
  614. }
  615. if (theirKeys.length != n) {
  616. client.chatMessage(SENDER, 'You do not have enough keys.');
  617. } else {
  618. Utils.getBadges(SENDER.getSteamID64(), (ERR, DATA) => {
  619. if (!ERR) {
  620. console.log('DEBUG#BADGE LOADED');
  621. if (!ERR) {
  622. let b = {}; // List with badges that CAN still be crafted
  623. if (DATA) {
  624. for (let i = 0; i < Object.keys(DATA).length; i++) {
  625. if (DATA[Object.keys(DATA)[i]] < 6) {
  626. b[Object.keys(DATA)[i]] = 5 - DATA[Object.keys(DATA)[i]];
  627. }
  628. }
  629. } else {
  630. client.chatMessage(SENDER.getSteamID64(), 'Your badges are empty, sending an offer without checking badges.');
  631. }
  632. console.log(DATA);
  633. console.log(b);
  634. // TODO: COUNT AMOUNT OF SETS BOT CAN GIVE HIM
  635. // 1: GET BOTS CARDS. DONE
  636. // 2: GET PLAYER's BADGES. DONE
  637. // 3: MAGIC
  638. let hisMaxSets = 0,
  639. botNSets = 0;
  640. // Loop for sets he has partially completed
  641. for (let i = 0; i < Object.keys(b).length; i++) {
  642. if (botSets[Object.keys(b)[i]] && botSets[Object.keys(b)[i]].length >= 5 - b[Object.keys(b)[i]].length) {
  643. hisMaxSets += 5 - b[Object.keys(b)[i]].length;
  644. }
  645. }
  646. console.log('DEBUG#LOOP 1 DONE');
  647. // Loop for sets he has never crafted
  648. for (let i = 0; i < Object.keys(botSets).length; i++) {
  649. if (Object.keys(b).indexOf(Object.keys(botSets)[i]) < 0) {
  650. if (botSets[Object.keys(botSets)[i]].length >= 5) {
  651. hisMaxSets += 5;
  652. } else {
  653. hisMaxSets += botSets[Object.keys(botSets)[i]].length;
  654. }
  655. }
  656. botNSets += botSets[Object.keys(botSets)[i]].length;
  657. }
  658. console.log('DEBUG#LOOP 2 DONE');
  659. // HERE
  660. if (amountofsets <= hisMaxSets) {
  661. hisMaxSets = amountofsets;
  662. console.log('DEBUG#TRADE CREATED');
  663. sortSetsByAmount(botSets, (DATA) => {
  664. console.log('DEBUG#' + DATA);
  665. console.log('DEBUG#SETS SORTED');
  666. firstLoop: for (let i = 0; i < DATA.length; i++) {
  667. if (b[DATA[i]] == 0) {
  668. continue firstLoop;
  669. } else {
  670. console.log('DEBUG#' + i);
  671. console.log('DEBUG#FOR LOOP ITEMS');
  672. if (hisMaxSets > 0) {
  673. console.log('DEBUG#MAXSETSMORETHAN1');
  674. if (b[DATA[i]] && botSets[DATA[i]].length >= b[DATA[i]]) {
  675. // BOT HAS ENOUGH SETS OF THIS KIND
  676. console.log('DEBUG#LOOP #1');
  677. for (let j = 0; j < 5 - b[DATA[i]]; j++) {
  678. if (j + 1 < b[DATA[i]] && hisMaxSets > 0) {
  679. console.log('DEBUG#LOOP #1: ITEM ADD');
  680. console.log('DEBUG#LOOP #1: ' + botSets[DATA[i]][j]);
  681. t.addMyItems(botSets[DATA[i]][j]);
  682. hisMaxSets--;
  683. console.log(hisMaxSets);
  684. } else {
  685. console.log('DEBUG#LOOP #1: RETURN');
  686. continue firstLoop;
  687. }
  688. }
  689. } else if (b[DATA[i]] && botSets[DATA[i]].length < b[DATA[i]]) {
  690. // BOT DOESNT HAVE ENOUGH SETS OF THIS KIND
  691. console.log('DEBUG#LOOP #1 CONTINUE');
  692. continue; // *
  693. } else if (!b[DATA[i]] && botSets[DATA[i]].length < 5 && botSets[DATA[i]].length - b[DATA[i]] > 0) { // TODO NOT FOR LOOP WITH BOTSETS. IT SENDS ALL
  694. // BOT HAS ENOUGH SETS AND USER NEVER CRAFTED THIS
  695. for (let j = 0; j < botSets[DATA[i]].length - b[DATA[i]]; j++) {
  696. if (botSets[DATA[i]][j] && hisMaxSets > 0) {
  697. t.addMyItems(botSets[DATA[i]][j]);
  698. console.log('DEBUG#LOOP #2 CONTINUE: ITEM ADD');
  699. hisMaxSets--;
  700. } else {
  701. console.log('DEBUG#LOOP #2 CONTINUE: RETURN');
  702. continue firstLoop;
  703. }
  704. }
  705. }
  706. else if (hisMaxSets < 5) {
  707. // BOT DOESNT HAVE CARDS USER AREADY CRAFTED, IF USER STILL NEEDS 5 SETS:
  708. console.log('DEBUG#LOOP #2');
  709. for (let j = 0; j != hisMaxSets; j++) {
  710. if (botSets[DATA[i]][j] && hisMaxSets > 0) {
  711. t.addMyItems(botSets[DATA[i]][j]);
  712. console.log('DEBUG#LOOP #2: ITEM ADD');
  713. hisMaxSets--;
  714. console.log(hisMaxSets);
  715. } else {
  716. console.log('DEBUG#LOOP #2: RETURN');
  717. continue firstLoop;
  718. }
  719. }
  720. } else {
  721. // BOT DOESNT HAVE CARDS USER AREADY CRAFTED, IF USER STILL NEEDS LESS THAN 5 SETS:
  722. console.log('DEBUG#LOOP #2');
  723. for (let j = 0; j != 5; j++ && hisMaxSets > 0) {
  724. if (botSets[DATA[i]][j] && hisMaxSets > 0) {
  725. t.addMyItems(botSets[DATA[i]][j]);
  726. console.log('DEBUG#LOOP #2: ITEM ADD');
  727. hisMaxSets--;
  728. console.log(hisMaxSets);
  729. } else {
  730. console.log('DEBUG#LOOP #2: RETURN');
  731. continue firstLoop;
  732. }
  733. }
  734. }
  735. } else {
  736. console.log('DEBUG#RETURN');
  737. break firstLoop;
  738. }
  739. }
  740. }
  741. if (hisMaxSets > 0) {
  742. client.chatMessage(SENDER, 'There are not enough sets. Please try again later.');
  743. } else {
  744. console.log('DEBUG#SENDING');
  745. t.addTheirItems(theirKeys);
  746. t.data('commandused', 'Buy');
  747. t.data('amountofkeys', n);
  748. t.data('amountofsets', amountofsets.toString());
  749. t.send((ERR, STATUS) => {
  750. if (ERR) {
  751. client.chatMessage(SENDER, 'An error occurred while sending your trade. Steam Trades could be down. Please try again later.');
  752. console.log('## An error occurred while sending trade: ' + ERR);
  753. } else {
  754. client.chatMessage(SENDER, 'Trade Sent! Click here to accept: https://steamcommunity.com/tradeoffer/' + t.id);
  755. console.log('## Trade offer sent');
  756. }
  757. });
  758. }
  759. });
  760. } else {
  761. client.chatMessage(SENDER, 'There are currently not enough sets that you have not used in stock for this amount of keys. Please try again later. If you want the bot to ignore your current badges use !buyany.');
  762. }
  763. // TO HERE
  764. } else {
  765. console.log('An error occurred while getting badges: ' + ERR);
  766. }
  767. } else {
  768. client.chatMessage(SENDER, 'An error occurred while getting your badges. Please try again.');
  769. console.log(SENDER, '## An error occurred while loading badges: ' + ERR);
  770. }
  771. });
  772. }
  773. } else {
  774. console.log('## An error occurred while getting inventory: ' + ERR);
  775. client.chatMessage(SENDER, 'An error occurred while loading your inventory, please make sure it\'s set to public.');
  776. }
  777. }
  778. });
  779. } else {
  780. client.chatMessage(SENDER, 'Please make sure you don\'t have a trade hold!');
  781. }
  782. });
  783. } else {
  784. client.chatMessage(SENDER, 'Please try a lower amount of keys.');
  785. }
  786. } else {
  787. client.chatMessage(SENDER, 'Please provide a valid amount of keys.');
  788. }
  789. } else {
  790. client.chatMessage(SENDER, 'Please try again later.');
  791. }
  792. } else if (MSG.toUpperCase() == '!PROOF') {
  793. client.chatMessage(SENDER, 'Bot hardly edited by Michaukso');
  794. } else if (CONFIG.ADMINS.indexOf(SENDER.getSteamID64()) >= 0 || CONFIG.ADMINS.indexOf(parseInt(SENDER.getSteamID64())) >= 0) {
  795. // Admin commands.
  796. if (MSG.toUpperCase().indexOf('!BLOCK') >= 0) {
  797. let n = MSG.toUpperCase().replace('!BLOCK ', '').toString();
  798. if (SID64REGEX.test(n)) {
  799. client.chatMessage(SENDER, 'User blocked.');
  800. client.blockUser(n);
  801. } else {
  802. client.chatMessage(SENDER, 'Please provide a valid SteamID64');
  803. }
  804. } else if (MSG.toUpperCase().indexOf('!GAME') >= 0) {
  805. let n = MSG.replace('!game ', '').toString();
  806. client.gamesPlayed([`${CONFIG.PLAYGAMES} | ${n}`]);
  807. } else if (MSG.toUpperCase().indexOf('!USERCHECK') >= 0) {
  808. let n = MSG.toUpperCase().replace('!USERCHECK ', '').toString();
  809. if (SID64REGEX.test(n)) {
  810. if (Object.keys(botSets).length > 0) {
  811. client.chatMessage(SENDER, 'Loading badges...');
  812. Utils.getBadges(n, (ERR, DATA) => {
  813. if (!ERR) {
  814. let b = {}; // List with badges that CAN still be crafted
  815. if (DATA) {
  816. for (let i = 0; i < Object.keys(DATA).length; i++) {
  817. if (DATA[Object.keys(DATA)[i]] < 6) {
  818. b[Object.keys(DATA)[i]] = 5 - DATA[Object.keys(DATA)[i]];
  819. }
  820. }
  821. } else {
  822. client.chatMessage(SENDER.getSteamID64(), n + '\'s badges are empty, sending an offer without checking badges.');
  823. }
  824. // console.log(b);
  825. // TODO: COUNT AMOUNT OF SETS BOT CAN GIVE HIM
  826. // 1: GET BOTS CARDS. DONE
  827. // 2: GET PLAYER's BADGES. DONE
  828. // 3: MAGIC
  829. let hisMaxSets = 0,
  830. botNSets = 0;
  831. // Loop for sets he has partially completed
  832. for (let i = 0; i < Object.keys(b).length; i++) {
  833. if (botSets[Object.keys(b)[i]] && botSets[Object.keys(b)[i]].length >= 5 - b[Object.keys(b)[i]].length) {
  834. hisMaxSets += 5 - b[Object.keys(b)[i]].length;
  835. }
  836. }
  837. // Loop for sets he has never crafted
  838. for (let i = 0; i < Object.keys(botSets).length; i++) {
  839. if (Object.keys(b).indexOf(Object.keys(botSets)[i]) < 0) {
  840. if (botSets[Object.keys(botSets)[i]].length >= 5) {
  841. hisMaxSets += 5;
  842. } else {
  843. hisMaxSets += botSets[Object.keys(botSets)[i]].length;
  844. }
  845. }
  846. botNSets += botSets[Object.keys(botSets)[i]].length;
  847. }
  848. client.chatMessage(SENDER, 'There are currently ' + hisMaxSets + '/' + botNSets + ' sets available which ' + n + ' has not fully crafted yet. Buying all of them will cost ' + parseInt(hisMaxSets / CONFIG.CARDS.BUY1KEYFORAMOUNTOFSETS * 100) / 100 + ' keys.');
  849. } else {
  850. client.chatMessage(SENDER, 'An error occurred while getting ' + n + '\'s badges. Please try again.');
  851. console.log('An error occurred while getting badges: ' + ERR);
  852. }
  853. });
  854. } else {
  855. client.chatMessage(SENDER, 'Please try again later.');
  856. }
  857. } else {
  858. client.chatMessage(SENDER, 'Please provide a valid SteamID64.');
  859. }
  860. }
  861. } else {
  862. client.chatMessage(SENDER, 'Command not recognized. Use !help to see how this bot works.');
  863. }
  864. });
  865.  
  866. client.on('friendRelationship', (SENDER, REL) => {
  867. if (REL === 2) {
  868. client.addFriend(SENDER);
  869. } else if (REL === 3) {
  870. if (CONFIG.INVITETOGROUPID) {
  871. client.inviteToGroup(SENDER, CONFIG.INVITETOGROUPID);
  872. }
  873. client.chatMessage(SENDER, CONFIG.MESSAGES.WELCOME);
  874. }
  875. });
  876.  
  877. manager.on('sentOfferChanged', (OFFER, OLDSTATE) => {
  878. Utils.getInventory(client.steamID.getSteamID64(), community, (ERR, DATA) => {
  879. if (!ERR) {
  880. let s = DATA;
  881. Utils.getSets(s, allCards, (ERR, DATA) => {
  882. if (!ERR) {
  883. botSets = DATA;
  884. console.log('## Bot\'s sets loaded.');
  885. } else {
  886. console.log('## An error occurred while getting bot sets: ' + ERR);
  887. }
  888. });
  889. } else {
  890. console.log('## An error occurred while getting bot inventory: ' + ERR);
  891. }
  892. });
  893. if (OFFER.state == 3) {
  894. if (CONFIG.INVITETOGROUPID) {
  895. client.inviteToGroup(OFFER.partner, CONFIG.INVITETOGROUPID);
  896. }
  897. let d = '' + OFFER.data('commandused') + '';
  898. d += '\nSets: ' + OFFER.data('amountofsets');
  899. d += '\nKeys: ' + OFFER.data('amountofkeys');
  900. d += '\nSteamID: ' + OFFER.partner.getSteamID64();
  901. // MICHAUKSO
  902. for (let j = 0; j < CONFIG.ADMINS.length; j++) {
  903. client.chatMessage(CONFIG.ADMINS[j], OFFER.data('commandused') + ' ' + OFFER.data('amountofkeys') + 'k ' + OFFER.partner.getSteamID64());
  904. }
  905. // /MICHAUKSO
  906.  
  907. fs.writeFile('./TradesAccepted/' + OFFER.id + '-' + OFFER.partner.getSteamID64() + '.txt', d, (ERR) => {
  908. if (ERR) {
  909. console.log('## An error occurred while writing trade file: ' + ERR);
  910. }
  911. });
  912. community.getSteamUser(OFFER.partner, (ERR, USER) => {
  913. if (ERR) {
  914. console.log('## An error occurred while getting user profile: ' + ERR);
  915. client.chatMessage(USER.steamID, 'An error occurred while getting your profile (to comment).');
  916. } else {
  917. USER.comment(CONFIG.COMMENTAFTERTRADE, (ERR) => {
  918. if (ERR) {
  919. console.log('## An error occurred while commenting on user profile: ' + ERR);
  920. client.chatMessage(USER.steamID, 'An error occurred while getting commenting on your profile.');
  921. } else {
  922. client.chatMessage(USER.steamID, 'Thanks for trading! :D');
  923. }
  924. });
  925. }
  926. });
  927. } else if (OFFER.state == 6) {
  928. client.chatMessage(OFFER.partner, 'Hey, you did not accept the offer. Please try again if you wish to receive sets!');
  929. }
  930. });
  931.  
  932. manager.on('newOffer', (OFFER) => {
  933. if (CONFIG.ADMINS.indexOf(OFFER.partner.getSteamID64()) >= 0 || CONFIG.ADMINS.indexOf(parseInt(OFFER.partner.getSteamID64())) >= 0) {
  934. OFFER.getUserDetails((ERR, ME, THEM) => {
  935. if (ERR) {
  936. console.log('## An error occurred while getting trade holds: ' + ERR);
  937. client.chatMessage(OFFER.partner, 'An error occurred while getting your trade holds. Please try again');
  938. OFFER.decline((ERR) => {
  939. if (ERR) {
  940. console.log('## An error occurred while declining trade: ' + ERR);
  941. }
  942. });
  943. } else if (ME.escrowDays == 0 && THEM.escrowDays == 0) {
  944. OFFER.accept((ERR) => {
  945. if (ERR) {
  946. console.log('## An error occurred while declining trade: ' + ERR);
  947. OFFER.decline((ERR) => {
  948. if (ERR) {
  949. console.log('## An error occurred while declining trade: ' + ERR);
  950. }
  951. });
  952. } else {
  953. client.chatMessage(OFFER.partner, 'Offer accepted!');
  954. }
  955. });
  956. } else {
  957. client.chatMessage(OFFER.partner, 'Please make sure you don\'t have a trade hold!');
  958. OFFER.decline((ERR) => {
  959. if (ERR) {
  960. console.log('## An error occurred while declining trade: ' + ERR);
  961. }
  962. });
  963. }
  964. });
  965. } else if (OFFER.itemsToGive.length == 0) {
  966. let onlySteam = true;
  967. for (let i = 0; i < OFFER.itemsToReceive.length; i++) {
  968. if (OFFER.itemsToReceive[i].appid != 753) {
  969. onlySteam = false;
  970. }
  971. }
  972. if (onlySteam) {
  973. OFFER.accept((ERR) => {
  974. if (ERR) {
  975. console.log('## An error occurred while declining trade: ' + ERR);
  976. }
  977. });
  978. }
  979. } else {
  980. OFFER.decline((ERR) => {
  981. if (ERR) {
  982. console.log('## An error occurred while declining trade: ' + ERR);
  983. }
  984. });
  985. }
  986. });
  987.  
  988. function sortSetsByAmount(SETS, callback) {
  989. callback(Object.keys(SETS).sort((k1, k2) => SETS[k1].length - SETS[k2].length).reverse());
  990. }
  991.  
  992. function sortSetsByAmountB(SETS, callback) {
  993. callback(Object.keys(SETS).sort((k1, k2) => SETS[k1].length - SETS[k2].length));
  994. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement