Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Steam = require('steam');
- var SteamUser = require('steam-user');
- var TradeOfferManager = require('steam-tradeoffer-manager');
- var client = new SteamUser();
- var fs = require("fs");
- var SteamTotp = require('steam-totp');
- var request = require('request');
- var SteamCommunity = require('steamcommunity');
- var community = new SteamCommunity(steamClient);
- var steamClient = new Steam.SteamClient();
- var steamUser = new Steam.SteamUser(steamClient);
- var steamFriends = new Steam.SteamFriends(steamClient);
- var colors = require('colors');
- var identity_secret;
- var config;
- function updateconfig() {
- try {
- config = JSON.parse(fs.readFileSync('./config.json'));
- } catch (err) {
- console.log("Error: unable to parse config.json.");
- console.log(err);
- process.exit(1);
- }
- }
- updateconfig();
- var manager = new TradeOfferManager({
- "community": community,
- "pollInterval": config.polltimeinseconds,
- "cancelTime": config.timebeforetradeiscancelled
- });
- const logOnOptions = {
- accountName: 'your_steam_username',
- password: 'your_steam_password',
- twoFactorCode: SteamTotp.generateAuthCode('your_steam_shared_secret')
- };
- client.logOn(logOnOptions);
- client.on('loggedOn', () => {
- console.log('Logged into Steam');
- client.setPersona(SteamUser.Steam.EPersonaState.Online);
- client.gamesPlayed(440);
- });
- client.on("webSession", function(steamID, cookies) {
- manager.setCookies(cookies);
- community.setCookies(cookies);
- SteamTotp.steamID = steamID;
- community.startConfirmationChecker(5000, config.identitySecret);
- });
- client.on('error', function(e) {
- console.log(e);
- });
- const friends = new Steam.SteamFriends(client.client);
- friends.on("friend", function(steamID, relationship) {
- console.log(relationship);
- if (relationship == Steam.EFriendRelationship.RequestRecipient) {
- console.log("New Friend request from " + steamID);
- friends.addFriend(steamID);
- friends.sendMessage(steamID, "Hello! I am a Banking Bot! Type !help to get started.);
- }
- });
- friends.on('relationships', function() {
- var friendcount = 0;
- // For every friend we have...
- for (steamID in friends.friends) {
- friendcount++;
- // If the status is a new friend request...
- if (friends.friends[steamID] === Steam.EFriendRelationship.RequestRecipient) {
- console.log("Friend request while offline from: " + steamID);
- // Accept friend requests from when we were offline
- friends.addFriend(steamID);
- }
- }
- if (friendcount > 200) {
- // We might be able to find old friends after using friends.requestFriendData([steamids])
- // but seishun will have to add support for it. Right now you can't see how long you've been friends through SteamFriends.
- // This is the only data available using requestFriendData function:
- // https://github.com/SteamRE/SteamKit/blob/master/Resources/Protobufs/steamclient/steammessages_clientserver.proto#L446-L469
- console.log("We're approaching the default friends limit. Maybe we need to purge old friends?");
- }
- });
- friends.on("friendMsg", function(user, msg, types) {
- msg = msg.toLowerCase();
- if (types == Steam.EChatEntryType.ChatMsg) {
- manager.getEscrowDuration(user, function(err, their, ours) {
- if (err) {
- return;
- }
- if (their != 0) {
- friends.sendMessage(user, "You have a trade hold, therefore i cannot trade with you. If you would like to remove this, check out this video: https://www.youtube.com/watch?v=2CUeUmeVBgs")
- return;
- }
- if (msg.startsWith("buykey")) {
- var reqkeyCount = 1;
- reqkeyCount = parseInt(msg.split(" ")[1]);
- if (isNaN(reqkeyCount)) {
- reqkeyCount = 1;
- }
- console.log("Incoming Request: User wants to buy " + reqkeyCount + " Key(s)")
- friends.sendMessage(user, "Please wait while I process your offer...");
- var offer = manager.createOffer(user);
- manager.loadInventory(440, 2, true, function(err, myItems) {
- if (err) {
- console.log(err);
- return;
- }
- offer.loadPartnerInventory(440, 2, function(err, theirItems) {
- if (err) {
- console.log(err);
- return;
- }
- var amount = reqkeyCount;
- for (i in myItems) {
- var item = myItems[i]
- if (amount <= 0) continue;
- if (item.market_name === "Mann Co. Supply Crate Key") {
- offer.addMyItem(item);
- amount -= 1;
- }
- }
- // Change logic if not enough keys to sell what we have!
- if (amount != 0) {
- friends.sendMessage(user, "Out of Keys! Check back later!");
- return;
- }
- var currencyRequired = reqkeyCount * config.keysellprice;
- var count = 0;
- for (i in theirItems) {
- var item = theirItems[i];
- if (item.market_name == "Refined Metal") {
- if (currencyRequired - 1 >= 0) {
- offer.addTheirItem(item);
- currencyRequired -= 1;
- count++;
- }
- }
- }
- for (i in theirItems) {
- var item = theirItems[i]
- if (item.market_name == "Reclaimed Metal") {
- if (currencyRequired - 1 / 3 >= 0) {
- offer.addTheirItem(item);
- currencyRequired -= 1 / 3;
- }
- }
- }
- for (i in theirItems) {
- var item = theirItems[i]
- if (item.market_name == "Scrap Metal") {
- if (toUpper(currencyRequired - 1 / 9) >= 0) {
- offer.addTheirItem(item);
- currencyRequired -= 1 / 9;
- }
- }
- }
- if (toLower(currencyRequired) > 0) {
- friends.sendMessage(user, "Failed To Send Offer: Could not find required metal to complete the transaction.");
- console.log("Underpay")
- return;
- }
- friends.sendMessage(user, "Confirming Offer...");
- var uuid = code()
- friends.sendMessage(user, "Your trade offer will arrive within 10 seconds. You will have 2 minutes to accept. Make sure your transaction ID is " + uuid + "!");
- offer.send("Transaction ID: " + uuid + ". Thanks for using " + config.ownername + "'s Trading Bot!");
- community.getSteamUser(user, function(err, cmt) {
- //cmt.comment(config.profilecomment)
- });
- });
- });
- } else
- if (msg.startsWith("sellkey")) {
- var reqkeyCount = 1;
- reqkeyCount = parseInt(msg.split(" ")[1]);
- console.log("Incoming Request: User wants to sell us " + reqkeyCount + " keys")
- friends.sendMessage(user, "Please wait while I process your offer...");
- var offer = manager.createOffer(user);
- manager.loadInventory(440, 2, true, function(err, theirItems) {
- if (err) {
- console.log(err);
- return;
- }
- offer.loadPartnerInventory(440, 2, function(err, myItems) {
- if (err) {
- console.log(err);
- return;
- }
- var amount = reqkeyCount;
- for (i in myItems) {
- var item = myItems[i]
- if (amount <= 0) continue;
- if (item.market_name === "Mann Co. Supply Crate Key") {
- offer.addTheirItem(item);
- amount -= 1;
- }
- }
- if (amount != 0) {
- friends.sendMessage(user, "Failed To Send Offer: Failed to find the Keys in your inventory. This may be due to the TF2 API.");
- return;
- }
- var currencyRequired = reqkeyCount * config.keybuyprice;
- var count = 0;
- for (i in theirItems) {
- var item = theirItems[i];
- if (item.market_name == "Refined Metal") {
- if (currencyRequired - 1 >= 0) {
- offer.addMyItem(item);
- currencyRequired -= 1;
- count++;
- }
- }
- }
- for (i in theirItems) {
- var item = theirItems[i]
- if (item.market_name == "Reclaimed Metal") {
- if (currencyRequired - 1 / 3 >= 0) {
- offer.addMyItem(item);
- currencyRequired -= 1 / 3;
- }
- }
- }
- for (i in theirItems) {
- var item = theirItems[i]
- if (item.market_name == "Scrap Metal") {
- if (toUpper(currencyRequired - 1 / 9) >= 0) {
- offer.addMyItem(item);
- currencyRequired -= 1 / 9;
- }
- }
- }
- if (toLower(currencyRequired) > 0) {
- friends.sendMessage(user, "Out of metal. Check back later.");
- console.log("Out of metal.")
- return;
- }
- friends.sendMessage(user, "Confirming Offer...");
- var uuid = code()
- friends.sendMessage(user, "Your trade offer will arrive within 10 seconds. You will have 2 minutes to accept. Make sure your transaction ID is " + uuid + "!");
- offer.send("Transaction ID: " + uuid + ". Thanks for using " + config.ownername + "'s Trading Bot!");
- community.getSteamUser(user, function(err, cmt) {
- // cmt.comment(config.profilecomment)
- });
- });
- });
- }
- });
- }
- });
- function getPersonaName(steamID, callback) {
- var url = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=" + config.steamapikey + "&steamids=" + steamID;
- request(url, function(error, response, body) {
- if (!error && response.statusCode == 200) {
- var name = JSON.parse(body).response.players[0].personaname;
- callback(name);
- }
- });
- }
- manager.on("newOffer", function(offer) {
- // offer.accept();
- });
- function currentTimeMillis() {
- var d = new Date();
- return d.getTime();
- }
- function getCurrencyValue(thing) {
- if (thing == "Mann Co. Supply Crate Key") {
- return 0;
- } else if (thing == "Refined Metal") {
- return 1;
- } else if (thing == "Reclaimed Metal") {
- return 1 / 3;
- } else if (thing == "Scrap Metal") {
- return 1 / 9;
- } else {
- return 0;
- }
- }
- friends.on("friendMsg", function(user, msg, types) {
- msg = msg.toLowerCase();
- if (types == Steam.EChatEntryType.ChatMsg) {
- manager.getEscrowDuration(user, function(err, their, ours) {
- if (err) {
- return;
- }
- if (their != 0) {
- friends.sendMessage(user, "You have a trade hold, therefore i cannot trade with you. If you would like to remove this, check out this video: https://www.youtube.com/watch?v=2CUeUmeVBgs")
- return;
- }
- if (msg.startsWith("buytod")) {
- var reqtodCount = 1;
- console.log("Incoming Request: User wants to buy " + reqtodCount + " ticket(s)")
- friends.sendMessage(user, "Please wait while I process your offer...");
- reqtodCount = parseInt(msg.split(" ")[1]);
- var offer = manager.createOffer(user);
- manager.loadInventory(440, 2, true, function(err, myItems) {
- if (err) {
- console.log(err);
- return;
- }
- offer.loadPartnerInventory(440, 2, function(err, theirItems) {
- if (err) {
- console.log(err);
- return;
- }
- var amount = reqtodCount;
- for (i in myItems) {
- var item = myItems[i]
- if (amount <= 0) continue;
- if (item.market_name === "Tour of Duty Ticket") {
- offer.addMyItem(item);
- amount -= 1;
- }
- }
- if (amount != 0) {
- friends.sendMessage(user, "Out of Tickets! Check back later!");
- return;
- }
- var currencyRequired = reqtodCount * config.todsellprice;
- var count = 0;
- for (i in theirItems) {
- var item = theirItems[i];
- if (item.market_name == "Refined Metal") {
- if (currencyRequired - 1 >= 0) {
- offer.addTheirItem(item);
- currencyRequired -= 1;
- count++;
- }
- }
- }
- for (i in theirItems) {
- var item = theirItems[i]
- if (item.market_name == "Reclaimed Metal") {
- if (currencyRequired - 1 / 3 >= 0) {
- offer.addTheirItem(item);
- currencyRequired -= 1 / 3;
- }
- }
- }
- for (i in theirItems) {
- var item = theirItems[i]
- if (item.market_name == "Scrap Metal") {
- if (toUpper(currencyRequired - 1 / 9) >= 0) {
- offer.addTheirItem(item);
- currencyRequired -= 1 / 9;
- }
- }
- }
- if (toLower(currencyRequired) > 0) {
- friends.sendMessage(user, "Failed To Send Offer: Could not find required metal to complete the trade offer.");
- console.log("Underpay")
- return;
- }
- friends.sendMessage(user, "Confirming Offer...");
- var uuid = code()
- friends.sendMessage(user, "Your trade offer will arrive within 10 seconds. You will have 2 minutes to accept. Make sure your transaction ID is " + uuid + "!");
- offer.send("Transaction ID: " + uuid + ". Thanks for using " + config.ownername + "'s Trading Bot!");
- community.getSteamUser(user, function(err, cmt) {
- // cmt.comment(config.profilecomment)
- });
- });
- });
- } else
- // Command to display stock levels
- if (msg.startsWith("stock")) {
- friends.sendMessage(user, "Checking Stock Levels...");
- console.log("Stock command received.");
- var no_key=0;
- var no_rec=0;
- var no_scrap=0;
- var no_ref=0;
- var no_tod=0;
- manager.loadInventory(440, 2, true, function(err, myItems) {
- if (err) {
- console.log(err);
- return;
- }
- for (i in myItems) {
- var item = myItems[i]
- if (item.market_name == "Mann Co. Supply Crate Key") {
- no_key++;
- } else
- if (item.market_name == "Reclaimed Metal") {
- no_rec++;
- } else
- if (item.market_name == "Scrap Metal") {
- no_scrap++;
- } else
- if (item.market_name == "Refined Metal") {
- no_ref++;
- } else
- if (item.market_name == "Tour of Duty Ticket") {
- no_tod++;
- }
- }
- var msguser = "Current Stock:"
- if (no_key >0) {
- msguser = msguser+"\r\nKeys: "+no_key
- }
- if (no_tod >0) {
- msguser = msguser+"\r\nTod: "+no_tod
- }
- if (no_ref >0) {
- msguser = msguser+"\r\nRef: "+no_ref
- }
- if (no_rec >0) {
- msguser = msguser+"\r\nRec: "+no_rec
- }
- if (no_scrap >0) {
- msguser = msguser+"\r\nScrap: "+no_scrap
- }
- console.log(msguser);
- friends.sendMessage(user, msguser);
- });
- } else
- if (msg.startsWith("selltod")) {
- var reqtodCount = 1;
- reqtodCount = parseInt(msg.split(" ")[1]);
- console.log("Incoming Request: User wants to sell us " + reqtodCount + " ticket(s)")
- friends.sendMessage(user, "Please wait while I process your offer...");
- var offer = manager.createOffer(user);
- manager.loadInventory(440, 2, true, function(err, theirItems) {
- if (err) {
- console.log(err);
- return;
- }
- offer.loadPartnerInventory(440, 2, function(err, myItems) {
- if (err) {
- console.log(err);
- return;
- }
- var amount = reqtodCount;
- for (i in myItems) {
- var item = myItems[i]
- if (amount <= 0) continue;
- if (item.market_name === "Tour of Duty Ticket") {
- offer.addTheirItem(item);
- amount -= 1;
- }
- }
- if (amount != 0) {
- friends.sendMessage(user, "Failed To Send Offer: Failed to find the Tickets in your inventory. This could also be due to the TF2 API.");
- return;
- }
- var currencyRequired = reqtodCount * config.todbuyprice;
- var count = 0;
- for (i in theirItems) {
- var item = theirItems[i];
- if (item.market_name == "Refined Metal") {
- if (currencyRequired - 1 >= 0) {
- offer.addMyItem(item);
- currencyRequired -= 1;
- count++;
- }
- }
- }
- for (i in theirItems) {
- var item = theirItems[i]
- if (item.market_name == "Reclaimed Metal") {
- if (currencyRequired - 1 / 3 >= 0) {
- offer.addMyItem(item);
- currencyRequired -= 1 / 3;
- }
- }
- }
- for (i in theirItems) {
- var item = theirItems[i]
- if (item.market_name == "Scrap Metal") {
- if (toUpper(currencyRequired - 1 / 9) >= 0) {
- offer.addMyItem(item);
- currencyRequired -= 1 / 9;
- }
- }
- }
- if (toLower(currencyRequired) > 0) {
- friends.sendMessage(user, "Out of metal. Check back later.");
- console.log("Out of metal. Possible that you have no change.")
- return;
- }
- friends.sendMessage(user, "Confirming Offer...");
- var uuid = code()
- friends.sendMessage(user, "Your trade offer will arrive within 10 seconds. You will have 2 minutes to accept. Make sure your transaction ID is " + uuid + "!");
- offer.send("Transaction ID: " + uuid + ". Thanks for using " + config.ownername + "'s Trading Bot!");
- });
- });
- }
- });
- }
- });
- function getPersonaName(steamID, callback) {
- var url = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?tod=" + config.steamapitod + "&steamids=" + steamID;
- request(url, function(error, response, body) {
- if (!error && response.statusCode == 200) {
- var name = JSON.parse(body).response.players[0].personaname;
- callback(name);
- }
- });
- }
- manager.on("newOffer", function(offer) {
- // offer.accept();
- });
- function currentTimeMillis() {
- var d = new Date();
- return d.getTime();
- }
- function getCurrencyValue(thing) {
- if (thing == "Tour of Duty Ticket") {
- return 0;
- } else if (thing == "Refined Metal") {
- return 1;
- } else if (thing == "Reclaimed Metal") {
- return 1 / 3;
- } else if (thing == "Scrap Metal") {
- return 1 / 9;
- } else {
- return 0;
- }
- }
- function toLower(num) {
- return Math.floor(num * 10) / 10
- }
- function toUpper(num) {
- return Math.ceil(num * 10) / 10
- }
- friends.on("friendMsg", function(user, msg, types) {
- msg = msg.toLowerCase();
- if (types == Steam.EChatEntryType.ChatMsg) {
- if (msg == "help") {
- friends.sendMessage(user, "Commands: \nbuykey <number of keys you want to buy> \nbuytod <number of TOD you want to buy> \nsellkey <number of keys you want to sell> \nselltod <number of TOD you want to sell> \nE.G: buykey 1 or selltod 2 \nprices: Prices bot buys/sells items for. \nstock: List of available items \ngroup: Link to the steam group. \nowner: Link to the owner's profile.");
- return;
- }
- }
- });
- friends.on("friendMsg", function(user, msg, types) {
- msg = msg.toLowerCase();
- if (types == Steam.EChatEntryType.ChatMsg) {
- if (msg == "price") {
- friends.sendMessage(user, "Current prices: \nBuying Keys for " + config.keybuyprice + ". \nSelling keys for " + config.keysellprice + ". \nBuying TOD Tickets for " + config.todbuyprice + ". \nSelling TOD Tickets for " + config.todsellprice + ". \nFor more useful commands type help.");
- return;
- }
- }
- });
- friends.on("friendMsg", function(user, msg, types) {
- msg = msg.toLowerCase();
- if (types == Steam.EChatEntryType.ChatMsg) {
- if (msg == "prices") {
- friends.sendMessage(user, "Current prices: \nBuying Keys for " + config.keybuyprice + ". \nSelling keys for " + config.keysellprice + ". \nBuying TOD Tickets for " + config.todbuyprice + ". \nSelling TOD Tickets for " + config.todsellprice + ". \nFor more useful commands type help.");
- return;
- }
- }
- });
- friends.on("friendMsg", function(user, msg, types) {
- msg = msg.toLowerCase();
- if (types == Steam.EChatEntryType.ChatMsg) {
- if (msg == "group") {
- friends.sendMessage(user, "Join the group for updates: " + "http://steamcommunity.com/groups/W3bb0sBots"+ " For more useful commands type help.");
- return;
- }
- }
- });
- friends.on("friendMsg", function(user, msg, types) {
- msg = msg.toLowerCase();
- if (types == Steam.EChatEntryType.ChatMsg) {
- if (msg == "owner") {
- friends.sendMessage(user, "Having problems? Add the owner of this bot here: " + config.ownerurl + " For more useful commands type help.");
- return;
- }
- }
- });
- community.on('confKeyNeeded', function(tag, callback) {
- var time = Math.floor(Date.now() / 1000);
- callback(null, time, SteamTotp.getConfirmationKey(config.identitysecret, time, tag));
- });
- community.on('newConfirmation', function(conf) {
- conf.respond(Math.floor(Date.now() / 1000), SteamTotp.getConfirmationKey(config.identitysecret, Math.floor(Date.now() / 1000), "allow"), true, function(err) {
- if (err) {
- console.log("Confirmation failed: " + err);
- return;
- }
- console.log("Sucessfully sent the trade. Waiting for User to accept...");
- console.log(colors.rainbow("=============================="));
- });
- });
- var token = config.bptftoken;
- var yes = setInterval(function() {
- console.log("Bot is still alive.");
- }, 1000 * 60 * 10);
- var no = setInterval(function() {
- if (client.steamID && token != "" && token != "optional") {
- heartbeat(client.steamID.toString(), config.bptftoken)
- }
- }, 1000 * 60 * 5);
- function heartbeat(sid, token) {
- if (!token) {
- return;
- }
- request.post({
- "uri": "https://backpack.tf/api/IAutomatic/IHeartBeat/",
- "form": {
- "method": "alive",
- "version": "1.2.4",
- "steamid": sid,
- "token": token
- },
- "json": true
- }, function(err, response, body) {
- if (err || response.statusCode != 200) {
- console.log("Error occurred contacting backpack.tf (" + (err ? err.message : "HTTP error " + response.statusCode) + "), trying again in 60 seconds");
- } else if (!body.success) {
- console.log(body.message || "Invalid backpack.tf token");
- } else {
- console.log("Heartbeat sent to backpack.tf." + (body.bumped ? " " + body.bumped + " listing" + (body.bumped == 1 ? '' : 's') + " bumped." : ""));
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement