Advertisement
Guest User

Untitled

a guest
Dec 19th, 2017
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 40.34 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 inv = DATA;
  234. Utils.getSets(s, allCards, (ERR, DATA) => {
  235. console.log('DEBUG#SETSLOADED');
  236. if (!ERR) {
  237.  
  238.  
  239. // console.log(b);
  240. // TODO: COUNT AMOUNT OF SETS BOT CAN GIVE HIM
  241. // 1: GET BOTS CARDS. DONE
  242. // 2: GET PLAYER's BADGES. DONE
  243. // 3: MAGIC
  244. let hisMaxSets = 0,
  245. botNSets = 0;
  246. // Loop for sets he has partially completed
  247. // Loop for sets he has never crafted
  248. for (let i = 0; i < Object.keys(DATA).length; i++) {
  249. if (DATA[Object.keys(DATA)[i]].length >= 5) {
  250. hisMaxSets += 5;
  251. } else {
  252. hisMaxSets += DATA[Object.keys(DATA)[i]].length;
  253. }
  254. botNSets += DATA[Object.keys(DATA)[i]].length;
  255. }
  256. 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.');
  257. } else {
  258. console.log('## An error occurred while getting user sets: ' + ERR);
  259. }
  260. });
  261. } else {
  262. console.log('## An error occurred while getting user inventory: ' + ERR);
  263. }
  264. });*/
  265.  
  266. manager.getUserInventoryContents(SENDER.getSteamID64(), 753, 6, true, (ERR, INV, CURR) => {
  267. let cards = 0;
  268. for(let i in INV) {
  269. if(INV[i].type == CONFIG.MKSO.NAME+' Trading Card') cards++;
  270. }
  271. client.chatMessage(SENDER, `You have ${cards} cards worth ${cards/12} keys.`);
  272. });
  273.  
  274. } else if (MSG.toUpperCase().indexOf('!CHECK') >= 0) {
  275. let n = parseInt(MSG.toUpperCase().replace('!CHECK ', ''));
  276. if (!isNaN(n)) {
  277. client.chatMessage(SENDER, 'With ' + n + ' keys you can get ' + n * CONFIG.CARDS.BUY1KEYFORAMOUNTOFSETS + ' sets.');
  278. } else {
  279. if (Object.keys(botSets).length > 0) {
  280. client.chatMessage(SENDER, 'Loading badges...');
  281. Utils.getBadges(SENDER.getSteamID64(), (ERR, DATA) => {
  282. if (!ERR) {
  283. let b = {}; // List with badges that CAN still be crafted
  284. if (DATA) {
  285. for (let i = 0; i < Object.keys(DATA).length; i++) {
  286. if (DATA[Object.keys(DATA)[i]] < 6) {
  287. b[Object.keys(DATA)[i]] = 5 - DATA[Object.keys(DATA)[i]];
  288. }
  289. }
  290. } else {
  291. client.chatMessage(SENDER.getSteamID64(), 'Your badges are empty, sending an offer without checking badges.');
  292. }
  293. // console.log(b);
  294. // TODO: COUNT AMOUNT OF SETS BOT CAN GIVE HIM
  295. // 1: GET BOTS CARDS. DONE
  296. // 2: GET PLAYER's BADGES. DONE
  297. // 3: MAGIC
  298. let hisMaxSets = 0,
  299. botNSets = 0;
  300. // Loop for sets he has partially completed
  301. for (let i = 0; i < Object.keys(b).length; i++) {
  302. if (botSets[Object.keys(b)[i]] && botSets[Object.keys(b)[i]].length >= 5 - b[Object.keys(b)[i]].length) {
  303. hisMaxSets += 5 - b[Object.keys(b)[i]].length;
  304. }
  305. }
  306. // Loop for sets he has never crafted
  307. for (let i = 0; i < Object.keys(botSets).length; i++) {
  308. if (Object.keys(b).indexOf(Object.keys(botSets)[i]) < 0) {
  309. if (botSets[Object.keys(botSets)[i]].length >= 5) {
  310. hisMaxSets += 5;
  311. } else {
  312. hisMaxSets += botSets[Object.keys(botSets)[i]].length;
  313. }
  314. }
  315. botNSets += botSets[Object.keys(botSets)[i]].length;
  316. }
  317. 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.');
  318. } else {
  319. client.chatMessage(SENDER, 'An error occurred while getting your badges. Please try again.');
  320. console.log('An error occurred while getting badges: ' + ERR);
  321. }
  322. });
  323. } else {
  324. client.chatMessage(SENDER, 'Please try again later.');
  325. }
  326. }
  327. } else if (MSG.toUpperCase().indexOf('!SELL') >= 0) {
  328. /*if (botSets) {
  329. //if (CONFIG.CARDS.PEOPLETHATCANSELL.indexOf(SENDER.getSteamID64().toString()) >= 0 || CONFIG.CARDS.PEOPLETHATCANSELL.indexOf(parseInt(SENDER.getSteamID64())) >= 0) {
  330. if(1 == 1) {
  331. let n = parseInt(MSG.toUpperCase().replace('!SELL ', '')),
  332. amountofsets = n * CONFIG.CARDS.GIVE1KEYPERAMOUNTOFSETS;
  333. if (!isNaN(n)) {
  334. if (n <= CONFIG.MESSAGES.MAXSELL) {
  335. client.chatMessage(SENDER, 'Processing your request.');
  336. let botKeys = [],
  337. t = manager.createOffer(SENDER.getSteamID64());
  338. t.getUserDetails((ERR, ME, THEM) => {
  339. if (ERR) {
  340. console.log('## An error occurred while getting trade holds: ' + ERR);
  341. client.chatMessage(SENDER, 'An error occurred while getting your trade holds. Please try again');
  342. } else if (ME.escrowDays == 0 && THEM.escrowDays == 0) {
  343. manager.getUserInventoryContents(client.steamID.getSteamID64(), CONFIG.KEYSFROMGAME, 2, true, (ERR, INV, CURR) => {
  344. if (ERR) {
  345. console.log('## An error occurred while getting bot inventory: ' + ERR);
  346. client.chatMessage(SENDER, 'An error occurred while loading the bot\'s inventory. Please try again.');
  347. } else {
  348. for (let i = 0; i < INV.length; i++) {
  349. if (botKeys.length < n && CONFIG.ACCEPTEDKEYS.indexOf(INV[i].market_hash_name) >= 0) {
  350. botKeys.push(INV[i]);
  351. }
  352. }
  353. if (botKeys.length != n) {
  354. client.chatMessage(SENDER, 'The bot does not have enough keys.');
  355. } else {
  356. let amountofB = amountofsets;
  357. Utils.getInventory(SENDER.getSteamID64(), community, (ERR, DATA) => {
  358. if (!ERR) {
  359. let s = DATA;
  360. Utils.getSets(s, allCards, (ERR, DDATA) => {
  361. if (!ERR) {
  362. sortSetsByAmountB(s, (DATA) => {
  363. let setsSent = {};
  364. firsttLoop: for (let i = 0; i < DATA.length; i++) {
  365. console.log(setsSent);
  366. console.log(DATA[i]);
  367. if (DDATA[DATA[i]]) {
  368. for (let j = 0; j < DDATA[DATA[i]].length; j++) {
  369. if (amountofB > 0) {
  370. if ((setsSent[DATA[i]] && setsSent[DATA[i]] < CONFIG.CARDS.MAXSETSELL) || !setsSent[DATA[i]]) {
  371. t.addTheirItems(DDATA[DATA[i]][j]);
  372. console.log('DEBUG#LOOP #2 CONTINUE: ITEM ADD');
  373. amountofB--;
  374. if (!setsSent[DATA[i]]) {
  375. setsSent[DATA[i]] = 1;
  376. } else {
  377. setsSent[DATA[i]] += 1;
  378. }
  379. } else {
  380. console.log('DEBUG#LOOP #2 CONTINUE: RETURN');
  381. continue firsttLoop;
  382. }
  383. } else {
  384. console.log('DEBUG#LOOP #2 CONTINUE: RETURN');
  385. continue firsttLoop;
  386. }
  387. }
  388. } else {
  389. console.log('DEBUG#LOOP #2 CONTINUE: RETURN 2');
  390. continue firsttLoop;
  391. }
  392. }
  393. });
  394. if (amountofB > 0) {
  395. 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.');
  396. } else {
  397. console.log('DEBUG#SENDING');
  398. t.addMyItems(botKeys);
  399. t.data('commandused', 'Sell');
  400. t.data('amountofsets', amountofsets.toString());
  401. t.data('amountofkeys', n);
  402. t.send((ERR, STATUS) => {
  403. if (ERR) {
  404. client.chatMessage(SENDER, 'An error occurred while sending your trade. Steam Trades could be down. Please try again later.');
  405. console.log('## An error occurred while sending trade: ' + ERR);
  406. } else {
  407. client.chatMessage(SENDER, 'Trade Sent! Click here to accept: https://steamcommunity.com/tradeoffer/' + t.id);
  408. console.log('## Trade offer sent!');
  409. }
  410. });
  411. }
  412. } else {
  413. console.log('## An error occurred while getting bot sets: ' + ERR);
  414. }
  415. });
  416. } else {
  417. console.log('## An error occurred while getting user inventory: ' + ERR);
  418. }
  419. });
  420. }
  421. }
  422. });
  423. } else {
  424. client.chatMessage(SENDER, 'Please make sure you don\'t have a trade hold!');
  425. }
  426. });
  427. } else {
  428. client.chatMessage(SENDER, 'Please try a lower amount of keys.');
  429. }
  430. } else {
  431. client.chatMessage(SENDER, 'Please enter a valid amount of keys!');
  432. }
  433. } else {
  434. client.chatMessage(SENDER, 'You are not able to sell sets.');
  435. }
  436. } else {
  437. client.chatMessage(SENDER, 'Please try again later.');
  438. }*/
  439. let n = parseInt(MSG.toUpperCase().replace('!SELL ', ''));
  440.  
  441. manager.getUserInventoryContents(client.steamID.getSteamID64(), CONFIG.KEYSFROMGAME, 2, true, (ERR, INV, CURR) => {
  442. if (ERR) {
  443. console.log('## An error occurred while getting bot inventory: ' + ERR);
  444. client.chatMessage(SENDER, 'An error occurred while loading the bot\'s inventory. Please try again.');
  445. } else {
  446. INV = shuffle(INV);
  447. let botKeys = [];
  448. for (let i = 0; i < INV.length; i++) {
  449. if (botKeys.length < n && CONFIG.ACCEPTEDKEYS.indexOf(INV[i].market_hash_name) >= 0) {
  450. botKeys.push(INV[i]);
  451. }
  452. }
  453. if (botKeys.length != n) {
  454. client.chatMessage(SENDER, 'The bot does not have enough keys.');
  455. } else {
  456.  
  457. manager.getUserInventoryContents(SENDER.getSteamID64(), 753, 6, true, (ERR, INV, CURR) => {
  458. if (ERR) {
  459. console.log('## An error occurred while getting inventory: ' + ERR);
  460. client.chatMessage(SENDER, 'An error occurred while loading inventory. Please try again.');
  461. } else {
  462. let cardsNeeded = n * CONFIG.MKSO.COUNT * CONFIG.CARDS.GIVE1KEYPERAMOUNTOFSETS - 1;
  463. let itemsAdded = 0;
  464. let cardsAdded = [];
  465. for(let i in INV) {
  466. //console.log('loop '+i, INV[i].type);
  467. let item = INV[i];
  468. if(item.type == CONFIG.MKSO.NAME+' Trading Card' && itemsAdded <= cardsNeeded) {
  469. console.log('add');
  470. itemsAdded++;
  471. cardsAdded.push(item);
  472. } else if(itemsAdded == cardsNeeded) break;
  473. }
  474. if(itemsAdded < cardsNeeded) {
  475. console.log('a', cardsAdded, 'n', cardsNeeded);
  476. client.chatMessage(SENDER, 'You don\'t have enough cards!');
  477. return;
  478. }
  479.  
  480. let t = manager.createOffer(SENDER.getSteamID64());
  481.  
  482. t.addMyItems(botKeys);
  483. t.addTheirItems(cardsAdded);
  484. t.data('commandused', 'Sell');
  485. t.data('amountofsets', itemsAdded.toString());
  486. t.data('amountofkeys', n);
  487.  
  488. t.send((ERR, STATUS) => {
  489. if (ERR) {
  490. client.chatMessage(SENDER, 'An error occurred while sending your trade. Steam Trades could be down. Please try again later.');
  491. console.log('## An error occurred while sending trade: ' + ERR);
  492. } else {
  493. client.chatMessage(SENDER, 'Trade Sent! Click here to accept: https://steamcommunity.com/tradeoffer/' + t.id);
  494. console.log('## Trade offer sent!');
  495. }
  496. });
  497. }
  498. });
  499. }
  500. }
  501. });
  502. } else if (MSG.toUpperCase().indexOf('!BUYANY') >= 0) {
  503. if (botSets) {
  504. let n = MSG.toUpperCase().replace('!BUYANY ', ''),
  505. amountofsets = parseInt(n) * CONFIG.CARDS.BUY1KEYFORAMOUNTOFSETS;
  506. if (!isNaN(n)) {
  507. if (n <= CONFIG.MESSAGES.MAXBUY) {
  508. let t = manager.createOffer(SENDER.getSteamID64());
  509. n = parseInt(n);
  510. let theirKeys = [];
  511. t.getUserDetails((ERR, ME, THEM) => {
  512. if (ERR) {
  513. console.log('## An error occurred while getting trade holds: ' + ERR);
  514. client.chatMessage(SENDER, 'An error occurred while getting your trade holds. Please try again');
  515. } else if (ME.escrowDays == 0 && THEM.escrowDays == 0) {
  516. client.chatMessage(SENDER, 'Processing your request.');
  517. manager.getUserInventoryContents(SENDER.getSteamID64(), CONFIG.KEYSFROMGAME, 2, true, (ERR, INV, CURR) => {
  518. if (ERR) {
  519. console.log('## An error occurred while getting inventory: ' + ERR);
  520. client.chatMessage(SENDER, 'An error occurred while loading your inventory. Please try later');
  521. } else {
  522. amountofB = amountofsets;
  523. for (let i = 0; i < INV.length; i++) {
  524. if (theirKeys.length < n && CONFIG.ACCEPTEDKEYS.indexOf(INV[i].market_hash_name) >= 0) {
  525. theirKeys.push(INV[i]);
  526. }
  527. }
  528. if (theirKeys.length != n) {
  529. client.chatMessage(SENDER, 'You do not have enough keys.');
  530. } else {
  531. sortSetsByAmount(botSets, (DATA) => {
  532. let setsSent = {};
  533. firstLoop: for (let i = 0; i < DATA.length; i++) {
  534. console.log(setsSent);
  535. console.log(DATA[i]);
  536. if (botSets[DATA[i]]) {
  537. for (let j = 0; j < botSets[DATA[i]].length; j++) {
  538. if (amountofB > 0) {
  539. if ((setsSent[DATA[i]] && setsSent[DATA[i]] < 5) || !setsSent[DATA[i]]) {
  540. t.addMyItems(botSets[DATA[i]][j]);
  541. console.log('DEBUG#LOOP #2 CONTINUE: ITEM ADD');
  542. amountofB--;
  543. if (!setsSent[DATA[i]]) {
  544. setsSent[DATA[i]] = 1;
  545. } else {
  546. setsSent[DATA[i]] += 1;
  547. }
  548. } else {
  549. console.log('DEBUG#LOOP #2 CONTINUE: RETURN');
  550. continue firstLoop;
  551. }
  552. } else {
  553. console.log('DEBUG#LOOP #2 CONTINUE: RETURN');
  554. continue firstLoop;
  555. }
  556. }
  557. } else {
  558. console.log('DEBUG#LOOP #2 CONTINUE: RETURN 2');
  559. continue firstLoop;
  560. }
  561. }
  562. });
  563. }
  564. }
  565. if (amountofB > 0) {
  566. client.chatMessage(SENDER, 'There are not enough sets. Please try again later.');
  567. } else {
  568. console.log('DEBUG#SENDING');
  569. t.addTheirItems(theirKeys);
  570. t.data('commandused', 'BuyAny');
  571. t.data('amountofsets', amountofsets.toString());
  572. t.data('amountofkeys', n);
  573. t.send((ERR, STATUS) => {
  574. if (ERR) {
  575. client.chatMessage(SENDER, 'An error occurred while sending your trade. Steam Trades could be down. Please try again later.');
  576. console.log('## An error occurred while sending trade: ' + ERR);
  577. } else {
  578. client.chatMessage(SENDER, 'Trade Sent! Click here to accept: https://steamcommunity.com/tradeoffer/' + t.id);
  579. console.log('## Trade offer sent!');
  580. }
  581. });
  582. }
  583. });
  584. } else {
  585. client.chatMessage(SENDER, 'Please make sure you don\'t have a trade hold!');
  586. }
  587. });
  588. } else {
  589. client.chatMessage(SENDER, 'Please try a lower amount of keys');
  590. }
  591. } else {
  592. client.chatMessage(SENDER, 'Please provide a valid amount of keys.');
  593. }
  594. } else {
  595. client.chatMessage(SENDER, 'Please try again later.');
  596. }
  597. } else if (MSG.toUpperCase().indexOf('!BUY') >= 0) {
  598. if (botSets) {
  599. let n = MSG.toUpperCase().replace('!BUY ', ''),
  600. amountofsets = parseInt(n) * CONFIG.CARDS.BUY1KEYFORAMOUNTOFSETS;
  601. if (!isNaN(n)) {
  602. if (n <= CONFIG.MESSAGES.MAXBUY) {
  603. let t = manager.createOffer(SENDER.getSteamID64());
  604. t.getUserDetails((ERR, ME, THEM) => {
  605. if (ERR) {
  606. console.log('## An error occurred while getting trade holds: ' + ERR);
  607. client.chatMessage(SENDER, 'An error occurred while getting your trade holds. Please try again');
  608. } else if (ME.escrowDays == 0 && THEM.escrowDays == 0) {
  609. n = parseInt(n);
  610. let theirKeys = [];
  611. client.chatMessage(SENDER, 'Processing your request.');
  612. manager.getUserInventoryContents(SENDER.getSteamID64(), CONFIG.KEYSFROMGAME, 2, true, (ERR, INV, CURR) => {
  613. if (ERR) {
  614. console.log('## An error occurred while getting inventory: ' + ERR);
  615. client.chatMessage(SENDER, 'An error occurred while loading your inventory. Please try later');
  616. } else {
  617. console.log('DEBUG#INV LOADED');
  618. if (!ERR) {
  619. console.log('DEBUG#INV LOADED NOERR');
  620. for (let i = 0; i < INV.length; i++) {
  621. if (theirKeys.length < n && CONFIG.ACCEPTEDKEYS.indexOf(INV[i].market_hash_name) >= 0) {
  622. theirKeys.push(INV[i]);
  623. }
  624. }
  625. if (theirKeys.length != n) {
  626. client.chatMessage(SENDER, 'You do not have enough keys.');
  627. } else {
  628. Utils.getBadges(SENDER.getSteamID64(), (ERR, DATA) => {
  629. if (!ERR) {
  630. console.log('DEBUG#BADGE LOADED');
  631. if (!ERR) {
  632. let b = {}; // List with badges that CAN still be crafted
  633. if (DATA) {
  634. for (let i = 0; i < Object.keys(DATA).length; i++) {
  635. if (DATA[Object.keys(DATA)[i]] < 6) {
  636. b[Object.keys(DATA)[i]] = 5 - DATA[Object.keys(DATA)[i]];
  637. }
  638. }
  639. } else {
  640. client.chatMessage(SENDER.getSteamID64(), 'Your badges are empty, sending an offer without checking badges.');
  641. }
  642. console.log(DATA);
  643. console.log(b);
  644. // TODO: COUNT AMOUNT OF SETS BOT CAN GIVE HIM
  645. // 1: GET BOTS CARDS. DONE
  646. // 2: GET PLAYER's BADGES. DONE
  647. // 3: MAGIC
  648. let hisMaxSets = 0,
  649. botNSets = 0;
  650. // Loop for sets he has partially completed
  651. for (let i = 0; i < Object.keys(b).length; i++) {
  652. if (botSets[Object.keys(b)[i]] && botSets[Object.keys(b)[i]].length >= 5 - b[Object.keys(b)[i]].length) {
  653. hisMaxSets += 5 - b[Object.keys(b)[i]].length;
  654. }
  655. }
  656. console.log('DEBUG#LOOP 1 DONE');
  657. // Loop for sets he has never crafted
  658. for (let i = 0; i < Object.keys(botSets).length; i++) {
  659. if (Object.keys(b).indexOf(Object.keys(botSets)[i]) < 0) {
  660. if (botSets[Object.keys(botSets)[i]].length >= 5) {
  661. hisMaxSets += 5;
  662. } else {
  663. hisMaxSets += botSets[Object.keys(botSets)[i]].length;
  664. }
  665. }
  666. botNSets += botSets[Object.keys(botSets)[i]].length;
  667. }
  668. console.log('DEBUG#LOOP 2 DONE');
  669. // HERE
  670. if (amountofsets <= hisMaxSets) {
  671. hisMaxSets = amountofsets;
  672. console.log('DEBUG#TRADE CREATED');
  673. sortSetsByAmount(botSets, (DATA) => {
  674. console.log('DEBUG#' + DATA);
  675. console.log('DEBUG#SETS SORTED');
  676. firstLoop: for (let i = 0; i < DATA.length; i++) {
  677. if (b[DATA[i]] == 0) {
  678. continue firstLoop;
  679. } else {
  680. console.log('DEBUG#' + i);
  681. console.log('DEBUG#FOR LOOP ITEMS');
  682. if (hisMaxSets > 0) {
  683. console.log('DEBUG#MAXSETSMORETHAN1');
  684. if (b[DATA[i]] && botSets[DATA[i]].length >= b[DATA[i]]) {
  685. // BOT HAS ENOUGH SETS OF THIS KIND
  686. console.log('DEBUG#LOOP #1');
  687. for (let j = 0; j < 5 - b[DATA[i]]; j++) {
  688. if (j + 1 < b[DATA[i]] && hisMaxSets > 0) {
  689. console.log('DEBUG#LOOP #1: ITEM ADD');
  690. console.log('DEBUG#LOOP #1: ' + botSets[DATA[i]][j]);
  691. t.addMyItems(botSets[DATA[i]][j]);
  692. hisMaxSets--;
  693. console.log(hisMaxSets);
  694. } else {
  695. console.log('DEBUG#LOOP #1: RETURN');
  696. continue firstLoop;
  697. }
  698. }
  699. } else if (b[DATA[i]] && botSets[DATA[i]].length < b[DATA[i]]) {
  700. // BOT DOESNT HAVE ENOUGH SETS OF THIS KIND
  701. console.log('DEBUG#LOOP #1 CONTINUE');
  702. continue; // *
  703. } 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
  704. // BOT HAS ENOUGH SETS AND USER NEVER CRAFTED THIS
  705. for (let j = 0; j < botSets[DATA[i]].length - b[DATA[i]]; j++) {
  706. if (botSets[DATA[i]][j] && hisMaxSets > 0) {
  707. t.addMyItems(botSets[DATA[i]][j]);
  708. console.log('DEBUG#LOOP #2 CONTINUE: ITEM ADD');
  709. hisMaxSets--;
  710. } else {
  711. console.log('DEBUG#LOOP #2 CONTINUE: RETURN');
  712. continue firstLoop;
  713. }
  714. }
  715. }
  716. else if (hisMaxSets < 5) {
  717. // BOT DOESNT HAVE CARDS USER AREADY CRAFTED, IF USER STILL NEEDS 5 SETS:
  718. console.log('DEBUG#LOOP #2');
  719. for (let j = 0; j != hisMaxSets; j++) {
  720. if (botSets[DATA[i]][j] && hisMaxSets > 0) {
  721. t.addMyItems(botSets[DATA[i]][j]);
  722. console.log('DEBUG#LOOP #2: ITEM ADD');
  723. hisMaxSets--;
  724. console.log(hisMaxSets);
  725. } else {
  726. console.log('DEBUG#LOOP #2: RETURN');
  727. continue firstLoop;
  728. }
  729. }
  730. } else {
  731. // BOT DOESNT HAVE CARDS USER AREADY CRAFTED, IF USER STILL NEEDS LESS THAN 5 SETS:
  732. console.log('DEBUG#LOOP #2');
  733. for (let j = 0; j != 5; j++ && hisMaxSets > 0) {
  734. if (botSets[DATA[i]][j] && hisMaxSets > 0) {
  735. t.addMyItems(botSets[DATA[i]][j]);
  736. console.log('DEBUG#LOOP #2: ITEM ADD');
  737. hisMaxSets--;
  738. console.log(hisMaxSets);
  739. } else {
  740. console.log('DEBUG#LOOP #2: RETURN');
  741. continue firstLoop;
  742. }
  743. }
  744. }
  745. } else {
  746. console.log('DEBUG#RETURN');
  747. break firstLoop;
  748. }
  749. }
  750. }
  751. if (hisMaxSets > 0) {
  752. client.chatMessage(SENDER, 'There are not enough sets. Please try again later.');
  753. } else {
  754. console.log('DEBUG#SENDING');
  755. t.addTheirItems(theirKeys);
  756. t.data('commandused', 'Buy');
  757. t.data('amountofkeys', n);
  758. t.data('amountofsets', amountofsets.toString());
  759. t.send((ERR, STATUS) => {
  760. if (ERR) {
  761. client.chatMessage(SENDER, 'An error occurred while sending your trade. Steam Trades could be down. Please try again later.');
  762. console.log('## An error occurred while sending trade: ' + ERR);
  763. } else {
  764. client.chatMessage(SENDER, 'Trade Sent! Click here to accept: https://steamcommunity.com/tradeoffer/' + t.id);
  765. console.log('## Trade offer sent');
  766. }
  767. });
  768. }
  769. });
  770. } else {
  771. 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.');
  772. }
  773. // TO HERE
  774. } else {
  775. console.log('An error occurred while getting badges: ' + ERR);
  776. }
  777. } else {
  778. client.chatMessage(SENDER, 'An error occurred while getting your badges. Please try again.');
  779. console.log(SENDER, '## An error occurred while loading badges: ' + ERR);
  780. }
  781. });
  782. }
  783. } else {
  784. console.log('## An error occurred while getting inventory: ' + ERR);
  785. client.chatMessage(SENDER, 'An error occurred while loading your inventory, please make sure it\'s set to public.');
  786. }
  787. }
  788. });
  789. } else {
  790. client.chatMessage(SENDER, 'Please make sure you don\'t have a trade hold!');
  791. }
  792. });
  793. } else {
  794. client.chatMessage(SENDER, 'Please try a lower amount of keys.');
  795. }
  796. } else {
  797. client.chatMessage(SENDER, 'Please provide a valid amount of keys.');
  798. }
  799. } else {
  800. client.chatMessage(SENDER, 'Please try again later.');
  801. }
  802. } else if (MSG.toUpperCase() == '!PROOF') {
  803. client.chatMessage(SENDER, 'Bot hardly edited by Michaukso');
  804. } else if (CONFIG.ADMINS.indexOf(SENDER.getSteamID64()) >= 0 || CONFIG.ADMINS.indexOf(parseInt(SENDER.getSteamID64())) >= 0) {
  805. // Admin commands.
  806. if (MSG.toUpperCase().indexOf('!BLOCK') >= 0) {
  807. let n = MSG.toUpperCase().replace('!BLOCK ', '').toString();
  808. if (SID64REGEX.test(n)) {
  809. client.chatMessage(SENDER, 'User blocked.');
  810. client.blockUser(n);
  811. } else {
  812. client.chatMessage(SENDER, 'Please provide a valid SteamID64');
  813. }
  814. } else if (MSG.toUpperCase().indexOf('!GAME') >= 0) {
  815. let n = MSG.replace('!game ', '').toString();
  816. client.gamesPlayed([`${CONFIG.PLAYGAMES} | ${n}`]);
  817. } else if (MSG.toUpperCase().indexOf('!USERCHECK') >= 0) {
  818. let n = MSG.toUpperCase().replace('!USERCHECK ', '').toString();
  819. if (SID64REGEX.test(n)) {
  820. if (Object.keys(botSets).length > 0) {
  821. client.chatMessage(SENDER, 'Loading badges...');
  822. Utils.getBadges(n, (ERR, DATA) => {
  823. if (!ERR) {
  824. let b = {}; // List with badges that CAN still be crafted
  825. if (DATA) {
  826. for (let i = 0; i < Object.keys(DATA).length; i++) {
  827. if (DATA[Object.keys(DATA)[i]] < 6) {
  828. b[Object.keys(DATA)[i]] = 5 - DATA[Object.keys(DATA)[i]];
  829. }
  830. }
  831. } else {
  832. client.chatMessage(SENDER.getSteamID64(), n + '\'s badges are empty, sending an offer without checking badges.');
  833. }
  834. // console.log(b);
  835. // TODO: COUNT AMOUNT OF SETS BOT CAN GIVE HIM
  836. // 1: GET BOTS CARDS. DONE
  837. // 2: GET PLAYER's BADGES. DONE
  838. // 3: MAGIC
  839. let hisMaxSets = 0,
  840. botNSets = 0;
  841. // Loop for sets he has partially completed
  842. for (let i = 0; i < Object.keys(b).length; i++) {
  843. if (botSets[Object.keys(b)[i]] && botSets[Object.keys(b)[i]].length >= 5 - b[Object.keys(b)[i]].length) {
  844. hisMaxSets += 5 - b[Object.keys(b)[i]].length;
  845. }
  846. }
  847. // Loop for sets he has never crafted
  848. for (let i = 0; i < Object.keys(botSets).length; i++) {
  849. if (Object.keys(b).indexOf(Object.keys(botSets)[i]) < 0) {
  850. if (botSets[Object.keys(botSets)[i]].length >= 5) {
  851. hisMaxSets += 5;
  852. } else {
  853. hisMaxSets += botSets[Object.keys(botSets)[i]].length;
  854. }
  855. }
  856. botNSets += botSets[Object.keys(botSets)[i]].length;
  857. }
  858. 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.');
  859. } else {
  860. client.chatMessage(SENDER, 'An error occurred while getting ' + n + '\'s badges. Please try again.');
  861. console.log('An error occurred while getting badges: ' + ERR);
  862. }
  863. });
  864. } else {
  865. client.chatMessage(SENDER, 'Please try again later.');
  866. }
  867. } else {
  868. client.chatMessage(SENDER, 'Please provide a valid SteamID64.');
  869. }
  870. }
  871. } else {
  872. client.chatMessage(SENDER, 'Command not recognized. Use !help to see how this bot works.');
  873. }
  874. });
  875.  
  876. client.on('friendRelationship', (SENDER, REL) => {
  877. if (REL === 2) {
  878. client.addFriend(SENDER);
  879. } else if (REL === 3) {
  880. if (CONFIG.INVITETOGROUPID) {
  881. client.inviteToGroup(SENDER, CONFIG.INVITETOGROUPID);
  882. }
  883. client.chatMessage(SENDER, CONFIG.MESSAGES.WELCOME);
  884. }
  885. });
  886.  
  887. manager.on('sentOfferChanged', (OFFER, OLDSTATE) => {
  888. Utils.getInventory(client.steamID.getSteamID64(), community, (ERR, DATA) => {
  889. if (!ERR) {
  890. let s = DATA;
  891. Utils.getSets(s, allCards, (ERR, DATA) => {
  892. if (!ERR) {
  893. botSets = DATA;
  894. console.log('## Bot\'s sets loaded.');
  895. } else {
  896. console.log('## An error occurred while getting bot sets: ' + ERR);
  897. }
  898. });
  899. } else {
  900. console.log('## An error occurred while getting bot inventory: ' + ERR);
  901. }
  902. });
  903. if (OFFER.state == 3) {
  904. if (CONFIG.INVITETOGROUPID) {
  905. client.inviteToGroup(OFFER.partner, CONFIG.INVITETOGROUPID);
  906. }
  907. let d = '' + OFFER.data('commandused') + '';
  908. d += '\nSets: ' + OFFER.data('amountofsets');
  909. d += '\nKeys: ' + OFFER.data('amountofkeys');
  910. d += '\nSteamID: ' + OFFER.partner.getSteamID64();
  911. // MICHAUKSO
  912. for (let j = 0; j < CONFIG.ADMINS.length; j++) {
  913. client.chatMessage(CONFIG.ADMINS[j], OFFER.data('commandused') + ' ' + OFFER.data('amountofkeys') + 'k ' + OFFER.partner.getSteamID64());
  914. }
  915. // /MICHAUKSO
  916.  
  917. fs.writeFile('./TradesAccepted/' + OFFER.id + '-' + OFFER.partner.getSteamID64() + '.txt', d, (ERR) => {
  918. if (ERR) {
  919. console.log('## An error occurred while writing trade file: ' + ERR);
  920. }
  921. });
  922. community.getSteamUser(OFFER.partner, (ERR, USER) => {
  923. if (ERR) {
  924. console.log('## An error occurred while getting user profile: ' + ERR);
  925. client.chatMessage(USER.steamID, 'An error occurred while getting your profile (to comment).');
  926. } else {
  927. USER.comment(CONFIG.COMMENTAFTERTRADE, (ERR) => {
  928. if (ERR) {
  929. console.log('## An error occurred while commenting on user profile: ' + ERR);
  930. client.chatMessage(USER.steamID, 'An error occurred while getting commenting on your profile.');
  931. } else {
  932. client.chatMessage(USER.steamID, 'Thanks for trading! :D');
  933. }
  934. });
  935. }
  936. });
  937. } else if (OFFER.state == 6) {
  938. client.chatMessage(OFFER.partner, 'Hey, you did not accept the offer. Please try again if you wish to receive sets!');
  939. }
  940. });
  941.  
  942. manager.on('newOffer', (OFFER) => {
  943. if (CONFIG.ADMINS.indexOf(OFFER.partner.getSteamID64()) >= 0 || CONFIG.ADMINS.indexOf(parseInt(OFFER.partner.getSteamID64())) >= 0) {
  944. OFFER.getUserDetails((ERR, ME, THEM) => {
  945. if (ERR) {
  946. console.log('## An error occurred while getting trade holds: ' + ERR);
  947. client.chatMessage(OFFER.partner, 'An error occurred while getting your trade holds. Please try again');
  948. OFFER.decline((ERR) => {
  949. if (ERR) {
  950. console.log('## An error occurred while declining trade: ' + ERR);
  951. }
  952. });
  953. } else if (ME.escrowDays == 0 && THEM.escrowDays == 0) {
  954. OFFER.accept((ERR) => {
  955. if (ERR) {
  956. console.log('## An error occurred while declining trade: ' + ERR);
  957. OFFER.decline((ERR) => {
  958. if (ERR) {
  959. console.log('## An error occurred while declining trade: ' + ERR);
  960. }
  961. });
  962. } else {
  963. client.chatMessage(OFFER.partner, 'Offer accepted!');
  964. }
  965. });
  966. } else {
  967. client.chatMessage(OFFER.partner, 'Please make sure you don\'t have a trade hold!');
  968. OFFER.decline((ERR) => {
  969. if (ERR) {
  970. console.log('## An error occurred while declining trade: ' + ERR);
  971. }
  972. });
  973. }
  974. });
  975. } else if (OFFER.itemsToGive.length == 0) {
  976. let onlySteam = true;
  977. for (let i = 0; i < OFFER.itemsToReceive.length; i++) {
  978. if (OFFER.itemsToReceive[i].appid != 753) {
  979. onlySteam = false;
  980. }
  981. }
  982. if (onlySteam) {
  983. OFFER.accept((ERR) => {
  984. if (ERR) {
  985. console.log('## An error occurred while declining trade: ' + ERR);
  986. }
  987. });
  988. }
  989. } else {
  990. OFFER.decline((ERR) => {
  991. if (ERR) {
  992. console.log('## An error occurred while declining trade: ' + ERR);
  993. }
  994. });
  995. }
  996. });
  997.  
  998. function sortSetsByAmount(SETS, callback) {
  999. callback(Object.keys(SETS).sort((k1, k2) => SETS[k1].length - SETS[k2].length).reverse());
  1000. }
  1001.  
  1002. function sortSetsByAmountB(SETS, callback) {
  1003. callback(Object.keys(SETS).sort((k1, k2) => SETS[k1].length - SETS[k2].length));
  1004. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement