Advertisement
Guest User

Untitled

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