Advertisement
Guest User

Untitled

a guest
Jun 18th, 2017
1,106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 95.36 KB | None | 0 0
  1. let SteamUser = require("steam-user"),
  2. SteamTotp = require("steam-totp"),
  3. TradeOfferManager = require("steam-tradeoffer-manager"),
  4. SteamCommunity = require("steamcommunity"),
  5. Utils = require("./utils.js"),
  6. CONFIG = require("./SETTINGS/config.js"),
  7. allCards = {},
  8. botSets = {},
  9. fs = require("fs"),
  10. users = {},
  11. userMsgs = {},
  12. SID64REGEX = new RegExp(/^[0-9]{17}$/),
  13. chatLogs = "",
  14. userLogs = {},
  15. totalBotSets = 0,
  16. setsThatShouldntBeSent = [];
  17.  
  18.  
  19. let client = new SteamUser(),
  20. manager = new TradeOfferManager({
  21. "steam": client,
  22. "language": "en",
  23. "pollInterval": "10000",
  24. "cancelTime": "7200000" // 2 hours in ms
  25. }),
  26. community = new SteamCommunity();
  27.  
  28. fs.readFile("./UserData/Users.json", (ERR, DATA) => {
  29. if (ERR) {
  30. console.log("## An error occurred while getting Users: " + ERR);
  31. } else {
  32. users = JSON.parse(DATA);
  33. }
  34. });
  35.  
  36. Utils.getCardsInSets((ERR, DATA) => {
  37. if (!ERR) {
  38. allCards = DATA;
  39. console.log("Card data loaded. [" + Object.keys(DATA).length + "]");
  40. } else {
  41. console.log("An error occurred while getting cards: " + ERR);
  42. }
  43. });
  44.  
  45. setTimeout(() => {
  46. console.log('Restarting...');
  47. process.exit(1);
  48. }, 2*1000*60*60);
  49.  
  50. setInterval(() => {
  51. for (let i = 0; i < Object.keys(users).length; i++) {
  52. if (users[Object.keys(users)[i]].idleforhours >= CONFIG.MAXHOURSADDED) {
  53. 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.");
  54. client.removeFriend(Object.keys(users)[i]);
  55. delete users[Object.keys(users)[i]];
  56. fs.writeFile("./UserData/Users.json", JSON.stringify(users), (ERR) => {
  57. if (ERR) {
  58. console.log("## An error occurred while writing UserData file: " + ERR);
  59. }
  60. });
  61. } else {
  62. users[Object.keys(users)[i]].idleforhours += 1;
  63. fs.writeFile("./UserData/Users.json", JSON.stringify(users), (ERR) => {
  64. if (ERR) {
  65. console.log("## An error occurred while writing UserData file: " + ERR);
  66. }
  67. });
  68. }
  69. }
  70. }, 1000 * 60 * 60);
  71.  
  72. setInterval(() => {
  73. for (let i = 0; i < Object.keys(userMsgs).length; i++) {
  74. if (userMsgs[Object.keys(userMsgs)[i]] > CONFIG.MAXMSGPERSEC) {
  75. client.chatMessage(Object.keys(userMsgs)[i], "You have been removed for spamming. Another offense will get you blocked.");
  76. client.removeFriend(Object.keys(userMsgs)[i]);
  77. for (let j = 0; j < CONFIG.ADMINS.length; j++) {
  78. client.chatMessage(CONFIG.ADMINS[j], "User #" + Object.keys(userMsgs)[i] + " has been removed for spamming. To block him use !block [STEAMID64]");
  79. }
  80. }
  81. }
  82. userMsgs = {};
  83. }, 1000);
  84.  
  85. client.logOn({
  86. accountName: CONFIG.USERNAME,
  87. password: CONFIG.PASSWORD,
  88. twoFactorCode: SteamTotp.getAuthCode(CONFIG.SHAREDSECRET)
  89. });
  90.  
  91. client.on("loggedOn", (details, parental) => {
  92. client.getPersonas([client.steamID], (personas) => {
  93. console.log("## Logged in as #" + client.steamID + " (" + personas[client.steamID].player_name + ")");
  94. });
  95. client.setPersona(1);
  96. });
  97.  
  98. client.on("webSession", (sessionID, cookies) => {
  99. manager.setCookies(cookies, (ERR) => {
  100. if (ERR) {
  101. console.log("## An error occurred while setting cookies.");
  102. } else {
  103. console.log("## Websession created and cookies set.");
  104. }
  105. });
  106. // Add people that added the bot while it was online.
  107. for (let i = 0; i < Object.keys(client.myFriends).length; i++) {
  108. if (client.myFriends[Object.keys(client.myFriends)[i]] == 2) {
  109. client.addFriend(Object.keys(client.myFriends)[i]);
  110. }
  111. }
  112. community.setCookies(cookies);
  113. community.startConfirmationChecker(10000, CONFIG.IDENTITYSECRET);
  114. client.blockUser(""); // Some people have tried to either crash the bot or buy it not directly from me, I'm going to make bots block them just in case. :)
  115. Utils.getInventory(client.steamID.getSteamID64(), community, (ERR, DATA) => {
  116. console.log("DEBUG#INVLOADED");
  117. if (!ERR) {
  118. let s = DATA;
  119. Utils.getSets(s, allCards, (ERR, DATA) => {
  120. console.log("DEBUG#SETSLOADED");
  121. if (!ERR) {
  122. botSets = DATA;
  123. console.log("## Bot's sets loaded.");
  124. let botNSets = 0;
  125. for (let i = 0; i < Object.keys(botSets).length; i++) {
  126. botNSets += botSets[Object.keys(botSets)[i]].length;
  127. }
  128. totalBotSets = botNSets;
  129. let playThis = CONFIG.PLAYGAMES;
  130. if (CONFIG.PLAYGAMES && typeof(CONFIG.PLAYGAMES[0]) == "string") {
  131. playThis[0] = parseString(playThis[0], totalBotSets);
  132. }
  133. client.gamesPlayed(playThis);
  134. } else {
  135. console.log("## An error occurred while getting bot sets: " + ERR);
  136. process.exit();
  137. }
  138. });
  139. } else {
  140. console.log("## An error occurred while getting bot inventory: " + ERR);
  141. }
  142. });
  143. });
  144.  
  145. community.on("sessionExpired", (ERR) => {
  146. console.log("## Session Expired. Relogging.");
  147. client.webLogOn();
  148. });
  149.  
  150. client.on("friendMessage", (SENDER, MSG) => {
  151. if (userLogs[SENDER.getSteamID64()]) {
  152. userLogs[SENDER.getSteamID64()].push(MSG);
  153. } else {
  154. userLogs[SENDER.getSteamID64()] = [];
  155. userLogs[SENDER.getSteamID64()].push(MSG);
  156. }
  157. fs.writeFile("./ChatLogs/UserLogs/" + SENDER.getSteamID64() + "-log-" + new Date().getDate() + "-" + new Date().getMonth() + "-" + new Date().getFullYear() + ".json", JSON.stringify({ logs: userLogs[SENDER.getSteamID64()] }), (ERR) => {
  158. if (ERR) {
  159. console.log("## An error occurred while writing UserLogs file: " + ERR);
  160. }
  161. });
  162. chatLogs += SENDER.getSteamID64() + " : " + MSG + "\n";
  163. fs.writeFile("./ChatLogs/FullLogs/log-" + new Date().getDate() + "-" + new Date().getMonth() + "-" + new Date().getFullYear() + ".txt", chatLogs, (ERR) => {
  164. if (ERR) {
  165. console.log("## An error occurred while writing FullLogs file: " + ERR);
  166. }
  167. });
  168. if (Object.keys(users).indexOf(SENDER.getSteamID64()) < 0) {
  169. users[SENDER.getSteamID64()] = {};
  170. users[SENDER.getSteamID64()].idleforhours = 0;
  171. fs.writeFile("./UserData/Users.json", JSON.stringify(users), (ERR) => {
  172. if (ERR) {
  173. console.log("## An error occurred while writing UserData file: " + ERR);
  174. }
  175. });
  176. } else {
  177. users[SENDER.getSteamID64()].idleforhours = 0;
  178. }
  179. if (userMsgs[SENDER.getSteamID64()]) {
  180. userMsgs[SENDER.getSteamID64()]++;
  181. } else {
  182. userMsgs[SENDER.getSteamID64()] = 1;
  183. }
  184. /*if (MSG.toUpperCase() == "!STOCK") {
  185. for (let i = 0; i < botSets.length; i++) {
  186. //
  187. }
  188. } else */
  189.  
  190.  
  191.  
  192. if (MSG.toUpperCase().indexOf("!LEVEL") >= 0) {
  193. let n = parseInt(MSG.toUpperCase().replace("!LEVEL ", ""));
  194. if (!isNaN(n) && parseInt(n) > 0) {
  195. if (n <= CONFIG.MESSAGES.MAXLEVEL) {
  196. Utils.getBadges(SENDER.getSteamID64(), (ERR, DATA, CURRENTLEVEL, XPNEEDED) => {
  197. if (!ERR) {
  198. if (DATA) {
  199. if (n > CURRENTLEVEL) {
  200. let s = 0,
  201. l = 0;
  202. for (let i = 0; i < (n - CURRENTLEVEL); i++) {
  203. s += parseInt((CURRENTLEVEL + l) / 10) + 1;
  204. l++;
  205. }
  206. 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.");
  207. } else {
  208. client.chatMessage(SENDER, "Please provide a valid level.");
  209. }
  210. } else {
  211. client.chatMessage(SENDER, "Your level could not be retrieved. Make sure your Steam Profile is public and try again.");
  212. }
  213. } else {
  214. console.log("## An error occurred while getting badge data: " + ERR);
  215. client.chatMessage(SENDER, "An error occurred while loading your badges. Please try again later.");
  216. }
  217. });
  218. } else {
  219. client.chatMessage(SENDER, "Please try a lower level.");
  220. }
  221. } else {
  222. client.chatMessage(SENDER, "Please provide a valid level.");
  223. }
  224. } else if (MSG.toUpperCase() === "!HELP") {
  225. client.chatMessage(SENDER, CONFIG.MESSAGES.HELP);
  226. } else if (MSG.toUpperCase().indexOf("!BUYONECHECK") >= 0) {
  227. client.chatMessage(SENDER, "Loading badges...");
  228. Utils.getBadges(SENDER.getSteamID64(), (ERR, DATA) => {
  229. if (!ERR) {
  230. let b = {}; // List with badges that CAN still be crafted
  231. if (DATA) {
  232. for (let i = 0; i < Object.keys(DATA).length; i++) {
  233. if (DATA[Object.keys(DATA)[i]] < 6) {
  234. b[Object.keys(DATA)[i]] = 5 - DATA[Object.keys(DATA)[i]];
  235. }
  236. }
  237. } else {
  238. client.chatMessage(SENDER.getSteamID64(), "Your badges are empty, sending an offer without checking badges.");
  239. }
  240. // console.log(b);
  241. // TODO: COUNT AMOUNT OF SETS BOT CAN GIVE HIM
  242. // 1: GET BOTS CARDS. DONE
  243. // 2: GET PLAYER's BADGES. DONE
  244. // 3: MAGIC
  245. let hisMaxSets = 0,
  246. botNSets = 0;
  247. // Loop for sets he has partially completed
  248. for (let i = 0; i < Object.keys(b).length; i++) {
  249. if (botSets[Object.keys(b)[i]] && botSets[Object.keys(b)[i]].length >= 5 - b[Object.keys(b)[i]].length) {
  250. hisMaxSets += 5 - b[Object.keys(b)[i]].length;
  251. }
  252. }
  253. // Loop for sets he has never crafted
  254. for (let i = 0; i < Object.keys(botSets).length; i++) {
  255. if (Object.keys(b).indexOf(Object.keys(botSets)[i]) < 0) {
  256. if (botSets[Object.keys(botSets)[i]].length >= 1) {
  257. hisMaxSets += 1;
  258. }
  259. }
  260. botNSets += botSets[Object.keys(botSets)[i]].length;
  261. }
  262. totalBotSets = botNSets;
  263. let playThis = CONFIG.PLAYGAMES;
  264. if (CONFIG.PLAYGAMES && typeof(CONFIG.PLAYGAMES[0]) == "string") {
  265. playThis[0] = parseString(playThis[0], totalBotSets);
  266. }
  267. client.gamesPlayed(playThis);
  268. client.chatMessage(SENDER, "There are currently sets from " + Object.keys(botSets).length + " different games, of which you have not crafted " + hisMaxSets + ". This would cost " + parseInt(hisMaxSets / CONFIG.CARDS.BUY1KEYFORAMOUNTOFSETS * 100) / 100 + " keys.");
  269. } else {
  270. client.chatMessage(SENDER, "An error occurred while getting your badges. Please try again.");
  271. console.log("An error occurred while getting badges: " + ERR);
  272. }
  273. });
  274. } else if (MSG.toUpperCase().indexOf("!SELLCHECK") >= 0) {
  275. let n = parseInt(MSG.toUpperCase().replace("!SELLCHECK ", ""));
  276. client.chatMessage(SENDER, "Loading inventory...");
  277.  
  278. Utils.getInventory(SENDER.getSteamID64(), community, (ERR, DATA) => {
  279. console.log("DEBUG#INVLOADED");
  280. if (!ERR) {
  281. let s = DATA;
  282. Utils.getSets(s, allCards, (ERR, DATA) => {
  283. console.log("DEBUG#SETSLOADED");
  284. if (!ERR) {
  285.  
  286. // console.log(b);
  287. // TODO: COUNT AMOUNT OF SETS BOT CAN GIVE HIM
  288. // 1: GET BOTS CARDS. DONE
  289. // 2: GET PLAYER's BADGES. DONE
  290. // 3: MAGIC
  291. let hisMaxSets = 0,
  292. botNSets = 0;
  293. // Loop for sets he has partially completed
  294. // Loop for sets he has never crafted
  295. for (let i = 0; i < Object.keys(DATA).length; i++) {
  296. if (DATA[Object.keys(DATA)[i]].length >= 5) {
  297. hisMaxSets += 5;
  298. } else {
  299. hisMaxSets += DATA[Object.keys(DATA)[i]].length;
  300. }
  301. botNSets += DATA[Object.keys(DATA)[i]].length;
  302. }
  303. totalBotSets = botNSets;
  304. let playThis = CONFIG.PLAYGAMES;
  305. if (CONFIG.PLAYGAMES && typeof(CONFIG.PLAYGAMES[0]) == "string") {
  306. playThis[0] = parseString(playThis[0], totalBotSets);
  307. }
  308. client.gamesPlayed(playThis);
  309. 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.");
  310. } else {
  311. console.log("## An error occurred while getting user sets: " + ERR);
  312. }
  313. });
  314. } else {
  315. console.log("## An error occurred while getting user inventory: " + ERR);
  316. }
  317. });
  318. } else if (MSG.toUpperCase().indexOf("!CHECK") >= 0) {
  319. let n = parseInt(MSG.toUpperCase().replace("!CHECK ", ""));
  320. if (!isNaN(n) && parseInt(n) > 0) {
  321. client.chatMessage(SENDER, "With " + n + " keys you can get " + n * CONFIG.CARDS.BUY1KEYFORAMOUNTOFSETS + " sets.");
  322. } else {
  323. if (Object.keys(botSets).length > 0) {
  324. client.chatMessage(SENDER, "Loading badges...");
  325. Utils.getBadges(SENDER.getSteamID64(), (ERR, DATA) => {
  326. if (!ERR) {
  327. let b = {}; // List with badges that CAN still be crafted
  328. if (DATA) {
  329. for (let i = 0; i < Object.keys(DATA).length; i++) {
  330. if (DATA[Object.keys(DATA)[i]] < 6) {
  331. b[Object.keys(DATA)[i]] = 5 - DATA[Object.keys(DATA)[i]];
  332. }
  333. }
  334. } else {
  335. client.chatMessage(SENDER.getSteamID64(), "Your badges are empty, sending an offer without checking badges.");
  336. }
  337. // console.log(b);
  338. // TODO: COUNT AMOUNT OF SETS BOT CAN GIVE HIM
  339. // 1: GET BOTS CARDS. DONE
  340. // 2: GET PLAYER's BADGES. DONE
  341. // 3: MAGIC
  342. let hisMaxSets = 0,
  343. botNSets = 0;
  344. // Loop for sets he has partially completed
  345. for (let i = 0; i < Object.keys(b).length; i++) {
  346. if (botSets[Object.keys(b)[i]] && botSets[Object.keys(b)[i]].length >= 5 - b[Object.keys(b)[i]].length) {
  347. hisMaxSets += 5 - b[Object.keys(b)[i]].length;
  348. }
  349. }
  350. // Loop for sets he has never crafted
  351. for (let i = 0; i < Object.keys(botSets).length; i++) {
  352. if (Object.keys(b).indexOf(Object.keys(botSets)[i]) < 0) {
  353. if (botSets[Object.keys(botSets)[i]].length >= 5) {
  354. hisMaxSets += 5;
  355. } else {
  356. hisMaxSets += botSets[Object.keys(botSets)[i]].length;
  357. }
  358. }
  359. botNSets += botSets[Object.keys(botSets)[i]].length;
  360. }
  361. totalBotSets = botNSets;
  362. let playThis = CONFIG.PLAYGAMES;
  363. if (CONFIG.PLAYGAMES && typeof(CONFIG.PLAYGAMES[0]) == "string") {
  364. playThis[0] = parseString(playThis[0], totalBotSets);
  365. }
  366. client.gamesPlayed(playThis);
  367. 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.");
  368. } else {
  369. client.chatMessage(SENDER, "An error occurred while getting your badges. Please try again.");
  370. console.log("An error occurred while getting badges: " + ERR);
  371. }
  372. });
  373. } else {
  374. client.chatMessage(SENDER, "Please try again later.");
  375. }
  376. }
  377. } else if (MSG.toUpperCase().indexOf("!SELL") >= 0) {
  378. if (botSets) {
  379. let n = parseInt(MSG.toUpperCase().replace("!SELL ", "")),
  380. amountofsets = n * CONFIG.CARDS.GIVE1KEYPERAMOUNTOFSETS;
  381. if (!isNaN(n) && parseInt(n) > 0) {
  382. if (n <= CONFIG.MESSAGES.MAXSELL) {
  383. client.chatMessage(SENDER, "Processing your request.");
  384. let botKeys = [],
  385. t = manager.createOffer(SENDER.getSteamID64());
  386. t.getUserDetails((ERR, ME, THEM) => {
  387. if (ERR) {
  388. console.log("## An error occurred while getting trade holds: " + ERR);
  389. client.chatMessage(SENDER, "An error occurred while getting your trade holds. Please try again");
  390. } else if (ME.escrowDays == 0 && THEM.escrowDays == 0) {
  391. manager.getUserInventoryContents(client.steamID.getSteamID64(), CONFIG.KEYSFROMGAME, 2, true, (ERR, INV, CURR) => {
  392. if (ERR) {
  393. console.log("## An error occurred while getting bot inventory: " + ERR);
  394. client.chatMessage(SENDER, "An error occurred while loading the bot's inventory. Please try again.");
  395. } else {
  396. for (let i = 0; i < INV.length; i++) {
  397. if (botKeys.length < n && CONFIG.ACCEPTEDKEYS.indexOf(INV[i].market_hash_name) >= 0) {
  398. botKeys.push(INV[i]);
  399. }
  400. }
  401. if (botKeys.length != n) {
  402. client.chatMessage(SENDER, "The bot does not have enough keys.");
  403. } else {
  404. let amountofB = amountofsets;
  405. Utils.getInventory(SENDER.getSteamID64(), community, (ERR, DATA) => {
  406. if (!ERR) {
  407. let s = DATA;
  408. Utils.getSets(s, allCards, (ERR, DDATA) => {
  409. if (!ERR) {
  410. sortSetsByAmountB(s, (DATA) => {
  411. let setsSent = {};
  412. firsttLoop: for (let i = 0; i < DATA.length; i++) {
  413. console.log(setsSent);
  414. console.log(DATA[i]);
  415. if (DDATA[DATA[i]]) {
  416. for (let j = 0; j < DDATA[DATA[i]].length; j++) {
  417. if (amountofB > 0) {
  418. if ((setsSent[DATA[i]] && setsSent[DATA[i]] < CONFIG.CARDS.MAXSETSELL) || !setsSent[DATA[i]]) {
  419. t.addTheirItems(DDATA[DATA[i]][j]);
  420. console.log("DEBUG#LOOP #2 CONTINUE: ITEM ADD");
  421. amountofB--;
  422. if (!setsSent[DATA[i]]) {
  423. setsSent[DATA[i]] = 1;
  424. } else {
  425. setsSent[DATA[i]] += 1;
  426. }
  427. } else {
  428. console.log("DEBUG#LOOP #2 CONTINUE: RETURN");
  429. continue firsttLoop;
  430. }
  431. } else {
  432. console.log("DEBUG#LOOP #2 CONTINUE: RETURN");
  433. continue firsttLoop;
  434. }
  435. }
  436. } else {
  437. console.log("DEBUG#LOOP #2 CONTINUE: RETURN 2");
  438. continue firsttLoop;
  439. }
  440. }
  441. });
  442. if (amountofB > 0) {
  443. 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.");
  444. } else {
  445. console.log("DEBUG#SENDING");
  446. t.addMyItems(botKeys);
  447. t.data("commandused", "Sell");
  448. t.data("amountofsets", amountofsets.toString());
  449. t.data("amountofkeys", n);
  450. t.send((ERR, STATUS) => {
  451. if (ERR) {
  452. client.chatMessage(SENDER, "An error has occured. I may be processing another trade, please try again shortly.");
  453. console.log("## An error occurred while sending trade: " + ERR);
  454. } else {
  455. client.chatMessage(SENDER, "Trade Sent! Confirming it...");
  456. console.log("## Trade offer sent!");
  457. }
  458. });
  459. }
  460. } else {
  461. console.log("## An error occurred while getting bot sets: " + ERR);
  462. }
  463. });
  464. } else {
  465. console.log("## An error occurred while getting user inventory: " + ERR);
  466. }
  467. });
  468. }
  469. }
  470. });
  471. } else {
  472. client.chatMessage(SENDER, "Please make sure you don't have a trade hold!");
  473. }
  474. });
  475. } else {
  476. client.chatMessage(SENDER, "Please try a lower amount of keys.");
  477. }
  478. } else {
  479. client.chatMessage(SENDER, "Please enter a valid amount of keys!");
  480. }
  481. } else {
  482. client.chatMessage(SENDER, "Please try again later.");
  483. }
  484. } else if (MSG.toUpperCase().indexOf("!BUYONE") >= 0) {
  485. if (botSets) {
  486. let n = MSG.toUpperCase().replace("!BUYONE ", ""),
  487. amountofsets = parseInt(n) * CONFIG.CARDS.BUY1KEYFORAMOUNTOFSETS;
  488. if (!isNaN(n) && parseInt(n) > 0) {
  489. if (n <= CONFIG.MESSAGES.MAXBUY) {
  490. let t = manager.createOffer(SENDER.getSteamID64());
  491. t.getUserDetails((ERR, ME, THEM) => {
  492. if (ERR) {
  493. console.log("## An error occurred while getting trade holds: " + ERR);
  494. client.chatMessage(SENDER, "An error occurred while getting your trade holds. Please try again");
  495. } else if (ME.escrowDays == 0 && THEM.escrowDays == 0) {
  496. n = parseInt(n);
  497. let theirKeys = [];
  498. client.chatMessage(SENDER, "Processing your request.");
  499. manager.getUserInventoryContents(SENDER.getSteamID64(), CONFIG.KEYSFROMGAME, 2, true, (ERR, INV, CURR) => {
  500. if (ERR) {
  501. console.log("## An error occurred while getting inventory: " + ERR);
  502. client.chatMessage(SENDER, "An error occurred while loading your inventory. Please try later");
  503. } else {
  504. console.log("DEBUG#INV LOADED");
  505. if (!ERR) {
  506. console.log("DEBUG#INV LOADED NOERR");
  507. for (let i = 0; i < INV.length; i++) {
  508. if (theirKeys.length < n && CONFIG.ACCEPTEDKEYS.indexOf(INV[i].market_hash_name) >= 0) {
  509. theirKeys.push(INV[i]);
  510. }
  511. }
  512. if (theirKeys.length != n) {
  513. client.chatMessage(SENDER, "You do not have enough keys.");
  514. } else {
  515. Utils.getBadges(SENDER.getSteamID64(), (ERR, DATA) => {
  516. if (!ERR) {
  517. console.log("DEBUG#BADGE LOADED");
  518. if (!ERR) {
  519. let b = {}; // List with badges that CAN still be crafted
  520. if (DATA) {
  521. for (let i = 0; i < Object.keys(DATA).length; i++) {
  522. if (DATA[Object.keys(DATA)[i]] < 6) {
  523. b[Object.keys(DATA)[i]] = 5 - DATA[Object.keys(DATA)[i]];
  524. }
  525. }
  526. } else {
  527. client.chatMessage(SENDER.getSteamID64(), "Your badges are empty, sending an offer without checking badges.");
  528. }
  529. console.log(DATA);
  530. console.log(b);
  531. // TODO: COUNT AMOUNT OF SETS BOT CAN GIVE HIM
  532. // 1: GET BOTS CARDS. DONE
  533. // 2: GET PLAYER's BADGES. DONE
  534. // 3: MAGIC
  535. let hisMaxSets = 0,
  536. botNSets = 0;
  537. // Loop for sets he has partially completed
  538. for (let i = 0; i < Object.keys(b).length; i++) {
  539. if (botSets[Object.keys(b)[i]] && botSets[Object.keys(b)[i]].length >= 5 - b[Object.keys(b)[i]].length) {
  540. hisMaxSets += 5 - b[Object.keys(b)[i]].length;
  541. }
  542. }
  543. console.log("DEBUG#LOOP 1 DONE");
  544. // Loop for sets he has never crafted
  545. for (let i = 0; i < Object.keys(botSets).length; i++) {
  546. if (Object.keys(b).indexOf(Object.keys(botSets)[i]) < 0) {
  547. if (botSets[Object.keys(botSets)[i]].length >= 5) {
  548. hisMaxSets += 5;
  549. } else {
  550. hisMaxSets += botSets[Object.keys(botSets)[i]].length;
  551. }
  552. }
  553. botNSets += botSets[Object.keys(botSets)[i]].length;
  554. }
  555. totalBotSets = botNSets;
  556. let playThis = CONFIG.PLAYGAMES;
  557. if (CONFIG.PLAYGAMES && typeof(CONFIG.PLAYGAMES[0]) == "string") {
  558. playThis[0] = parseString(playThis[0], totalBotSets);
  559. }
  560. client.gamesPlayed(playThis);
  561. console.log("DEBUG#LOOP 2 DONE");
  562. // HERE
  563. if (amountofsets <= hisMaxSets) {
  564. hisMaxSets = amountofsets;
  565. console.log("DEBUG#TRADE CREATED");
  566. sortSetsByAmount(botSets, (DATA) => {
  567. console.log("DEBUG#" + DATA);
  568. console.log("DEBUG#SETS SORTED");
  569. firstLoop: for (let i = 0; i < DATA.length; i++) {
  570. if (b[DATA[i]] == 0) {
  571. continue firstLoop;
  572. } else {
  573. console.log("DEBUG#" + i);
  574. console.log("DEBUG#FOR LOOP ITEMS");
  575. if (hisMaxSets > 0) {
  576. console.log("DEBUG#MAXSETSMORETHAN1");
  577. if (!b[DATA[i]] && botSets[DATA[i]].length > 0) { // TODO NOT FOR LOOP WITH BOTSETS. IT SENDS ALL
  578. // BOT HAS ENOUGH SETS AND USER NEVER CRAFTED THIS
  579. bLoop: for (let j = 0; j < botSets[DATA[i]].length; j++) {
  580. if (botSets[DATA[i]][j] && hisMaxSets > 0) {
  581. t.addMyItems(botSets[DATA[i]][j]);
  582. console.log("DEBUG#LOOP #2 CONTINUE: ITEM ADD");
  583. hisMaxSets--;
  584. continue firstLoop;
  585. } else {
  586. console.log("DEBUG#LOOP #2 CONTINUE: RETURN");
  587. continue firstLoop;
  588. }
  589. }
  590. }
  591. } else {
  592. console.log("DEBUG#RETURN");
  593. break firstLoop;
  594. }
  595. }
  596. }
  597. if (hisMaxSets > 0) {
  598. client.chatMessage(SENDER, "There are not enough sets. Please try again later.");
  599. } else {
  600. console.log("DEBUG#SENDING");
  601. t.addTheirItems(theirKeys);
  602. t.data("commandused", "BuyOne");
  603. t.data("amountofkeys", n);
  604. t.data("amountofsets", amountofsets.toString());
  605. t.data("index", setsThatShouldntBeSent.length);
  606. setsThatShouldntBeSent.push(t.itemsToGive);
  607. t.send((ERR, STATUS) => {
  608. if (ERR) {
  609. client.chatMessage(SENDER, "An error occurred while sending your trade. Steam Trades could be down. Please try again later.");
  610. console.log("## An error occurred while sending trade: " + ERR);
  611. } else {
  612. client.chatMessage(SENDER, "Trade Sent! Confirming it...");
  613. console.log("## Trade offer sent");
  614. }
  615. });
  616. }
  617. });
  618. } else {
  619. 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.");
  620. }
  621. // TO HERE
  622. } else {
  623. console.log("An error occurred while getting badges: " + ERR);
  624. }
  625. } else {
  626. client.chatMessage(SENDER, "An error occurred while getting your badges. Please try again.");
  627. console.log(SENDER, "## An error occurred while loading badges: " + ERR);
  628. }
  629. });
  630. }
  631. } else {
  632. console.log("## An error occurred while getting inventory: " + ERR);
  633. client.chatMessage(SENDER, "An error occurred while loading your inventory, please make sure it's set to public.");
  634. }
  635. }
  636. });
  637. } else {
  638. client.chatMessage(SENDER, "Please make sure you don't have a trade hold!");
  639. }
  640. });
  641. } else {
  642. client.chatMessage(SENDER, "Please try a lower amount of keys.");
  643. }
  644. } else {
  645. client.chatMessage(SENDER, "Please provide a valid amount of keys.");
  646. }
  647. } else {
  648. client.chatMessage(SENDER, "Please try again later.");
  649. }
  650. } else if (MSG.toUpperCase().indexOf("!BUYANY") >= 0) {
  651. if (botSets) {
  652. let n = MSG.toUpperCase().replace("!BUYANY ", ""),
  653. amountofsets = parseInt(n) * CONFIG.CARDS.BUY1KEYFORAMOUNTOFSETS;
  654. if (!isNaN(n) && parseInt(n) > 0) {
  655. if (n <= CONFIG.MESSAGES.MAXBUY) {
  656. let t = manager.createOffer(SENDER.getSteamID64());
  657. n = parseInt(n);
  658. let theirKeys = [];
  659. t.getUserDetails((ERR, ME, THEM) => {
  660. if (ERR) {
  661. console.log("## An error occurred while getting trade holds: " + ERR);
  662. client.chatMessage(SENDER, "An error occurred while getting your trade holds. Please try again");
  663. } else if (ME.escrowDays == 0 && THEM.escrowDays == 0) {
  664. client.chatMessage(SENDER, "Processing your request.");
  665. manager.getUserInventoryContents(SENDER.getSteamID64(), CONFIG.KEYSFROMGAME, 2, true, (ERR, INV, CURR) => {
  666. if (ERR) {
  667. console.log("## An error occurred while getting inventory: " + ERR);
  668. client.chatMessage(SENDER, "An error occurred while loading your inventory. Please try later");
  669. } else {
  670. let amountofB = amountofsets;
  671. for (let i = 0; i < INV.length; i++) {
  672. if (theirKeys.length < n && CONFIG.ACCEPTEDKEYS.indexOf(INV[i].market_hash_name) >= 0) {
  673. theirKeys.push(INV[i]);
  674. }
  675. }
  676. if (theirKeys.length != n) {
  677. client.chatMessage(SENDER, "You do not have enough keys.");
  678. } else {
  679. sortSetsByAmount(botSets, (DATA) => {
  680. let setsSent = {};
  681. firstLoop: for (let i = 0; i < DATA.length; i++) {
  682. console.log(setsSent);
  683. console.log(DATA[i]);
  684. if (botSets[DATA[i]]) {
  685. for (let j = 0; j < botSets[DATA[i]].length; j++) {
  686. if (amountofB > 0) {
  687. if ((setsSent[DATA[i]] && setsSent[DATA[i]] > -1) || !setsSent[DATA[i]]) {
  688. t.addMyItems(botSets[DATA[i]][j]);
  689. console.log("DEBUG#LOOP #2 CONTINUE: ITEM ADD");
  690. amountofB--;
  691. if (!setsSent[DATA[i]]) {
  692. setsSent[DATA[i]] = 1;
  693. } else {
  694. setsSent[DATA[i]] += 1;
  695. }
  696. } else {
  697. console.log("DEBUG#LOOP #2 CONTINUE: RETURN");
  698. continue firstLoop;
  699. }
  700. } else {
  701. console.log("DEBUG#LOOP #2 CONTINUE: RETURN");
  702. continue firstLoop;
  703. }
  704. }
  705. } else {
  706. console.log("DEBUG#LOOP #2 CONTINUE: RETURN 2");
  707. continue firstLoop;
  708. }
  709. }
  710. });
  711. }
  712. if (amountofB > 0) {
  713. client.chatMessage(SENDER, "There are not enough sets. Please try again later.");
  714. } else {
  715. console.log("DEBUG#SENDING");
  716. t.addTheirItems(theirKeys);
  717. t.data("commandused", "BuyAny");
  718. t.data("amountofsets", amountofsets.toString());
  719. t.data("amountofkeys", n);
  720. t.data("index", setsThatShouldntBeSent.length);
  721. setsThatShouldntBeSent.push(t.itemsToGive);
  722. t.send((ERR, STATUS) => {
  723. if (ERR) {
  724. client.chatMessage(SENDER, "An error occurred while sending your trade. Steam Trades could be down. Please try again later.");
  725. console.log("## An error occurred while sending trade: " + ERR);
  726. } else {
  727. client.chatMessage(SENDER, "Trade Sent! Confirming it...");
  728. console.log("## Trade offer sent!");
  729. }
  730. });
  731. }
  732. }
  733. });
  734. } else {
  735. client.chatMessage(SENDER, "Please make sure you don't have a trade hold!");
  736. }
  737. });
  738. } else {
  739. client.chatMessage(SENDER, "Please try a lower amount of keys");
  740. }
  741. } else {
  742. client.chatMessage(SENDER, "Please provide a valid amount of keys.");
  743. }
  744. } else {
  745. client.chatMessage(SENDER, "Please try again later.");
  746. }
  747. }
  748.  
  749.  
  750.  
  751. else if (MSG.toUpperCase().indexOf("!GEMSBUY") >= 0) {
  752. if (botSets) {
  753. let n = MSG.toUpperCase().replace("!GEMSBUY ", ""),
  754. amountofsets = parseInt(n) * 1;
  755.  
  756. if (!isNaN(n)) {
  757. if (n <= CONFIG.MESSAGES.MAXBUY) {
  758. let t = manager.createOffer(SENDER.getSteamID64());
  759. t.getUserDetails((ERR, ME, THEM) => {
  760. if (ERR) {
  761. console.log(" !!! An error occurred while checking trade holds (!buygems): " + ERR);
  762. client.chatMessage(SENDER, ":exclamationPointRed: An error occurred while checking your Steam trade holds. Please try again in 2 minutes.");
  763. } else if (ME.escrowDays == 0 && THEM.escrowDays == 0) {
  764. n = parseInt(n);
  765.  
  766.  
  767. var item = [];
  768. var nonUsableGems = [];
  769.  
  770. client.chatMessage(SENDER, ":revclock: Processing your request...");
  771. manager.getUserInventoryContents(SENDER.getSteamID64(), 753, 6, true, (ERR, INV, CURR) => {
  772. if (ERR) {
  773. console.log(" !!! An error occurred while loading Steam inventory inventory (!buygems): " + ERR);
  774. client.chatMessage(SENDER, ":exclamationPointRed: An error occurred while loading your Steam inventory. Please try again in 2 minutes.");
  775. } else {
  776. if (!ERR) {
  777. for (let i = 0; i < INV.length; i++) {
  778. var gems = INV[i];
  779. var amountNeeded = CONFIG.CARDS.GIVE1SETPERAMOUNTOFGEMS * n;
  780. var amountOfGems = INV[i].amount;
  781. if (gems.market_hash_name === "753-Gems"){
  782. if(gems.amount >= amountNeeded){
  783. item.push({assetid: gems.id, appid: '753', contextid: '6', amount: amountNeeded});
  784. console.log(item);
  785. } else {
  786. nonUsableGems.push({assetid: gems.id, appid: '753', contextid: '6', amount: amountOfGems});
  787. console.log(nonUsableGems);
  788. }
  789. }
  790. }
  791. }
  792. if (item.length != 1) {
  793. if(nonUsableGems.length != 0){
  794. client.chatMessage(SENDER, ":exclamationPointRed: You don't have enough gems.");
  795. client.chatMessage(SENDER, "This BOT's !GEMSBUY command accepts Steam Gems. If you purchased your gems at Steam market, your gems will have a 7 day trade restriction. You may check your gems by clicking on them at your inventory." + "\n" + "\n" + ":ns_key: Tradeable gems I can see in your inventory: " + nonUsableGems[0].amount);
  796. } else {
  797. client.chatMessage(SENDER, ":exclamationPointRed: There are no gems in your inventory.");
  798. }
  799. } else {
  800. Utils.getBadges(SENDER.getSteamID64(), (ERR, DATA) => {
  801. if (!ERR) {
  802. if (!ERR) {
  803. let b = {}; // List with badges that CAN still be crafted
  804. if (DATA) {
  805. for (let i = 0; i < Object.keys(DATA).length; i++) {
  806. if (DATA[Object.keys(DATA)[i]] < 6) {
  807. b[Object.keys(DATA)[i]] = 5 - DATA[Object.keys(DATA)[i]];
  808. }
  809. }
  810. } else {
  811. console.log(" # Badges are empty, sending offer without checking badges. (!buy)")
  812. client.chatMessage(SENDER.getSteamID64(), ":exclamationPointRed: Your badges are empty, sending an offer without checking badges.");
  813. }
  814. let hisMaxSets = 0,
  815. botNSets = 0;
  816. // Loop for sets he has partially completed
  817. for (let i = 0; i < Object.keys(b).length; i++) {
  818. if (botSets[Object.keys(b)[i]] && botSets[Object.keys(b)[i]].length >= 5 - b[Object.keys(b)[i]].length) {
  819. hisMaxSets += 5 - b[Object.keys(b)[i]].length;
  820. }
  821. }
  822. // Loop for sets he has never crafted
  823. for (let i = 0; i < Object.keys(botSets).length; i++) {
  824. if (Object.keys(b).indexOf(Object.keys(botSets)[i]) < 0) {
  825. if (botSets[Object.keys(botSets)[i]].length >= 5) {
  826. hisMaxSets += 5;
  827. } else {
  828. hisMaxSets += botSets[Object.keys(botSets)[i]].length;
  829. }
  830. }
  831. botNSets += botSets[Object.keys(botSets)[i]].length;
  832. }
  833. // HERE
  834. if (amountofsets <= hisMaxSets) {
  835. hisMaxSets = amountofsets;
  836. sortSetsByAmount(botSets, (DATA) => {
  837. firstLoop: for (let i = 0; i < DATA.length; i++) {
  838. if (b[DATA[i]] == 0) {
  839. continue firstLoop;
  840. } else {
  841. if (hisMaxSets > 0) {
  842. if (b[DATA[i]] && botSets[DATA[i]].length >= b[DATA[i]]) {
  843. // BOT HAS ENOUGH SETS OF THIS KIND
  844. sLoop: for (let j = 0; j < 5 - b[DATA[i]]; j++) {
  845. if (j + 1 < b[DATA[i]] && hisMaxSets > 0) {
  846. t.addMyItems(botSets[DATA[i]][j]);
  847. hisMaxSets--;
  848. } else {
  849. continue firstLoop;
  850. }
  851. }
  852. } else if (b[DATA[i]] && botSets[DATA[i]].length < b[DATA[i]]) {
  853. // BOT DOESNT HAVE ENOUGH SETS OF THIS KIND
  854. continue; // *
  855. } 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
  856. // BOT HAS ENOUGH SETS AND USER NEVER CRAFTED THIS
  857. bLoop: for (let j = 0; j < botSets[DATA[i]].length - b[DATA[i]]; j++) {
  858. if (botSets[DATA[i]][j] && hisMaxSets > 0) {
  859. t.addMyItems(botSets[DATA[i]][j]);
  860. hisMaxSets--;
  861. } else {
  862. continue firstLoop;
  863. }
  864. }
  865. }
  866. else if (hisMaxSets < 5) {
  867. // BOT DOESNT HAVE CARDS USER AREADY CRAFTED, IF USER STILL NEEDS 5 SETS:
  868. tLoop: for (let j = 0; j != hisMaxSets; j++) {
  869. if (botSets[DATA[i]][j] && hisMaxSets > 0) {
  870. t.addMyItems(botSets[DATA[i]][j]);
  871. hisMaxSets--;
  872. } else {
  873. continue firstLoop;
  874. }
  875. }
  876. } else {
  877. // BOT DOESNT HAVE CARDS USER AREADY CRAFTED, IF USER STILL NEEDS LESS THAN 5 SETS:
  878. xLoop: for (let j = 0; j != 5; j++ && hisMaxSets > 0) {
  879. if (botSets[DATA[i]][j] && hisMaxSets > 0) {
  880. t.addMyItems(botSets[DATA[i]][j]);
  881. hisMaxSets--;
  882. } else {
  883. continue firstLoop;
  884. }
  885. }
  886. }
  887. } else {
  888. break firstLoop;
  889. }
  890. }
  891. }
  892. if (hisMaxSets > 0) {
  893. client.chatMessage(SENDER, ":exclamationPointRed: There are not enough sets. Please try again later.");
  894. } else {
  895. console.log(" # Sending trade offer... (!buy)");
  896. t.addTheirItems(item);
  897. t.data("commandused", "Buy");
  898. t.data("amountofkeys", n);
  899. t.data("amountofsets", amountofsets.toString());
  900. t.setMessage("Here are " + t.data("amountofsets") + " cards sets in exchange for " + t.data("amountofkeys") + " CS:GO keys.")
  901. t.send((ERR, STATUS) => {
  902. if (ERR) {
  903. console.log(" !!! An error occurred while sending trade (!buy): " + ERR);
  904. client.chatMessage(SENDER, ":exclamationPointRed: An error occurred while sending your trade. Steam Trades could be down. Please try again in 2 minutes.");
  905. loadSets();
  906. } else {
  907. console.log(" # Trade offer sent! (!buy)");
  908. client.chatMessage(SENDER, ":clickpointer: Trade offer sent! Check your Steam notifications or click here to accept: https://steamcommunity.com/tradeoffer/" + t.id);
  909. }
  910. });
  911. }
  912. });
  913. } else {
  914. client.chatMessage(SENDER, ":exclamationPointRed: There are currently not enough sets that you have not used in stock for this amount of sets. Please try again later. If you want the BOT to ignore your current badges use !buyany.");
  915. }
  916. // TO HERE
  917. } else {
  918. console.log(" !!! An error occurred while getting buyer badges (!buy): " + ERR);
  919. }
  920. } else {
  921. console.log(" !!! An error occurred while loading buyer badges (!buy): " + ERR);
  922. client.chatMessage(SENDER, ":exclamationPointRed: An error occurred while checking your badges. Please try again in 2 minutes.");
  923. }
  924. });
  925. }
  926. }
  927. });
  928.  
  929. } else {
  930. client.chatMessage(SENDER, ":exclamationPointRed: Please make sure you don't have a trade hold. If you don't own a smartphone, use SteamDesktopAuthenticator." + "\n" + "\n" + "https://help.steampowered.com/pt-br/wizard/HelpWhyCantITrade");
  931. }
  932. });
  933. } else {
  934. client.chatMessage(SENDER, ":exclamationPointRed: You can buy up to " + CONFIG.MESSAGES.MAXBUY + " sets at once.");
  935. }
  936. } else {
  937. client.chatMessage(SENDER, ":exclamationPointRed: Please provide a valid amount of sets - example: !GEMSBUY 2");
  938. }
  939. } else {
  940. client.chatMessage(SENDER, ":exclamationPointRed: Please try again later.");
  941. }
  942. }
  943. else if (MSG.toUpperCase().indexOf("!BUY") >= 0) {
  944. if (botSets) {
  945. let n = MSG.toUpperCase().replace("!BUY ", ""),
  946. amountofsets = parseInt(n) * CONFIG.CARDS.BUY1KEYFORAMOUNTOFSETS;
  947. if (!isNaN(n) && parseInt(n) > 0) {
  948. if (n <= CONFIG.MESSAGES.MAXBUY) {
  949. let t = manager.createOffer(SENDER.getSteamID64());
  950. t.getUserDetails((ERR, ME, THEM) => {
  951. if (ERR) {
  952. console.log("## An error occurred while getting trade holds: " + ERR);
  953. client.chatMessage(SENDER, "An error occurred while getting your trade holds. Please try again");
  954. } else if (ME.escrowDays == 0 && THEM.escrowDays == 0) {
  955. n = parseInt(n);
  956. let theirKeys = [];
  957. client.chatMessage(SENDER, "Processing your request.");
  958. manager.getUserInventoryContents(SENDER.getSteamID64(), CONFIG.KEYSFROMGAME, 2, true, (ERR, INV, CURR) => {
  959. if (ERR) {
  960. console.log("## An error occurred while getting inventory: " + ERR);
  961. client.chatMessage(SENDER, "An error occurred while loading your inventory. Please try later");
  962. } else {
  963. console.log("DEBUG#INV LOADED");
  964. if (!ERR) {
  965. console.log("DEBUG#INV LOADED NOERR");
  966. for (let i = 0; i < INV.length; i++) {
  967. if (theirKeys.length < n && CONFIG.ACCEPTEDKEYS.indexOf(INV[i].market_hash_name) >= 0) {
  968. theirKeys.push(INV[i]);
  969. }
  970. }
  971. if (theirKeys.length != n) {
  972. client.chatMessage(SENDER, "You do not have enough keys.");
  973. } else {
  974. Utils.getBadges(SENDER.getSteamID64(), (ERR, DATA) => {
  975. if (!ERR) {
  976. console.log("DEBUG#BADGE LOADED");
  977. if (!ERR) {
  978. let b = {}; // List with badges that CAN still be crafted
  979. if (DATA) {
  980. for (let i = 0; i < Object.keys(DATA).length; i++) {
  981. if (DATA[Object.keys(DATA)[i]] < 6) {
  982. b[Object.keys(DATA)[i]] = 5 - DATA[Object.keys(DATA)[i]];
  983. }
  984. }
  985. } else {
  986. client.chatMessage(SENDER.getSteamID64(), "Your badges are empty, sending an offer without checking badges.");
  987. }
  988. console.log(DATA);
  989. console.log(b);
  990. // TODO: COUNT AMOUNT OF SETS BOT CAN GIVE HIM
  991. // 1: GET BOTS CARDS. DONE
  992. // 2: GET PLAYER's BADGES. DONE
  993. // 3: MAGIC
  994. let hisMaxSets = 0,
  995. botNSets = 0;
  996. // Loop for sets he has partially completed
  997. for (let i = 0; i < Object.keys(b).length; i++) {
  998. if (botSets[Object.keys(b)[i]] && botSets[Object.keys(b)[i]].length >= 5 - b[Object.keys(b)[i]].length) {
  999. hisMaxSets += 5 - b[Object.keys(b)[i]].length;
  1000. }
  1001. }
  1002. console.log("DEBUG#LOOP 1 DONE");
  1003. // Loop for sets he has never crafted
  1004. for (let i = 0; i < Object.keys(botSets).length; i++) {
  1005. if (Object.keys(b).indexOf(Object.keys(botSets)[i]) < 0) {
  1006. if (botSets[Object.keys(botSets)[i]].length >= 5) {
  1007. hisMaxSets += 5;
  1008. } else {
  1009. hisMaxSets += botSets[Object.keys(botSets)[i]].length;
  1010. }
  1011. }
  1012. botNSets += botSets[Object.keys(botSets)[i]].length;
  1013. }
  1014. console.log("DEBUG#LOOP 2 DONE");
  1015. // HERE
  1016. if (amountofsets <= hisMaxSets) {
  1017. hisMaxSets = amountofsets;
  1018. console.log("DEBUG#TRADE CREATED");
  1019. sortSetsByAmount(botSets, (DATA) => {
  1020. console.log("DEBUG#" + DATA);
  1021. console.log("DEBUG#SETS SORTED");
  1022. firstLoop: for (let i = 0; i < DATA.length; i++) {
  1023. if (b[DATA[i]] == 0) {
  1024. continue firstLoop;
  1025. } else {
  1026. console.log("DEBUG#" + i);
  1027. console.log("DEBUG#FOR LOOP ITEMS");
  1028. if (hisMaxSets > 0) {
  1029. console.log("DEBUG#MAXSETSMORETHAN1");
  1030. if (b[DATA[i]] && botSets[DATA[i]].length >= b[DATA[i]]) {
  1031. // BOT HAS ENOUGH SETS OF THIS KIND
  1032. console.log("DEBUG#LOOP #1");
  1033. sLoop: for (let j = 0; j < 5 - b[DATA[i]]; j++) {
  1034. if (j + 1 < b[DATA[i]] && hisMaxSets > 0) {
  1035. console.log("DEBUG#LOOP #1: ITEM ADD");
  1036. console.log("DEBUG#LOOP #1: " + botSets[DATA[i]][j]);
  1037. t.addMyItems(botSets[DATA[i]][j]);
  1038. hisMaxSets--;
  1039. console.log(hisMaxSets);
  1040. } else {
  1041. console.log("DEBUG#LOOP #1: RETURN");
  1042. continue firstLoop;
  1043. }
  1044. }
  1045. } else if (b[DATA[i]] && botSets[DATA[i]].length < b[DATA[i]]) {
  1046. // BOT DOESNT HAVE ENOUGH SETS OF THIS KIND
  1047. console.log("DEBUG#LOOP #1 CONTINUE");
  1048. continue; // *
  1049. } 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
  1050. // BOT HAS ENOUGH SETS AND USER NEVER CRAFTED THIS
  1051. bLoop: for (let j = 0; j < botSets[DATA[i]].length - b[DATA[i]]; j++) {
  1052. if (botSets[DATA[i]][j] && hisMaxSets > 0) {
  1053. t.addMyItems(botSets[DATA[i]][j]);
  1054. console.log("DEBUG#LOOP #2 CONTINUE: ITEM ADD");
  1055. hisMaxSets--;
  1056. } else {
  1057. console.log("DEBUG#LOOP #2 CONTINUE: RETURN");
  1058. continue firstLoop;
  1059. }
  1060. }
  1061. }
  1062. else if (hisMaxSets < 5) {
  1063. // BOT DOESNT HAVE CARDS USER AREADY CRAFTED, IF USER STILL NEEDS 5 SETS:
  1064. console.log("DEBUG#LOOP #2");
  1065. tLoop: for (let j = 0; j != hisMaxSets; j++) {
  1066. if (botSets[DATA[i]][j] && hisMaxSets > 0) {
  1067. t.addMyItems(botSets[DATA[i]][j]);
  1068. console.log("DEBUG#LOOP #2: ITEM ADD");
  1069. hisMaxSets--;
  1070. console.log(hisMaxSets);
  1071. } else {
  1072. console.log("DEBUG#LOOP #2: RETURN");
  1073. continue firstLoop;
  1074. }
  1075. }
  1076. } else {
  1077. // BOT DOESNT HAVE CARDS USER AREADY CRAFTED, IF USER STILL NEEDS LESS THAN 5 SETS:
  1078. console.log("DEBUG#LOOP #2");
  1079. xLoop: for (let j = 0; j != 5; j++ && hisMaxSets > 0) {
  1080. if (botSets[DATA[i]][j] && hisMaxSets > 0) {
  1081. t.addMyItems(botSets[DATA[i]][j]);
  1082. console.log("DEBUG#LOOP #2: ITEM ADD");
  1083. hisMaxSets--;
  1084. console.log(hisMaxSets);
  1085. } else {
  1086. console.log("DEBUG#LOOP #2: RETURN");
  1087. continue firstLoop;
  1088. }
  1089. }
  1090. }
  1091. } else {
  1092. console.log("DEBUG#RETURN");
  1093. break firstLoop;
  1094. }
  1095. }
  1096. }
  1097. if (hisMaxSets > 0) {
  1098. client.chatMessage(SENDER, "There are not enough sets. Please try again later.");
  1099. } else {
  1100. console.log("DEBUG#SENDING");
  1101. t.addTheirItems(theirKeys);
  1102. t.data("commandused", "Buy");
  1103. t.data("amountofkeys", n);
  1104. t.data("amountofsets", amountofsets.toString());
  1105. t.data("index", setsThatShouldntBeSent.length);
  1106. setsThatShouldntBeSent.push(t.itemsToGive);
  1107. t.send((ERR, STATUS) => {
  1108. if (ERR) {
  1109. client.chatMessage(SENDER, "An error occurred while sending your trade. Steam Trades could be down. Please try again later.");
  1110. console.log("## An error occurred while sending trade: " + ERR);
  1111. } else {
  1112. client.chatMessage(SENDER, "Trade Sent! Confirming it...");
  1113. console.log("## Trade offer sent");
  1114. }
  1115. });
  1116. }
  1117. });
  1118. } else {
  1119. 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.");
  1120. }
  1121. // TO HERE
  1122. } else {
  1123. console.log("An error occurred while getting badges: " + ERR);
  1124. }
  1125. } else {
  1126. client.chatMessage(SENDER, "An error occurred while getting your badges. Please try again.");
  1127. console.log(SENDER, "## An error occurred while loading badges: " + ERR);
  1128. }
  1129. });
  1130. }
  1131. } else {
  1132. console.log("## An error occurred while getting inventory: " + ERR);
  1133. client.chatMessage(SENDER, "An error occurred while loading your inventory, please make sure it's set to public.");
  1134. }
  1135. }
  1136. });
  1137. } else {
  1138. client.chatMessage(SENDER, "Please make sure you don't have a trade hold!");
  1139. }
  1140. });
  1141. } else {
  1142. client.chatMessage(SENDER, "Please try a lower amount of keys.");
  1143. }
  1144. } else {
  1145. client.chatMessage(SENDER, "Please provide a valid amount of keys.");
  1146. }
  1147. } else {
  1148. client.chatMessage(SENDER, "Please try again later.");
  1149. }
  1150. } else if (MSG.toUpperCase() == "!PROOF") {
  1151. client.chatMessage(SENDER, "This bot was made by ???");
  1152. } else if (CONFIG.ADMINS.indexOf(SENDER.getSteamID64()) >= 0 || CONFIG.ADMINS.indexOf(parseInt(SENDER.getSteamID64())) >= 0) {
  1153. // Admin commands.
  1154. if (MSG.toUpperCase().indexOf("!BLOCK") >= 0) {
  1155. let n = MSG.toUpperCase().replace("!BLOCK ", "").toString();
  1156. if (SID64REGEX.test(n)) {
  1157. client.chatMessage(SENDER, "User blocked.");
  1158. client.blockUser(n);
  1159. } else {
  1160. client.chatMessage(SENDER, "Please provide a valid SteamID64");
  1161. }
  1162. } else if (MSG.toUpperCase().indexOf("!USERCHECK") >= 0) {
  1163. let n = MSG.toUpperCase().replace("!USERCHECK ", "").toString();
  1164. if (SID64REGEX.test(n)) {
  1165. if (Object.keys(botSets).length > 0) {
  1166. client.chatMessage(SENDER, "Loading badges...");
  1167. Utils.getBadges(n, (ERR, DATA) => {
  1168. if (!ERR) {
  1169. let b = {}; // List with badges that CAN still be crafted
  1170. if (DATA) {
  1171. for (let i = 0; i < Object.keys(DATA).length; i++) {
  1172. if (DATA[Object.keys(DATA)[i]] < 6) {
  1173. b[Object.keys(DATA)[i]] = 5 - DATA[Object.keys(DATA)[i]];
  1174. }
  1175. }
  1176. } else {
  1177. client.chatMessage(SENDER.getSteamID64(), n + "'s badges are empty, sending an offer without checking badges.");
  1178. }
  1179. // console.log(b);
  1180. // TODO: COUNT AMOUNT OF SETS BOT CAN GIVE HIM
  1181. // 1: GET BOTS CARDS. DONE
  1182. // 2: GET PLAYER's BADGES. DONE
  1183. // 3: MAGIC
  1184. let hisMaxSets = 0,
  1185. botNSets = 0;
  1186. // Loop for sets he has partially completed
  1187. for (let i = 0; i < Object.keys(b).length; i++) {
  1188. if (botSets[Object.keys(b)[i]] && botSets[Object.keys(b)[i]].length >= 5 - b[Object.keys(b)[i]].length) {
  1189. hisMaxSets += 5 - b[Object.keys(b)[i]].length;
  1190. }
  1191. }
  1192. // Loop for sets he has never crafted
  1193. for (let i = 0; i < Object.keys(botSets).length; i++) {
  1194. if (Object.keys(b).indexOf(Object.keys(botSets)[i]) < 0) {
  1195. if (botSets[Object.keys(botSets)[i]].length >= 5) {
  1196. hisMaxSets += 5;
  1197. } else {
  1198. hisMaxSets += botSets[Object.keys(botSets)[i]].length;
  1199. }
  1200. }
  1201. botNSets += botSets[Object.keys(botSets)[i]].length;
  1202. }
  1203. 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.");
  1204. } else {
  1205. client.chatMessage(SENDER, "An error occurred while getting " + n + "'s badges. Please try again.");
  1206. console.log("An error occurred while getting badges: " + ERR);
  1207. }
  1208. });
  1209. } else {
  1210. client.chatMessage(SENDER, "Please try again later.");
  1211. }
  1212. } else {
  1213. client.chatMessage(SENDER, "Please provide a valid SteamID64.");
  1214. }
  1215. } else if (MSG.toUpperCase() == "!KEYS") {
  1216. manager.getInventoryContents(CONFIG.KEYSFROMGAME, 2, true, (ERR, INV, CURR) => {
  1217. if (ERR) {
  1218. client.chatMessage(SENDER, "An error occurred while loading the bot's inventory.");
  1219. console.log("## An error occurred while getting inventory: " + ERR);
  1220. } else {
  1221. let t = manager.createOffer(SENDER);
  1222. for (let i = 0; i < INV.length; i++) {
  1223. if (CONFIG.ACCEPTEDKEYS.indexOf(INV[i].market_hash_name) >= 0) {
  1224. t.addMyItem(INV[i]);
  1225. }
  1226. t.send();
  1227. }
  1228. }
  1229. });
  1230. } else {
  1231. client.chatMessage(SENDER, "Command not recognized.");;
  1232. }
  1233. } else {
  1234. client.chatMessage(SENDER, "Command not recognized. Use !help to see how this bot works.");
  1235. }
  1236. });
  1237.  
  1238. client.on("friendRelationship", (SENDER, REL) => {
  1239. if (REL === 2) {
  1240. client.addFriend(SENDER);
  1241. } else if (REL === 3) {
  1242. if (CONFIG.INVITETOGROUPID) {
  1243. client.inviteToGroup(SENDER, CONFIG.INVITETOGROUPID);
  1244. }
  1245. client.chatMessage(SENDER, CONFIG.MESSAGES.WELCOME);
  1246. }
  1247. });
  1248.  
  1249. // manager.on("unknownOfferSent", (o, os) => {
  1250. // if (o.state == 9) {
  1251. // console.log("## OFFER SENT LIST");
  1252. // community.checkConfirmations();
  1253. // }
  1254. // });
  1255.  
  1256. // community.on("newConfirmation", (CONF) => {
  1257. // console.log("## New confirmation.");
  1258. // community.acceptConfirmationForObject(CONFIG.IDENTITYSECRET, CONF.id);
  1259. // });
  1260.  
  1261. manager.on("sentOfferChanged", (OFFER, OLDSTATE) => {
  1262. if (OFFER.state == 2) {
  1263. client.chatMessage(OFFER.partner, "Trade confirmed! Click here to accept it: https://www.steamcommunity.com/tradeoffer/" + OFFER.id);
  1264. } else if (OFFER.state == 3) {
  1265. Utils.getInventory(client.steamID.getSteamID64(), community, (ERR, DATA) => {
  1266. if (!ERR) {
  1267. let s = DATA;
  1268. Utils.getSets(s, allCards, (ERR, DATA) => {
  1269. if (!ERR) {
  1270. botSets = DATA;
  1271. console.log("## Bot's sets loaded.");
  1272. } else {
  1273. console.log("## An error occurred while getting bot sets: " + ERR);
  1274. }
  1275. let botNSets = 0;
  1276. for (let i = 0; i < Object.keys(botSets).length; i++) {
  1277. botNSets += botSets[Object.keys(botSets)[i]].length;
  1278. }
  1279. totalBotSets = botNSets;
  1280. let playThis = CONFIG.PLAYGAMES;
  1281. if (CONFIG.PLAYGAMES && typeof(CONFIG.PLAYGAMES[0]) == "string") {
  1282. playThis[0] = parseString(playThis[0], totalBotSets);
  1283. }
  1284. client.gamesPlayed(playThis);
  1285. });
  1286. } else {
  1287. console.log("## An error occurred while getting bot inventory: " + ERR);
  1288. }
  1289. });
  1290. if (CONFIG.INVITETOGROUPID) {
  1291. client.inviteToGroup(OFFER.partner, CONFIG.INVITETOGROUPID);
  1292. }
  1293. let d = "" + OFFER.data("commandused") + "";
  1294. d += "\nSets: " + OFFER.data("amountofsets");
  1295. d += "\nKeys: " + OFFER.data("amountofkeys");
  1296. d += "\nSteamID: " + OFFER.partner.getSteamID64();
  1297. fs.writeFile("./TradesAccepted/" + OFFER.id + "-" + OFFER.partner.getSteamID64() + ".txt", d, (ERR) => {
  1298. if (ERR) {
  1299. console.log("## An error occurred while writing trade file: " + ERR);
  1300. }
  1301. });
  1302. community.getSteamUser(OFFER.partner, (ERR, USER) => {
  1303. if (ERR) {
  1304. console.log("## An error occurred while getting user profile: " + ERR);
  1305. client.chatMessage(OFFER.partner, "An error occurred while getting your profile (to comment).");
  1306. else {
  1307. USER.comment(CONFIG.COMMENTAFTERTRADE, (ERR) => {
  1308. // if (ERR) {
  1309. // console.log("## An error occurred while commenting on user profile: " + ERR);
  1310. // client.chatMessage(OFFER.partner, "An error occurred while getting commenting on your profile.");
  1311. // } else {
  1312. // client.chatMessage(OFFER.partner, "Thanks for trading! :D");
  1313. // }
  1314. });
  1315. }
  1316.  
  1317. } else if (OFFER.state == 6) {
  1318. client.chatMessage(OFFER.partner, "Hey, you did not accept the offer. Please try again if you wish to receive sets!");
  1319. }
  1320. /* else if (OFFER.state == 9) {
  1321. community.checkConfirmations();
  1322. }*/
  1323. });
  1324.  
  1325. manager.on("newOffer", (OFFER) => {
  1326. if (CONFIG.ADMINS.indexOf(OFFER.partner.getSteamID64()) >= 0 || CONFIG.ADMINS.indexOf(parseInt(OFFER.partner.getSteamID64())) >= 0) {
  1327. OFFER.getUserDetails((ERR, ME, THEM) => {
  1328. if (ERR) {
  1329. console.log("## An error occurred while getting trade holds: " + ERR);
  1330. client.chatMessage(OFFER.partner, "An error occurred while getting your trade holds. Please try again");
  1331. OFFER.decline((ERR) => {
  1332. if (ERR) {
  1333. console.log("## An error occurred while declining trade: " + ERR);
  1334. }
  1335. });
  1336. } else if (ME.escrowDays == 0 && THEM.escrowDays == 0) {
  1337. OFFER.accept((ERR) => {
  1338. if (ERR) {
  1339. console.log("## An error occurred while declining trade: " + ERR);
  1340. OFFER.decline((ERR) => {
  1341. if (ERR) {
  1342. console.log("## An error occurred while declining trade: " + ERR);
  1343. }
  1344. });
  1345. } else {
  1346. client.chatMessage(OFFER.partner, "Offer accepted!");
  1347. }
  1348. });
  1349. } else {
  1350. client.chatMessage(OFFER.partner, "Please make sure you don't have a trade hold!");
  1351. OFFER.decline((ERR) => {
  1352. if (ERR) {
  1353. console.log("## An error occurred while declining trade: " + ERR);
  1354. }
  1355. });
  1356. }
  1357. });
  1358. } else if (OFFER.itemsToGive.length == 0) {
  1359. OFFER.accept((ERR) => {
  1360. console.log("DONATION ACCEPT");
  1361. if (ERR) {
  1362. console.log("## An error occurred while declining trade: " + ERR);
  1363. }
  1364. });
  1365. } else {
  1366. OFFER.decline((ERR) => {
  1367. if (ERR) {
  1368. console.log("## An error occurred while declining trade: " + ERR);
  1369. }
  1370. });
  1371. }
  1372. });
  1373.  
  1374. community.on("newConfirmation", (CONF) => {
  1375. console.log("## New confirmation.");
  1376. community.acceptConfirmationForObject(CONFIG.IDENTITYSECRET, CONF.id, (ERR) => {
  1377. if (ERR) {
  1378. console.log("## An error occurred while accepting confirmation: " + ERR);
  1379. } else {
  1380. console.log("## Confirmation accepted.");
  1381. }
  1382. });
  1383. });
  1384.  
  1385. function sortSetsByAmount(SETS, callback) {
  1386. callback(Object.keys(SETS).sort((k1, k2) => SETS[k1].length - SETS[k2].length).reverse());
  1387. }
  1388.  
  1389. function sortSetsByAmountB(SETS, callback) {
  1390. callback(Object.keys(SETS).sort((k1, k2) => SETS[k1].length - SETS[k2].length));
  1391. }
  1392.  
  1393. function parseString(INPUT, SETS) {
  1394. return INPUT.replace(":sets:", SETS);
  1395. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement