Skip to content

Instantly share code, notes, and snippets.

@ewancook
Created October 26, 2015 13:12
Show Gist options
  • Save ewancook/27a95d70d31f8d93aec5 to your computer and use it in GitHub Desktop.
Save ewancook/27a95d70d31f8d93aec5 to your computer and use it in GitHub Desktop.
Key Bank Bot (old)
var Steam = require("steam");
var SteamTrade = require("steam-trade");
var _ = require("underscore");
var steam = new Steam.SteamClient();
var steamTrade = new SteamTrade();
var config = require("./config.js");
var adminID = config.adminID
var username = config.username;
var password = config.password;
var finished = false
var playTF = true;
var steamGuard = require("fs").existsSync("sentry") ? require("fs").readFileSync("sentry")
: ""
var keyPrice = 68;
var sellPrice = 67;
var correctTrade = false;
var tradeTimeout = 1000*60*5;
var friendTimeout = 1000*60*60;
var currentlyTrading = true;
var message_1 = "Hi, I buy and sell keys.";
var message_2 = "You will get "+(keyPrice / 9).toFixed(2)+" refined for every key, and 1 key for every "+(sellPrice / 9).toFixed(2)+" refined";
var message_3 = "I'm currently trading. Please wait a few minutes, then send me a trade request.";
steam.on("debug", console.log);
steam.logOn({accountName: username, password: password, shaSentryfile: steamGuard});
var sendWelcome = function(steamID) {
steam.sendMessage(steamID, message_1);
steam.sendMessage(steamID, message_2);
if (currentlyTrading) {
steam.sendMessage(steamID, message_3);
}
};
var addPendingFriends = function() {
console.log("searching for pending friend requests...");
_.each(steam.friends, function(relationship, steamID) {
if (relationship == Steam.EFriendRelationship.RequestRecipient) {
steam.addFriend(steamID);
console.log("added "+steamID+" as a friend");
sendWelcome(steamID);
console.log("welcome messages sent to", steamID);
setTimeout(function() {
steam.removeFriend(steamID);
console.log("auto-removing", steamID);
}, friendTimeout);
}
});
console.log("finished searching");
};
steam.on("loggedOn", function(result) {
console.log("logged in.")
});
steam.on("sentry", function(sentryHash) {
require("fs").writeFile("sentry", sentryHash, function(err) {
if (err) {console.log(err);}
else {console.log("saved sentry hash in 'sentry'");}
});
});
steam.on("error", function(error) {console.log("error: ", error);});
steam.on("webSessionID", function(sessionID) {
console.log("new session id: ", sessionID);
steamTrade.sessionID = sessionID;
steam.webLogOn(function(cookies) {
console.log("new cookie: ", cookies)
cookies.forEach(function(cookie) {
steamTrade.setCookie(cookie);
});
addPendingFriends();
currentlyTrading = false;
steam.setPersonaState(Steam.EPersonaState.Online);
if (playTF) {
steam.gamesPlayed([440]);
console.log("starting tf2");
}
});
});
steam.on("tradeProposed", function(tradeID, otherClient) {
console.log("trade proposal from", otherClient);
if (!currentlyTrading) {
steam.respondToTrade(tradeID, true);
}
else {
steam.respondToTrade(tradeID, false);
steam.sendMessage(otherClient, message_3);
}
});
steam.on("message", function(source, message, type, chatter) {
if (message != "") {
console.log("message from "+source+": "+message);
}
if (message == "trade") {
if (currentlyTrading) {steam.sendMessage(source, message_3);}
else {console.log("sent trade request to"+source); steam.trade(source);}
}
});
steam.on("friend", function(steamID, relationship) {
if (relationship == Steam.EFriendRelationship.PendingInvitee) {
console.log("friend invitation from "+steamID+" awaits.");
steam.addFriend(steamID);
console.log("friend request from "+steamID+" accepted.");
sendWelcome(steamID);
console.log("welcome messages sent to", steamID);
if (!currentlyTrading) {
steam.trade(steamID);
}
setTimeout(function() {
steam.removeFriend(steamID);
console.log("auto-removing", steamID);
}, friendTimeout);
}
else if (relationship == Steam.EFriendRelationship.None) {
console.log(steamID+" has been removed from your friend list")
}
});
steamTrade.on("end", function(status, getItems) {
console.log("trade", status);
currentlyTrading = false;
finished = true;
});
//my inventory
var inventory, myRef, myRec, myScrap, addedRef, addedRec, addedScrap, myKeys, addedKeys, addedMetal;
//their inventory
var theirKeys, theirMetal, clientID;
steam.on("sessionStart", function(otherClient) {
clientID = otherClient;
addedMetal = 0;
theirKeys = 0;
theirMetal = 0;
inventory = [];
myRef = [];
myRec = [];
myScrap = [];
myKeys = [];
addedRef = 0;
addedRec = 0;
addedScrap = 0;
addedKeys = 0;
currentlyTrading = true;
console.log("trading with", otherClient);
steamTrade.open(otherClient);
steamTrade.loadInventory(440, 2, function(inv) {
inventory = inv;
myRef = inv.filter(function(item) {
return item.name == "Refined Metal";
});
myRec = inv.filter(function(item) {
return item.name == "Reclaimed Metal";
});
myScrap = inv.filter(function(item) {
return item.name == "Scrap Metal";
});
myKeys = inv.filter(function(item) {
return item.name == "Mann Co. Supply Crate Key";
});
});
setTimeout(function() {
if (!finished) {
console.log(otherClient+"'s trade timed out - cancelling");
steamTrade.cancel();
currentlyTrading = false;
steam.sendMessage(otherClient, "Your trade lasted too long.");
}
}, tradeTimeout);
});
steamTrade.on("offerChanged", function(added, item) {
correctTrade = false;
console.log("they "+(added ? "added" : "removed"), item.name);
switch (item.name) {
case "Mann Co. Supply Crate Key":
theirKeys += added ? 1 : -1;
keyForRef(added);
break;
case "Scrap Metal":
theirMetal += added ? 1 : -1;
refForKey(added);
break;
case "Reclaimed Metal":
theirMetal += added ? 3 : -3;
refForKey(added);
break;
case "Refined Metal":
theirMetal += added ? 9 : -9;
refForKey(added);
break;
default:
console.log("unexpected item in trade");
}
});
steamTrade.on("chatMsg", function(message) {
console.log("trade chat message:", message);
if (message == "give" && clientID == adminID) {
steamTrade.addItems(myRef.slice(0, myRef.length));
steamTrade.addItems(myKeys.slice(0, myKeys.length));
console.log("emptying inventory to", clientID);
correctTrade = true;
}
});
steamTrade.on("ready", function() {
if (correctTrade) {
console.log("user is ready to trade");
steamTrade.ready(function() {
steamTrade.confirm();
console.log("trade confirmed");
});
}
else {
steam.sendMessage(clientID, "Something's not quite right...");
}
});
var keyForRef = function(added) {
var keyValue = theirKeys*keyPrice;
var availableMetal = ((myRef.length*9) + (myRec.length*3) + myScrap.length);
if (addedMetal == (keyValue)) {correctTrade = true;}
if (availableMetal < (keyValue)) {
var msg = "not enough metal in inventory; there's only "+(availableMetal/9).toFixed(2)+" metal left";
console.log(msg);
steam.sendMessage(clientID, msg);
}
else {
var doneAdding = false;
while (!doneAdding && added == true ) {
if ((myRef.length-addedRef) && addedMetal+9 <= (keyValue)) {
steamTrade.addItem(myRef[addedRef]);
addedMetal += 9;
addedRef++;
}
else if ((myRec.length-addedRec) && addedMetal+3 <= (keyValue)) {
steamTrade.addItem(myRec[addedRec]);
addedMetal += 3;
addedRec++;
}
else if ((myScrap.length-addedScrap) && addedMetal+1 <= (keyValue)) {
steamTrade.addItem(myScrap[addedScrap]);
addedMetal++;
addedScrap++;
}
else if (addedMetal == (keyValue)) {
correctTrade = true;
doneAdding = true;
}
else if ((myScrap.length-addedScrap) == 0) {
if (addedMetal+1 == (keyValue) || addedMetal+2 == (keyValue)) {
steam.sendMessage(clientID, "I don't have enough scrap to complete your request. Remove/Add a Key");
console.log("not enough scrap");
doneAdding = true;
}
}
}
while(!doneAdding && added == false) {
if (addedRef > 0 && addedMetal-9 >= (keyValue)) {
steamTrade.removeItem(myRef[addedRef-1]);
addedMetal -= 9;
addedRef--;
console.log("1");
}
else if (addedRec > 0 && addedMetal-3 >= (keyValue)) {
steamTrade.removeItem(myRec[addedRec-1]);
addedMetal -= 3;
addedRec--;
console.log("2");
}
else if (addedScrap > 0 && addedMetal-1 >= (keyValue)) {
steamTrade.removeItem(myScrap[addedScrap-1]);
addedMetal -= 1;
addedScrap--;
console.log("3");
}
else if (addedMetal == (keyValue)) {
doneAdding = true;
correctTrade = true;
}
else if (addedScrap == 0) {
if (addedMetal-1 == (keyValue) || addedMetal-2 == (keyValue)) {
steam.sendMessage(clientID, "I can't remove the right amount of metal. I may be out of scrap. Try adding/removing some keys");
doneAdding = true;
}
}
}
}
};
var refForKey = function(added) {
if ((theirMetal % sellPrice == 0) && (theirMetal / sellPrice) == addedKeys) {correctTrade = true;}
if (added) {
if (myKeys.length < (theirMetal/sellPrice)) {
var msg = "not enough keys in inventory; i only have "+(myKeys.length - addedKeys)+" left";
steam.sendMessage(clientID, msg);
console.log(msg);
}
else if (addedKeys < (theirMetal / sellPrice) && theirMetal % sellPrice == 0) {
var keysToAdd = (theirMetal / sellPrice) - addedKeys;
steamTrade.addItem(myKeys[addedKeys]);
addedKeys += keysToAdd;
correctTrade = true;
}
}
else {
if (addedKeys > (theirMetal / sellPrice)) {
while (addedKeys > (theirMetal / sellPrice)) {
steamTrade.removeItem(myKeys[addedKeys-1]);
addedKeys -= 1;
}
corrrectTrade = true;
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment