Jump to content
McKay Development

Recommended Posts

Posted
const checkTradeStatus = (socket, tradeOfferId, steamID) => {
  const CHECK_INTERVAL = 10000; // 10 seconds
  const TIMEOUT_DURATION = 300000; // 5 minutes in milliseconds
  const startTime = Date.now();
 
  const interval = setInterval(() => {
    bot.manager.getOffer(tradeOfferId, (err, tradeOffer) => {
      if (err) {
        console.error('Error fetching trade offer:', err.message);
        socket.emit("failure", `Error: ${err.message}`);
        clearInterval(interval); // Stop checking on error
        return;
      }
 
      if (!tradeOffer) {
        console.error('Trade offer is undefined');
        socket.emit("failure", 'Trade offer is undefined');
        clearInterval(interval); // Stop checking if trade offer is undefined
        return;
      }
 
      const tradeOfferState = tradeOffer.state;
      const itemsToGive = tradeOffer.itemsToGive || [];
      const assetIdsToGive = itemsToGive.map(item => item.assetid);
 
      if (tradeOfferState === TradeOfferManager.ETradeOfferState.Accepted) {
        console.log("Trade is completed!");
        socket.emit("success", assetIdsToGive);
        clearInterval(interval); // Stop checking once trade is complete
        fetchTrade(assetIdsToGive, steamID); 
      } else if (tradeOfferState !== TradeOfferManager.ETradeOfferState.Active) {
        console.log("Trade failed with state:", tradeOfferState);
        socket.emit('failure', {
          message: 'Trade failed or is in an unexpected state.',
          tradeOfferState
        });
        clearInterval(interval); // Stop checking if trade fails
      } else {
        console.log(`Trade offer ${tradeOfferId} is still active.`);
      }
 
      // Check if the timeout has been reached
      if (Date.now() - startTime > TIMEOUT_DURATION) {
        console.log('Timeout reached while waiting for trade offer to complete.');
        socket.emit('failure', 'Timeout reached while waiting for trade offer to complete.');
        clearInterval(interval); // Stop checking after timeout
      }
    });
  }, CHECK_INTERVAL);
};
class SteamBot {
  constructor(logOnOptions) {
    this.client = new SteamUser();
    this.community = new SteamCommunity();
    this.manager = new TradeOfferManager({
      steam: this.client,
      community: this.community,
      language: 'en',
      pollInterval: 30000
    });
   
    this.client.logOn(logOnOptions);
 
    this.client.on('loggedOn', () => {
      console.log('Logged into Steam');
 
      this.client.setPersona(SteamUser.EPersonaState.Online);
    });
 
    this.client.on('webSession', (sessionID, cookies) => {
      this.manager.setCookies(cookies, (err) => {
        if (err) {
          console.error('Error setting cookies for TradeOfferManager:', err);
        } else {
          console.log('Cookies set for TradeOfferManager');
        }
      });
 
      this.community.setCookies(cookies);
      this.community.startConfirmationChecker(10000, 'placeholder');
    });
 
    this.manager.on('newOffer', (offer) => {
      console.log('Incoming trade offer received'); // Log when a new offer is received
      if (offer.partner.getSteamID64() === allowedSteamID) {
        offer.accept((err, status) => {
          if (err) {
            console.error(`Unable to accept offer: ${err.message}`);
          } else {
            console.log(`Offer accepted: ${status}`);
          }
        });
      } else {
        console.log(`Offer from unauthorized Steam ID: ${offer.partner.getSteamID64()}`);
      }
    });
  }
 
  sendDepositTrade(partner, assetIds, callback) {
    const offer = this.manager.createOffer(partner);
 
    this.manager.getUserInventoryContents(partner, 730, 2, true, (err, inv) => {
      if (err) {
        console.log('Error fetching user inventory:', err);
        callback(err, false); // Invoke the callback with the error
      } else {
        const itemsToAdd = inv.filter((item) => {
          const itemAssetId = String(item.assetid);
          return assetIds && assetIds.includes(itemAssetId);
        });
 
        if (itemsToAdd.length > 0) {
          console.log('Items found in inventory matching assetIds:', itemsToAdd);
 
          itemsToAdd.forEach((item) => {
            offer.addTheirItem(item);
            console.log("Added item to trade offer:", item);
          });
 
          offer.setMessage('Acceptera för omedelbar utbetalning via Swish');
          offer.send((err, status) => {
            console.log("offer status " + status);
            if (err) {
              console.error('Error sending trade offer:', err);
              callback(err, false, offer); // Invoke the callback with the error
            } else {
               
                callback(null, true, offer.id); // Trade sent successfully
         
          }
          });
        }
      }
    });
  }
}
 
 
 
module.exports = SteamBot;

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...