Advertisement
enos

mycode

Feb 23rd, 2017
445
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // strict mode
  2. 'use strict';
  3.  
  4. // initialize
  5. var SteamUser = require('steam-user');
  6. var SteamCommunity = require('steamcommunity');
  7. var SteamTradeOfferManager = require('steam-tradeoffer-manager');
  8. var SteamTOTP = require('steam-totp');
  9. var fs = require('fs');
  10. var config = require('./config');
  11. var db = require('./db');
  12.  
  13. var colors = require('colors/safe');
  14. var mysql = require('mysql'); //DB
  15.  
  16. // create a steam client
  17. var client = new SteamUser();
  18.  
  19. // create a steam community object
  20. var community = new SteamCommunity();
  21.  
  22. // create a steam trade offer manager
  23. var manager = new SteamTradeOfferManager({
  24.   steam: client,
  25.   language: 'en'
  26. });
  27.  
  28.  
  29. ///*
  30. var connection = mysql.createConnection({
  31.   host     : 'localhost', //103.227.176.11
  32.   user     : 'root',
  33.   password : '',
  34.   database : 'new_csgo'
  35. });
  36. //*/
  37.  
  38. connection.connect(function(err){
  39.  if(!err) {
  40.      console.log("================================================");
  41.      console.log(colors.green("Database is connected ... \n"));  
  42.  } else {
  43.      console.log("Error connecting database ... \n\n");  
  44.  }
  45.  });
  46.  
  47.  
  48. // poll information
  49. if(fs.existsSync('polldata.json')) {
  50.   manager.pollData = JSON.parse(fs.readFileSync('polldata.json'));
  51. }
  52.  
  53. // steam login
  54. console.log('connecting to steam account...');
  55.  
  56. client.logOn({
  57.   accountName: config.accountName,
  58.   password: config.password,
  59.   twoFactorCode: SteamTOTP.getAuthCode(config.shared_secret)
  60. });
  61.  
  62. // login successful
  63. client.on('loggedOn', function(details, parental) {
  64.  
  65.   console.log('connected\n');
  66.   console.log('SteamID2R :', client.steamID.getSteam2RenderedID());
  67.   console.log('SteamID3R :', client.steamID.getSteam3RenderedID());
  68.   console.log('SteamID64 :', client.steamID.getSteamID64());
  69.  
  70.   // set persona state to online
  71.   client.setPersona(SteamUser.EPersonaState.Online);
  72.  
  73.   // acknowledge admin
  74.   client.chatMessage(config.admin, 'Yo! I am now online.');
  75. });
  76.  
  77. // login error
  78. client.on('error', function(error) {
  79.  
  80.   console.log('connection error\n');
  81.   console.log(error);
  82. });
  83.  
  84. // account information
  85. client.on('accountInfo', function(name, country, authedMachines, flags, fbID, fbName) {
  86.  
  87.   console.log('SteamName :', name);
  88.   console.log('SteamDevs :', authedMachines);
  89. });
  90.  
  91. // account limitations
  92. client.on('accountLimitations', function(limited, communityBanned, locked, canInviteFriends) {
  93.  
  94.   console.log('IsLimited :', limited);
  95.   console.log('IsCBanned :', communityBanned);
  96.   console.log('IsSLocked :', locked);
  97.   console.log('CanInvite :', canInviteFriends);
  98. });
  99.  
  100. // web session
  101. client.on('webSession', function(sessionID, cookies) {
  102.    
  103.    
  104.     var a = {sessionid:''+sessionID+'', cookies:''+cookies+''};
  105.     //var b = 'bbb';    update tabel a set colom x=x, y=y where id=1
  106.     connection.query('update cookies SET ?  where id=?' ,[a, 1], function(err){
  107.         if(!err) {
  108.             console.log("cookies berhasil disimpan \n\n");  
  109.         } else {
  110.             console.log("Error... \n\n");  
  111.         }
  112.     });
  113.  
  114.   community.setCookies(cookies);
  115.   community.startConfirmationChecker(10000, identity_secret);
  116.  
  117.   manager.setCookies(cookies, null, function(error) {
  118.  
  119.     // failed to obtain steam api key
  120.     if(error) {
  121.       console.log('\nError: Failed to Obtain Steam API Key\n');
  122.       console.log(error);
  123.       process.exit();
  124.       return;
  125.     }
  126.    
  127.     console.log("API key: " + manager.apiKey);
  128.        
  129.         console.log("============================  \n ");
  130.    
  131.   });
  132. });
  133.  
  134. // admin commands
  135. client.on('friendMessage#' + config.admin, function(steamID, message) {
  136.  
  137.   console.log('--------------------------------------------------------------------------------');
  138.   console.log('AdminsCmd :', steamID.getSteam3RenderedID(), message);
  139.  
  140.   var command = message.toLowerCase();
  141.  
  142.   // GIVE AN AUTHENTICATION CODE
  143.   if(command === '!authcode') {
  144.     // generate an authentication code
  145.     var authCode = SteamTOTP.getAuthCode(config.shared_secret);
  146.  
  147.     // send the authentication code to admin
  148.     client.chatMessage(config.admin, authCode);
  149.   }
  150.  
  151.   // SEND INVENTORY ITEMS TO ADMIN
  152.   // [1] STEAM ITEMS
  153.   else if(command === '!send steam items') {
  154.     sendTradeOffer(steamID, 753, 6);
  155.   }
  156.  
  157.   // [2] TEAM FORTRESS 2 ITEMS
  158.   else if(command === '!send tf2 items') {
  159.     sendTradeOffer(steamID, 440, 2);
  160.   }
  161.  
  162.   // [3] DOTA 2 ITEMS
  163.   else if(command === '!send dota2 items') {
  164.     sendTradeOffer(steamID, 570, 2);
  165.   }
  166.  
  167.   // [4] COUNTER-STRIKE GLOBAL OFFENSIVE ITEMS
  168.   else if(command === '!send csgo items') {
  169.     sendTradeOffer(steamID, 730, 2);
  170.   }
  171.  
  172.   // UNKNOWN COMMAND
  173.   else {
  174.     // acknowledge admin
  175.     client.chatMessage(config.admin, 'Sorry! I didn\'t understand that.');
  176.   }
  177. });
  178.  
  179. // process a received trade offer
  180. manager.on('newOffer', function(offer) {
  181.  
  182.   console.log('--------------------------------------------------------------------------------');
  183.   console.log('StatusMsg : Received new trade offer #${'+ offer.id +'} from ${offer.partner.getSteam3RenderedID()}');
  184.  
  185.   processTradeOffer(offer);
  186. })
  187.  
  188. // sent trade offer changed
  189. manager.on('sentOfferChanged', function(offer, oldState) {
  190.  
  191.   console.log('StatusMsg : Trade Offer #${'+ offer.id +'} updated [${'+ SteamTradeOfferManager.ETradeOfferState[oldState] +'} -> ${'+ SteamTradeOfferManager.ETradeOfferState[offer.state] +'}]');
  192. });
  193.  
  194. // received trade offer changed
  195. manager.on('receivedOfferChanged', function(offer, oldState) {
  196.  
  197.   console.log('StatusMsg : Trade Offer #${'+ offer.id +'} updated [${'+ SteamTradeOfferManager.ETradeOfferState[oldState] +'} -> ${'+ SteamTradeOfferManager.ETradeOfferState[offer.state] +'}]');
  198. });
  199.  
  200. // update the poll information
  201. manager.on('pollData', function(pollData) {
  202.  
  203.   fs.writeFile('polldata.json', JSON.stringify(pollData));
  204. });
  205.  
  206.  
  207. // send a trade offer
  208. function sendTradeOffer(steamID, appID, contextID) {
  209.  
  210.   // load inventory
  211.   console.log('StatusMsg : Loading intentory...');
  212.  
  213.   manager.getInventoryContents(appID, contextID, true, function(error, inventory, currencies) {
  214.  
  215.     // failed to load inventory
  216.     if(error) {
  217.       console.log('\nError: Failed to load inventory\n');
  218.       console.log(error);
  219.       return;
  220.     }
  221.  
  222.     // empty inventory
  223.     if(inventory.length === 0) {
  224.       console.log('StatusMsg : No tradable items found in inventory');
  225.  
  226.       // acknowledge admin
  227.       client.chatMessage(config.admin, 'Sorry! I don\'t have any item to send.');
  228.  
  229.       return;
  230.     }
  231.  
  232.     // inventory loaded
  233.     console.log('StatusMsg : Total ' + inventory.length + ' tradable items found in inventory');
  234.  
  235.     // create a trade offer
  236.     var offer = manager.createOffer(steamID);
  237.     offer.addMyItems(inventory);
  238.  
  239.     // send the trade offer
  240.     offer.send(function(error, status) {
  241.  
  242.       // failed to send the trade offer
  243.       if(error) {
  244.         console.log('\nError: Failed to send the trade offer\n');
  245.         console.log(error);
  246.         return;
  247.       }
  248.  
  249.       // the trade offer is sent but needs confirmation
  250.       else if(status === 'pending') {
  251.         console.log('StatusMsg : The trade offer is sent but needs confirmation');
  252.  
  253.         // confirm the trade offer
  254.         community.acceptConfirmationForObject(config.identity_secret, offer.id, function(error) {
  255.  
  256.           // failed to confirm the trade offer
  257.           if(error) {
  258.             console.log('\nError: Failed to confirm the trade offer\n');
  259.             console.log(error);
  260.             return;
  261.           }
  262.  
  263.           // the trade offer is confirmed
  264.           else {
  265.             console.log('StatusMsg : The trade offer is confirmed');
  266.  
  267.             // acknowledge admin
  268.             client.chatMessage(config.admin, 'Yo! I have sent you ' + inventory.length + ' items.');
  269.  
  270.             return;
  271.           }
  272.         });
  273.       }
  274.  
  275.       // the trade offer is sent successfully
  276.       else {
  277.         console.log('StatusMsg : The trade offer is sent');
  278.  
  279.         // acknowledge admin
  280.         client.chatMessage(config.admin, 'Yo! I have sent you ' + inventory.length + ' items.');
  281.  
  282.         return;
  283.       }
  284.     });
  285.   });
  286. }
  287.  
  288.  
  289. // process a received trade offer
  290. function processTradeOffer(offer) {
  291.  
  292.   // unconditionally decline trade offers if glitched or in escrow
  293.   if(offer.isGlitched() || offer.state === 11) {
  294.     console.log('StatusMsg : Received invalid trade offer #${'+ offer.id +'}');
  295.     declineTradeOffer(offer);
  296.   }
  297.  
  298.   // // unconditionally accept a trade offer from admin
  299.   // else if(offer.partner.getSteamID64() === config.admin) {
  300.   //   console.log('StatusMsg : Received trade offer #${offer.id} from admin');
  301.   //   acceptTradeOffer(offer);
  302.   // }
  303.  
  304.   // trade offer from others
  305.   else {
  306.     var itemsToGive = offer.itemsToGive;
  307.     var itemsToReceive = offer.itemsToReceive
  308.     var outgoingValue = 0;
  309.     var incomingValue = 0;
  310.  
  311.     // calculate total value of outgoing items
  312.     for(var i in itemsToGive) {
  313.       var item = itemsToGive[i].market_hash_name;
  314.       if(db[item]) {
  315.         outgoingValue += db[item].sell;
  316.       }
  317.       else {
  318.         outgoingValue += 99999999;
  319.       }
  320.     }
  321.  
  322.     // calculate total value of incoming items
  323.     for(var i in itemsToReceive) {
  324.       var item = itemsToReceive[i].market_hash_name;
  325.       if(db[item]) {
  326.         incomingValue += db[item].buy;
  327.       }
  328.     }
  329.  
  330.     console.log('StatusMsg : Total outgoing value is ' + outgoingValue);
  331.     console.log('StatusMsg : Total incoming value is ' + incomingValue);
  332.  
  333.     // accept trade offer if total outgoing value is less than or equal to
  334.     // total incoming value decline otherwise
  335.     if(outgoingValue <= incomingValue) {
  336.       acceptTradeOffer(offer);
  337.     }
  338.  
  339.     else {
  340.       declineTradeOffer(offer);
  341.     }
  342.   }
  343. }
  344.  
  345.  
  346. // decline a trade offer
  347. function declineTradeOffer(offer) {
  348.  
  349.   offer.decline(function(error) {
  350.  
  351.     // failed to decline the trade offer
  352.     if(error) {
  353.       console.log('\nError: Failed to decline the trade offer #${'+ offer.id +'}\n');
  354.       console.log(error);
  355.       return;
  356.     }
  357.  
  358.     // the trade offer is declined
  359.     else {
  360.       console.log('StatusMsg : The trade offer #${'+ offer.id +'} is declined');
  361.       return;
  362.     }
  363.   });
  364. }
  365.  
  366.  
  367. // accept a trade offer
  368. function acceptTradeOffer(offer) {
  369.  
  370.   offer.accept(false, function(error, status) {
  371.  
  372.     // failed to accept the trade offer
  373.     if(error) {
  374.       console.log('\nError: Failed to accept the trade offer #${'+ offer.id +'}\n');
  375.       console.log(error);
  376.       return;
  377.     }
  378.  
  379.     // the trade offer is accepted but needs confirmation
  380.     else if(status === 'pending') {
  381.       console.log('StatusMsg : The trade offer #${'+ offer.id +'} is accepted but needs confirmation');
  382.  
  383.       // confirm the trade offer
  384.       community.acceptConfirmationForObject(config.identity_secret, offer.id, function(error) {
  385.  
  386.         // failed to confirm the trade offer
  387.         if(error) {
  388.           console.log('\nError: Failed to confirm the trade offer #${'+ offer.id +'}\n');
  389.           console.log(error);
  390.           return;
  391.         }
  392.  
  393.         // the trade offer is confirmed
  394.         else {
  395.           console.log('StatusMsg : The trade offer #${'+ offer.id +'} is confirmed');
  396.           return;
  397.         }
  398.       });
  399.     }
  400.  
  401.     // the trade offer is accepted successfully
  402.     else {
  403.       console.log('StatusMsg : The trade offer #${'+ offer.id +'} is accepted');
  404.       return;
  405.     }
  406.   });  
  407. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement