const TradeOfferManager = require('steam-tradeoffer-manager');
const SteamCommunity = require('steamcommunity');
const SteamUser = require('steam-user');
const SteamTotp = require('steam-totp');
const Prices = require('./prices.json');
const config = require('./config.json');
const client = new SteamUser();
const community = new SteamCommunity();
const manager = new TradeOfferManager({
steam: client,
community: community,
language: 'en'
});
const logOnOptions = {
accountName: config.username,
password: config.password,
twoFactorCode:SteamTotp.generateAuthCode(config.sharedSecret)
};
client.logOn(logOnOptions);
client.on('loggedOn', () => {
console.log('Succesfully logged on.');
client.setPersona(SteamUser.EPersonaState.Online, 'BOT');
client.gamesPlayed(440);
});
client.on('friendRelationship', (steamid, relationship) => {
if (relationship === 2) {
client.addFriend(steamid);
client.chatMessage(steamid, 'Hello there! Thanks for adding me!');
console.log("Added a friend")
}
});
client.on('webSession', (sessionid, cookies) => {
manager.setCookies(cookies);
community.setCookies(cookies);
community.startConfirmationChecker(20000, config.identitySecret);
});
function acceptOffer(offer) {
offer.accept((err) => {
community.checkConfirmations();
console.log("We Accepted an offer");
if (err) console.log("There was an error accepting the offer.");
});
}
function declineOffer(offer) {
offer.decline((err) => {
console.log("We Declined an offer");
if (err) console.log("There was an error declining the offer.");
});
}
function processOffer(offer) {
if (offer.isGlitched() || offer.state === 11) {
console.log("Offer Declined!");
declineOffer(offer);
} else if (offer.partner.getSteamID64() === config.ownerID) {
acceptOffer(offer);
} else {
var ourItems = offer.itemsToGive;
var theirItems = offer.itemsToReceive;
var ourValue = 0;
var theirValue = 0;
for (var i in ourItems) {
var item = ourItems[i].market_name;
if(Prices[item]) {
ourValue += Prices[item].sell;
} else {
console.log("Invalid value.");
ourValue += 99999;
}
}
for(var i in theirItems) {
var item= theirItems[i].market_name;
if(Prices[item]) {
theirValue += Prices[item].buy;
} else {
}
}
}
if (ourValue <= theirValue) {
acceptOffer(offer);
} else {
declineOffer(offer);
}
}
client.setOption("promptSteamGuardCode", false);
manager.on('newOffer', offer => {
processOffer();
});