Advertisement
Guest User

Trading Bot

a guest
Dec 1st, 2018
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const SteamUser = require('steam-user');
  2. const SteamTotp = require('steam-totp');
  3. const SteamCommunity = require('steamcommunity');
  4. const TradeOfferManager = require('steam-tradeoffer-manager');
  5. const TeamFortress2 = require('tf2');
  6.  
  7.  
  8. const Prices = require('./prices.json');
  9. const config = require('./config.json');
  10.  
  11. const client = new SteamUser();
  12. const tf2 = new TeamFortress2(client);
  13. const community = new SteamCommunity();
  14. const manager = new TradeOfferManager ({
  15.     steam: client,
  16.     community: community,
  17.     language: 'en'
  18. });
  19.  
  20. const logOnOptions = {
  21.    accountName: config.username,
  22.    password: config.password,
  23.    twoFactorCode: SteamTotp.generateAuthCode(config.sharedSecret)
  24. };
  25.  
  26. client.logOn(logOnOptions);
  27.  
  28. client.on('loggedOn', () => {
  29.    console.log('successfully logged on.')
  30.    client.setPersona(SteamUser.Steam.EPersonaState.Online);
  31.    client.gamesPlayed([440]);
  32. });
  33.  
  34. client.on("friendMessage", function(steamID, message) {
  35.   if (message == "!help") {
  36.     client.chatMessage(steamID, "Hey! I'm an actual trader, but the bot is still a work-in-progress. Just go by the classifieds and, hopefully, it should work. Thanks again!");
  37.   }
  38. });
  39.  
  40. client.on('webSession', (sessionid, cookies) => {
  41.     manager.setCookies(cookies);
  42.  
  43.     community.setCookies(cookies)
  44.     community.startConfirmationChecker(20000, config.identitySecret);
  45. });
  46.  
  47. function acceptOffer(offer) {
  48.     offer.accept((err) => {
  49.         community.checkConfirmations();
  50.         console.log("We accepted an offer!");
  51.         if (err) console.log("Oof! That's not radical. There was an error with accepting the offer, man.");
  52.     });
  53. }
  54.  
  55. function declineOffer(offer) {
  56.     offer.decline((err) => {
  57.         console.log("We declined an offer.");
  58.         if (err) console.log("Oof! That's not radical. There was an error with declining the offer, man.");
  59.     });
  60. }
  61.  
  62. function processOffer(offer) {
  63.     if (offer.isGlitched() || offer.state === 11) {
  64.         console.log("Ooohhh.... that's not good. The offer's glitched. Declining...");
  65.         declineOffer(offer);
  66.     } else if (offer.partner.getSteamID64() === config.ownerID) {
  67.         acceptOffer(offer);
  68.     } else {
  69.         var ourItems = offer.itemsToGive;
  70.         var theirItems = offer.itemsToReceive;
  71.         var ourValue = 0;
  72.         var theirValue = 0;
  73.         for (var i in ourItems) {
  74.             var item = ourItems[i].market_name
  75.             if(Prices[item]) {
  76.                 ourValue += Prices[item].sell;
  77.             } else {
  78.                 console.log("Woah, slow down there, dude! That's the invalid value.")
  79.                 ourValue += 99999;
  80.             }
  81.         }
  82.         for(var i in theirItems) {
  83.             var item= theirItems[i].market_name;
  84.             if(Prices[item]) {
  85.                 theirValue += Prices[item].buy;
  86.             } else {
  87.             console.log("Dude, their value was different.")
  88.             }
  89.         }
  90.     }
  91.     console.log("Our value: "+ourValue);
  92.     console.log("Their value: "+theirValue);
  93.  
  94.     if (ourValue <= theirValue) {
  95.         acceptOffer(offer);
  96.     } else {
  97.         declineOffer(offer);
  98.     }
  99. }
  100.  
  101. client.setOption("promptSteamGuardCode", false);
  102.  
  103. manager.on('newOffer', (offer) => {
  104.     processOffer(offer);
  105. });
  106.  
  107. /* Crafting */
  108.  
  109. var scrapAmt = 25;
  110. var pollCraft = 30;
  111.  
  112. tf2.on('connectedToGC', function() {
  113.     console.log("Gnarly, bro! Just connected to the TF2 game server.")
  114. });
  115.  
  116. tf2.on('backpackLoaded', function () {
  117.     console.log("Backpack just got loaded, bro.");
  118. });
  119.  
  120. function craftS(amtNeedScrap) {
  121.     if (tf2.backpack == undefined) {
  122.         console.log("Damnit, we can't load the backpack! No crafting here.");
  123.         return
  124.     } else {
  125.         console.log("Hang on, bro. We're attempting to craft...")
  126.         var amtOfScrap = 0;
  127.         for (var i = 0; i < tf2.backpack.length; i++) {
  128.             if (tf2.backpack[i].defIndex === 5000) {
  129.                 amtOfScrap++;
  130.             }
  131.         }
  132.         for (var i = 0; i < tf2.backpack.length; i++) {
  133.             if (tf2.backpack[i].defIndex = 5002) {
  134.                 amtOfScrap+=9;
  135.                 var beep = new Array;
  136.                 beep.push(parseInt(tf2.backpack[i].id));
  137.                 tf2.craft(beep);
  138.             } else if (tf2.backpack[i]defIndex === 5001) {
  139.                 amtOfScrap +=3;
  140.                 var beep = new Array;
  141.                 beep.push(parseInt(tf2.backpack[i].id));
  142.                 tf2.craft(beep);
  143.             }
  144.            if (amtOfScrap >= amtNeedScrap) {
  145.                 break;
  146.            }
  147.         }
  148.        
  149.     }
  150. }
  151.  
  152. tf2.on('craftingComplete', function(e) {
  153.     conosle.log("Finished crafting, dude!");
  154. });
  155.  
  156. client.on('friendMessage#'+config.ownerID, function(steamID, message) {
  157.     if (message == "!craft") {
  158.         craft(scrapAmt);
  159.         console.log("Received order to craft!")
  160.     } else {
  161.         console.log("Damn! Craft error, man.")
  162.     }
  163. });            
  164.    
  165. setInterval(function() {
  166.     craftS(scrapAmt);
  167. }, 1000 * 60 * pollCraft)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement