Advertisement
Guest User

Untitled

a guest
Jun 10th, 2017
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.51 KB | None | 0 0
  1. const async = require('async')
  2. const redis = require('redis')
  3. const rc = redis.createClient()
  4. const SteamId = require('steamid')
  5. const SteamUser = require('steam-user')
  6. const SteamTOTP = require('steam-totp')
  7. const SteamCommunity = require('steamcommunity')
  8. const TradeOfferManager = require('steam-tradeoffer-manager')
  9.  
  10. const log = require('../utils/log')
  11. // const market = require('../utils/market')
  12. const logger = require('../utils/logger')
  13. const config = require('../configs/config')
  14. const getHash = require('./functions/getHash')
  15. const updateItemsPrice = require('./functions/updateItemPrice')
  16.  
  17. class SteamBot {
  18. constructor(data) {
  19. this._status = 0
  20. this._data = data
  21. this._name = data.account_name
  22. this._password = data.password
  23. this._shared_secret = data.shared_secret
  24. this._identity_secret = data.identity_secret
  25. this._id = data.steamid
  26. this.timer = 0
  27.  
  28. setInterval(() => logger.debug(`Bot status: ${this._status}`), 5000)
  29. }
  30.  
  31. init(callback) {
  32. this._client = new SteamUser();
  33. this._community = new SteamCommunity();
  34.  
  35. this._manager = new TradeOfferManager({
  36. 'steam': this._client,
  37. 'domain': config.domain,
  38. 'language': 'en',
  39. 'cancelTime': 60 * 1000,
  40. 'pollInterval': 15 * 1000
  41. });
  42.  
  43. this.logOn();
  44.  
  45. this._client.setOption('promptSteamGuardCode', false);
  46.  
  47. this._community.on('sessionExpired', error => {
  48. if (error) return logger.error(`Session expired, should re-login:\n${log(error)}`);
  49. this._status = -2;
  50. this.logOn();
  51. });
  52.  
  53. this._client.on('steamGuard', (domain, callback, error) => {
  54. if (error) logger.error(`Steam Guard error:\n${log(error)}`)
  55. SteamTOTP.getTimeOffset((error, offset) => {
  56. if (error) {
  57. logger.error(`Error getting time offset:\n${log(error)}`)
  58. callback()
  59. }
  60. const code = SteamTOTP.generateAuthCode(this._shared_secret, offset)
  61. callback(code);
  62. })
  63. })
  64.  
  65. this._client.on('loggedOn', () => {
  66. logger.info('Logged into Steam successfully!')
  67. this._client.setPersona(SteamUser.EPersonaState.Online)
  68. })
  69.  
  70. this._client.on('error', error => {
  71. logger.error(`SteamUser error:\n${log(error)}`)
  72. if (error.eresult === 84) {
  73. logger.error('Steam Limit! Will try re-login in 1 hour!')
  74. setTimeout(() => {
  75. this.logOn()
  76. }, 60 * 60 * 1000 + 500)
  77. }
  78. })
  79.  
  80. this._client.on('disconnected', () => {
  81. logger.info('Disconnected!')
  82. this._status = -1
  83. })
  84.  
  85. this._client.on('webSession', (sessionID, cookies) => {
  86. this._manager.setCookies(cookies, error => {
  87. if (error) {
  88. logger.error(`Web Session can's set cookies:\n${log(error)}`)
  89. if (!this._data.apikey) {
  90. logger.error('No API key found!')
  91. }
  92. this._manager.apiKey = this._data.apikey
  93. this._manager.doPoll()
  94. return
  95. }
  96. logger.info(`Got API key: ${this._manager.apiKey}`)
  97. this._apiKey = this._manager.apiKey
  98. logger.debug(`apiKey: ${this._apiKey}`)
  99. this._status = 1
  100. // this.setInventory(error => {
  101. // if (error) {
  102. // return logger.error(`setInventory() error:\n${log(error)}`)
  103. // }
  104. // logger.info('Inventory has been added to DB!')
  105. // })
  106. callback()
  107. })
  108.  
  109. this._community.setCookies(cookies)
  110. this._community.startConfirmationChecker(config.confirmationInterval, this._identity_secret)
  111. })
  112.  
  113. this._manager.on('sentOfferChanged', (offer, oldState) => {
  114. logger.info(`Offer #${offer.id} new state: ${offer.state} (old state: ${oldState})`)
  115. })
  116.  
  117. this._manager.on('newOffer', offer => {
  118. const tryAcceptOffer = (offer, callback, tries = 0) => {
  119. tries++
  120. logger.info(`Trying to accept offer, try #${tries}`)
  121. offer.accept(error => {
  122. if (error) {
  123. logger.error(`Offer accept error:\n${log(error)}`)
  124. if (tries === config.acceptOfferMaxAttempts) {
  125. logger.info('Last try, offer does not accepted!')
  126. return
  127. }
  128. setTimeout(() => {
  129. tryAcceptOffer(offer, callback, tries)
  130. }, 5000)
  131. }
  132.  
  133. logger.info(`Offer accepted:\n${log(offer.rawJson)}`)
  134. return callback(offer)
  135. })
  136. }
  137.  
  138. const partnerSteamID = offer.partner.getSteamID64()
  139.  
  140. if (config.admin === partnerSteamID) {
  141. logger.info('Got offer from admin! Trying to accept!')
  142. tryAcceptOffer(offer, () => logger.info('Admin offer accepted!'))
  143. } else {
  144. logger.info(`Items to receive: ${getHash(offer.itemsToReceive)}`)
  145. if ((offer.itemsToGive.length === 0) && (offer.itemsToReceive.length < 21)) {
  146. logger.info('Got offer from non-admin Steam ID, empty itemsToGive, trying to accept!')
  147. tryAcceptOffer(offer, () => {
  148. updateItemsPrice(this._apiKey, () => logger.info('Prices have been upgraded!'))
  149. })
  150. // () => {
  151. // updateItemsPrice(this._apiKey, () => logger.info('Prices have been upgraded!'))
  152. // }
  153. } else {
  154. logger.info('Got offer from non-admin Steam ID! Trying to decline!')
  155. offer.decline(error => {
  156. if (error) {
  157. logger.error(`Unable to decline offer:\n${log(error)}`)
  158. return
  159. }
  160. logger.info('Offer declined!')
  161. })
  162. }
  163. }
  164. })
  165. }
  166.  
  167. loadInventory(callback) {
  168. if (this._status !== 1) {
  169. this.logOn()
  170. return callback(new Error(`Seems like bot does not logged into Steam, bot status: ${this._status}`))
  171. }
  172.  
  173. this._community.getUserInventoryContents(this._id, 730, 2, true, (error, inventory) => {
  174. if (error) callback(error)
  175. const out = inventory
  176. async.eachOf(inventory, (item, key, nextItem) => {
  177. rc.get('item:my:' + item.assetid, (err, price) => {
  178. if (err) return nextItem()
  179. out[key].price = price
  180. nextItem()
  181. })
  182. }, () => {
  183. callback(null, out)
  184. })
  185. })
  186. }
  187.  
  188. logOn() {
  189. const config = {
  190. 'accountName': this._name,
  191. 'password': this._password
  192. };
  193. if (this._client.steamID) {
  194. this._client.webLogOn()
  195. } else {
  196. SteamTOTP.getTimeOffset((error, offset, latency) => {
  197. if (error && this.timer === 0) {
  198. this.timer = setTimeout(() => {
  199. this.logOn()
  200. this.timer = 0
  201. }, 5000)
  202. return logger.error(`AuthCode error:\n${log(error)}`)
  203. }
  204. this.timer = 0
  205. config['twoFactorCode'] = SteamTOTP.generateAuthCode(this._shared_secret, offset)
  206. this._client.logOn(config)
  207. });
  208. }
  209. }
  210.  
  211. // setInventory (callback) {
  212. // if (this._status !== 1) {
  213. // return callback(new Error(`Seems like bot does not logged into Steam, bot status: ${this._status}`))
  214. // }
  215. //
  216. // this._community.getUserInventoryContents(this._id, 730, 2, true, (error, inventory) => {
  217. // let i = 0
  218. //
  219. // const getHash = item => item.market_hash_name
  220. //
  221. // const task = (hash, callback) => {
  222. // market.getItemPrice(hash, null, (error, price) => {
  223. // const getId = item => `item:my:${item.assetid}`
  224. // if (error) {
  225. // logger.error(`Error getting ${hash} price:\n${log(error)}`)
  226. // setTimeout(() => task(hash, callback), 500)
  227. // return
  228. // }
  229. // if (i === inventory.length - 1) callback()
  230. // rc.set(getId(inventory[i]), `${price}`)
  231. // if (++i < inventory.length) {
  232. // rc.get(getId(inventory[i]), (err, price) => {
  233. // if (err) {
  234. // logger.error(`Error getting next after ${hash} price:\n${log(error)}`)
  235. // setTimeout(() => task(getHash(inventory[i]), callback), 500)
  236. // }
  237. // if (price) {
  238. // setTimeout(() => task(getHash(inventory[++i]), callback), 500)
  239. // } else {
  240. // setTimeout(() => task(getHash(inventory[i]), callback), 500)
  241. // }
  242. // })
  243. // }
  244. // })
  245. // }
  246. //
  247. // if (error) callback(error)
  248. // if (inventory.length === 0) {
  249. // return logger.error('Empty inventory!')
  250. // }
  251. //
  252. // task(getHash(inventory[0]), callback)
  253. //
  254. // // for (let item of inventory) {
  255. // // const hash = item.market_hash_name
  256. // // market.getItemPrice(hash, null, (error, price) => {
  257. // // rc.get(`item:my:${item.assetid}`, (err, price) => {
  258. // // if (err) return
  259. // // if (!price) {
  260. // // if (error) {
  261. // // return logger.error(`Error getting ${hash} price:\n${log(error)}`)
  262. // // }
  263. // // rc.set(`item:my:${item.assetid}`, `${price}`)
  264. // // }
  265. // // })
  266. // // })
  267. // // }
  268. // })
  269. // }
  270.  
  271. sendOffer(tradeURL, items, message, callback) {
  272. tradeURL = tradeURL.split('/?')[1]
  273. const regexp = /partner=([0-9]*)&token=([a-zA-Z0-9_-]*)/gi
  274. const match = regexp.exec(tradeURL)
  275. const partner = match[1]
  276. const token = match[2]
  277. const sid = new SteamId('STEAM_0:' + (partner & 1) + ':' + (partner >> 1))
  278. const steamid = sid.getSteamID64()
  279. const offer = this._manager.createOffer(new TradeOfferManager.SteamID(steamid), token)
  280. const itemsToGive = items.map(x => {
  281. return {
  282. assetid: x,
  283. appid: 730,
  284. contextid: 2
  285. }
  286. })
  287. const count = offer.addMyItems(itemsToGive)
  288. if (count !== items.length) logger.error('Wrong itemsToGive[] length!')
  289. offer.message = message
  290. offer.send(err => {
  291. if (err) return callback(err)
  292. callback(null, offer.id)
  293. })
  294. }
  295.  
  296. cancelOffer(tradeofferid, callback) {
  297. this._manager.getOffer(tradeofferid, function (err, offer) {
  298. if (err) return callback(err)
  299. offer.cancel(function (err) {
  300. if (err) return callback(err)
  301. callback(null, tradeofferid)
  302. })
  303. })
  304. }
  305.  
  306. checkStatus(tradeofferid, callback) {
  307. this._manager.getOffer(tradeofferid, (err, offer) => {
  308. if (err) return callback(err)
  309. offer.update(function (err) {
  310. if (err) return callback(err)
  311. callback(null, offer.state)
  312. })
  313. })
  314. }
  315. }
  316.  
  317. module.exports = SteamBot
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement