Advertisement
Guest User

Botkod, Fix pls

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