Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Init
- var steamBot;
- //Modules
- var fs = require("fs");
- var util = require("util");
- var JSON5 = require("json5");
- var requ = require('request'); // Request
- var Steam = require('steam');
- var SteamCommunity = require('steamcommunity');
- var SteamTradeOffers = require('steam-tradeoffers');
- var TradeOfferManager = require('steam-tradeoffer-manager');
- var SteamTotp = require('steam-totp');
- var SteamWebLogOn = require('steam-weblogon'); // Steam web
- //Classes
- var g = require("./general.js");
- //Functions
- steamBot = (function(config, mysql, client) {
- //Variables steam
- steamBot.prototype.bots = {};
- steamBot.prototype.botNames = {};
- steamBot.prototype.steam = {};
- steamBot.prototype.steamCom = {};
- steamBot.prototype.steamClient = {};
- steamBot.prototype.steamUser = {};
- steamBot.prototype.steamFriends = {};
- steamBot.prototype.steamWeb = {};
- steamBot.prototype.steamProfiles = {};
- //Variables init
- steamBot.prototype.config = config;
- steamBot.prototype.mysql = mysql;
- steamBot.prototype.client = client;
- //Variables confirmation
- steamBot.prototype.confsAllowed = {};
- //Variables with offers
- steamBot.prototype.sendedOffers = {};
- steamBot.prototype.sendsOffers = {};
- steamBot.prototype.queueOffers = {};
- steamBot.prototype.offers = {};
- steamBot.prototype.sleepOffers = {};
- steamBot.prototype.playerOffers = {};
- //Variables with items
- steamBot.prototype.botItems = {};
- steamBot.prototype.botItemsDef = {};
- steamBot.prototype.itemsSum = 0;
- steamBot.prototype.itemsPrices = {};
- steamBot.prototype.itemsGets = {};
- //Variables general
- steamBot.prototype.assignBots = false;
- steamBot.prototype.players = {};
- //Variables with lock
- steamBot.prototype.lockEmpties = false;
- //Variables with timers
- steamBot.prototype.timerSendChange = false;
- steamBot.prototype.timerEmpties = false;
- steamBot.prototype.timerEmptiesGlobal = false;
- steamBot.prototype.timerConfs = false;
- //Init Client
- steamBot.prototype.clientConnect = function(uID, steamid) {
- //Update all informations for bot
- if(this.assignBots) {
- this.refreshInventory(uID);
- this.getPlayersData(uID, steamid);
- }
- }
- //Init steam
- steamBot.prototype.getPlayers = function() {
- mysql.select_query("steam_accounts", "*", function(e, r) {
- if(!e && r && r.length > 0) {
- r.forEach(function(num, indx) {
- if(num && num.trade) {
- this.players[num.steamid] = num;
- }
- if(indx == (parseInt(r.length) - 1)) {
- this.getPlayersData();
- setTimeout(function() {
- this.getPlayers();
- }.bind(this), 10000);
- }
- }.bind(this));
- }
- }.bind(this));
- }
- //Init
- steamBot.prototype.botInit = function(id) {
- //Check exist bot
- if(id && this.bots[id]) {
- this.botNames[this.bots[id].user] = id;
- this.setTradeURL(id);
- this.botOffers(id);
- if(this.assignBots == false) {
- this.assignBots = true;
- console.log("Find empties!");
- setTimeout(function() {
- this.findEmptiesBot();
- this.checkTransfers();
- this.getConfirmations();
- this.checkSendedOffers();
- }.bind(this), 5000);
- }
- } else {
- console.warn("Steam:ERROR:: Konto ID: " + id + " nie istnieje!");
- }
- }
- //Assign item to bot
- steamBot.prototype.assignItemFor = function(item2, id) {
- var uid = randomInt(1000000, 9999999);
- if(item2 && item2.tradable && item2.marketable) {
- (function(item) {
- this.itemPrice(item.market_name, function(price) {
- var price = 0.03;
- if(!this.botItems[uid] && !this.botItemsDef[item.id]) {
- this.botItems[uid] = {
- uid: uid,
- id: item.id,
- botid: id,
- itemid: item.assetid + "_" + item.instanceid,
- assetid: item.assetid,
- instanceid: item.instanceid,
- pos: item.pos,
- icon: item.icon_url,
- name: item.name,
- market_name: item.market_name,
- price: price,
- type: item.type
- };
- this.botItemsDef[item.id] = uid;
- this.itemsSum = randomInt(10000, 9999999);
- if(this.timerSendChange) {
- clearTimeout(this.timerSendChange);
- }
- this.timerSendChange = setTimeout(function() {
- this.timerSendChange = false;
- this.refreshInventory(0);
- }.bind(this), 2500);
- }
- }.bind(this));
- }.bind(this))(item2);
- }
- }
- //Wutgdraw items
- steamBot.prototype.withdrawClientItems = function(sid, items) {
- console.log("Steam -> Withdraw items for steamID -> " + sid);
- if(this.players && this.players[sid]) {
- if(this.sendedOffers[sid]) {
- this.client.sendMessage(sid, "Opssss", "Twoja poprzednia oferta jest aktywna, anuluj poprzednią - aby wysłać kolejną!", "warning", 5);
- } else {
- this.sendedOffers[sid] = true;
- var cash = parseFloat(this.players[sid].cash)
- var cashMust = 0;
- var assignItems = {};
- var assignCash = {};
- var countItems = 0;
- for(var iKey in items) {
- if(this.botItems && this.botItems[iKey]) {
- var item = this.botItems[iKey];
- var coins = this.client.getCoins(item.price, 0);
- cashMust += parseFloat(coins);
- if(!(assignCash && assignCash[item.botid])) {
- assignCash[item.botid] = 0;
- }
- assignCash[item.botid] += parseFloat(coins);
- if(cashMust <= cash) {
- if(!(assignItems && assignItems[item.botid])) {
- assignItems[item.botid] = {};
- }
- assignItems[item.botid][Object.keys(assignItems[item.botid]).length] = {
- "appid": this.config.modules[this.bots[item.botid].action].inv.appID,
- "contextid": this.config.modules[this.bots[item.botid].action].inv.contextID,
- "amount": 1,
- "assetid": item.id
- };
- countItems++;
- }
- } else {
- console.log("Item -> " + iKey + " is not exists on Bot Skins!");
- }
- }
- if(cashMust > cash) {
- console.log("Steam -> Client -> " + sid + " dont have enought money for withdraw skins! (" + cashMust + "/" + cash + ")");
- this.client.sendMessage(sid, "Ooo NIE!!!", "Niestety, nie masz wystarczającej liczby coinsów ;(", "error", 5);
- this.sendedOffers[sid] = false;
- } else if(this.config.modules.market.with.min_coins_dep > this.players[sid].deposites) {
- var diffCoins = parseFloat(this.config.modules.market.with.min_coins_dep) - parseFloat(this.players[sid].deposites);
- console.log("Steam -> Client -> " + sid + " dont have enought deposited money");
- this.client.sendMessage(sid, "Ooo NIE!!!", "Niestety, nie masz zdeponowanej wystarczającej liczby coinsów, potrzebujesz jeszcze: " + diffCoins, "error", 5);
- this.sendedOffers[sid] = false;
- } else if(countItems > this.config.modules.market.with.items_max) {
- console.log("Steam -> Client -> " + sid + " too many items");
- this.client.sendMessage(sid, "Ooo NIE!!!", "Niestety, maksymalna liczba itemów w 1 wypłacie to: " + this.config.modules.market.with.items_max, "error", 5);
- this.sendedOffers[sid] = false;
- } else {
- this.client.sendMessage(sid, "Sukces!", "Twoja oferta jest przygotowywana!", "success", 3);
- this.makeOffers(sid, assignItems, assignCash);
- }
- }
- }
- }
- //Check sended offers
- steamBot.prototype.checkSendedOffers = function() {
- //console.log("Steam -> Check Sended Offers");
- var confWith = parseInt(this.config.modules.market.with.autoDecline) * 60;
- for(var id2 in this.bots) {
- (function(id) {
- for(var oID in this.sendsOffers) {
- if(id == this.sendsOffers[oID].bid) {
- (function(offer, oID2) {
- this.steam[id].getOffer({
- tradeofferid: offer.id
- }, function(err, body) {
- if(!err && body) {
- if(!err && body && body.response && body.response.offer) {
- var offer2 = body.response.offer;
- var sid = offer2.steamid_other;
- var ost = offer2.trade_offer_state;
- var cashMust = offer.coins;
- var createdAt = offer.created_at;
- if(ost === 2 || ost === 9 || ost === 11) {
- //Wait for accept
- if(ost !== 11) {
- //Check is time expired
- if(time() > (createdAt + confWith)) {
- this.steam[id].cancelOffer({
- tradeofferid: offer.id
- }, function(e9,r9) {});
- this.client.sendMessage(sid, "Info!", "Upłynął czas oczekiwania na akceptacje, Twoja oferta została odrzucona!", "info", 3);
- console.log("Steam -> Offer -> " + offer.id + " >> " + sid + " -> " + ost + " [AUTO DECLINE]");
- }
- }
- }
- else if(ost === 3) {
- delete this.queueOffers[sid];
- delete this.sendedOffers[sid];
- delete this.sendsOffers[oID2];
- this.mysql.update_noa_query("transfer", "`status` = '4', `confirmed_at` = '" + time() + "'", undefined, "`oid` = '" + offer.id + "'");
- this.client.sendMessage(sid, "Sukces!", "Twoja oferta zostala poprawnie zaakceptowana!", "success", 3);
- console.log("Steam -> Offer -> " + offer.id + " >> " + sid + " -> " + ost + " [ACCEPT]");
- } else {
- delete this.queueOffers[sid];
- delete this.sendedOffers[sid];
- delete this.sendsOffers[oID2];
- this.steam[id].cancelOffer({
- tradeofferid: offer.id
- }, function(e9,r9) {});
- if(ost !== 6) {
- this.client.sendMessage(sid, "Info!", "Twoja oferta zostala odrzucona!", "info", 3);
- console.log("Steam -> Offer -> " + offer.id + " >> " + sid + " -> " + ost + " [DECLINE]");
- }
- this.players[sid].cash += cashMust;
- this.mysql.update_noa_query("transfer", "`status` = '0', `canceled` = '1'", undefined, "`oid` = '" + offer.id + "'");
- this.mysql.update_noa_query("steam_accounts", "`cash` = cash + '" + cashMust + "'", undefined, "`steamid` = '" + sid + "'");
- }
- }
- }
- }.bind(this));
- }.bind(this))(this.sendsOffers[oID], oID);
- }
- }
- }.bind(this))(id2);
- }
- setTimeout(function() {
- this.checkSendedOffers();
- }.bind(this), 1000 * this.config.timers.Sends);
- }
- //Make offers
- steamBot.prototype.makeOffers = function(sid, assignItems, assignCash) {
- if(assignItems && assignCash && sid) {
- var iID = 0;
- for(var bid in assignItems) {
- iID++;
- this.makeOffer(sid, assignItems[bid], bid, assignCash[bid]);
- }
- }
- }
- //Make offer
- steamBot.prototype.makeOffer = function(sid, items, id, cash) {
- if(items && id && this.players[sid] && this.players[sid].trade_token) {
- if(this.queueOffers[sid]) {
- setTimeout(function() {
- this.makeOffer(sid, items, id, cash);
- }.bind(this), 1500);
- } else {
- this.players[sid].cash -= cash;
- this.mysql.update_noa_query("steam_accounts", "`cash` = cash - '" + cash + "'", undefined, "`steamid` = '" + sid + "'");
- this.queueOffers[sid] = true;
- this.steam[id].makeOffer({
- partnerSteamId: sid,
- accessToken: this.players[sid].trade_token,
- itemsFromMe: items,
- message: this.config.modules.market.with.msg
- }, function(err, res) {
- if(!err && res) {
- var oid = res.tradeofferid;
- this.mysql.insert_query("transfer", {
- "oid": oid,
- "items": Object.keys(items).length,
- "dir": 2,
- "created_at": time(),
- "coins": cash,
- "sid": sid,
- "status": 2,
- "botid": this.bots[id].user
- }, function(id, e) { }.bind(this));
- this.client.sendMessage(sid, "Sukces!", "Twoja oferta zostala utworzona i oczekuje na potwierdzenie mobilne!", "success", 3);
- this.confsAllowed[oid] = {
- oid: oid,
- type: 1,
- sid: sid,
- showConfirm: true
- };
- console.log("Success");
- this.getConfirmations(id);
- } else {
- console.log("############################");
- console.log("SEND OFFER ERROR");
- print_r(err);
- this.client.sendMessage(sid, "Opsss!", "Niestety, nie udało się wysłać oferty :(", "danger", 3);
- console.log("############################");
- this.mysql.insert_query("transfer", {
- "oid": 0,
- "items": Object.keys(items).length,
- "dir": 2,
- "created_at": time(),
- "coins": cash,
- "sid": sid,
- "status": 1,
- "botid": this.bots[id].user
- }, function(id, e) { }.bind(this));
- this.queueOffers[sid] = false;
- }
- }.bind(this));
- }
- }
- }
- //Check transfers
- steamBot.prototype.checkTransfers = function() {
- this.mysql.select_query("transfer", "*", function(e, r) {
- if(!e && r && r.length > 0) {
- //console.log("Steam-> Transfer -> Check sended offers");
- var changed = false;
- var iid = 0;
- r.forEach(function(num) {
- if(parseInt(num['dir']) == 2 && parseInt(num['canceled']) == 0) {
- //Sended offers
- var created = num['created_at'];
- var status = parseInt(num['status']);
- var cashMust = parseFloat(num['coins']);
- var sid = num['sid'];
- if(status == 1 && (time() - this.config.modules.market.with.wait_creat) > created && num['oid'] == "0") {
- this.sendedOffers[sid] = false;
- console.log("Steam -> Cancel queue offer for steamID -> " + sid + "(" + num['id'] + ")");
- this.client.sendMessage(sid, "Informacja!", "Twoje coinsy wróciły, mozesz sprobowac wyslac kolejna oferte!", "info", 3);
- this.players[sid].cash += cashMust;
- this.mysql.update_noa_query("steam_accounts", "`cash` = cash + '" + cashMust + "'", undefined, "`steamid` = '" + sid + "'");
- this.mysql.update_noa_query("transfer", "`canceled` = '1'", undefined, "`id` = '" + num['id'] + "'");
- changed = true;
- } else if((status == 2 || status == 3)) {
- if(!num['canceled']) {
- if(!this.sendedOffers[sid])
- this.sendedOffers[sid] = true;
- if(num['oid'] != "0") {
- var offer = num['oid'];
- if(!this.sendsOffers[offer]) {
- this.sendsOffers[offer] = {
- id: offer,
- bid: this.botNames[num['botid']],
- coins: cashMust,
- create: num['created_at']
- };
- if(status == 2) {
- this.confsAllowed[offer] = {
- oid: offer,
- type: 1,
- sid: sid,
- showConfirm: true
- };
- }
- }
- }
- }
- }
- }
- iid++;
- if(r.length == iid) {
- if(changed)
- this.getPlayersData(0);
- setTimeout(function() {
- this.checkTransfers();
- }.bind(this), 1000 * this.config.timers.RefTransfer);
- }
- }.bind(this));
- } else {
- setTimeout(function() {
- this.checkTransfers();
- }.bind(this), 1000 * this.config.timers.RefTransfer);
- }
- }.bind(this), "`canceled` = '0'");
- }
- //Refresh inventory
- steamBot.prototype.refreshInventory = function(uid) {
- //console.log("@Refresh Inventory");
- var uid = typeof uid !== "undefined" ? uid : 0;
- if(this.botItems && parseInt(this.itemsSum) > 0) {
- this.client.updateInventory(this.botItems, this.itemsSum, uid);
- }
- }
- //Get total coins from steamID
- steamBot.prototype.getPlayersData = function(uid, steamid) {
- var uid = typeof uid !== "undefined" ? uid : 0;
- var steamid = typeof steamid !== "undefined" ? steamid : 0;
- if(steamid == 0) {
- this.client.setPlayersData(this.players);
- } else {
- this.client.setPlayerData(uid, steamid, this.players[steamid])
- }
- }
- //Get empties bot
- steamBot.prototype.findEmptiesBot = function() {
- if(!this.lockEmpties || parseInt(this.lockEmpties) < parseInt(time())) {
- var currentBot = -1;
- var itemsBot = 9999999;
- this.lockEmpties = time() + parseInt(this.config.lock.FindEmpties);
- //console.log("Steam-> Find emptied BOT!");
- for(var bkey in this.bots) {
- var itemsTemp = {};
- (function(id, bot) {
- if(bot.connected && bot.loggedIn) {
- this.steamProfiles[id].getInventory(this.config.modules[bot.action].inv.appID, this.config.modules[bot.action].inv.contextID, this.config.modules[bot.action].inv.tradeableOnly, function(err, inv, curr) {
- if(err) {
- print_r(err);
- } else {
- if(inv && inv.length > 0) {
- for(var key in inv) {
- itemsTemp[inv[key].id] = true;
- if(!this.botItemsDef[inv[key].id]) {
- this.assignItemFor(inv[key], id);
- }
- }
- this.bots[id].items = inv.length;
- } else {
- this.bots[id].items = 0;
- }
- if(this.bots[id].items < itemsBot) {
- itemsBot = this.bots[id].items;
- currentBot = id;
- }
- if(Object.keys(this.bots).length == (parseInt(id)+1)) {
- this.assignBots = currentBot;
- this.client.setTradeURL(this.bots[currentBot].tradeURL);
- setTimeout(function() {
- for(var iKey in this.botItemsDef) {
- var item3 = this.botItemsDef[iKey];
- var item4 = this.botItems[item3];
- if(itemsTemp && !itemsTemp[iKey]) {
- console.log("Steam -> BOT -> DELETE ITEMID -> " + this.botItems[item3].id);
- delete this.botItemsDef[iKey];
- delete this.botItems[item3];
- this.itemsSum = randomInt(10000, 9999999);
- }
- }
- this.refreshInventory(0);
- }.bind(this), 500);
- if((Object.keys(this.bots).length - 1) == id) {
- if(!this.timerEmptiesGlobal) {
- this.timerEmptiesGlobal = setTimeout(function() {
- this.timerEmptiesGlobal = false;
- this.findEmptiesBot();
- }.bind(this), 1000 * this.config.timers.FindBots);
- }
- }
- }
- }
- }.bind(this));
- }
- }.bind(this))(bkey, this.bots[bkey]);
- }
- } else {
- console.log("Steam-> To fast check find empties bot!");
- if(!this.timerEmpties) {
- this.timerEmpties = setTimeout(function() {
- this.timerEmpties = false;
- this.findEmptiesBot();
- }.bind(this), 30000);
- }
- }
- }
- //Offer response
- steamBot.prototype.setOfferResponse = function(oid, resp) {
- console.log("#2 Response from oid -> " + oid + " resp -> " + resp);
- //Set response
- if(oid && this.offers[oid]) {
- this.offers[oid].response = resp;
- this.botOffer(oid, false);
- }
- }
- //Set trade url
- steamBot.prototype.setTradeURL = function(id) {
- if(this.bots[id].accid && this.bots[id].steamid && this.bots[id].token) {
- var url = this.config.general.tradeURL;
- url = url.replace("[accountid]", this.bots[id].accid);
- url = url.replace("[token]", this.bots[id].token);
- this.bots[id].tradeURL = url;
- } else {
- console.log("Nie udalo sie ustawic trade URL ");
- }
- }
- //Add handle to new offer
- steamBot.prototype.botOffer = function(oid, addTimer) {
- if(this.offers && this.offers[oid]) {
- var offerManage = this.offers[oid];
- var bid = offerManage.bot;
- var bot = this.bots[bid];
- var sid = offerManage.sid;
- addTimer = typeof addTimer !== "undefined" ? addTimer : true;
- if(bot.action == "market" && offerManage && !offerManage.del) {
- if(offerManage.response == 0) {
- client.showOffer(offerManage);
- } else if(!this.sleepOffers[oid]) {
- this.sleepOffers[oid] = true;
- var resp = parseInt(offerManage.response);
- if(resp == 1) {
- if(this.players[sid]) {
- this.steam[bid].getTradeHoldDuration({
- tradeOfferId: oid
- }, function(e2, rt) {
- if(!e2 && rt) {
- print_r(rt);
- if(rt && rt.their === 0 && rt.my === 0) {
- this.steam[bid].acceptOffer({
- tradeOfferId: oid,
- partnerSteamId: sid
- }, function(e, r) {
- if(e) {
- //Error with accept
- console.log("Steam:Error Trade Accept offerID -> " + oid + " for user -> " + sid);
- } else {
- this.offers[oid].response = 3;
- }
- delete this.sleepOffers[oid];
- }.bind(this));
- } else {
- this.client.sendMessage(sid, "Ooo NIE!!!", "Niestety, Twoje konto jest ograniczone!", "error", 5);
- delete this.playerOffers[sid];
- delete this.sleepOffers[oid];
- this.offers[oid].del = 1;
- }
- } else {
- delete this.sleepOffers[oid];
- }
- }.bind(this));
- } else {
- this.client.sendMessage(sid, "Ooo NIE!!!", "Niestety, Twoje konto nie ma przypisanego TradeURL!", "error", 5);
- delete this.sleepOffers[oid];
- }
- } else if(resp == 2) {
- this.steam[bid].declineOffer({
- tradeOfferId: oid
- }, function(e, r) {
- if(e) {
- //Error with accept
- console.log("Steam:Error Trade Decline offerID -> " + oid + " for user -> " + sid);
- } else {
- this.offers[oid].response = 3;
- }
- delete this.sleepOffers[oid];
- }.bind(this));
- } else if(resp == 3) {
- this.steam[bid].getOffer({
- tradeOfferId: oid
- }, function(e, r) {
- if(!e && r && r.response && r.response.offer) {
- var offer = r.response.offer;
- var state = offer.trade_offer_state;
- var resetOffer = false;
- if(state === 3) {
- console.log("Steam:INFO Offer -> " + oid + " is accepted for steamID -> " + sid + " Add: " + this.offers[oid].coins + " coins!");
- this.client.sendMessage(sid, "Sukces!", "Twoja oferta zostala poprawnie zaakceptowana!", "success", 3);
- this.mysql.update_noa_query("steam_accounts", "`cash` = cash + '" + this.offers[oid].coins + "', `deposites` = deposites + '" + this.offers[oid].coins + "'", function(e9, r9) { }, "`steamid` = '" + sid + "'");
- resetOffer = true;
- } else if(state === 2) {
- console.log("Steam:INFO Offer -> " + oid + " is waiting for accept/decline for steamID -> " + sid);
- } else {
- console.log("Steam:INFO Offer -> " + oid + " is declined for steamID -> " + sid + " :: Status -> " + state);
- this.client.sendMessage(sid, "Sukces!", "Twoja oferta zostala poprawnie odrzucona!", "success", 3);
- resetOffer = true;
- }
- if(resetOffer) {
- delete this.playerOffers[sid];
- this.offers[oid].del = 1;
- }
- delete this.sleepOffers[oid];
- } else {
- delete this.sleepOffers[oid];
- }
- }.bind(this));
- } else if(resp == 4) {
- this.steam[bid].declineOffer({
- tradeOfferId: oid
- }, function(e, r) {
- if(e) {
- //Error with accept
- console.log("Steam:Error Trade Decline offerID -> " + oid + " for user -> " + sid);
- } else {
- if(this.offers[oid].oErr == 1) {
- this.client.sendMessage(sid, "Blad!", "Minimalna liczba coinsow 1 itemu to: " + this.config.modules[bot.action].dep.min_item + "!", "error", 3);
- } else if(this.offers[oid].oErr == 2) {
- this.client.sendMessage(sid, "Blad!", "Minimalna liczba coinsow w 1 ofercie to: " + this.config.modules[bot.action].dep.min_total + "!", "error", 3);
- } else if(this.offers[oid].oErr == 3) {
- this.client.sendMessage(sid, "Blad!", "Maksymalna liczba itemow w 1 ofercie to: " + this.config.modules[bot.action].dep.items_max + "!", "error", 3);
- }
- delete this.playerOffers[sid];
- this.offers[oid].del = 1;
- }
- delete this.sleepOffers[oid];
- }.bind(this));
- }
- }
- if(addTimer && !offerManage.del) {
- setTimeout(function() {
- this.botOffer(oid);
- }.bind(this), 1000 * parseInt(this.config.timers.Offer));
- }
- } else if(offerManage && !offerManage.del) {
- this.botOffer(oid);
- }
- }
- }
- //Get item price
- steamBot.prototype.itemPrice = function(name, callback) {
- var timeUpdatePrice = 60;
- if(name && name !== "undefined" && name.length > 2) {
- var price_type = 1;
- if(this.config.price) {
- this.config.price.type = typeof this.config.price.type !== "undefined" ? this.config.price.type : 1;
- this.config.price.link = typeof this.config.price.link !== "undefined" ? this.config.price.link : "http://google.pl";
- this.config.price.key = typeof this.config.price.key !== "undefined" ? this.config.price.key : "none";
- this.config.price.base = typeof this.config.price.base !== "undefined" ? this.config.price.base : 0;
- this.config.price.update = typeof this.config.price.update !== "undefined" ? parseInt(this.config.price.update * 60 * 60) : timeUpdatePrice;
- } else {
- this.config.price.type = 1;
- this.config.price.link = "http://google.pl";
- this.config.price.key = "none";
- this.config.price.base = 0;
- this.config.price.update = timeUpdatePrice;
- }
- timeUpdatePrice = this.config.price.update;
- if(!this.itemsGets[name])
- this.itemsGets[name] = 0;
- if(!this.itemsPrices[name])
- this.itemsPrices[name] = 0.00;
- if(this.itemsGets[name] == 0 || parseFloat(this.itemsPrices[name]) <= 0 || parseInt(this.itemsGets[name]) < parseInt(time())) {
- this.itemsGets[name] = parseInt(time()) + parseInt(60 * 60 * 6);
- try {
- var link_price = "";
- if(this.config.price.type === 1)
- link_price = this.config.price.link + encodeURI(name);
- else {
- link_price = this.config.price.link;
- link_price = link_price.replace("[name]", encodeURI(name));
- link_price = link_price.replace("[key]", this.config.price.key);
- }
- var itemTimer = time();
- //console.log(link_price);
- if(this.config.price.base == 1) {
- this.mysql.select_query("items_price", "*", function(err, rows) {
- var updatePrice = true;
- var priceFromBase = 0;
- if(!err && rows && rows.length > 0) {
- var num = rows[0];
- var lupdate = parseInt(num.last_update) + parseInt(timeUpdatePrice);
- if(lupdate > parseInt(time())) {
- priceFromBase = parseFloat(num.price);
- updatePrice = false;
- } else {
- updatePrice = true;
- }
- }
- if(updatePrice) {
- requ({
- uri: link_price,
- timeout: 3000
- }, function(err, resp, body) {
- if(!err && resp.statusCode == 200) {
- var b2 = JSON.parse(body);
- if(b2.success) {
- var priceItem = 0;
- if(this.config.price.type === 1) {
- priceItem = parseFloat(b2.lowest_price.replace("$", ""));
- } else {
- priceItem = parseFloat(b2.median_price.replace("$", ""));
- }
- if(priceItem > 0) {
- itemTimer = parseInt(time()) - parseInt(itemTimer);
- this.itemsPrices[name] = priceItem;
- if(!err && rows && rows.length > 0) {
- this.mysql.update_query("items_price", {
- "price": priceItem,
- "last_update": time()
- }, function(e, r) {
- if(e) {
- console.log("Price Error -> " + e);
- }
- }, "`name` = '" + encodeURI(name) + "'");
- } else {
- this.mysql.insert_query("items_price", {
- "name": encodeURI(name),
- "price": priceItem,
- "last_update": time()
- });
- }
- }
- callback(priceItem);
- } else
- callback(0.00);
- } else {
- //console.log("Error with: " + name)
- if(err.Error == "ETIMEDOUT") {
- console.log("Timeout price!");
- }
- callback(0.00);
- }
- }.bind(this));
- } else {
- itemTimer = parseInt(time()) - parseInt(itemTimer);
- this.itemsPrices[name] = priceFromBase;
- callback(priceFromBase);
- }
- }.bind(this), "`name` = '" + encodeURI(name) + "'");
- } else {
- requ({
- uri: link_price,
- timeout: 3000
- }, function(err, resp, body) {
- if(!err && resp.statusCode == 200) {
- var b2 = JSON.parse(body);
- if(b2.success) {
- var priceItem = 0;
- if(this.config.price.type === 1) {
- priceItem = parseFloat(b2.lowest_price.replace("$", ""));
- } else {
- priceItem = parseFloat(b2.median_price.replace("$", ""));
- }
- if(priceItem > 0) {
- itemTimer = parseInt(time()) - parseInt(itemTimer);
- this.itemsPrices[name] = priceItem;
- }
- callback(priceItem);
- } else {
- callback(0.00);
- }
- } else {
- //console.log("Error with: " + name)
- if(err.Error == "ETIMEDOUT") {
- console.log("Timeout price!");
- }
- callback(0.00);
- }
- }.bind(this));
- }
- }
- catch(e) {
- callback(0.00);
- }
- } else {
- callback(parseFloat(this.itemsPrices[name]));
- }
- }
- }
- //Get accepted items
- steamBot.prototype.itemAccepted = function(item) {
- var allowed = false;
- for(id in this.config.acceptGames) {
- var game = this.config.acceptGames[id];
- if(parseInt(game.appid) == parseInt(item.appid) && parseInt(game.contextid) == parseInt(item.contextid)) {
- allowed = true;
- break;
- }
- }
- return allowed;
- }
- //Get descriptions of items
- steamBot.prototype.decodeDesc = function(desc, c) {
- var itemsDesc = {};
- var i = 0;
- if(desc.length > 0) {
- desc.forEach(function(item) {
- var name = item.classid + "_" + item.instanceid;
- if(!itemsDesc[name]) {
- delete item.descriptions;
- delete item.tags;
- itemsDesc[name] = item;
- }
- i++;
- if(i == desc.length) {
- c(itemsDesc);
- }
- }.bind(this));
- } else {
- c(itemsDesc);
- }
- }
- //Get all offers
- steamBot.prototype.botOffers = function(id, waitFor) {
- var timerSec = typeof waitFor === 'undefined' ? 50 : (1000 * parseInt(this.config.timers.Offers));
- setTimeout(function() {
- if(this.bots[id].connected && this.bots[id].loggedIn) {
- //console.log("Get offers for : " + this.bots[id].user);
- this.steam[id].getOffers({
- get_received_offers: 1,
- get_descriptions: 1,
- active_only: 1,
- time_historical_cutoff: Math.round(Date.now() / 1000)
- }, function(error, body) {
- if(!error) {
- if(body && body.response && body.response.trade_offers_received) {
- var offers = body.response.trade_offers_received;
- var desc = body.response.descriptions;
- if(offers.length > 0) {
- var offersId = 0;
- offers.forEach(function(offer) {
- var items = offer.items_to_receive;
- var oid = offer.tradeofferid;
- var ost = offer.trade_offer_state;
- var sid = offer.steamid_other;
- var oErr = 0;
- offersId++;
- if(this.playerOffers[sid]) {
- if(offers.length == offersId) {
- this.botOffers(id, true);
- }
- } else {
- this.playerOffers[sid] = oid;
- if(!this.offers[oid] || this.offers[oid] && !this.offers[oid].del) {
- console.log("Check offer -> " + oid + " for player -> " + sid);
- this.client.sendMessage(sid, "Twoja oferta!", "Twoja oferta jest przetwarzana!", "info", 3);
- }
- if(this.offers[oid] && this.offers[oid].citems == items.length) {
- if(offers.length == offersId) {
- this.botOffers(id, true);
- }
- } else {
- this.decodeDesc(desc, function(itemsDesc) {
- if(items.length > 0) {
- var itemsAllow = 0;
- var itemsId = 0;
- var itemsTradable = 0;
- var itemsPrice = 0;
- var itemsPrices = 0;
- items.forEach(function(item, itemKey) {
- var itemDesc = itemsDesc[item.classid + "_" + item.instanceid];
- if(this.itemAccepted(item)) {
- itemsAllow++;
- }
- if(itemDesc.tradable) {
- itemsTradable++;
- }
- this.itemPrice(itemDesc.market_name, function(price) {
- itemsId++;
- price = parseFloat(price);
- console.log("Steam Price-> " + itemDesc.name + " price-> " + price);
- if(parseFloat(price) > 0) {
- itemsPrice++;
- itemsPrices += price;
- items[itemKey].price = price;
- if(parseFloat(this.client.getCoins(price, false)) < parseFloat(this.config.modules[this.bots[id].action].dep.min_item)) {
- oErr = 1;
- }
- }
- if(items.length == itemsId) {
- if(itemsAllow == items.length && itemsTradable == items.length) {
- if(itemsPrice == items.length) {
- var resp = 0;
- if(parseFloat(this.client.getCoins(itemsPrices)) < parseFloat(this.config.modules[this.bots[id].action].dep.min_total)) {
- oErr = 2;
- } else if(items.length > this.config.modules[this.bots[id].action].dep.items_max) {
- oErr = 3;
- }
- if(oErr != 0) {
- resp = 4;
- }
- //All allowed
- this.offers[oid] = {
- added: time(),
- citems: items.length,
- offer: offer,
- items: items,
- itemsDesc: itemsDesc,
- prices: itemsPrices,
- coins: this.client.getCoins(itemsPrices),
- state: ost,
- oid: oid,
- bot: id,
- sid: sid,
- response: resp,
- oErr: oErr
- };
- this.botOffer(oid);
- } else {
- delete this.playerOffers[sid];
- console.warn("Steam:ERROR:: Konto -> " + this.bots[id].user + " ofertcie: " + oid + " brak zaladowania cen wszystkich itemow!");
- }
- } else {
- //Any item is not allowed, decline offer!
- if(ost === 2) {
- console.warn("Steam:ERROR:: Konto -> " + this.bots[id].user + " oferta: " + oid + " posiada niedozwolone itemy!");
- delete this.playerOffers[sid];
- this.steam[id].declineOffer({tradeOfferId: oid});
- }
- }
- if(offers.length == offersId) {
- this.botOffers(id, true);
- }
- }
- }.bind(this));
- }.bind(this));
- } else {
- delete this.playerOffers[sid];
- this.botOffers(id, true);
- }
- }.bind(this));
- }
- }
- }.bind(this));
- } else {
- this.botOffers(id, true);
- }
- } else {
- this.botOffers(id, true);
- }
- } else {
- this.botOffers(id, true);
- }
- }.bind(this));
- } else {
- this.botOffers(id, true);
- }
- }.bind(this), timerSec);
- }
- //Confirmation key
- steamBot.prototype.getConfKey = function(id, conf) {
- if(parseInt(conf) == 1)
- conf = "allow";
- else if(parseInt(conf) == 2)
- conf = "cancel";
- else if(parseInt(conf) == 3)
- conf = "details";
- else if(parseInt(conf) == 4)
- conf = "conf";
- var time = Math.floor(Date.now() / 1000);
- return SteamTotp.getConfirmationKey(this.bots[id].file.identity_secret, time, conf);
- }
- //New confirmation
- steamBot.prototype.newConfirmation = function(id, conf) {
- //this.confsAllowed
- if(conf && conf.type == 2 && this.confsAllowed[conf.offerID]) {
- this.steamCom[id].respondToConfirmation(conf.id, conf.key, time(), this.getConfKey(id, 1), true, function(e, r) {
- if(!e) {
- var sid = this.confsAllowed[conf.offerID].sid;
- if(this.confsAllowed[conf.offerID].showConfirm) {
- this.client.sendMessage(sid, "Sukces!", "Twoja oferta zostala potwierdzona. <a href='" + this.client.getOfferLink(conf.offerID) + "' target='_blank'>Przejdź do oferty</a>!", "success", 10);
- }
- }
- }.bind(this));
- }
- }
- //Get confirmations
- steamBot.prototype.getConfirmations = function() {
- if(this.timerConfs) {
- clearTimeout(this.timerConfs);
- this.timerConfs = false;
- }
- for(var id2 in this.bots) {
- (function(id) {
- this.steamCom[id].getConfirmations(time(), this.getConfKey(id, 4), function(e, r) {
- if(!e && r && r.length > 0) {
- r.forEach(function(conf) {
- this.newConfirmation(id, conf);
- }.bind(this));
- }
- }.bind(this));
- this.timerConfs = setTimeout(function() {
- this.getConfirmations(id);
- }.bind(this), 1000 * this.config.timers.Confs);
- }.bind(this))(id2);
- }
- }
- //Connect
- steamBot.prototype.connect = function(id, bot, c) {
- //Add logs
- console.info("Steam:INFO:: Konto -> " + bot.user + " inicializacja!");
- if (!fs.existsSync("./" + this.config.general.steamFa + "/" + bot.user + ".2fa")) {
- console.warn("Steam:WARN:: Konto -> " + bot.user + " nie posiada pliku z TwoFactoryCode!");
- bot.connected = false;
- } else {
- bot.file = JSON5.parse(fs.readFileSync("./" + this.config.general.steamFa + "/" + bot.user + ".2fa"));
- bot.code = SteamTotp.generateAuthCode(bot.file.shared_secret);
- this.steamClient[id] = new Steam.SteamClient();
- this.steamUser[id] = new Steam.SteamUser(this.steamClient[id]);
- this.steamFriends[id] = new Steam.SteamFriends(this.steamClient[id]);
- this.steamCom[id] = new SteamCommunity();
- this.steamWeb[id] = new SteamWebLogOn(this.steamClient[id], this.steamUser[id]);
- this.steam[id] = new SteamTradeOffers();
- this.steamClient[id].connect();
- this.steamClient[id].on('connected', function() {
- bot.connected = true;
- console.log("Steam:SUCCESS:: Konto -> " + bot.user + " polaczono poprawnie!");
- this.steamUser[id].logOn({
- account_name: bot.user,
- password: bot.pass,
- two_factor_code: bot.code
- });
- }.bind(this));
- this.steamClient[id].on('logOnResponse', function(resp) {
- if (resp.eresult == Steam.EResult.OK) {
- console.log("Steam:SUCCESS:: Konto -> " + bot.user + " zalogowano poprawnie!");
- this.steamFriends[id].setPersonaState(Steam.EPersonaState.Online);
- bot.loggedIn = true;
- this.steamWeb[id].webLogOn(function(sessionID, newCookie) {
- this.steamCom[id].setCookies(newCookie);
- this.steamCom[id].getWebApiKey(this.config.general.address, function(e, api) {
- if(e) {
- console.warn("Steam:ERROR:: Konto -> " + bot.user + " nie udalo sie pobrac ApiKeya!");
- console.warn(e);
- this.bots[id] = bot;
- } else {
- bot.api = api;
- bot.steamid = this.steamClient[id].steamID;
- bot.accid = this.steamCom[id].steamID.accountid;
- this.bots[id] = bot;
- if(api) {
- this.steam[id].setup({
- sessionID: sessionID,
- webCookie: newCookie,
- APIKey: bot.api
- });
- this.steam[id].getOfferToken(function(err6, token) {
- if(err6) {
- print_r(err6);
- } else {
- bot.token = token;
- this.steamCom[id].getSteamUser(this.steamCom[id].steamID, function(e1, r1) {
- if(!e1) {
- bot.name = r1.name;
- this.steamProfiles[id] = r1;
- if(r1.isLimitedAccount) {
- console.log("Steam:WARNING:: Konto -> " + bot.name + " posiada limit, ktory uniemozliwa trade!");
- bot.tradable = false;
- } else if(r1.tradeBanState != 'None') {
- console.log("Steam:WARNING:: Konto -> " + bot.name + " posiada TradeBAN, ktory uniemozliwa trade!");
- bot.tradable = false;
- } else {
- if(bot && bot.username && bot.username !== false && bot.username != bot.name) {
- this.steamFriends[id].setPersonaName(bot.username);
- console.log("Steam:INFO:: Konto -> " + bot.name + " zmieniono poprawnie nick na -> " + bot.username);
- bot.name = bot.username;
- }
- bot.tradable = true;
- this.steamCom[id].on('newConfirmation', function(conf) {
- this.newConfirmation(id, conf);
- }.bind(this));
- }
- } else {
- bot.name = "[NIE WCZYTANO]";
- }
- this.bots[id] = bot;
- (function(id2) {
- setTimeout(function() {
- this.botInit(id2);
- }.bind(this), 1000);
- }.bind(this))(id);
- }.bind(this));
- }
- }.bind(this));
- }
- }
- c(bot);
- }.bind(this));
- }.bind(this));
- } else {
- if(resp.eresult == 5) {
- console.warn("Steam:ERROR:: Konto -> " + bot.user + " niepoprawny login lub haslo!");
- } else {
- console.warn("Steam:ERROR:: Konto -> " + bot.user + " powod: " + resp.eresult + "!");
- }
- }
- }.bind(this));
- //Messages
- this.steamFriends[id].on("message", function(source, msg, type, chatter) {
- if(msg.length > 1 && this.config.admins[source]) {
- if(msg.indexOf("!getcode") != -1 || msg.indexOf("!gc") != -1 || msg.indexOf("!code") != -1) {
- bot.code = SteamTotp.generateAuthCode(bot.file.shared_secret);
- this.steamFriends[id].sendMessage(source, "Użyj kodu do zalogowania: " + bot.code, Steam.EChatEntryType.ChatMsg);
- } else {
- this.steamFriends[id].sendMessage(source, "Niepoprawna komenda, uzyj !help", Steam.EChatEntryType.ChatMsg);
- }
- } else if(msg.length > 1) {
- console.log("Steam:: User -> " + source + " send message -> " + msg + " to bot -> " + bot.steamid);
- this.steamFriends[id].sendMessage(source, "Nie masz uprawnien, do tej komendy!", Steam.EChatEntryType.ChatMsg);
- }
- }.bind(this));
- }
- }
- //Excute function
- this.getPlayers();
- });
- //Export
- module.exports = steamBot;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement