Advertisement
Guest User

bot.js steam trade card

a guest
Apr 19th, 2018
3,727
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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.         var datalength = (!!DATA) ? Object.keys(DATA).length:0;
  40.         console.log("Card data loaded. [" + datalength  + "]");
  41.     } else {
  42.         console.log("An error occurred while getting cards: " + ERR);
  43.     }
  44. });
  45.  
  46. setInterval(() => {
  47.     for (let i = 0; i < Object.keys(users).length; i++) {
  48.         if (users[Object.keys(users)[i]].idleforhours >= CONFIG.MAXHOURSADDED) {
  49.             client.chatMessage(Object.keys(users)[i], "Hi, you have been inactive on my friends list for too long. If you wish to use this bot again re-add it.");
  50.             client.removeFriend(Object.keys(users)[i]);
  51.             delete users[Object.keys(users)[i]];
  52.             fs.writeFile("./UserData/Users.json", JSON.stringify(users), (ERR) => {
  53.                 if (ERR) {
  54.                     console.log("## An error occurred while writing UserData file: " + ERR);
  55.                 }
  56.             });
  57.         } else {
  58.             users[Object.keys(users)[i]].idleforhours += 1;
  59.             fs.writeFile("./UserData/Users.json", JSON.stringify(users), (ERR) => {
  60.                 if (ERR) {
  61.                     console.log("## An error occurred while writing UserData file: " + ERR);
  62.                 }
  63.             });
  64.         }
  65.     }
  66. }, 1000 * 60 * 60);
  67.  
  68. setInterval(() => {
  69.     for (let i = 0; i < Object.keys(userMsgs).length; i++) {
  70.         if (userMsgs[Object.keys(userMsgs)[i]] > CONFIG.MAXMSGPERSEC) {
  71.             client.chatMessage(Object.keys(userMsgs)[i], "You have been removed for spamming. Another offense will get you blocked.");
  72.             client.removeFriend(Object.keys(userMsgs)[i]);
  73.             for (let j = 0; j < CONFIG.ADMINS.length; j++) {
  74.                 client.chatMessage(CONFIG.ADMINS[j], "User #" + Object.keys(userMsgs)[i] + " has been removed for spamming. To block him use !block [STEAMID64]");
  75.             }
  76.         }
  77.     }
  78.     userMsgs = {};
  79. }, 1000);
  80.  
  81. client.logOn({
  82.     accountName: CONFIG.USERNAME,
  83.     password: CONFIG.PASSWORD,
  84.     twoFactorCode: SteamTotp.getAuthCode(CONFIG.SHAREDSECRET)
  85. });
  86.  
  87. client.on("loggedOn", (details, parental) => {
  88.     client.getPersonas([client.steamID], (personas) => {
  89.         console.log("## Logged in as #" + client.steamID + " (" + personas[client.steamID].player_name + ")");
  90.     });
  91.     client.setPersona(1);
  92. });
  93.  
  94. client.on("webSession", (sessionID, cookies) => {
  95.     manager.setCookies(cookies, (ERR) => {
  96.         if (ERR) {
  97.             console.log("## An error occurred while setting cookies.");
  98.         } else {
  99.             console.log("## Websession created and cookies set.");
  100.         }
  101.     });
  102.     // Add people that added the bot while it was online.
  103.     for (let i = 0; i < Object.keys(client.myFriends).length; i++) {
  104.         if (client.myFriends[Object.keys(client.myFriends)[i]] == 2) {
  105.             client.addFriend(Object.keys(client.myFriends)[i]);
  106.         }
  107.     }
  108.     community.setCookies(cookies);
  109.     community.startConfirmationChecker(10000, CONFIG.IDENTITYSECRET);
  110.     client.blockUser("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. :)
  111.     Utils.getInventory(client.steamID.getSteamID64(), community, (ERR, DATA) => {
  112.         console.log("DEBUG#INVLOADED");
  113.         if (!ERR) {
  114.             let s = DATA;
  115.             Utils.getSets(s, allCards, (ERR, DATA) => {
  116.                 console.log("DEBUG#SETSLOADED");
  117.                 if (!ERR) {
  118.                     botSets = DATA;
  119.                     console.log("## Bot's sets loaded.");
  120.                     let botNSets = 0;
  121.                     for (let i = 0; i < Object.keys(botSets).length; i++) {
  122.                         botNSets += botSets[Object.keys(botSets)[i]].length;
  123.                     }
  124.                     totalBotSets = botNSets;
  125.                     let playThis = CONFIG.PLAYGAMES;
  126.                     if (CONFIG.PLAYGAMES && typeof(CONFIG.PLAYGAMES[0]) == "string") {
  127.                         playThis[0] = parseString(playThis[0], totalBotSets);
  128.                     }
  129.                     client.gamesPlayed(playThis);
  130.                 } else {
  131.                     console.log("## An error occurred while getting bot sets: " + ERR);
  132.                     process.exit();
  133.                 }
  134.             });
  135.         } else {
  136.             console.log("## An error occurred while getting bot inventory: " + ERR);
  137.         }
  138.     });
  139. });
  140.  
  141. community.on("sessionExpired", (ERR) => {
  142.     console.log("## Session Expired. Relogging.");
  143.     client.webLogOn();
  144. });
  145.  
  146. client.on("friendMessage", (SENDER, MSG) => {
  147.     if (userLogs[SENDER.getSteamID64()]) {
  148.         userLogs[SENDER.getSteamID64()].push(MSG);
  149.     } else {
  150.         userLogs[SENDER.getSteamID64()] = [];
  151.         userLogs[SENDER.getSteamID64()].push(MSG);
  152.     }
  153.     fs.writeFile("./ChatLogs/UserLogs/" + SENDER.getSteamID64() + "-log-" + new Date().getDate() + "-" + new Date().getMonth() + "-" + new Date().getFullYear() + ".json", JSON.stringify({ logs: userLogs[SENDER.getSteamID64()] }), (ERR) => {
  154.         if (ERR) {
  155.             console.log("## An error occurred while writing UserLogs file: " + ERR);
  156.         }
  157.     });
  158.     chatLogs += SENDER.getSteamID64() + " : " + MSG + "\n";
  159.     fs.writeFile("./ChatLogs/FullLogs/log-" + new Date().getDate() + "-" + new Date().getMonth() + "-" + new Date().getFullYear() + ".txt", chatLogs, (ERR) => {
  160.         if (ERR) {
  161.             console.log("## An error occurred while writing FullLogs file: " + ERR);
  162.         }
  163.     });
  164.     if (Object.keys(users).indexOf(SENDER.getSteamID64()) < 0) {
  165.         users[SENDER.getSteamID64()] = {};
  166.         users[SENDER.getSteamID64()].idleforhours = 0;
  167.         fs.writeFile("./UserData/Users.json", JSON.stringify(users), (ERR) => {
  168.             if (ERR) {
  169.                 console.log("## An error occurred while writing UserData file: " + ERR);
  170.             }
  171.         });
  172.     } else {
  173.         users[SENDER.getSteamID64()].idleforhours = 0;
  174.     }
  175.     if (userMsgs[SENDER.getSteamID64()]) {
  176.         userMsgs[SENDER.getSteamID64()]++;
  177.     } else {
  178.         userMsgs[SENDER.getSteamID64()] = 1;
  179.     }
  180.     /*if (MSG.toUpperCase() == "!STOCK") {
  181.         for (let i = 0; i < botSets.length; i++) {
  182.             //
  183.         }
  184.     } else */
  185.     if (MSG.toUpperCase().indexOf("!LEVEL") >= 0) {
  186.         let n = parseInt(MSG.toUpperCase().replace("!LEVEL ", ""));
  187.         if (!isNaN(n) && parseInt(n) > 0) {
  188.             if (n <= CONFIG.MESSAGES.MAXLEVEL) {
  189.                 Utils.getBadges(SENDER.getSteamID64(), (ERR, DATA, CURRENTLEVEL, XPNEEDED) => {
  190.                     if (!ERR) {
  191.                         if (DATA) {
  192.                             if (n > CURRENTLEVEL) {
  193.                                 let s = 0,
  194.                                     l = 0;
  195.                                 for (let i = 0; i < (n - CURRENTLEVEL); i++) {
  196.                                     s += parseInt((CURRENTLEVEL + l) / 10) + 1;
  197.                                     l++;
  198.                                 }
  199.                                 client.chatMessage(SENDER, "To get to level " + n + " you will need " + (s - Math.floor(XPNEEDED / 100)) + " sets. That would cost " + parseInt((s - Math.floor(XPNEEDED / 100)) / CONFIG.CARDS.BUY1KEYFORAMOUNTOFSETS * 100) / 100 + " keys.");
  200.                             } else {
  201.                                 client.chatMessage(SENDER, "Please provide a valid level.");
  202.                             }
  203.                         } else {
  204.                             client.chatMessage(SENDER, "Your level could not be retrieved. Make sure your Steam Profile is public and try again.");
  205.                         }
  206.                     } else {
  207.                         console.log("## An error occurred while getting badge data: " + ERR);
  208.                         client.chatMessage(SENDER, "An error occurred while loading your badges. Please try again later.");
  209.                     }
  210.                 });
  211.             } else {
  212.                 client.chatMessage(SENDER, "Please try a lower level.");
  213.             }
  214.         } else {
  215.             client.chatMessage(SENDER, "Please provide a valid level.");
  216.         }
  217.     } else if (MSG.toUpperCase() === "!HELP") {
  218.         client.chatMessage(SENDER, CONFIG.MESSAGES.HELP);
  219.         if (CONFIG.CARDS.PEOPLETHATCANSELL.indexOf(SENDER.getSteamID64()) >= 0) {
  220.             client.chatMessage(SENDER, CONFIG.MESSAGES.SELLHELP);
  221.         }
  222.     } else if (MSG.toUpperCase().indexOf("!BUYONECHECK") >= 0) {
  223.         client.chatMessage(SENDER, "Loading badges...");
  224.         Utils.getBadges(SENDER.getSteamID64(), (ERR, DATA) => {
  225.             if (!ERR) {
  226.                 let b = {}; // List with badges that CAN still be crafted
  227.                 if (DATA) {
  228.                     for (let i = 0; i < Object.keys(DATA).length; i++) {
  229.                         if (DATA[Object.keys(DATA)[i]] < 6) {
  230.                             b[Object.keys(DATA)[i]] = 5 - DATA[Object.keys(DATA)[i]];
  231.                         }
  232.                     }
  233.                 } else {
  234.                     client.chatMessage(SENDER.getSteamID64(), "Your badges are empty, sending an offer without checking badges.");
  235.                 }
  236.                 // console.log(b);
  237.                 // TODO: COUNT AMOUNT OF SETS BOT CAN GIVE HIM
  238.                 // 1: GET BOTS CARDS. DONE
  239.                 // 2: GET PLAYER's BADGES. DONE
  240.                 // 3: MAGIC
  241.                 let hisMaxSets = 0,
  242.                     botNSets = 0;
  243.                 // Loop for sets he has partially completed
  244.                 for (let i = 0; i < Object.keys(b).length; i++) {
  245.                     if (botSets[Object.keys(b)[i]] && botSets[Object.keys(b)[i]].length >= 5 - b[Object.keys(b)[i]].length) {
  246.                         hisMaxSets += 5 - b[Object.keys(b)[i]].length;
  247.                     }
  248.                 }
  249.                 // Loop for sets he has never crafted
  250.                 for (let i = 0; i < Object.keys(botSets).length; i++) {
  251.                     if (Object.keys(b).indexOf(Object.keys(botSets)[i]) < 0) {
  252.                         if (botSets[Object.keys(botSets)[i]].length >= 1) {
  253.                             hisMaxSets += 1;
  254.                         }
  255.                     }
  256.                     botNSets += botSets[Object.keys(botSets)[i]].length;
  257.                 }
  258.                 totalBotSets = botNSets;
  259.                 let playThis = CONFIG.PLAYGAMES;
  260.                 if (CONFIG.PLAYGAMES && typeof(CONFIG.PLAYGAMES[0]) == "string") {
  261.                     playThis[0] = parseString(playThis[0], totalBotSets);
  262.                 }
  263.                 client.gamesPlayed(playThis);
  264.                 client.chatMessage(SENDER, "There are currently sets from " + Object.keys(botSets).length + " different games, of which you have not crafted " + hisMaxSets + ". This would cost " + parseInt(hisMaxSets / CONFIG.CARDS.BUY1KEYFORAMOUNTOFSETS * 100) / 100 + " keys.");
  265.             } else {
  266.                 client.chatMessage(SENDER, "An error occurred while getting your badges. Please try again.");
  267.                 console.log("An error occurred while getting badges: " + ERR);
  268.             }
  269.         });
  270.     } else if (MSG.toUpperCase().indexOf("!SELLCHECK") >= 0 && (CONFIG.CARDS.PEOPLETHATCANSELL.indexOf(SENDER.getSteamID64().toString()) >= 0 || CONFIG.CARDS.PEOPLETHATCANSELL.indexOf(parseInt(SENDER.getSteamID64())) >= 0)) {
  271.         let n = parseInt(MSG.toUpperCase().replace("!SELLCHECK ", ""));
  272.         client.chatMessage(SENDER, "Loading inventory...");
  273.  
  274.         Utils.getInventory(SENDER.getSteamID64(), community, (ERR, DATA) => {
  275.             console.log("DEBUG#INVLOADED");
  276.             if (!ERR) {
  277.                 let s = DATA;
  278.                 Utils.getSets(s, allCards, (ERR, DATA) => {
  279.                     console.log("DEBUG#SETSLOADED");
  280.                     if (!ERR) {
  281.  
  282.                         // console.log(b);
  283.                         // TODO: COUNT AMOUNT OF SETS BOT CAN GIVE HIM
  284.                         // 1: GET BOTS CARDS. DONE
  285.                         // 2: GET PLAYER's BADGES. DONE
  286.                         // 3: MAGIC
  287.                         let hisMaxSets = 0,
  288.                             botNSets = 0;
  289.                         // Loop for sets he has partially completed
  290.                         // Loop for sets he has never crafted
  291.                         for (let i = 0; i < Object.keys(DATA).length; i++) {
  292.                             if (DATA[Object.keys(DATA)[i]].length >= 5) {
  293.                                 hisMaxSets += 5;
  294.                             } else {
  295.                                 hisMaxSets += DATA[Object.keys(DATA)[i]].length;
  296.                             }
  297.                             botNSets += DATA[Object.keys(DATA)[i]].length;
  298.                         }
  299.                         totalBotSets = botNSets;
  300.                         let playThis = CONFIG.PLAYGAMES;
  301.                         if (CONFIG.PLAYGAMES && typeof(CONFIG.PLAYGAMES[0]) == "string") {
  302.                             playThis[0] = parseString(playThis[0], totalBotSets);
  303.                         }
  304.                         client.gamesPlayed(playThis);
  305.                         client.chatMessage(SENDER, "You currently have " + botNSets + " sets available which the bot can buy. For all of them the bot will pay you " + parseInt(botNSets / CONFIG.CARDS.GIVE1KEYPERAMOUNTOFSETS * 100) / 100 + " keys.");
  306.                     } else {
  307.                         console.log("## An error occurred while getting user sets: " + ERR);
  308.                     }
  309.                 });
  310.             } else {
  311.                 console.log("## An error occurred while getting user inventory: " + ERR);
  312.             }
  313.         });
  314.     } else if (MSG.toUpperCase().indexOf("!CHECK") >= 0) {
  315.         let n = parseInt(MSG.toUpperCase().replace("!CHECK ", ""));
  316.         if (!isNaN(n) && parseInt(n) > 0) {
  317.             client.chatMessage(SENDER, "With " + n + " keys you can get " + n * CONFIG.CARDS.BUY1KEYFORAMOUNTOFSETS + " sets.");
  318.         } else {
  319.             if (Object.keys(botSets).length > 0) {
  320.                 client.chatMessage(SENDER, "Loading badges...");
  321.                 Utils.getBadges(SENDER.getSteamID64(), (ERR, DATA) => {
  322.                     if (!ERR) {
  323.                         let b = {}; // List with badges that CAN still be crafted
  324.                         if (DATA) {
  325.                             for (let i = 0; i < Object.keys(DATA).length; i++) {
  326.                                 if (DATA[Object.keys(DATA)[i]] < 6) {
  327.                                     b[Object.keys(DATA)[i]] = 5 - DATA[Object.keys(DATA)[i]];
  328.                                 }
  329.                             }
  330.                         } else {
  331.                             client.chatMessage(SENDER.getSteamID64(), "Your badges are empty, sending an offer without checking badges.");
  332.                         }
  333.                         // console.log(b);
  334.                         // TODO: COUNT AMOUNT OF SETS BOT CAN GIVE HIM
  335.                         // 1: GET BOTS CARDS. DONE
  336.                         // 2: GET PLAYER's BADGES. DONE
  337.                         // 3: MAGIC
  338.                         let hisMaxSets = 0,
  339.                             botNSets = 0;
  340.                         // Loop for sets he has partially completed
  341.                         for (let i = 0; i < Object.keys(b).length; i++) {
  342.                             if (botSets[Object.keys(b)[i]] && botSets[Object.keys(b)[i]].length >= 5 - b[Object.keys(b)[i]].length) {
  343.                                 hisMaxSets += 5 - b[Object.keys(b)[i]].length;
  344.                             }
  345.                         }
  346.                         // Loop for sets he has never crafted
  347.                         for (let i = 0; i < Object.keys(botSets).length; i++) {
  348.                             if (Object.keys(b).indexOf(Object.keys(botSets)[i]) < 0) {
  349.                                 if (botSets[Object.keys(botSets)[i]].length >= 5) {
  350.                                     hisMaxSets += 5;
  351.                                 } else {
  352.                                     hisMaxSets += botSets[Object.keys(botSets)[i]].length;
  353.                                 }
  354.                             }
  355.                             botNSets += botSets[Object.keys(botSets)[i]].length;
  356.                         }
  357.                         totalBotSets = botNSets;
  358.                         let playThis = CONFIG.PLAYGAMES;
  359.                         if (CONFIG.PLAYGAMES && typeof(CONFIG.PLAYGAMES[0]) == "string") {
  360.                             playThis[0] = parseString(playThis[0], totalBotSets);
  361.                         }
  362.                         client.gamesPlayed(playThis);
  363.                         client.chatMessage(SENDER, "There are currently " + hisMaxSets + "/" + botNSets + " sets available which you have not fully crafted yet. Buying all of them will cost you " + parseInt(hisMaxSets / CONFIG.CARDS.BUY1KEYFORAMOUNTOFSETS * 100) / 100 + " keys.");
  364.                     } else {
  365.                         client.chatMessage(SENDER, "An error occurred while getting your badges. Please try again.");
  366.                         console.log("An error occurred while getting badges: " + ERR);
  367.                     }
  368.                 });
  369.             } else {
  370.                 client.chatMessage(SENDER, "Please try again later.");
  371.             }
  372.         }
  373.     } else if (MSG.toUpperCase().indexOf("!SELL") >= 0) {
  374.         if (botSets) {
  375.             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, "Processing your request.");
  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 try again");
  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.");
  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 per set type 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, "Trade Sent! Confirming it...");
  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.");
  514.                                         } else {
  515.                                             Utils.getBadges(SENDER.getSteamID64(), (ERR, DATA) => {
  516.                                                 if (!ERR) {
  517.                                                     console.log("DEBUG#BADGE LOADED");
  518.                                                     if (!ERR) {
  519.                                                         let b = {}; // List with badges that CAN still be crafted
  520.                                                         if (DATA) {
  521.                                                             for (let i = 0; i < Object.keys(DATA).length; i++) {
  522.                                                                 if (DATA[Object.keys(DATA)[i]] < 6) {
  523.                                                                     b[Object.keys(DATA)[i]] = 5 - DATA[Object.keys(DATA)[i]];
  524.                                                                 }
  525.                                                             }
  526.                                                         } else {
  527.                                                             client.chatMessage(SENDER.getSteamID64(), "Your badges are empty, sending an offer without checking badges.");
  528.                                                         }
  529.                                                         console.log(DATA);
  530.                                                         console.log(b);
  531.                                                         // TODO: COUNT AMOUNT OF SETS BOT CAN GIVE HIM
  532.                                                         // 1: GET BOTS CARDS. DONE
  533.                                                         // 2: GET PLAYER's BADGES. DONE
  534.                                                         // 3: MAGIC
  535.                                                         let hisMaxSets = 0,
  536.                                                             botNSets = 0;
  537.                                                         // Loop for sets he has partially completed
  538.                                                         for (let i = 0; i < Object.keys(b).length; i++) {
  539.                                                             if (botSets[Object.keys(b)[i]] && botSets[Object.keys(b)[i]].length >= 5 - b[Object.keys(b)[i]].length) {
  540.                                                                 hisMaxSets += 5 - b[Object.keys(b)[i]].length;
  541.                                                             }
  542.                                                         }
  543.                                                         console.log("DEBUG#LOOP 1 DONE");
  544.                                                         // Loop for sets he has never crafted
  545.                                                         for (let i = 0; i < Object.keys(botSets).length; i++) {
  546.                                                             if (Object.keys(b).indexOf(Object.keys(botSets)[i]) < 0) {
  547.                                                                 if (botSets[Object.keys(botSets)[i]].length >= 5) {
  548.                                                                     hisMaxSets += 5;
  549.                                                                 } else {
  550.                                                                     hisMaxSets += botSets[Object.keys(botSets)[i]].length;
  551.                                                                 }
  552.                                                             }
  553.                                                             botNSets += botSets[Object.keys(botSets)[i]].length;
  554.                                                         }
  555.                                                         totalBotSets = botNSets;
  556.                                                         let playThis = CONFIG.PLAYGAMES;
  557.                                                         if (CONFIG.PLAYGAMES && typeof(CONFIG.PLAYGAMES[0]) == "string") {
  558.                                                             playThis[0] = parseString(playThis[0], totalBotSets);
  559.                                                         }
  560.                                                         client.gamesPlayed(playThis);
  561.                                                         console.log("DEBUG#LOOP 2 DONE");
  562.                                                         // HERE
  563.                                                         if (amountofsets <= hisMaxSets) {
  564.                                                             hisMaxSets = amountofsets;
  565.                                                             console.log("DEBUG#TRADE CREATED");
  566.                                                             sortSetsByAmount(botSets, (DATA) => {
  567.                                                                 console.log("DEBUG#" + DATA);
  568.                                                                 console.log("DEBUG#SETS SORTED");
  569.                                                                 firstLoop: for (let i = 0; i < DATA.length; i++) {
  570.                                                                     if (b[DATA[i]] == 0) {
  571.                                                                         continue firstLoop;
  572.                                                                     } else {
  573.                                                                         console.log("DEBUG#" + i);
  574.                                                                         console.log("DEBUG#FOR LOOP ITEMS");
  575.                                                                         if (hisMaxSets > 0) {
  576.                                                                             console.log("DEBUG#MAXSETSMORETHAN1");
  577.                                                                             if (!b[DATA[i]] && botSets[DATA[i]].length > 0) { // TODO NOT FOR LOOP WITH BOTSETS. IT SENDS ALL
  578.                                                                                 // BOT HAS ENOUGH SETS AND USER NEVER CRAFTED THIS
  579.                                                                                 bLoop: for (let j = 0; j < botSets[DATA[i]].length; j++) {
  580.                                                                                     if (botSets[DATA[i]][j] && hisMaxSets > 0) {
  581.                                                                                         t.addMyItems(botSets[DATA[i]][j]);
  582.                                                                                         console.log("DEBUG#LOOP #2 CONTINUE: ITEM ADD");
  583.                                                                                         hisMaxSets--;
  584.                                                                                         continue firstLoop;
  585.                                                                                     } else {
  586.                                                                                         console.log("DEBUG#LOOP #2 CONTINUE: RETURN");
  587.                                                                                         continue firstLoop;
  588.                                                                                     }
  589.                                                                                 }
  590.                                                                             }
  591.                                                                         } else {
  592.                                                                             console.log("DEBUG#RETURN");
  593.                                                                             break firstLoop;
  594.                                                                         }
  595.                                                                     }
  596.                                                                 }
  597.                                                                 if (hisMaxSets > 0) {
  598.                                                                     client.chatMessage(SENDER, "There are not enough sets. Please try again later.");
  599.                                                                 } else {
  600.                                                                     console.log("DEBUG#SENDING");
  601.                                                                     t.addTheirItems(theirKeys);
  602.                                                                     t.data("commandused", "BuyOne");
  603.                                                                     t.data("amountofkeys", n);
  604.                                                                     t.data("amountofsets", amountofsets.toString());
  605.                                                                     t.data("index", setsThatShouldntBeSent.length);
  606.                                                                     setsThatShouldntBeSent.push(t.itemsToGive);
  607.                                                                     t.send((ERR, STATUS) => {
  608.                                                                         if (ERR) {
  609.                                                                             client.chatMessage(SENDER, "An error occurred while sending your trade. Steam Trades could be down. Please try again later.");
  610.                                                                             console.log("## An error occurred while sending trade: " + ERR);
  611.                                                                         } else {
  612.                                                                             client.chatMessage(SENDER, "Trade Sent! Confirming it...");
  613.                                                                             console.log("## Trade offer sent");
  614.                                                                         }
  615.                                                                     });
  616.                                                                 }
  617.                                                             });
  618.                                                         } else {
  619.                                                             client.chatMessage(SENDER, "There are currently not enough sets that you have not used in stock for this amount of keys. Please try again later. If you want the bot to ignore your current badges use !buyany.");
  620.                                                         }
  621.                                                         // TO HERE
  622.                                                     } else {
  623.                                                         console.log("An error occurred while getting badges: " + ERR);
  624.                                                     }
  625.                                                 } else {
  626.                                                     client.chatMessage(SENDER, "An error occurred while getting your badges. Please try again.");
  627.                                                     console.log(SENDER, "## An error occurred while loading badges: " + ERR);
  628.                                                 }
  629.                                             });
  630.                                         }
  631.                                     } else {
  632.                                         console.log("## An error occurred while getting inventory: " + ERR);
  633.                                         client.chatMessage(SENDER, "An error occurred while loading your inventory, please make sure it's set to public.");
  634.                                     }
  635.                                 }
  636.                             });
  637.                         } else {
  638.                             client.chatMessage(SENDER, "Please make sure you don't have a trade hold!");
  639.                         }
  640.                     });
  641.                 } else {
  642.                     client.chatMessage(SENDER, "Please try a lower amount of keys.");
  643.                 }
  644.             } else {
  645.                 client.chatMessage(SENDER, "Please provide a valid amount of keys.");
  646.             }
  647.         } else {
  648.             client.chatMessage(SENDER, "Please try again later.");
  649.         }
  650.     } else if (MSG.toUpperCase().indexOf("!BUYANY") >= 0) {
  651.         if (botSets) {
  652.             let n = MSG.toUpperCase().replace("!BUYANY ", ""),
  653.                 amountofsets = parseInt(n) * CONFIG.CARDS.BUY1KEYFORAMOUNTOFSETS;
  654.             if (!isNaN(n) && parseInt(n) > 0) {
  655.                 if (n <= CONFIG.MESSAGES.MAXBUY) {
  656.                     let t = manager.createOffer(SENDER.getSteamID64());
  657.                     n = parseInt(n);
  658.                     let theirKeys = [];
  659.                     t.getUserDetails((ERR, ME, THEM) => {
  660.                         if (ERR) {
  661.                             console.log("## An error occurred while getting trade holds: " + ERR);
  662.                             client.chatMessage(SENDER, "An error occurred while getting your trade holds. Please try again");
  663.                         } else if (ME.escrowDays == 0 && THEM.escrowDays == 0) {
  664.                             client.chatMessage(SENDER, "Processing your request.");
  665.                             manager.getUserInventoryContents(SENDER.getSteamID64(), CONFIG.KEYSFROMGAME, 2, true, (ERR, INV, CURR) => {
  666.                                 if (ERR) {
  667.                                     console.log("## An error occurred while getting inventory: " + ERR);
  668.                                     client.chatMessage(SENDER, "An error occurred while loading your inventory. Please try later");
  669.                                 } else {
  670.                                     let amountofB = amountofsets;
  671.                                     for (let i = 0; i < INV.length; i++) {
  672.                                         if (theirKeys.length < n && CONFIG.ACCEPTEDKEYS.indexOf(INV[i].market_hash_name) >= 0) {
  673.                                             theirKeys.push(INV[i]);
  674.                                         }
  675.                                     }
  676.                                     if (theirKeys.length != n) {
  677.                                         client.chatMessage(SENDER, "You do not have enough keys.");
  678.                                     } else {
  679.                                         sortSetsByAmount(botSets, (DATA) => {
  680.                                             let setsSent = {};
  681.                                             firstLoop: for (let i = 0; i < DATA.length; i++) {
  682.                                                 console.log(setsSent);
  683.                                                 console.log(DATA[i]);
  684.                                                 if (botSets[DATA[i]]) {
  685.                                                     for (let j = 0; j < botSets[DATA[i]].length; j++) {
  686.                                                         if (amountofB > 0) {
  687.                                                             if ((setsSent[DATA[i]] && setsSent[DATA[i]] > -1) || !setsSent[DATA[i]]) {
  688.                                                                 t.addMyItems(botSets[DATA[i]][j]);
  689.                                                                 console.log("DEBUG#LOOP #2 CONTINUE: ITEM ADD");
  690.                                                                 amountofB--;
  691.                                                                 if (!setsSent[DATA[i]]) {
  692.                                                                     setsSent[DATA[i]] = 1;
  693.                                                                 } else {
  694.                                                                     setsSent[DATA[i]] += 1;
  695.                                                                 }
  696.                                                             } else {
  697.                                                                 console.log("DEBUG#LOOP #2 CONTINUE: RETURN");
  698.                                                                 continue firstLoop;
  699.                                                             }
  700.                                                         } else {
  701.                                                             console.log("DEBUG#LOOP #2 CONTINUE: RETURN");
  702.                                                             continue firstLoop;
  703.                                                         }
  704.                                                     }
  705.                                                 } else {
  706.                                                     console.log("DEBUG#LOOP #2 CONTINUE: RETURN 2");
  707.                                                     continue firstLoop;
  708.                                                 }
  709.                                             }
  710.                                         });
  711.                                     }
  712.                                     if (amountofB > 0) {
  713.                                         client.chatMessage(SENDER, "There are not enough sets. Please try again later.");
  714.                                     } else {
  715.                                         console.log("DEBUG#SENDING");
  716.                                         t.addTheirItems(theirKeys);
  717.                                         t.data("commandused", "BuyAny");
  718.                                         t.data("amountofsets", amountofsets.toString());
  719.                                         t.data("amountofkeys", n);
  720.                                         t.data("index", setsThatShouldntBeSent.length);
  721.                                         setsThatShouldntBeSent.push(t.itemsToGive);
  722.                                         t.send((ERR, STATUS) => {
  723.                                             if (ERR) {
  724.                                                 client.chatMessage(SENDER, "An error occurred while sending your trade. Steam Trades could be down. Please try again later.");
  725.                                                 console.log("## An error occurred while sending trade: " + ERR);
  726.                                             } else {
  727.                                                 client.chatMessage(SENDER, "Trade Sent! Confirming it...");
  728.                                                 console.log("## Trade offer sent!");
  729.                                             }
  730.                                         });
  731.                                     }
  732.                                 }
  733.                             });
  734.                         } else {
  735.                             client.chatMessage(SENDER, "Please make sure you don't have a trade hold!");
  736.                         }
  737.                     });
  738.                 } else {
  739.                     client.chatMessage(SENDER, "Please try a lower amount of keys");
  740.                 }
  741.             } else {
  742.                 client.chatMessage(SENDER, "Please provide a valid amount of keys.");
  743.             }
  744.         } else {
  745.             client.chatMessage(SENDER, "Please try again later.");
  746.         }
  747.     } else if (MSG.toUpperCase().indexOf("!BUY") >= 0) {
  748.         if (botSets) {
  749.             let n = MSG.toUpperCase().replace("!BUY ", ""),
  750.                 amountofsets = parseInt(n) * CONFIG.CARDS.BUY1KEYFORAMOUNTOFSETS;
  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.                             let theirKeys = [];
  761.                             client.chatMessage(SENDER, "Processing your request.");
  762.                             manager.getUserInventoryContents(SENDER.getSteamID64(), CONFIG.KEYSFROMGAME, 2, true, (ERR, INV, CURR) => {
  763.                                 if (ERR) {
  764.                                     console.log("## An error occurred while getting inventory: " + ERR);
  765.                                     client.chatMessage(SENDER, "An error occurred while loading your inventory. Please try later");
  766.                                 } else {
  767.                                     console.log("DEBUG#INV LOADED");
  768.                                     if (!ERR) {
  769.                                         console.log("DEBUG#INV LOADED NOERR");
  770.                                         for (let i = 0; i < INV.length; i++) {
  771.                                             if (theirKeys.length < n && CONFIG.ACCEPTEDKEYS.indexOf(INV[i].market_hash_name) >= 0) {
  772.                                                 theirKeys.push(INV[i]);
  773.                                             }
  774.                                         }
  775.                                         if (theirKeys.length != n) {
  776.                                             client.chatMessage(SENDER, "You do not have enough keys.");
  777.                                         } else {
  778.                                             Utils.getBadges(SENDER.getSteamID64(), (ERR, DATA) => {
  779.                                                 if (!ERR) {
  780.                                                     console.log("DEBUG#BADGE LOADED");
  781.                                                     if (!ERR) {
  782.                                                         let b = {}; // List with badges that CAN still be crafted
  783.                                                         if (DATA) {
  784.                                                             for (let i = 0; i < Object.keys(DATA).length; i++) {
  785.                                                                 if (DATA[Object.keys(DATA)[i]] < 6) {
  786.                                                                     b[Object.keys(DATA)[i]] = 5 - DATA[Object.keys(DATA)[i]];
  787.                                                                 }
  788.                                                             }
  789.                                                         } else {
  790.                                                             client.chatMessage(SENDER.getSteamID64(), "Your badges are empty, sending an offer without checking badges.");
  791.                                                         }
  792.                                                         console.log(DATA);
  793.                                                         console.log(b);
  794.                                                         // TODO: COUNT AMOUNT OF SETS BOT CAN GIVE HIM
  795.                                                         // 1: GET BOTS CARDS. DONE
  796.                                                         // 2: GET PLAYER's BADGES. DONE
  797.                                                         // 3: MAGIC
  798.                                                         let hisMaxSets = 0,
  799.                                                             botNSets = 0;
  800.                                                         // Loop for sets he has partially completed
  801.                                                         for (let i = 0; i < Object.keys(b).length; i++) {
  802.                                                             if (botSets[Object.keys(b)[i]] && botSets[Object.keys(b)[i]].length >= 5 - b[Object.keys(b)[i]].length) {
  803.                                                                 hisMaxSets += 5 - b[Object.keys(b)[i]].length;
  804.                                                             }
  805.                                                         }
  806.                                                         console.log("DEBUG#LOOP 1 DONE");
  807.                                                         // Loop for sets he has never crafted
  808.                                                         for (let i = 0; i < Object.keys(botSets).length; i++) {
  809.                                                             if (Object.keys(b).indexOf(Object.keys(botSets)[i]) < 0) {
  810.                                                                 if (botSets[Object.keys(botSets)[i]].length >= 5) {
  811.                                                                     hisMaxSets += 5;
  812.                                                                 } else {
  813.                                                                     hisMaxSets += botSets[Object.keys(botSets)[i]].length;
  814.                                                                 }
  815.                                                             }
  816.                                                             botNSets += botSets[Object.keys(botSets)[i]].length;
  817.                                                         }
  818.                                                         console.log("DEBUG#LOOP 2 DONE");
  819.                                                         // HERE
  820.                                                         if (amountofsets <= hisMaxSets) {
  821.                                                             hisMaxSets = amountofsets;
  822.                                                             console.log("DEBUG#TRADE CREATED");
  823.                                                             sortSetsByAmount(botSets, (DATA) => {
  824.                                                                 console.log("DEBUG#" + DATA);
  825.                                                                 console.log("DEBUG#SETS SORTED");
  826.                                                                 firstLoop: for (let i = 0; i < DATA.length; i++) {
  827.                                                                     if (b[DATA[i]] == 0) {
  828.                                                                         continue firstLoop;
  829.                                                                     } else {
  830.                                                                         console.log("DEBUG#" + i);
  831.                                                                         console.log("DEBUG#FOR LOOP ITEMS");
  832.                                                                         if (hisMaxSets > 0) {
  833.                                                                             console.log("DEBUG#MAXSETSMORETHAN1");
  834.                                                                             if (b[DATA[i]] && botSets[DATA[i]].length >= b[DATA[i]]) {
  835.                                                                                 // BOT HAS ENOUGH SETS OF THIS KIND
  836.                                                                                 console.log("DEBUG#LOOP #1");
  837.                                                                                 sLoop: for (let j = 0; j < 5 - b[DATA[i]]; j++) {
  838.                                                                                     if (j + 1 < b[DATA[i]] && hisMaxSets > 0) {
  839.                                                                                         console.log("DEBUG#LOOP #1: ITEM ADD");
  840.                                                                                         console.log("DEBUG#LOOP #1: " + botSets[DATA[i]][j]);
  841.                                                                                         t.addMyItems(botSets[DATA[i]][j]);
  842.                                                                                         hisMaxSets--;
  843.                                                                                         console.log(hisMaxSets);
  844.                                                                                     } else {
  845.                                                                                         console.log("DEBUG#LOOP #1: RETURN");
  846.                                                                                         continue firstLoop;
  847.                                                                                     }
  848.                                                                                 }
  849.                                                                             } else if (b[DATA[i]] && botSets[DATA[i]].length < b[DATA[i]]) {
  850.                                                                                 // BOT DOESNT HAVE ENOUGH SETS OF THIS KIND
  851.                                                                                 console.log("DEBUG#LOOP #1 CONTINUE");
  852.                                                                                 continue; // *
  853.                                                                             } 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
  854.                                                                                 // BOT HAS ENOUGH SETS AND USER NEVER CRAFTED THIS
  855.                                                                                 bLoop: for (let j = 0; j < botSets[DATA[i]].length - b[DATA[i]]; j++) {
  856.                                                                                     if (botSets[DATA[i]][j] && hisMaxSets > 0) {
  857.                                                                                         t.addMyItems(botSets[DATA[i]][j]);
  858.                                                                                         console.log("DEBUG#LOOP #2 CONTINUE: ITEM ADD");
  859.                                                                                         hisMaxSets--;
  860.                                                                                     } else {
  861.                                                                                         console.log("DEBUG#LOOP #2 CONTINUE: RETURN");
  862.                                                                                         continue firstLoop;
  863.                                                                                     }
  864.                                                                                 }
  865.                                                                             }
  866.                                                                             else if (hisMaxSets < 5) {
  867.                                                                                 // BOT DOESNT HAVE CARDS USER AREADY CRAFTED, IF USER STILL NEEDS 5 SETS:
  868.                                                                                 console.log("DEBUG#LOOP #2");
  869.                                                                                 tLoop: for (let j = 0; j != hisMaxSets; j++) {
  870.                                                                                     if (botSets[DATA[i]][j] && hisMaxSets > 0) {
  871.                                                                                         t.addMyItems(botSets[DATA[i]][j]);
  872.                                                                                         console.log("DEBUG#LOOP #2: ITEM ADD");
  873.                                                                                         hisMaxSets--;
  874.                                                                                         console.log(hisMaxSets);
  875.                                                                                     } else {
  876.                                                                                         console.log("DEBUG#LOOP #2: RETURN");
  877.                                                                                         continue firstLoop;
  878.                                                                                     }
  879.                                                                                 }
  880.                                                                             } else {
  881.                                                                                 // BOT DOESNT HAVE CARDS USER AREADY CRAFTED, IF USER STILL NEEDS LESS THAN 5 SETS:
  882.                                                                                 console.log("DEBUG#LOOP #2");
  883.                                                                                 xLoop: for (let j = 0; j != 5; j++ && hisMaxSets > 0) {
  884.                                                                                     if (botSets[DATA[i]][j] && hisMaxSets > 0) {
  885.                                                                                         t.addMyItems(botSets[DATA[i]][j]);
  886.                                                                                         console.log("DEBUG#LOOP #2: ITEM ADD");
  887.                                                                                         hisMaxSets--;
  888.                                                                                         console.log(hisMaxSets);
  889.                                                                                     } else {
  890.                                                                                         console.log("DEBUG#LOOP #2: RETURN");
  891.                                                                                         continue firstLoop;
  892.                                                                                     }
  893.                                                                                 }
  894.                                                                             }
  895.                                                                         } else {
  896.                                                                             console.log("DEBUG#RETURN");
  897.                                                                             break firstLoop;
  898.                                                                         }
  899.                                                                     }
  900.                                                                 }
  901.                                                                 if (hisMaxSets > 0) {
  902.                                                                     client.chatMessage(SENDER, "There are not enough sets. Please try again later.");
  903.                                                                 } else {
  904.                                                                     console.log("DEBUG#SENDING");
  905.                                                                     t.addTheirItems(theirKeys);
  906.                                                                     t.data("commandused", "Buy");
  907.                                                                     t.data("amountofkeys", n);
  908.                                                                     t.data("amountofsets", amountofsets.toString());
  909.                                                                     t.data("index", setsThatShouldntBeSent.length);
  910.                                                                     setsThatShouldntBeSent.push(t.itemsToGive);
  911.                                                                     t.send((ERR, STATUS) => {
  912.                                                                         if (ERR) {
  913.                                                                             client.chatMessage(SENDER, "An error occurred while sending your trade. Steam Trades could be down. Please try again later.");
  914.                                                                             console.log("## An error occurred while sending trade: " + ERR);
  915.                                                                         } else {
  916.                                                                             client.chatMessage(SENDER, "Trade Sent! Confirming it...");
  917.                                                                             console.log("## Trade offer sent");
  918.                                                                         }
  919.                                                                     });
  920.                                                                 }
  921.                                                             });
  922.                                                         } else {
  923.                                                             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.");
  924.                                                         }
  925.                                                         // TO HERE
  926.                                                     } else {
  927.                                                         console.log("An error occurred while getting badges: " + ERR);
  928.                                                     }
  929.                                                 } else {
  930.                                                     client.chatMessage(SENDER, "An error occurred while getting your badges. Please try again.");
  931.                                                     console.log(SENDER, "## An error occurred while loading badges: " + ERR);
  932.                                                 }
  933.                                             });
  934.                                         }
  935.                                     } else {
  936.                                         console.log("## An error occurred while getting inventory: " + ERR);
  937.                                         client.chatMessage(SENDER, "An error occurred while loading your inventory, please make sure it's set to public.");
  938.                                     }
  939.                                 }
  940.                             });
  941.                         } else {
  942.                             client.chatMessage(SENDER, "Please make sure you don't have a trade hold!");
  943.                         }
  944.                     });
  945.                 } else {
  946.                     client.chatMessage(SENDER, "Please try a lower amount of keys.");
  947.                 }
  948.             } else {
  949.                 client.chatMessage(SENDER, "Please provide a valid amount of keys.");
  950.             }
  951.         } else {
  952.             client.chatMessage(SENDER, "Please try again later.");
  953.         }
  954.     } else if (MSG.toUpperCase() == "!PROOF") {
  955.         client.chatMessage(SENDER, "Secret");
  956.     } else if (CONFIG.ADMINS.indexOf(SENDER.getSteamID64()) >= 0 || CONFIG.ADMINS.indexOf(parseInt(SENDER.getSteamID64())) >= 0) {
  957.         // Admin commands.
  958.         if (MSG.toUpperCase().indexOf("!BLOCK") >= 0) {
  959.             let n = MSG.toUpperCase().replace("!BLOCK ", "").toString();
  960.             if (SID64REGEX.test(n)) {
  961.                 client.chatMessage(SENDER, "User blocked.");
  962.                 client.blockUser(n);
  963.             } else {
  964.                 client.chatMessage(SENDER, "Please provide a valid SteamID64");
  965.             }
  966.         } else if (MSG.toUpperCase().indexOf("!USERCHECK") >= 0) {
  967.             let n = MSG.toUpperCase().replace("!USERCHECK ", "").toString();
  968.             if (SID64REGEX.test(n)) {
  969.                 if (Object.keys(botSets).length > 0) {
  970.                     client.chatMessage(SENDER, "Loading badges...");
  971.                     Utils.getBadges(n, (ERR, DATA) => {
  972.                         if (!ERR) {
  973.                             let b = {}; // List with badges that CAN still be crafted
  974.                             if (DATA) {
  975.                                 for (let i = 0; i < Object.keys(DATA).length; i++) {
  976.                                     if (DATA[Object.keys(DATA)[i]] < 6) {
  977.                                         b[Object.keys(DATA)[i]] = 5 - DATA[Object.keys(DATA)[i]];
  978.                                     }
  979.                                 }
  980.                             } else {
  981.                                 client.chatMessage(SENDER.getSteamID64(), n + "'s badges are empty, sending an offer without checking badges.");
  982.                             }
  983.                             // console.log(b);
  984.                             // TODO: COUNT AMOUNT OF SETS BOT CAN GIVE HIM
  985.                             // 1: GET BOTS CARDS. DONE
  986.                             // 2: GET PLAYER's BADGES. DONE
  987.                             // 3: MAGIC
  988.                             let hisMaxSets = 0,
  989.                                 botNSets = 0;
  990.                             // Loop for sets he has partially completed
  991.                             for (let i = 0; i < Object.keys(b).length; i++) {
  992.                                 if (botSets[Object.keys(b)[i]] && botSets[Object.keys(b)[i]].length >= 5 - b[Object.keys(b)[i]].length) {
  993.                                     hisMaxSets += 5 - b[Object.keys(b)[i]].length;
  994.                                 }
  995.                             }
  996.                             // Loop for sets he has never crafted
  997.                             for (let i = 0; i < Object.keys(botSets).length; i++) {
  998.                                 if (Object.keys(b).indexOf(Object.keys(botSets)[i]) < 0) {
  999.                                     if (botSets[Object.keys(botSets)[i]].length >= 5) {
  1000.                                         hisMaxSets += 5;
  1001.                                     } else {
  1002.                                         hisMaxSets += botSets[Object.keys(botSets)[i]].length;
  1003.                                     }
  1004.                                 }
  1005.                                 botNSets += botSets[Object.keys(botSets)[i]].length;
  1006.                             }
  1007.                             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.");
  1008.                         } else {
  1009.                             client.chatMessage(SENDER, "An error occurred while getting " + n + "'s badges. Please try again.");
  1010.                             console.log("An error occurred while getting badges: " + ERR);
  1011.                         }
  1012.                     });
  1013.                 } else {
  1014.                     client.chatMessage(SENDER, "Please try again later.");
  1015.                 }
  1016.             } else {
  1017.                 client.chatMessage(SENDER, "Please provide a valid SteamID64.");
  1018.             }
  1019.         }
  1020.  
  1021.  
  1022.         /*
  1023.                 else if (MSG.toUpperCase() == "!KEYS") {
  1024.                     manager.getInventoryContents(CONFIG.KEYSFROMGAME, 2, true, (ERR, INV, CURR) => {
  1025.                         if (ERR) {
  1026.                             client.chatMessage(SENDER, "An error occurred while loading the bot's inventory.");
  1027.                             console.log("## An error occurred while getting inventory: " + ERR);
  1028.                         } else {
  1029.                             let t = manager.createOffer(SENDER);
  1030.                             for (let i = 0; i < INV.length; i++) {
  1031.                                 if (CONFIG.ACCEPTEDKEYS.indexOf(INV[i].market_hash_name) >= 0) {
  1032.                                     t.addMyItem(INV[i]);
  1033.                                 }
  1034.                                 t.send();
  1035.                             }
  1036.                         }
  1037.                     });
  1038.                 }
  1039.         */
  1040.         else if (MSG.toUpperCase() == "!DEPOSIT") {
  1041.             t = manager.createOffer(SENDER.getSteamID64());
  1042.             t.getUserDetails((ERR, ME, THEM) => {
  1043.                 if (ERR) {
  1044.                     console.log("## An error occurred while getting trade holds: " + ERR);
  1045.                     client.chatMessage(SENDER, "An error occurred while getting your trade holds. Please try again");
  1046.                 } else if (ME.escrowDays == 0 && THEM.escrowDays == 0) {
  1047.                     if (ERR) {
  1048.                         console.log("## An error occurred while getting bot inventory: " + ERR);
  1049.                         client.chatMessage(SENDER, "An error occurred while loading the bot's inventory. Please try again.");
  1050.                     } else {
  1051.                         Utils.getInventory(SENDER.getSteamID64(), community, (ERR, DATA) => {
  1052.                             if (!ERR) {
  1053.                                 let s = DATA;
  1054.                                 Utils.getSets(s, allCards, (ERR, DDATA) => {
  1055.                                     if (!ERR) {
  1056.                                         sortSetsByAmountB(s, (DATA) => {
  1057.                                             let setsSent = {};
  1058.                                             firsttLoop: for (let i = 0; i < DATA.length; i++) {
  1059.                                                 console.log(setsSent);
  1060.                                                 console.log(DATA[i]);
  1061.                                                 if (DDATA[DATA[i]]) {
  1062.                                                     for (let j = 0; j < DDATA[DATA[i]].length; j++) {
  1063.                                                         if ((setsSent[DATA[i]] && setsSent[DATA[i]] < CONFIG.CARDS.MAXSETSELL) || !setsSent[DATA[i]]) {
  1064.                                                             t.addTheirItems(DDATA[DATA[i]][j]);
  1065.                                                             console.log("DEBUG#LOOP #2 CONTINUE: ITEM ADD");
  1066.                                                             if (!setsSent[DATA[i]]) {
  1067.                                                                 setsSent[DATA[i]] = 1;
  1068.                                                             } else {
  1069.                                                                 setsSent[DATA[i]] += 1;
  1070.                                                             }
  1071.                                                         } else {
  1072.                                                             console.log("DEBUG#LOOP #2 CONTINUE: RETURN");
  1073.                                                             continue firsttLoop;
  1074.                                                         }
  1075.                                                     }
  1076.                                                 } else {
  1077.                                                     console.log("DEBUG#LOOP #2 CONTINUE: RETURN 2");
  1078.                                                     continue firsttLoop;
  1079.                                                 }
  1080.                                             }
  1081.                                         });
  1082.                                         console.log("DEBUG#SENDING");
  1083.                                         t.data("commandused", "Deposit");
  1084.                                         t.send((ERR, STATUS) => {
  1085.                                             if (ERR) {
  1086.                                                 client.chatMessage(SENDER, "An error occurred while sending your trade. Steam Trades could be down. Please try again later.");
  1087.                                                 console.log("## An error occurred while sending trade: " + ERR);
  1088.                                             } else {
  1089.                                                 client.chatMessage(t.partner, "Trade confirmed! Click here to accept it: https://www.steamcommunity.com/tradeoffer/" + t.id);
  1090.                                                 console.log("## Trade offer sent!");
  1091.                                             }
  1092.                                         });
  1093.                                     } else {
  1094.                                         console.log("## An error occurred while getting bot sets: " + ERR);
  1095.                                     }
  1096.                                 });
  1097.                             } else {
  1098.                                 console.log("## An error occurred while getting user inventory: " + ERR);
  1099.                             }
  1100.                         });
  1101.                     }
  1102.                 } else {
  1103.                     client.chatMessage(SENDER, "Please make sure you don't have a trade hold!");
  1104.                 }
  1105.             });
  1106.         } else {
  1107.             client.chatMessage(SENDER, "Command not recognized.");
  1108.         }
  1109.     } else {
  1110.         client.chatMessage(SENDER, "Command not recognized. Use !help to see how this bot works.");
  1111.     }
  1112. });
  1113.  
  1114. client.on("friendRelationship", (SENDER, REL) => {
  1115.     if (REL === 2) {
  1116.         client.addFriend(SENDER);
  1117.     } else if (REL === 3) {
  1118.         if (CONFIG.INVITETOGROUPID) {
  1119.             client.inviteToGroup(SENDER, CONFIG.INVITETOGROUPID);
  1120.         }
  1121.         client.chatMessage(SENDER, CONFIG.MESSAGES.WELCOME);
  1122.     }
  1123. });
  1124.  
  1125. // manager.on("unknownOfferSent", (o, os) => {
  1126. //     if (o.state == 9) {
  1127. //         console.log("## OFFER SENT LIST");
  1128. //         community.checkConfirmations();
  1129. //     }
  1130. // });
  1131.  
  1132. // community.on("newConfirmation", (CONF) => {
  1133. //     console.log("## New confirmation.");
  1134. //     community.acceptConfirmationForObject(CONFIG.IDENTITYSECRET, CONF.id);
  1135. // });
  1136.  
  1137. manager.on("sentOfferChanged", (OFFER, OLDSTATE) => {
  1138.     if (OFFER.state == 2) {
  1139.         client.chatMessage(OFFER.partner, "Trade confirmed! Click here to accept it: https://www.steamcommunity.com/tradeoffer/" + OFFER.id);
  1140.     } else if (OFFER.state == 3) {
  1141.         Utils.getInventory(client.steamID.getSteamID64(), community, (ERR, DATA) => {
  1142.             if (!ERR) {
  1143.                 let s = DATA;
  1144.                 Utils.getSets(s, allCards, (ERR, DATA) => {
  1145.                     if (!ERR) {
  1146.                         botSets = DATA;
  1147.                         console.log("## Bot's sets loaded.");
  1148.                     } else {
  1149.                         console.log("## An error occurred while getting bot sets: " + ERR);
  1150.                     }
  1151.                     let botNSets = 0;
  1152.                     for (let i = 0; i < Object.keys(botSets).length; i++) {
  1153.                         botNSets += botSets[Object.keys(botSets)[i]].length;
  1154.                     }
  1155.                     totalBotSets = botNSets;
  1156.                     let playThis = CONFIG.PLAYGAMES;
  1157.                     if (CONFIG.PLAYGAMES && typeof(CONFIG.PLAYGAMES[0]) == "string") {
  1158.                         playThis[0] = parseString(playThis[0], totalBotSets);
  1159.                     }
  1160.                     client.gamesPlayed(playThis);
  1161.                 });
  1162.             } else {
  1163.                 console.log("## An error occurred while getting bot inventory: " + ERR);
  1164.             }
  1165.         });
  1166.         if (CONFIG.INVITETOGROUPID) {
  1167.             client.inviteToGroup(OFFER.partner, CONFIG.INVITETOGROUPID);
  1168.         }
  1169.         let d = "" + OFFER.data("commandused") + "";
  1170.         d += "\nSets: " + OFFER.data("amountofsets");
  1171.         d += "\nKeys: " + OFFER.data("amountofkeys");
  1172.         d += "\nSteamID: " + OFFER.partner.getSteamID64();
  1173.         fs.writeFile("./TradesAccepted/" + OFFER.id + "-" + OFFER.partner.getSteamID64() + ".txt", d, (ERR) => {
  1174.             if (ERR) {
  1175.                 console.log("## An error occurred while writing trade file: " + ERR);
  1176.             }
  1177.         });
  1178.         community.getSteamUser(OFFER.partner, (ERR, USER) => {
  1179.             if (ERR) {
  1180.                 console.log("## An error occurred while getting user profile: " + ERR);
  1181.                 client.chatMessage(OFFER.partner, "An error occurred while getting your profile (to comment).");
  1182.             } else {
  1183.                 USER.comment(CONFIG.COMMENTAFTERTRADE, (ERR) => {
  1184.                     if (ERR) {
  1185.                         console.log("## An error occurred while commenting on user profile: " + ERR);
  1186.                         client.chatMessage(OFFER.partner, "An error occurred while getting commenting on your profile.");
  1187.                     } else {
  1188.                         client.chatMessage(OFFER.partner, "Thanks for trading! :D");
  1189.                     }
  1190.                 });
  1191.             }
  1192.         });
  1193.     } else if (OFFER.state == 6) {
  1194.         client.chatMessage(OFFER.partner, "Hey, you did not accept the offer. Please try again if you wish to receive sets!");
  1195.     }
  1196.     /* else if (OFFER.state == 9) {
  1197.             community.checkConfirmations();
  1198.         }*/
  1199. });
  1200.  
  1201. manager.on("newOffer", (OFFER) => {
  1202.     if (CONFIG.ADMINS.indexOf(OFFER.partner.getSteamID64()) >= 0 || CONFIG.ADMINS.indexOf(parseInt(OFFER.partner.getSteamID64())) >= 0) {
  1203.         OFFER.getUserDetails((ERR, ME, THEM) => {
  1204.             if (ERR) {
  1205.                 console.log("## An error occurred while getting trade holds: " + ERR);
  1206.                 client.chatMessage(OFFER.partner, "An error occurred while getting your trade holds. Please try again");
  1207.                 OFFER.decline((ERR) => {
  1208.                     if (ERR) {
  1209.                         console.log("## An error occurred while declining trade: " + ERR);
  1210.                     }
  1211.                 });
  1212.             } else if (ME.escrowDays == 0 && THEM.escrowDays == 0) {
  1213.                 OFFER.accept((ERR) => {
  1214.                     if (ERR) {
  1215.                         console.log("## An error occurred while declining trade: " + ERR);
  1216.                         OFFER.decline((ERR) => {
  1217.                             if (ERR) {
  1218.                                 console.log("## An error occurred while declining trade: " + ERR);
  1219.                             }
  1220.                         });
  1221.                     } else {
  1222.                         client.chatMessage(OFFER.partner, "Offer accepted!");
  1223.                     }
  1224.                 });
  1225.             } else {
  1226.                 client.chatMessage(OFFER.partner, "Please make sure you don't have a trade hold!");
  1227.                 OFFER.decline((ERR) => {
  1228.                     if (ERR) {
  1229.                         console.log("## An error occurred while declining trade: " + ERR);
  1230.                     }
  1231.                 });
  1232.             }
  1233.         });
  1234.     } else if (OFFER.itemsToGive.length == 0) {
  1235.         OFFER.accept((ERR) => {
  1236.             console.log("DONATION ACCEPT");
  1237.             if (ERR) {
  1238.                 console.log("## An error occurred while declining trade: " + ERR);
  1239.             }
  1240.         });
  1241.     } else {
  1242.         OFFER.decline((ERR) => {
  1243.             if (ERR) {
  1244.                 console.log("## An error occurred while declining trade: " + ERR);
  1245.             }
  1246.         });
  1247.     }
  1248. });
  1249.  
  1250. community.on("newConfirmation", (CONF) => {
  1251.     console.log("## New confirmation.");
  1252.     community.acceptConfirmationForObject(CONFIG.IDENTITYSECRET, CONF.id, (ERR) => {
  1253.         if (ERR) {
  1254.             console.log("## An error occurred while accepting confirmation: " + ERR);
  1255.         } else {
  1256.             console.log("## Confirmation accepted.");
  1257.         }
  1258.     });
  1259. });
  1260.  
  1261. function sortSetsByAmount(SETS, callback) {
  1262.     callback(Object.keys(SETS).sort((k1, k2) => SETS[k1].length - SETS[k2].length).reverse());
  1263. }
  1264.  
  1265. function sortSetsByAmountB(SETS, callback) {
  1266.     callback(Object.keys(SETS).sort((k1, k2) => SETS[k1].length - SETS[k2].length));
  1267. }
  1268.  
  1269. function parseString(INPUT, SETS) {
  1270.     return INPUT.replace(":sets:", SETS);
  1271. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement