Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const async = require('async')
- const redis = require('redis')
- const rc = redis.createClient()
- const SteamId = require('steamid')
- const SteamUser = require('steam-user')
- const SteamTOTP = require('steam-totp')
- const SteamCommunity = require('steamcommunity')
- const TradeOfferManager = require('steam-tradeoffer-manager')
- const log = require('../utils/log')
- // const market = require('../utils/market')
- const logger = require('../utils/logger')
- const config = require('../configs/config')
- const getHash = require('./functions/getHash')
- const updateItemsPrice = require('./functions/updateItemPrice')
- class SteamBot {
- constructor(data) {
- this._status = 0
- this._data = data
- this._name = data.account_name
- this._password = data.password
- this._shared_secret = data.shared_secret
- this._identity_secret = data.identity_secret
- this._id = data.steamid
- this.timer = 0
- setInterval(() => logger.debug(`Bot status: ${this._status}`), 5000)
- }
- init(callback) {
- this._client = new SteamUser();
- this._community = new SteamCommunity();
- this._manager = new TradeOfferManager({
- 'steam': this._client,
- 'domain': config.domain,
- 'language': 'en',
- 'cancelTime': 60 * 1000,
- 'pollInterval': 15 * 1000
- });
- this.logOn();
- this._client.setOption('promptSteamGuardCode', false);
- this._community.on('sessionExpired', error => {
- if (error) return logger.error(`Session expired, should re-login:\n${log(error)}`);
- this._status = -2;
- this.logOn();
- });
- this._client.on('steamGuard', (domain, callback, error) => {
- if (error) logger.error(`Steam Guard error:\n${log(error)}`)
- SteamTOTP.getTimeOffset((error, offset) => {
- if (error) {
- logger.error(`Error getting time offset:\n${log(error)}`)
- callback()
- }
- const code = SteamTOTP.generateAuthCode(this._shared_secret, offset)
- callback(code);
- })
- })
- this._client.on('loggedOn', () => {
- logger.info('Logged into Steam successfully!')
- this._client.setPersona(SteamUser.EPersonaState.Online)
- })
- this._client.on('error', error => {
- logger.error(`SteamUser error:\n${log(error)}`)
- if (error.eresult === 84) {
- logger.error('Steam Limit! Will try re-login in 1 hour!')
- setTimeout(() => {
- this.logOn()
- }, 60 * 60 * 1000 + 500)
- }
- })
- this._client.on('disconnected', () => {
- logger.info('Disconnected!')
- this._status = -1
- })
- this._client.on('webSession', (sessionID, cookies) => {
- this._manager.setCookies(cookies, error => {
- if (error) {
- logger.error(`Web Session can's set cookies:\n${log(error)}`)
- if (!this._data.apikey) {
- logger.error('No API key found!')
- }
- this._manager.apiKey = this._data.apikey
- this._manager.doPoll()
- return
- }
- logger.info(`Got API key: ${this._manager.apiKey}`)
- this._apiKey = this._manager.apiKey
- logger.debug(`apiKey: ${this._apiKey}`)
- this._status = 1
- // this.setInventory(error => {
- // if (error) {
- // return logger.error(`setInventory() error:\n${log(error)}`)
- // }
- // logger.info('Inventory has been added to DB!')
- // })
- callback()
- })
- this._community.setCookies(cookies)
- this._community.startConfirmationChecker(config.confirmationInterval, this._identity_secret)
- })
- this._manager.on('sentOfferChanged', (offer, oldState) => {
- logger.info(`Offer #${offer.id} new state: ${offer.state} (old state: ${oldState})`)
- })
- this._manager.on('newOffer', offer => {
- const tryAcceptOffer = (offer, callback, tries = 0) => {
- tries++
- logger.info(`Trying to accept offer, try #${tries}`)
- offer.accept(error => {
- if (error) {
- logger.error(`Offer accept error:\n${log(error)}`)
- if (tries === config.acceptOfferMaxAttempts) {
- logger.info('Last try, offer does not accepted!')
- return
- }
- setTimeout(() => {
- tryAcceptOffer(offer, callback, tries)
- }, 5000)
- }
- logger.info(`Offer accepted:\n${log(offer.rawJson)}`)
- return callback(offer)
- })
- }
- const partnerSteamID = offer.partner.getSteamID64()
- if (config.admin === partnerSteamID) {
- logger.info('Got offer from admin! Trying to accept!')
- tryAcceptOffer(offer, () => logger.info('Admin offer accepted!'))
- } else {
- logger.info(`Items to receive: ${getHash(offer.itemsToReceive)}`)
- if ((offer.itemsToGive.length === 0) && (offer.itemsToReceive.length < 21)) {
- logger.info('Got offer from non-admin Steam ID, empty itemsToGive, trying to accept!')
- tryAcceptOffer(offer, () => {
- updateItemsPrice(this._apiKey, () => logger.info('Prices have been upgraded!'))
- })
- // () => {
- // updateItemsPrice(this._apiKey, () => logger.info('Prices have been upgraded!'))
- // }
- } else {
- logger.info('Got offer from non-admin Steam ID! Trying to decline!')
- offer.decline(error => {
- if (error) {
- logger.error(`Unable to decline offer:\n${log(error)}`)
- return
- }
- logger.info('Offer declined!')
- })
- }
- }
- })
- }
- loadInventory(callback) {
- if (this._status !== 1) {
- this.logOn()
- return callback(new Error(`Seems like bot does not logged into Steam, bot status: ${this._status}`))
- }
- this._community.getUserInventoryContents(this._id, 730, 2, true, (error, inventory) => {
- if (error) callback(error)
- const out = inventory
- async.eachOf(inventory, (item, key, nextItem) => {
- rc.get('item:my:' + item.assetid, (err, price) => {
- if (err) return nextItem()
- out[key].price = price
- nextItem()
- })
- }, () => {
- callback(null, out)
- })
- })
- }
- logOn() {
- const config = {
- 'accountName': this._name,
- 'password': this._password
- };
- if (this._client.steamID) {
- this._client.webLogOn()
- } else {
- SteamTOTP.getTimeOffset((error, offset, latency) => {
- if (error && this.timer === 0) {
- this.timer = setTimeout(() => {
- this.logOn()
- this.timer = 0
- }, 5000)
- return logger.error(`AuthCode error:\n${log(error)}`)
- }
- this.timer = 0
- config['twoFactorCode'] = SteamTOTP.generateAuthCode(this._shared_secret, offset)
- this._client.logOn(config)
- });
- }
- }
- // setInventory (callback) {
- // if (this._status !== 1) {
- // return callback(new Error(`Seems like bot does not logged into Steam, bot status: ${this._status}`))
- // }
- //
- // this._community.getUserInventoryContents(this._id, 730, 2, true, (error, inventory) => {
- // let i = 0
- //
- // const getHash = item => item.market_hash_name
- //
- // const task = (hash, callback) => {
- // market.getItemPrice(hash, null, (error, price) => {
- // const getId = item => `item:my:${item.assetid}`
- // if (error) {
- // logger.error(`Error getting ${hash} price:\n${log(error)}`)
- // setTimeout(() => task(hash, callback), 500)
- // return
- // }
- // if (i === inventory.length - 1) callback()
- // rc.set(getId(inventory[i]), `${price}`)
- // if (++i < inventory.length) {
- // rc.get(getId(inventory[i]), (err, price) => {
- // if (err) {
- // logger.error(`Error getting next after ${hash} price:\n${log(error)}`)
- // setTimeout(() => task(getHash(inventory[i]), callback), 500)
- // }
- // if (price) {
- // setTimeout(() => task(getHash(inventory[++i]), callback), 500)
- // } else {
- // setTimeout(() => task(getHash(inventory[i]), callback), 500)
- // }
- // })
- // }
- // })
- // }
- //
- // if (error) callback(error)
- // if (inventory.length === 0) {
- // return logger.error('Empty inventory!')
- // }
- //
- // task(getHash(inventory[0]), callback)
- //
- // // for (let item of inventory) {
- // // const hash = item.market_hash_name
- // // market.getItemPrice(hash, null, (error, price) => {
- // // rc.get(`item:my:${item.assetid}`, (err, price) => {
- // // if (err) return
- // // if (!price) {
- // // if (error) {
- // // return logger.error(`Error getting ${hash} price:\n${log(error)}`)
- // // }
- // // rc.set(`item:my:${item.assetid}`, `${price}`)
- // // }
- // // })
- // // })
- // // }
- // })
- // }
- sendOffer(tradeURL, items, message, callback) {
- tradeURL = tradeURL.split('/?')[1]
- const regexp = /partner=([0-9]*)&token=([a-zA-Z0-9_-]*)/gi
- const match = regexp.exec(tradeURL)
- const partner = match[1]
- const token = match[2]
- const sid = new SteamId('STEAM_0:' + (partner & 1) + ':' + (partner >> 1))
- const steamid = sid.getSteamID64()
- const offer = this._manager.createOffer(new TradeOfferManager.SteamID(steamid), token)
- const itemsToGive = items.map(x => {
- return {
- assetid: x,
- appid: 730,
- contextid: 2
- }
- })
- const count = offer.addMyItems(itemsToGive)
- if (count !== items.length) logger.error('Wrong itemsToGive[] length!')
- offer.message = message
- offer.send(err => {
- if (err) return callback(err)
- callback(null, offer.id)
- })
- }
- cancelOffer(tradeofferid, callback) {
- this._manager.getOffer(tradeofferid, function (err, offer) {
- if (err) return callback(err)
- offer.cancel(function (err) {
- if (err) return callback(err)
- callback(null, tradeofferid)
- })
- })
- }
- checkStatus(tradeofferid, callback) {
- this._manager.getOffer(tradeofferid, (err, offer) => {
- if (err) return callback(err)
- offer.update(function (err) {
- if (err) return callback(err)
- callback(null, offer.state)
- })
- })
- }
- }
- module.exports = SteamBot
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement