Advertisement
Guest User

keylist

a guest
Apr 14th, 2018
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.56 KB | None | 0 0
  1. // NPM shit
  2. require('console-stamp')(console, 'HH:MM:ss');
  3. const colors = require('colors');
  4. const SteamUser = require('steam-user');
  5. const SteamTotp = require('steam-totp');
  6. const SteamCommunity = require('steamcommunity');
  7. const TradeOfferManager = require ('steam-tradeoffer-manager');
  8. const TeamFortress2 = require('tf2');
  9. const request = require('request');
  10. var webshot = require('webshot');
  11.  
  12. const Prices = require('./configstuff/prices.json');
  13. const config = require('./configstuff/config.json');
  14. const bplists = require('./configstuff/listings.js')
  15.  
  16. const client = new SteamUser();
  17. const tf2 = new TeamFortress2(client);
  18. const community = new SteamCommunity();
  19. const manager = new TradeOfferManager ({
  20. steam: client,
  21. community: community,
  22. language: 'en'
  23. });
  24.  
  25. // Colours
  26. colors.setTheme({
  27. log: 'green',
  28. info: 'blue',
  29. warn: 'yellow',
  30. error: 'red'
  31. });
  32.  
  33. /*
  34. var options = {
  35. screenSize: {
  36. width: 1500
  37. },
  38. shotSize: {
  39. height: 'all',
  40. quality : 85
  41. },
  42. phantomConfig: {
  43. 'ignore-ssl-errors': 'true'
  44. },
  45. cookies: [
  46. {
  47. name: 'Steam_Language',
  48. value: 'english',
  49. domain: '.steamcommunity.com',
  50. path: '/'
  51. },
  52. {
  53. name: 'sessionid',
  54. value: 'd422cfcbb41de3f4aaa4fc8d',
  55. domain: '.steamcommunity.com',
  56. path: '/'
  57. },
  58. {
  59. name: 'steamLogin',
  60. value: '76561197969827060%7c%7c580E063115E83A5ADF1397DA4BFE0232445DCDFC',
  61. domain: '.steamcommunity.com',
  62. path: '/'
  63. },
  64. {
  65. name: 'steamLoginSecure',
  66. value: '76561197969827060%7c%7c9904D2962EDDA36BC88DAF497701AB24636A2838',
  67. domain: '.steamcommunity.com',
  68. path: '/'
  69. }
  70. ]
  71. };
  72. webshot('steamcommunity.com/id/ScrapWhore/tradeoffers', 'tradeoffer.png', options, function(err) {
  73. if (err) {
  74. console.log(err);
  75. } else {
  76. console.log('Saved!');
  77. }
  78. }); */
  79.  
  80. // Discord stuff
  81. var discord = require('discord.io');
  82.  
  83. var Dbot = new discord.Client({
  84. token: "Mzc3ODAxMDg4NzcxOTQ4NTQ0.DZBTUw.pObzG6keTOHiiRAR1Tf4aXZVHB8",
  85. autorun: true
  86. });
  87.  
  88. Dbot.on('ready', function() {
  89. console.log('Logged in as %s - %s\n', Dbot.username, Dbot.id);
  90. });
  91.  
  92. Dbot.on('message', function(user, userID, channelID, message, event) {
  93. if (message === "checkStatus") {
  94. Dbot.sendMessage({
  95. to: channelID,
  96. message: "I am still online :D"
  97. });
  98. }
  99. });
  100.  
  101. // Steam login
  102. const logOnOptions = {
  103. accountName: config.username,
  104. password: config.password,
  105. twoFactorCode: SteamTotp.generateAuthCode(config.sharedSecret)
  106. };
  107.  
  108. client.setOption("promptSteamGuardCode", false);
  109.  
  110. client.logOn(logOnOptions);
  111.  
  112. client.on('loggedOn', () => {
  113. console.log(colors.log('Logged into Steam! Sending a heartbeat every 120 seconds'));
  114. client.setPersona(SteamUser.Steam.EPersonaState.LookingToTrade);
  115. client.gamesPlayed(["Trading Items! 24/7!", 440]); /* 440 is TF2 */
  116. });
  117.  
  118. // Accepting friend requests + messages
  119. client.on('friendRelationship', (steamID, relationship) => {
  120. if (relationship === 2) {
  121. console.log(colors.log("Friend request received: " + steamID))
  122. client.addFriend(steamID);
  123. console.log(colors.log("Sending add message"))
  124. client.chatMessage(steamID, config.addMessage);
  125. }
  126. });
  127.  
  128. client.on("friendMessage", function(steamID, message) {
  129. if (message == "!info") {
  130. client.chatMessage(steamID, config.infoMessage);
  131. } else {
  132. client.chatMessage(steamID, "Command not recognized, type !info to get some information about the bot!");
  133. }
  134. });
  135.  
  136. client.on('webSession', (sessionid, cookies) => {
  137. manager.setCookies(cookies);
  138. community.setCookies(cookies);
  139. community.startConfirmationChecker(20000, config.identitySecret);
  140. });
  141.  
  142. // Handling trade offers
  143. function acceptOffer(offer, steamID) {
  144. offer.accept((err) => {
  145. community.checkConfirmations();
  146. console.warn(colors.warn("Accepted an offer from " + offer.partner))
  147. if (err) console.error(colors.error("There was an error accepting the offer: " + err));
  148. });
  149. }
  150.  
  151. function declineOffer(offer, steamID) {
  152. offer.decline((err) => {
  153. console.warn(colors.warn("Declined an offer from " + offer.partner))
  154. if (err) console.error(colors.error("There was an error declining the offer: " + err));
  155. });
  156. }
  157.  
  158. function roundRefined(float){
  159. var int = Math.floor(float);
  160. var r = Math.round((float-int) / 0.11 );
  161. return(int + (9 === r ? 1 : 0.11*r)).toFixed(2)
  162. }
  163.  
  164. function smoothRefined(float){
  165. return float%1!==0&&(float-Math.floor(float)).toPrecision(2)*100%11!==0?(float-0.01).toFixed(2):float;
  166. }
  167.  
  168. manager.on('receivedOfferChanged', function(offer, oldState) {
  169. console.log(`Offer #${offer.id} changed: ${TradeOfferManager.ETradeOfferState[oldState]} -> ${TradeOfferManager.ETradeOfferState[offer.state]}`);
  170. Dbot.sendMessage({
  171. to: '425024336529981460', message: `Offer #${offer.id} changed: ${TradeOfferManager.ETradeOfferState[oldState]} -> ${TradeOfferManager.ETradeOfferState[offer.state]}`});
  172. if (offer.state == TradeOfferManager.ETradeOfferState.Accepted) {
  173. offer.getExchangeDetails((err, status, tradeInitTime, receivedItems, sentItems) => {
  174. if (err) {
  175. console.log(`Error ${err}`);
  176. return;
  177. }
  178.  
  179. // Create arrays of just the new assetids using Array.prototype.map and arrow functions
  180. let newReceivedItems = receivedItems.map(item => item.market_hash_name);
  181. let newSentItems = sentItems.map(item => item.market_hash_name);
  182.  
  183. Dbot.sendMessage({
  184. to: '425024336529981460', message: `**status:** ${TradeOfferManager.ETradeStatus[status]} \n **Received items:** ${newReceivedItems.join(', ')} \n **Sent Items:** ${newSentItems.join(', ')}`
  185. });
  186.  
  187. var listings = [];
  188.  
  189. var options = {
  190. uri:"https://backpack.tf/api/classifieds/list/v1",
  191. json: true,
  192. method: "POST",
  193. gzip: true,
  194. qs: {
  195. token: config.token
  196. }
  197. }
  198.  
  199. let CKey;
  200.  
  201. if(Prices["Mann Co. Supply Crate Key"])
  202. CKey = roundRefined(Prices["Mann Co. Supply Crate Key"].sell * 0.11);
  203.  
  204. receivedItems.forEach(item => {
  205. if(Prices[item.market_hash_name].sell){
  206.  
  207. if(!CKey)
  208. throw new Error("Cannot use undefined key value.");
  209.  
  210. let metal = roundRefined(Prices[item.market_hash_name].sell * 0.11);
  211. let keys = CKey && metal > CKey ? Math.floor(metal / CKey) : 0;
  212. metal = keys > 0 ? smoothRefined(metal % CKey) : metal;
  213.  
  214. listings.push({
  215. intent: 1,
  216. id: item.new_assetid,
  217. offers: 1,
  218. buyout: 1,
  219. promoted: 0,
  220. currencies: {
  221. keys: keys,
  222. metal: metal
  223. },
  224. details: config.listingComment
  225. });
  226. }
  227. });
  228.  
  229. setTimeout(() => {
  230. refreshInventory((err) => {
  231. if(!err){
  232. options["body"] = {listings: listings};
  233. request(options, function (error, response, body) {
  234. if(!error){
  235. console.log("Created new Sell Listings");
  236. console.log(body);
  237. } else {
  238. console.log(error);
  239. }
  240. });
  241. }
  242. });
  243. }, 1000 * 60 * 5);
  244.  
  245. })
  246. }
  247. });
  248.  
  249. function refreshInventory(callback){
  250. request("https://backpack.tf/_inventory/"+client.steamID.getSteamID64()+"?time=&source=steam", (err, response, body) => {
  251. if(!err && response.statusCode === 200) {
  252. callback(null);
  253. } else {
  254. return refreshInventory(callback);
  255. }
  256. });
  257. }
  258.  
  259. function processOffer(offer) {
  260. if (offer.isGlitched() || offer.state === 11) {
  261. console.warn(colors.warn("Offer was unavailable, declining."));
  262. declineOffer(offer);
  263. Dbot.sendMessage({
  264. to: '425024336529981460', message: "Declined an offer, offer unavailable"});
  265. } else if (offer.partner.getSteamID64() === config.owner1) {
  266. console.info(colors.info("Offer recieved from Procave, accepting."))
  267. acceptOffer(offer);
  268. Dbot.sendMessage({
  269. to: '425024336529981460', message: "Offer recieved from Procave, accepting."});
  270. } else if (offer.partner.getSteamID64() === config.owner2) {
  271. console.info(colors.info("Offer recieved from Zeus, accepting."))
  272. acceptOffer(offer);
  273. Dbot.sendMessage({
  274. to: '425024336529981460', message: "Offer recieved from Zeus, accepting."});
  275. } else if (offer.partner.getSteamID64() === config.owner3) {
  276. console.info(colors.info("Offer recieved from Lemons, accepting."))
  277. acceptOffer(offer);
  278. Dbot.sendMessage({
  279. to: '425024336529981460', message: "Offer recieved from Lemons, accepting."});
  280. } else {
  281. var ourItems = offer.itemsToGive;
  282. var theirItems = offer.itemsToReceive;
  283. var ourValue = 0;
  284. var theirValue = 0;
  285. for (var i in ourItems) {
  286. var item = ourItems[i].market_name;
  287. if(Prices[item]) {
  288. ourValue += Prices[item].sell
  289. } else {
  290. console.warn(colors.warn("Item we're giving doesn't have a value: declining."))
  291. ourValue+= 99999;
  292. }
  293. }
  294. for (var i in theirItems) {
  295. var item = theirItems[i].market_name;
  296. if(Prices[item]) {
  297. theirValue += Prices[item].buy;
  298. } else {
  299. console.warn(colors.warn("Item they're giving doesn't have a value: declining."))
  300. }
  301. }
  302. console.info(colors.info("Our value: " + ourValue));
  303. console.info(colors.info("Their value: " + theirValue));
  304.  
  305. if (ourValue <= theirValue) {
  306. acceptOffer(offer);
  307. client.chatMessage('76561198144346135', "Accepted an offer, their value: " + theirValue+ " and our value: " + ourValue);
  308. Dbot.sendMessage({
  309. to: '425024336529981460', message: "Accepted an offer, their value: " + theirValue+ " and our value: " + ourValue});
  310. } else {
  311. declineOffer(offer);
  312. client.chatMessage('76561198144346135', "Declined an offer, their value: " + theirValue+ " and our value: " + ourValue);
  313. Dbot.sendMessage({
  314. to: '425024336529981460', message: "Declined an offer, their value: " + theirValue+ " and our value: " + ourValue});
  315. }
  316. }
  317. }
  318.  
  319. manager.on('newOffer', (offer) => {
  320. console.info(colors.info("New offer received from " + offer.partner))
  321. processOffer(offer);
  322. if (offer.itemsToGive.length === 0) {
  323. offer.accept((err, status) => {
  324. if (err) {
  325. console.error(colors.error(err));
  326. } else {
  327. console.info(colors.info(`Donation accepted.`));
  328. }
  329. });
  330. }
  331. });
  332.  
  333. // heartbeat
  334.  
  335. var timeout = setInterval(function() {
  336. heartbeat();
  337. }, 120000); // 2 minutes
  338.  
  339. function heartbeat () {
  340. request.post(
  341. 'https://backpack.tf/api/aux/heartbeat/v1',
  342. { json:
  343. {
  344. token: config.token, // <--
  345. automatic: 'all',
  346. i_understand_the_risk: 'true',
  347. }
  348. },
  349. function (error, response, body) {
  350. console.log(body);
  351. }
  352. );
  353. }
  354.  
  355. // getPrices
  356.  
  357. var timeoutprices = setInterval(function() {
  358. getPrices();
  359. }, 300000); // 5 minutes
  360.  
  361. function getPrices () {
  362. request.get(
  363. 'https://backpack.tf/api/IGetPrices/v4',
  364. { json:
  365. {
  366. format: 'json',
  367. callback: 'getPrices',
  368. key: config.apiKey, // <--
  369. raw: "1",
  370. }
  371. });
  372. }
  373.  
  374. // getCurrency
  375.  
  376. var timeoutcurrency = setInterval(function() {
  377. getCurrency();
  378. }, 300000); // 5 minutes
  379.  
  380. function getCurrency () {
  381. request.get(
  382. 'https://backpack.tf/api/IGetCurrencies/v1',
  383. { json:
  384. {
  385. format: 'json',
  386. callback: 'getCurrency',
  387. key: config.apiKey, // <--
  388. raw: "1",
  389. }
  390. }
  391. );
  392. }
  393.  
  394. // listings
  395.  
  396. var timeoutlistings = setInterval(function() {
  397. postListings();
  398. }, 2100000); // 2100000 MS = 35 minutes
  399.  
  400. function postListings () {
  401. request.post(
  402. 'https://backpack.tf/api/classifieds/list/v1',
  403. { json:
  404. {
  405. token: config.token, // <--
  406. listings: bplists
  407. },
  408. },
  409. function (error, response, body) {
  410. console.log(body);
  411. }
  412. );
  413. }
  414. // lists on startup
  415. postListings();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement