Browse Source

relog fix

master
gsd 1 year ago
parent
commit
2a309d7cb1
  1. 57
      tradebot.js

57
tradebot.js

@ -6,8 +6,13 @@ var backend = new backend_integration();
var trade_checker = new trade_checker_imp();
const VERSION = require("./version");
var SteamTradeOfferManager = require('steam-tradeoffer-manager');
const consoleTime = () => {
return (new Date()).toLocaleString();
}
async function sync(timeout) {
console.log(`Build date: ${VERSION}`);
console.log(consoleTime(), `Build date: ${VERSION}`);
let sleep = function sleep (howLong) {
return new Promise(function (resolve) {
setTimeout(() => {resolve()}, howLong)
@ -20,11 +25,11 @@ async function sync(timeout) {
if (backend.up) {
backend.getPrices(trade_checker);
if (steam_client === undefined) {
console.log("Setup Steam Client...")
console.log(consoleTime(), "Setup Steam Client...")
steam_client = setupClient();
}
} else {
console.log(`wait ${timeout} seconds after destroy, where pulse failed`);
console.log(consoleTime(), `wait ${timeout} seconds after destroy, where pulse failed`);
await sleep(timeout * 1000);
if (!backend.up) {
process.exit(228);
@ -32,27 +37,27 @@ async function sync(timeout) {
}
});
} catch (e) {
console.log(e);
console.log(consoleTime(), e);
}
}
}
function calculate(offer) {
if (offer.state !== SteamTradeOfferManager.ETradeOfferState.Active) {
console.log(`[${offer.id}] trade have not active stats`)
console.log(consoleTime(), `[${offer.id}] trade have not active stats`)
return false;
}
if (offer.itemsToGive.length>0) {
console.log(`[${offer.id}] partner wanna get bot items, decine trade`);
console.log(consoleTime(), `[${offer.id}] partner wanna get bot items, decine trade`);
return false;
}
if (offer.itemsToReceive.length>50) {
console.log(`[${offer.id}] cannot accept trade with more 50 items`)
console.log(consoleTime(), `[${offer.id}] cannot accept trade with more 50 items`)
return false;
}
const seconds = trade_checker.Items2Seconds(offer.itemsToReceive);
if (seconds === 0) {
console.log(`[${offer.id}] cannot accept trade not valid items`)
console.log(consoleTime(), `[${offer.id}] cannot accept trade not valid items`)
return false;
}
return seconds > 0;
@ -72,7 +77,7 @@ function setupClient() {
language: 'en'
});
/////////////////////////////////////////////////////////////////
console.log('connecting to steam account...');
console.log(consoleTime(), 'connecting to steam account...');
client.logOn({
accountName: config.login,
password: config.password,
@ -80,18 +85,18 @@ function setupClient() {
});
/////////////////////////////////////////////////////////////////
client.on('loggedOn', function(details, parental) {
console.log('SteamID64 :', client.steamID.getSteamID64());
console.log(consoleTime(), 'SteamID64 :', client.steamID.getSteamID64());
client.setPersona(SteamUser.EPersonaState.Online);
});
client.on('webSession', function(sessionID, cookies) {
manager.setCookies(cookies, function(err) {
if (err) {
console.log(err);
console.log(consoleTime(), err);
process.exit(1); // Fatal error since we couldn't get our API key
return;
}
console.log("Web session created!");
console.log(consoleTime(), "Web session created!");
});
community.setCookies(cookies);
@ -99,27 +104,37 @@ function setupClient() {
client.on('error', function(error) {
console.log('connection error\n');
console.log(error);
console.log(consoleTime(), 'connection error\n');
console.log(consoleTime(), error);
});
////////////////////////////////////////////////////////////////////
manager.on('newOffer', function(offer) {
console.log(`[${offer.id}] Incoming trade from ${offer.partner.getSteamID64()}`);
console.log(consoleTime(), `[${offer.id}] Incoming trade from ${offer.partner.getSteamID64()}`);
if (calculate(offer)) {
console.log(`[${offer.id}] Try accept trade`);
console.log(consoleTime(), `[${offer.id}] Try accept trade`);
offer.accept(false, function(error, status) {
console.log("Cannot accept trade", error, status)
if (error.message === "Not Logged In") {
console.log(consoleTime(), "Need relog to accept");
client.relog();
} else {
console.log(consoleTime(), "Cannot accept trade", error, status);
}
});
} else {
console.log(`[${offer.id}] Try decline trade`);
console.log(consoleTime(), `[${offer.id}] Try decline trade`);
offer.decline(function(error, status) {
console.log("Cannot decline trade", error, status)
if (error.message === "Not Logged In") {
console.log(consoleTime(), "Need relog to decline");
client.relog();
} else {
console.log(consoleTime(), "Cannot decline trade", error, status);
}
})
}
})
manager.on('receivedOfferChanged', function(offer, oldState) {
console.log(`[${offer.id}] changed: ${SteamTradeOfferManager.ETradeOfferState[oldState]} -> ${SteamTradeOfferManager.ETradeOfferState[offer.state]}`);
console.log(consoleTime(), `[${offer.id}] changed: ${SteamTradeOfferManager.ETradeOfferState[oldState]} -> ${SteamTradeOfferManager.ETradeOfferState[offer.state]}`);
if (offer.state === SteamTradeOfferManager.ETradeOfferState.Accepted) {
offer.getExchangeDetails((err, status, tradeInitTime, receivedItems, sentItems) => {
const seconds = trade_checker.Items2Seconds(receivedItems);
@ -127,7 +142,7 @@ function setupClient() {
const extra = `keys=${trade_checker.mannco_key(receivedItems)};metal=${trade_checker.pure_metal(receivedItems)};`;
backend.vip(offer.partner.getSteamID64(), seconds, extra, offer.id);
} else {
console.log(`[${offer.id}] Trade accepted, but vip not gived!`);
console.log(consoleTime(), `[${offer.id}] Trade accepted, but vip not gived!`);
}
})
}

Loading…
Cancel
Save