seishun / node-steam

Interface directly with Steam servers from Node.js
MIT License
1k stars 181 forks source link

TypeError: Cannot read property 'on' of undifined #392

Closed wntjq6902 closed 7 years ago

wntjq6902 commented 7 years ago

G:\node.js\custom_steambot>node tftrade.js G:\node.js\custom_steambot\node_modules\steam\lib\handlers\trading\index.js:10 this._client.on('message', function(header, body, callback) { ^

TypeError: Cannot read property 'on' of undefined at new SteamTrading (G:\node.js\custom_steambot\node_modules\steam\lib\handl ers\trading\index.js:10:15) at Object. (G:\node.js\custom_steambot\tftrade.js:14:20) at Module._compile (module.js:571:32) at Object.Module._extensions..js (module.js:580:10) at Module.load (module.js:488:32) at tryModuleLoad (module.js:447:12) at Function.Module._load (module.js:439:3) at Module.runMain (module.js:605:10) at run (bootstrap_node.js:420:7) at startup (bootstrap_node.js:139:9)

seishun commented 7 years ago

Testcase?

wntjq6902 commented 7 years ago

windows 7 64 bit (no sp) node.js 7.3.0

tftrade.js: var fs = require('fs'); var Steam = require('steam'); var admin = '76561198056988602';

var logOnOptions = { account_name: 'wbot01', password: '---------------' };

var crypto = require('crypto'); var SteamWebLogOn = require('steam-weblogon'); var getSteamAPIKey = require('steam-web-api-key'); var SteamTradeOffers = require('steam-tradeoffers'); var steamTrading = new Steam.SteamTrading(steamClient); var SteamTrade = require('steam-trade'); var steamTrade = new SteamTrade();

var sentryFileName = 'sentry'; // steam guard data file name

try { logOnOptions.sha_sentryfile = getSHA1(fs.readFileSync(sentryFileName)); } catch (e) { if (authCode !== '') { logOnOptions.auth_code = authCode; } }

if (fs.existsSync('servers')) { Steam.servers = JSON.parse(fs.readFileSync('servers')); }

var steamClient = new Steam.SteamClient(); var steamUser = new Steam.SteamUser(steamClient); var steamFriends = new Steam.SteamFriends(steamClient);

steamClient.connect(); steamClient.on('connected', function() { steamUser.logOn({ account_name: 'wbot01', password: '721016wntjq' }); });

steamClient.on('logOnResponse', function(logonResp) { if (logonResp.eresult == Steam.EResult.OK) { console.info('Logged in!'); steamFriends.setPersonaState(Steam.EPersonaState.Online); // to display your bot's status as "Online" steamFriends.setPersonaName('Error Bot #01 [TFITEM]'); // to change its nickname steamWebLogOn.webLogOn(function(sessionID, newCookie) { getSteamAPIKey({ sessionID: sessionID, webCookie: newCookie }, function(err, APIKey) { offers.setup({ sessionID: sessionID, webCookie: newCookie, APIKey: APIKey }); handleOffers(); }); }); } });

steamFriends.on('chatInvite', function(chatRoomID, chatRoomName, patronID) { console.info('Got an invite to ' + chatRoomName + ' from ' + steamFriends.personaStates[patronID].player_name); steamFriends.joinChat(chatRoomID); // autojoin on invite fs.writeFile('servers', JSON.stringify(servers)); });

steamFriends.on('message', function(source, message, type, chatter) { // respond to both chat room and private messages console.info('Received message: ' + message); if (message == 'echo') { steamFriends.sendMessage(source, 'Hello, World!', Steam.EChatEntryType.ChatMsg); // ChatMsg by default }else if (message == 'price') { steamFriends.sendMessage(source, '>>buy<< <>\nKey >>29.33 ref<< <<29.44 ref>>\nbill\'s hat >>1 key 19 ref<< <<1 key 19.11 ref>>\nmax\'s head >>18 key<< << 18 key 0.11 ref>>') }else if (message == 'stock') { steamFriends.sendMessage(source, 'current stock\nkey: 0/20\nbill\'s hat: 0/5\nmax\'s head: 0/5\nraw metals: none') }else if (message == 'ping') { steamFriends.sendMessage(source, 'ping: 2\^32-1 ms') steamFriends.sendMessage(source, 'just kidding lol') }else if (message == 'help') { steamFriends.sendMessage(source, 'Hi there! i\'m wip trading steam bot by error name!\n type commends for all commends') }else if (message == 'commends') { steamFriends.sendMessage(source, 'price stock ping help commends shutdown') }else if (message == 'buy') { steamTrade.open(source)

} }); steamUser.on('updateMachineAuth', function(sentry, callback) { fs.writeFileSync(sentryFileName, sentry.bytes); callback({ sha_file: getSHA1(sentry.bytes) }); });

steamUser.on('tradeOffers', function(number) { if (number > 0) { handleOffers(); } });

function handleOffers() { offers.getOffers({ get_received_offers: 1, active_only: 1, time_historical_cutoff: Math.round(Date.now() / 1000), get_descriptions: 1 }, function(error, body) { if (error) { return log(error); }

if (
  body
  && body.response
  && body.response.trade_offers_received
) {
  var descriptions = {};

  body.response.descriptions = body.response.descriptions || [];

  body.response.descriptions.forEach(function (desc) {
    descriptions[
      desc.appid + ';' + desc.classid + ';' + desc.instanceid
    ] = desc;
  });

  body.response.trade_offers_received.forEach(function (offer) {
    if (offer.trade_offer_state !== 2) {
      return;
    }

    var offerMessage = 'Got an offer ' + offer.tradeofferid +
      ' from ' + offer.steamid_other + '\n';

    if (offer.items_to_receive) {
      offerMessage += 'Items to receive: ' +
        offer.items_to_receive.map(function (item) {
          var desc = descriptions[
            item.appid + ';' + item.classid + ';' + item.instanceid
          ];
          return desc.name + ' (' + desc.type + ')';
        }).join(', ') + '\n';
    }

    if (offer.items_to_give) {
      offerMessage += 'Items to give: ' +
        offer.items_to_give.map(function (item) {
          var desc = descriptions[
            item.appid + ';' + item.classid + ';' + item.instanceid
          ];
          return desc.name + ' (' + desc.type + ')';
        }).join(', ') + '\n';
    }

    if (offer.message && offer.message !== '') {
      offerMessage += 'Message: ' + offer.message;
    }

    log(offerMessage);

    if (offer.steamid_other === admin || !offer.items_to_give) {
      offers.acceptOffer({
        tradeOfferId: offer.tradeofferid,
        partnerSteamId: offer.steamid_other
      }, function (error, result) {
        if (error) {
          return log(error);
        }

        log('Offer ' + offer.tradeofferid + ' accepted');

        offers.getOffer({
          tradeofferid: offer.tradeofferid
        }, function (error, result) {
          if (error) {
            return log(error);
          }

          if (result
            && result.response
            && result.response.offer
            && result.response.offer.tradeid
          ) {
            offers.getItems({
              tradeId: result.response.offer.tradeid
            }, function (error, result) {
              if (error) {
                return log(error);
              }

              var items = 'Got items:\n' +
                result.map(function (item) {
                  return 'http://steamcommunity.com/profiles/' +
                    item.owner + '/inventory/#' +
                    item.appid + '_' + item.contextid + '_' + item.id;
                }).join('\n');

              log(items);
            });
          }
        });
      });
    } else {
      offers.declineOffer({
        tradeOfferId: offer.tradeofferid
      }, function (error, result) {
        if (error) {
          return log(error);
        }

        log('Offer ' + offer.tradeofferid + ' declined');
      });
    }
  });
}

}); }

function log (message) { console.log(new Date().toString() + ' - ' + message); steamFriends.sendMessage(admin, message.toString()); }

function getSHA1 (bytes) { var shasum = crypto.createHash('sha1'); shasum.end(bytes); return shasum.read(); }

yes, my code are bad, i'm still learning js

seishun commented 7 years ago
  1. You should learn how to paste code in GitHub comments.
  2. You passed undefined to the SteamTrading constructor because steamClient wasn't initialized at that point.