Advertisement
Guest User

Untitled

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