Advertisement
Guest User

index.js

a guest
Nov 22nd, 2017
676
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 202.47 KB | None | 0 0
  1. let SteamUser = require("steam-user"),
  2. SteamTotp = require("steam-totp"),
  3. TradeOfferManager = require("steam-tradeoffer-manager"),
  4. SteamCommunity = require("steamcommunity"),
  5. Utils = require("./utils.js"),
  6. CONFIG = require("./SETTINGS/config.js"),
  7. allCards = {},
  8. botSets = {},
  9. fs = require("fs"),
  10. users = {},
  11. userMsgs = {},
  12. SID64REGEX = new RegExp(/^[0-9]{17}$/),
  13. chatLogs = "",
  14. userLogs = {},
  15. totalBotSets = 0;
  16. fs = require('fs'),
  17. gamesstockfilename = './games.json',
  18. jsonfile = require('jsonfile');
  19.  
  20. let client = new SteamUser(),
  21. manager = new TradeOfferManager({
  22. "steam": client,
  23. "language": "en",
  24. "pollInterval": "10000",
  25. "cancelTime": "7200000" // 2 hours in ms
  26. }),
  27. community = new SteamCommunity();
  28.  
  29. fs.readFile("./UserData/Users.json", (ERR, DATA) => {
  30. if (ERR) {
  31. console.log("## An error occurred while getting Users: " + ERR);
  32. } else {
  33. users = JSON.parse(DATA);
  34. }
  35. });
  36.  
  37. Utils.getCardsInSets((ERR, DATA) => {
  38. if (!ERR) {
  39. allCards = DATA;
  40. console.log("Card data loaded. [" + Object.keys(DATA).length + "]");
  41. } else {
  42. console.log("An error occurred while getting cards: " + ERR);
  43. }
  44. });
  45.  
  46. setInterval(() => {
  47. for (let i = 0; i < Object.keys(users).length; i++) {
  48. if (users[Object.keys(users)[i]].idleforhours >= CONFIG.MAXHOURSADDED) {
  49. client.chatMessage(Object.keys(users)[i], "Hi, you have been inactive on my friends list for too long. If you wish to use this bot again re-add it.");
  50. client.removeFriend(Object.keys(users)[i]);
  51. delete users[Object.keys(users)[i]];
  52. fs.writeFile("./UserData/Users.json", JSON.stringify(users), (ERR) => {
  53. if (ERR) {
  54. console.log("## An error occurred while writing UserData file: " + ERR);
  55. }
  56. });
  57. } else {
  58. users[Object.keys(users)[i]].idleforhours += 1;
  59. fs.writeFile("./UserData/Users.json", JSON.stringify(users), (ERR) => {
  60. if (ERR) {
  61. console.log("## An error occurred while writing UserData file: " + ERR);
  62. }
  63. });
  64. }
  65. }
  66. }, 1000 * 60 * 60);
  67.  
  68. setInterval(() => {
  69. for (let i = 0; i < Object.keys(userMsgs).length; i++) {
  70. if (userMsgs[Object.keys(userMsgs)[i]] > CONFIG.MAXMSGPERSEC) {
  71. client.chatMessage(Object.keys(userMsgs)[i], "You have been removed for spamming. Another offense will get you blocked.");
  72. client.removeFriend(Object.keys(userMsgs)[i]);
  73. for (let j = 0; j < CONFIG.ADMINS.length; j++) {
  74. client.chatMessage(CONFIG.ADMINS[j], "User #" + Object.keys(userMsgs)[i] + " has been removed for spamming. To block him use !block [STEAMID64]");
  75. }
  76. }
  77. }
  78. userMsgs = {};
  79. }, 1000);
  80.  
  81. client.logOn({
  82. accountName: CONFIG.USERNAME,
  83. password: CONFIG.PASSWORD,
  84. twoFactorCode: SteamTotp.getAuthCode(CONFIG.SHAREDSECRET),
  85. rememberPassword: true
  86. });
  87.  
  88. client.on("loggedOn", (details, parental) => {
  89. client.getPersonas([client.steamID], (personas) => {
  90. console.log("## Logged in as #" + client.steamID + " (" + personas[client.steamID].player_name + ")");
  91. });
  92. client.setPersona(1);
  93. });
  94.  
  95. client.on("webSession", (sessionID, cookies) => {
  96. manager.setCookies(cookies, (ERR) => {
  97. if (ERR) {
  98. console.log("## An error occurred while setting cookies.");
  99. } else {
  100. console.log("## Websession created and cookies set.");
  101. }
  102. });
  103. community.setCookies(cookies);
  104. community.startConfirmationChecker(10000, CONFIG.IDENTITYSECRET);
  105. Utils.getInventory(client.steamID.getSteamID64(), community, (ERR, DATA) => {
  106. console.log("DEBUG#INVLOADED");
  107. if (!ERR) {
  108. let s = DATA;
  109. Utils.getSets(s, allCards, (ERR, DATA) => {
  110. console.log("DEBUG#SETSLOADED");
  111. if (!ERR) {
  112. botSets = DATA;
  113. console.log("## Bot's sets loaded.");
  114. let botNSets = 0;
  115. for (let i = 0; i < Object.keys(botSets).length; i++) {
  116. botNSets += botSets[Object.keys(botSets)[i]].length;
  117. }
  118. totalBotSets = botNSets;
  119. let playThis = CONFIG.PLAYGAMES;
  120. if (CONFIG.PLAYGAMES && typeof(CONFIG.PLAYGAMES[0]) === "string") {
  121. playThis[0] = parseString(playThis[0], totalBotSets);
  122. }
  123. client.gamesPlayed(playThis);
  124. } else {
  125. console.log("## An error occurred while getting bot sets: " + ERR);
  126. process.exit();
  127. }
  128. });
  129. } else {
  130. console.log("## An error occurred while getting bot inventory: " + ERR);
  131. }
  132. });
  133. });
  134.  
  135. community.on("sessionExpired", (ERR) => {
  136. console.log("## Session Expired. Relogging.");
  137. client.webLogOn();
  138. });
  139.  
  140. client.on("friendMessage", (SENDER, MSG) => {
  141. if (userLogs[SENDER.getSteamID64()]) {
  142. userLogs[SENDER.getSteamID64()].push(MSG);
  143. } else {
  144. userLogs[SENDER.getSteamID64()] = [];
  145. userLogs[SENDER.getSteamID64()].push(MSG);
  146. }
  147. fs.writeFile("./ChatLogs/UserLogs/" + SENDER.getSteamID64() + "-log-" + new Date().getDate() + "-" + new Date().getMonth() + "-" + new Date().getFullYear() + ".json", JSON.stringify({logs: userLogs[SENDER.getSteamID64()]}), (ERR) => {
  148. if (ERR) {
  149. console.log("## An error occurred while writing UserLogs file: " + ERR);
  150. }
  151. });
  152. chatLogs += SENDER.getSteamID64() + " : " + MSG + "\n";
  153. fs.writeFile("./ChatLogs/FullLogs/log-" + new Date().getDate() + "-" + new Date().getMonth() + "-" + new Date().getFullYear() + ".txt", chatLogs, (ERR) => {
  154. if (ERR) {
  155. console.log("## An error occurred while writing FullLogs file: " + ERR);
  156. }
  157. });
  158. if (Object.keys(users).indexOf(SENDER.getSteamID64()) < 0) {
  159. users[SENDER.getSteamID64()] = {};
  160. users[SENDER.getSteamID64()].idleforhours = 0;
  161. fs.writeFile("./UserData/Users.json", JSON.stringify(users), (ERR) => {
  162. if (ERR) {
  163. console.log("## An error occurred while writing UserData file: " + ERR);
  164. }
  165. });
  166. } else {
  167. users[SENDER.getSteamID64()].idleforhours = 0;
  168. }
  169. if (userMsgs[SENDER.getSteamID64()]) {
  170. userMsgs[SENDER.getSteamID64()]++;
  171. } else {
  172. userMsgs[SENDER.getSteamID64()] = 1;
  173. }
  174. if (MSG.toUpperCase().indexOf("!LEVEL") >= 0) {
  175. let n = parseInt(MSG.toUpperCase().replace("!LEVEL ", ""));
  176. if (!isNaN(n) && parseInt(n) > 0) {
  177. if (n <= CONFIG.MESSAGES.MAXLEVEL) {
  178. Utils.getBadges(SENDER.getSteamID64(), (ERR, DATA, CURRENTLEVEL, XPNEEDED) => {
  179. if (!ERR) {
  180. if (DATA) {
  181. if (n > CURRENTLEVEL) {
  182. let s = 0,
  183. l = 0;
  184. for (let i = 0; i < (n - CURRENTLEVEL); i++) {
  185. s += parseInt((CURRENTLEVEL + l) / 10) + 1;
  186. l++;
  187. }
  188. client.chatMessage(SENDER, "To get to level " + n + " you will need " + (s - Math.floor(XPNEEDED / 100)) + " sets. That would cost " + parseInt((s - Math.floor(XPNEEDED / 100)) / CONFIG.CARDS.CSGO.BUY1FORAMOUNTOFSETS * 100) / 100 + " keys.");
  189. } else {
  190. client.chatMessage(SENDER, "Please provide a valid level.");
  191. }
  192. } else {
  193. client.chatMessage(SENDER, "Your level could not be retrieved. Make sure your Steam Profile is public and try again.");
  194. }
  195. } else {
  196. console.log("## An error occurred while getting badge data: " + ERR);
  197. client.chatMessage(SENDER, "An error occurred while loading your badges. Please try again later.");
  198. }
  199. });
  200. } else {
  201. client.chatMessage(SENDER, "Please try a lower level.");
  202. }
  203. } else {
  204. client.chatMessage(SENDER, "Please provide a valid level.");
  205. }
  206. }
  207. else if (MSG.toUpperCase() === "!HELP") {
  208. client.chatMessage(SENDER, "\nList of commands:" +
  209. "\n\n!owner - show the admin profile, if you have any problems you may contact me!" +
  210. "\n\n!credits - show the developers of this bot." +
  211. "\n\n!level [your dream level] - calculate how many sets and how many keys it will cost to desired level" +
  212. "\n\n!check - show how many sets the bot have available and how much you can craft" +
  213. "\n\n!check [amount] - show how many sets and which level you would reach for a specific amount of keys" +
  214. "\n\n!checktf [amount] - show how many sets and which level you would reach for a specific amount of keys" +
  215. "\n\n!checkgems [amount] - show how many sets and which level you would reach for a specific amount of gems" +
  216. "\n\n!buy [amount of CS:GO keys] - use to buy that amount of CS:GO keys for sets you dont have, following the current BOT rate" +
  217. "\n\n!buytf [amount of CS:GO keys] - use to buy that amount of TF2 keys for sets you dont have, following the current BOT rate" +
  218. "\n\n!buygems [amount of sets] - use to buy that amount of sets for gems, following the current BOT rate" +
  219. "\n\n!buyany [amount of CS:GO keys] - use to buy that amount of CS:GO keys for any sets, following the current BOT rate" +
  220. "\n\n!buyanytf [amount of TF2 keys] - use to buy that amount of TF2 keys for any sets, following the current BOT rate" +
  221. "\n\n!buyanygems [amount of sets] - use to buy that amount of any sets for gems, following the current BOT rate" +
  222. "\n\n!buyone [amount of CS:GO keys] - use this if you are a badge collector. BOT will send only one set of each game, following the current BOT rate" +
  223. "\n\n!buyonetf [amount of TF2 keys] - use this if you are a badge collector. BOT will send only one set of each game, following the current BOT rate" +
  224. "\n\n!buyonegems [amount of sets] - use this if you are a badge collector. sames as !buyone , buy you pay with gems!" +
  225. "\n\n!sell [amount of CS:GO keys] - use to sell your sets for CS:GO Key(s)" +
  226. "\n\n!sellgems [amount of sets] - use to sell your sets for Gems" +
  227. "\n\n!selltf [amount of TF2 keys] - use to sell your sets for TF2 Key(s)" +
  228. "\n\n!sellcheck [amount of CS:GO keys] - use to check information about sets you can sell"
  229. );
  230. }
  231. else if (MSG.toUpperCase().indexOf("!BUYONECHECK") >= 0) {
  232. client.chatMessage(SENDER, "Loading badges...");
  233. Utils.getBadges(SENDER.getSteamID64(), (ERR, DATA) => {
  234. if (!ERR) {
  235. let b = {}; // List with badges that CAN still be crafted
  236. if (DATA) {
  237. for (let i = 0; i < Object.keys(DATA).length; i++) {
  238. if (DATA[Object.keys(DATA)[i]] < 6) {
  239. b[Object.keys(DATA)[i]] = 5 - DATA[Object.keys(DATA)[i]];
  240. }
  241. }
  242. } else {
  243. client.chatMessage(SENDER.getSteamID64(), "Your badges are empty, sending an offer without checking badges.");
  244. }
  245. // console.log(b);
  246. // TODO: COUNT AMOUNT OF SETS BOT CAN GIVE HIM
  247. // 1: GET BOTS CARDS. DONE
  248. // 2: GET PLAYER's BADGES. DONE
  249. // 3: MAGIC
  250. let hisMaxSets = 0,
  251. botNSets = 0;
  252. // Loop for sets he has partially completed
  253. for (let i = 0; i < Object.keys(b).length; i++) {
  254. if (botSets[Object.keys(b)[i]] && botSets[Object.keys(b)[i]].length >= 5 - b[Object.keys(b)[i]].length) {
  255. hisMaxSets += 5 - b[Object.keys(b)[i]].length;
  256. }
  257. }
  258. // Loop for sets he has never crafted
  259. for (let i = 0; i < Object.keys(botSets).length; i++) {
  260. if (Object.keys(b).indexOf(Object.keys(botSets)[i]) < 0) {
  261. if (botSets[Object.keys(botSets)[i]].length >= 1) {
  262. hisMaxSets += 1;
  263. }
  264. }
  265. botNSets += botSets[Object.keys(botSets)[i]].length;
  266. }
  267. totalBotSets = botNSets;
  268. let playThis = CONFIG.PLAYGAMES;
  269. if (CONFIG.PLAYGAMES && typeof(CONFIG.PLAYGAMES[0]) === "string") {
  270. playThis[0] = parseString(playThis[0], totalBotSets);
  271. }
  272. client.gamesPlayed(playThis);
  273. client.chatMessage(SENDER, "There are currently sets from " + Object.keys(botSets).length + " different games, of which you have not crafted " + hisMaxSets + ". This would cost " + parseInt(hisMaxSets / CONFIG.CARDS.CSGO.BUY1FORAMOUNTOFSETS * 100) / 100 + " keys.");
  274. } else {
  275. client.chatMessage(SENDER, "An error occurred while getting your badges. Please try again.");
  276. console.log("An error occurred while getting badges: " + ERR);
  277. }
  278. });
  279. }
  280. else if (MSG.toUpperCase().indexOf("!SELLCHECK") >= 0) {
  281. let n = parseInt(MSG.toUpperCase().replace("!SELLCHECK ", ""));
  282. client.chatMessage(SENDER, "Loading inventory...");
  283. Utils.getInventory(SENDER.getSteamID64(), community, (ERR, DATA) => {
  284. console.log("DEBUG#INVLOADED");
  285. if (!ERR) {
  286. let s = DATA;
  287. Utils.getSets(s, allCards, (ERR, DATA) => {
  288. console.log("DEBUG#SETSLOADED");
  289. if (!ERR) {
  290. // console.log(b);
  291. // TODO: COUNT AMOUNT OF SETS BOT CAN GIVE HIM
  292. // 1: GET BOTS CARDS. DONE
  293. // 2: GET PLAYER's BADGES. DONE
  294. // 3: MAGIC
  295. let hisMaxSets = 0,
  296. botNSets = 0;
  297. // Loop for sets he has partially completed
  298. // Loop for sets he has never crafted
  299. for (let i = 0; i < Object.keys(DATA).length; i++) {
  300. if (DATA[Object.keys(DATA)[i]].length >= 5) {
  301. hisMaxSets += 5;
  302. } else {
  303. hisMaxSets += DATA[Object.keys(DATA)[i]].length;
  304. }
  305. botNSets += DATA[Object.keys(DATA)[i]].length;
  306. }
  307. totalBotSets = botNSets;
  308. let playThis = CONFIG.PLAYGAMES;
  309. if (CONFIG.PLAYGAMES && typeof(CONFIG.PLAYGAMES[0]) === "string") {
  310. playThis[0] = parseString(playThis[0], totalBotSets);
  311. }
  312. if (botNSets === 0) {
  313. client.chatMessage(SENDER, "You currently don't have any available set which the bot can buy.");
  314. }
  315. else {
  316. client.gamesPlayed(playThis);
  317. client.chatMessage(SENDER, "You currently have " + botNSets + " set(s) available which the bot can buy. " +
  318. "\r\nFor all of them the bot will pay you:" +
  319. "\r\n" + parseInt(botNSets / CONFIG.CARDS.CSGO.give_one_for * 100) / 100 + " CSGO Key(s) or" +
  320. "\r\n" + parseInt(botNSets / CONFIG.CARDS.TF2.give_one_for * 100) / 100 + " TF2 Key(s) or" +
  321. "\r\n" + parseInt(botNSets * CONFIG.CARDS.GEMS.give_one_set_for * 100) / 100 + " Gems."
  322. );
  323. }
  324. } else {
  325. console.log("## An error occurred while getting user sets: " + ERR);
  326. }
  327. });
  328. } else {
  329. console.log("## An error occurred while getting user inventory: " + ERR);
  330. }
  331. });
  332. }
  333. else if (MSG.toUpperCase() === "!CHECKGAMES") {
  334. let gamesStock = jsonfile.readFileSync(gamesstockfilename);
  335. let gamesstring = "";
  336. let length = Object.keys(gamesStock.stock).length;
  337. if (length > 0) {
  338. for (let i = 0; i < length; i++) {
  339. let j = Object.keys(gamesStock.stock)[i];
  340. if (gamesStock.stock[j].keys.length > 0)
  341. gamesstring += "[" + j + "] " + gamesStock.stock[j].name + " | Stock: " + gamesStock.stock[j].keys.length + " | Price: " + gamesStock.stock[j].price + " Key(s)\r\n";
  342. }
  343. client.chatMessage(SENDER, "\nWe have these games in stock:\r\n\n" +
  344. gamesstring +
  345. "\n!buygame [AppID] - Buy game with given AppID according to list upper."
  346. );
  347. } else {
  348. client.chatMessage(SENDER, "\nWe have currently don't have games in stock. Try again later...");
  349. }
  350. }
  351. else if (MSG.toUpperCase().indexOf("!BUYGAME") >= 0) {
  352. let n = MSG.toUpperCase().replace("!BUYGAME ", "");
  353. if (!isNaN(n) && parseInt(n) > 0) {
  354. let gamesStock = jsonfile.readFileSync(gamesstockfilename);
  355. let length = Object.keys(gamesStock.stock).length;
  356. if (length > 0) {
  357. let stock = gamesStock.stock;
  358. if (typeof stock[n] !== 'undefined') {
  359. client.chatMessage(SENDER, "You're going to buy game \"" + stock[n].name + "\" for " + stock[n].price + " Key(s).\r\nProcessing your request...");
  360. let t = manager.createOffer(SENDER.getSteamID64());
  361. t.getUserDetails((ERR, ME, THEM) => {
  362. let price = stock[n].price;
  363. if (ERR) {
  364. console.log("## An error occurred while getting trade holds: " + ERR);
  365. client.chatMessage(SENDER, "An error occurred while getting your trade holds. Please try again");
  366. } else if (ME.escrowDays === 0 && THEM.escrowDays === 0) {
  367. let theirKeys = [];
  368. manager.getUserInventoryContents(SENDER.getSteamID64(), CONFIG.CSGOGAME, 2, true, (ERR, INV, CURR) => {
  369. if (ERR) {
  370. console.log("## An error occurred while getting inventory: " + ERR);
  371. client.chatMessage(SENDER, "An error occurred while loading your inventory. Please try later");
  372. } else {
  373. console.log("DEBUG#INV LOADED");
  374. if (!ERR) {
  375. console.log("DEBUG#INV LOADED NOERR");
  376. for (let i = 0; i < INV.length; i++) {
  377. if (theirKeys.length < price && CONFIG.ACCEPTEDKEYS.indexOf(INV[i].market_hash_name) >= 0) {
  378. theirKeys.push(INV[i]);
  379. }
  380. }
  381. if (theirKeys.length !== price) {
  382. client.chatMessage(SENDER, "You do not have enough keys.");
  383. } else {
  384. console.log("DEBUG#SENDING");
  385. t.addTheirItems(theirKeys);
  386. t.data("commandused", "BuyGame");
  387. t.setMessage("After you confirm trade, I will send you Steam CD Key to chat!");
  388. t.data("index", n);
  389. t.send((ERR, STATUS) => {
  390. if (ERR) {
  391. client.chatMessage(SENDER, "An error occurred while sending your trade. Steam Trades could be down. Please try again later.");
  392. console.log("## An error occurred while sending trade: " + ERR);
  393. } else {
  394. client.chatMessage(SENDER, "Trade Sent! Confirming it...");
  395. console.log("## Trade offer sent");
  396. client.chatMessage(SENDER, "After you confirm trade, I will send you Steam CD Key to this chat!");
  397. client.chatMessage(SENDER, "Don't close me!");
  398. }
  399. });
  400. }
  401. }
  402. }
  403. });
  404. }
  405. });
  406. } else {
  407. client.chatMessage(SENDER, "Game with this AppID doesn't exists. Check available games by !checkgames command");
  408. }
  409. } else {
  410. client.chatMessage(SENDER, "We have currently don't have games in stock. Try again later...");
  411. }
  412. } else {
  413. client.chatMessage(SENDER, "Please provide a valid game Index.");
  414. }
  415. }
  416. else if (MSG.toUpperCase().indexOf("!CHECK") >= 0) {
  417. let n = parseInt(MSG.toUpperCase().replace("!CHECK ", ""));
  418. if (!isNaN(n) && parseInt(n) > 0) {
  419. client.chatMessage(SENDER, "\n\nThe currently prices are:" +
  420. "\r\nWith " + n + " CS:GO Key(s) you can get " + n * CONFIG.CARDS.CSGO.buy_sets_by_one + " set(s)." +
  421. "\r\nWith " + n + " TF2 Key(s) you can get " + n * CONFIG.CARDS.TF2.buy_sets_by_one + " set(s)." +
  422. "\r\n" + n + " set(s) you can get for " + n * CONFIG.CARDS.GEMS.buy_one_set_for + " Gems." +
  423. "\r\n" +
  424. "\r\nAlso, we're buying sets:" +
  425. "\r\nFor " + n * CONFIG.CARDS.CSGO.give_one_for + " set(s) you can get " + n + " CSGO Key(s)." +
  426. "\r\nFor " + n * CONFIG.CARDS.TF2.give_one_for + " set(s) you can get " + n + " TF2 Key{s)." +
  427. "\r\nFor " + n * CONFIG.CARDS.GEMS.give_one_set_for + " Gems you can get " + n + " Set{s)."
  428. );
  429. } else {
  430. if (Object.keys(botSets).length > 0) {
  431. client.chatMessage(SENDER, "Loading badges...");
  432. Utils.getBadges(SENDER.getSteamID64(), (ERR, DATA) => {
  433. if (!ERR) {
  434. let b = {}; // List with badges that CAN still be crafted
  435. if (DATA) {
  436. for (let i = 0; i < Object.keys(DATA).length; i++) {
  437. if (DATA[Object.keys(DATA)[i]] < 6) {
  438. b[Object.keys(DATA)[i]] = 5 - DATA[Object.keys(DATA)[i]];
  439. }
  440. }
  441. } else {
  442. client.chatMessage(SENDER.getSteamID64(), "Your badges are empty, sending an offer without checking badges.");
  443. }
  444. // console.log(b);
  445. // TODO: COUNT AMOUNT OF SETS BOT CAN GIVE HIM
  446. // 1: GET BOTS CARDS. DONE
  447. // 2: GET PLAYER's BADGES. DONE
  448. // 3: MAGIC
  449. let hisMaxSets = 0,
  450. botNSets = 0;
  451. // Loop for sets he has partially completed
  452. for (let i = 0; i < Object.keys(b).length; i++) {
  453. if (botSets[Object.keys(b)[i]] && botSets[Object.keys(b)[i]].length >= 5 - b[Object.keys(b)[i]].length) {
  454. hisMaxSets += 5 - b[Object.keys(b)[i]].length;
  455. }
  456. }
  457. // Loop for sets he has never crafted
  458. for (let i = 0; i < Object.keys(botSets).length; i++) {
  459. if (Object.keys(b).indexOf(Object.keys(botSets)[i]) < 0) {
  460. if (botSets[Object.keys(botSets)[i]].length >= 5) {
  461. hisMaxSets += 5;
  462. } else {
  463. hisMaxSets += botSets[Object.keys(botSets)[i]].length;
  464. }
  465. }
  466. botNSets += botSets[Object.keys(botSets)[i]].length;
  467. }
  468. totalBotSets = botNSets;
  469. let playThis = CONFIG.PLAYGAMES;
  470. if (CONFIG.PLAYGAMES && typeof(CONFIG.PLAYGAMES[0]) === "string") {
  471. playThis[0] = parseString(playThis[0], totalBotSets);
  472. }
  473. client.gamesPlayed(playThis);
  474. client.chatMessage(SENDER, "There are currently " + hisMaxSets + "/" + botNSets + " sets available which you have not fully crafted yet." +
  475. "Buying all of them will cost you:" +
  476. "\r\n" + parseInt(hisMaxSets / CONFIG.CARDS.CSGO.buy_sets_by_one * 100) / 100 + " CSGO Key(s) or" +
  477. "\r\n" + parseInt(hisMaxSets / CONFIG.CARDS.TF2.buy_sets_by_one * 100) / 100 + " TF2 Key(s) or" +
  478. "\r\n" + parseInt(hisMaxSets * CONFIG.CARDS.GEMS.buy_one_set_for * 100) / 100 + " Gems.");
  479. } else {
  480. client.chatMessage(SENDER, "An error occurred while getting your badges. Please try again.");
  481. console.log("An error occurred while getting badges: " + ERR);
  482. }
  483. });
  484. } else {
  485. client.chatMessage(SENDER, "Please try again later.");
  486. }
  487. }
  488. }
  489.  
  490. else if (MSG.toUpperCase().indexOf("!SELLTF") >= 0) {
  491. if (botSets) {
  492. let n = parseInt(MSG.toUpperCase().replace("!SELLTF ", "")),
  493. amountofsets = n * CONFIG.CARDS.TF2.give_one_for;
  494. if (!isNaN(n) && parseInt(n) > 0) {
  495. if (n <= CONFIG.MESSAGES.MAXSELL) {
  496. client.chatMessage(SENDER, "Processing your request.");
  497. let botKeys = [],
  498. t = manager.createOffer(SENDER.getSteamID64());
  499. t.getUserDetails((ERR, ME, THEM) => {
  500. if (ERR) {
  501. console.log("## An error occurred while getting trade holds: " + ERR);
  502. client.chatMessage(SENDER, "An error occurred while getting your trade holds. Please try again");
  503. } else if (ME.escrowDays === 0 && THEM.escrowDays === 0) {
  504. manager.getUserInventoryContents(client.steamID.getSteamID64(), CONFIG.TFGAME, 2, true, (ERR, INV, CURR) => {
  505. if (ERR) {
  506. console.log("## An error occurred while getting bot inventory: " + ERR);
  507. client.chatMessage(SENDER, "An error occurred while loading the bot's inventory. Please try again.");
  508. } else {
  509. for (let i = 0; i < INV.length; i++) {
  510. if (botKeys.length < n && CONFIG.TFACCEPTEDKEYS.indexOf(INV[i].market_hash_name) >= 0) {
  511. botKeys.push(INV[i]);
  512. }
  513. }
  514. if (botKeys.length !== n) {
  515. client.chatMessage(SENDER, "The bot does not have enough keys.");
  516. } else {
  517. let amountofB = amountofsets;
  518. Utils.getInventory(SENDER.getSteamID64(), community, (ERR, DATA) => {
  519. if (!ERR) {
  520. let s = DATA;
  521. Utils.getSets(s, allCards, (ERR, DDATA) => {
  522. if (!ERR) {
  523. sortSetsByAmountB(s, (DATA) => {
  524. let setsSent = {};
  525. firsttLoop: for (let i = 0; i < DATA.length; i++) {
  526. console.log(setsSent);
  527. console.log(DATA[i]);
  528. if (DDATA[DATA[i]]) {
  529. for (let j = 0; j < DDATA[DATA[i]].length; j++) {
  530. if (amountofB > 0) {
  531. if ((setsSent[DATA[i]] && setsSent[DATA[i]] < CONFIG.CARDS.TF2.MAXSETSELL) || !setsSent[DATA[i]]) {
  532. t.addTheirItems(DDATA[DATA[i]][j]);
  533. console.log("DEBUG#LOOP #2 CONTINUE: ITEM ADD");
  534. amountofB--;
  535. if (!setsSent[DATA[i]]) {
  536. setsSent[DATA[i]] = 1;
  537. } else {
  538. setsSent[DATA[i]] += 1;
  539. }
  540. } else {
  541. console.log("DEBUG#LOOP #2 CONTINUE: RETURN");
  542. continue firsttLoop;
  543. }
  544. } else {
  545. console.log("DEBUG#LOOP #2 CONTINUE: RETURN");
  546. continue firsttLoop;
  547. }
  548. }
  549. } else {
  550. console.log("DEBUG#LOOP #2 CONTINUE: RETURN 2");
  551. continue firsttLoop;
  552. }
  553. }
  554. });
  555. if (amountofB > 0) {
  556. client.chatMessage(SENDER, "You do not have enough sets, (this bot only accepts " + CONFIG.CARDS.TF2.MAXSETSELL + " sets per set type at a time). Please try again later.");
  557. } else {
  558. console.log("DEBUG#SENDING");
  559. t.addMyItems(botKeys);
  560. t.data("commandused", "Sell");
  561. t.data("amountofsets", amountofsets.toString());
  562. t.data("amountofkeys", n);
  563. t.send((ERR, STATUS) => {
  564. if (ERR) {
  565. client.chatMessage(SENDER, "An error occurred while sending your trade. Steam Trades could be down. Please try again later.");
  566. console.log("## An error occurred while sending trade: " + ERR);
  567. } else {
  568. client.chatMessage(SENDER, "Trade Sent! Confirming it...");
  569. console.log("## Trade offer sent!");
  570. }
  571. });
  572. }
  573. } else {
  574. console.log("## An error occurred while getting bot sets: " + ERR);
  575. }
  576. });
  577. } else {
  578. console.log("## An error occurred while getting user inventory: " + ERR);
  579. }
  580. });
  581. }
  582. }
  583. });
  584. } else {
  585. client.chatMessage(SENDER, "Please make sure you don't have a trade hold!");
  586. }
  587. });
  588. } else {
  589. client.chatMessage(SENDER, "Please try a lower amount of keys.");
  590. }
  591. } else {
  592. client.chatMessage(SENDER, "Please enter a valid amount of keys!");
  593. }
  594. } else {
  595. client.chatMessage(SENDER, "Please try again later.");
  596. }
  597. }
  598. else if (MSG.toUpperCase().indexOf("!SELLGEMS") >= 0) {
  599. if (botSets) {
  600. let n = parseInt(MSG.toUpperCase().replace("!SELLGEMS ", "")),
  601. amountofsets = n;
  602. if (!isNaN(n) && parseInt(n) > 0) {
  603. if (n <= CONFIG.MESSAGES.MAXSELL) {
  604. client.chatMessage(SENDER, "Processing your request.");
  605. let gemsAmount = 0;
  606. let botGems = [];
  607. let t = manager.createOffer(SENDER.getSteamID64());
  608. t.getUserDetails((ERR, ME, THEM) => {
  609. if (ERR) {
  610. console.log("## An error occurred while getting trade holds: " + ERR);
  611. client.chatMessage(SENDER, "An error occurred while getting your trade holds. Please try again");
  612. } else if (ME.escrowDays === 0 && THEM.escrowDays === 0) {
  613. manager.getUserInventoryContents(client.steamID.getSteamID64(), CONFIG.STEAMGAME, 6, true, (ERR, INV, CURR) => {
  614. if (ERR) {
  615. console.log("## An error occurred while getting bot inventory: " + ERR);
  616. client.chatMessage(SENDER, "An error occurred while loading the bot's inventory. Please try again.");
  617. } else {
  618. for (let i = 0; i < INV.length; i++) {
  619. if (CONFIG.STEAMGEMS.indexOf(INV[i].market_hash_name) >= 0) {
  620. gemsAmount = INV[i].amount;
  621. INV[i].amount = (n * CONFIG.CARDS.GEMS.give_one_set_for);
  622. botGems.push(INV[i]);
  623. break;
  624. }
  625. }
  626. if (gemsAmount < n) {
  627. client.chatMessage(SENDER, "The bot does not have enough Gems.");
  628. } else {
  629. let amountofB = amountofsets;
  630. Utils.getInventory(SENDER.getSteamID64(), community, (ERR, DATA) => {
  631. if (!ERR) {
  632. let s = DATA;
  633. Utils.getSets(s, allCards, (ERR, DDATA) => {
  634. if (!ERR) {
  635. sortSetsByAmountB(s, (DATA) => {
  636. let setsSent = {};
  637. firsttLoop: for (let i = 0; i < DATA.length; i++) {
  638. console.log(setsSent);
  639. console.log(DATA[i]);
  640. if (DDATA[DATA[i]]) {
  641. for (let j = 0; j < DDATA[DATA[i]].length; j++) {
  642. if (amountofB > 0) {
  643. if ((setsSent[DATA[i]] && setsSent[DATA[i]] < CONFIG.CARDS.TF2.MAXSETSELL) || !setsSent[DATA[i]]) {
  644. t.addTheirItems(DDATA[DATA[i]][j]);
  645. console.log("DEBUG#LOOP #2 CONTINUE: ITEM ADD");
  646. amountofB--;
  647. if (!setsSent[DATA[i]]) {
  648. setsSent[DATA[i]] = 1;
  649. } else {
  650. setsSent[DATA[i]] += 1;
  651. }
  652. } else {
  653. console.log("DEBUG#LOOP #2 CONTINUE: RETURN");
  654. continue firsttLoop;
  655. }
  656. } else {
  657. console.log("DEBUG#LOOP #2 CONTINUE: RETURN");
  658. continue firsttLoop;
  659. }
  660. }
  661. } else {
  662. console.log("DEBUG#LOOP #2 CONTINUE: RETURN 2");
  663. continue firsttLoop;
  664. }
  665. }
  666. });
  667. if (amountofB > 0) {
  668. client.chatMessage(SENDER, "You do not have enough sets, (this bot only accepts " + CONFIG.CARDS.GEMS.MAXSETSELL + " sets per set type at a time). Please try again later.");
  669. } else {
  670. console.log("DEBUG#SENDING");
  671. t.addMyItems(botGems);
  672. t.data("commandused", "Sell");
  673. t.data("amountofsets", amountofsets.toString());
  674. t.data("amountofkeys", n);
  675. t.send((ERR, STATUS) => {
  676. if (ERR) {
  677. client.chatMessage(SENDER, "An error occurred while sending your trade. Steam Trades could be down. Please try again later.");
  678. console.log("## An error occurred while sending trade: " + ERR);
  679. } else {
  680. client.chatMessage(SENDER, "Trade Sent! Confirming it...");
  681. console.log("## Trade offer sent!");
  682. }
  683. });
  684. }
  685. } else {
  686. console.log("## An error occurred while getting bot sets: " + ERR);
  687. }
  688. });
  689. } else {
  690. console.log("## An error occurred while getting user inventory: " + ERR);
  691. }
  692. });
  693. }
  694. }
  695. });
  696. } else {
  697. client.chatMessage(SENDER, "Please make sure you don't have a trade hold!");
  698. }
  699. });
  700. } else {
  701. client.chatMessage(SENDER, "Please try a lower amount of sets.");
  702. }
  703. } else {
  704. client.chatMessage(SENDER, "Please enter a valid amount of setss!");
  705. }
  706. } else {
  707. client.chatMessage(SENDER, "Please try again later.");
  708. }
  709. }
  710. else if (MSG.toUpperCase().indexOf("!SELL") >= 0) {
  711. if (botSets) {
  712. let n = parseInt(MSG.toUpperCase().replace("!SELL ", "")),
  713. amountofsets = n * CONFIG.CARDS.CSGO.give_one_for;
  714. if (!isNaN(n) && parseInt(n) > 0) {
  715. if (n <= CONFIG.MESSAGES.MAXSELL) {
  716. client.chatMessage(SENDER, "Processing your request.");
  717. let botKeys = [],
  718. t = manager.createOffer(SENDER.getSteamID64());
  719. t.getUserDetails((ERR, ME, THEM) => {
  720. if (ERR) {
  721. console.log("## An error occurred while getting trade holds: " + ERR);
  722. client.chatMessage(SENDER, "An error occurred while getting your trade holds. Please try again");
  723. } else if (ME.escrowDays === 0 && THEM.escrowDays === 0) {
  724. manager.getUserInventoryContents(client.steamID.getSteamID64(), CONFIG.CSGOGAME, 2, true, (ERR, INV, CURR) => {
  725. if (ERR) {
  726. console.log("## An error occurred while getting bot inventory: " + ERR);
  727. client.chatMessage(SENDER, "An error occurred while loading the bot's inventory. Please try again.");
  728. } else {
  729. for (let i = 0; i < INV.length; i++) {
  730. if (botKeys.length < n && CONFIG.ACCEPTEDKEYS.indexOf(INV[i].market_hash_name) >= 0) {
  731. botKeys.push(INV[i]);
  732. }
  733. }
  734. if (botKeys.length !== n) {
  735. client.chatMessage(SENDER, "The bot does not have enough keys.");
  736. } else {
  737. let amountofB = amountofsets;
  738. Utils.getInventory(SENDER.getSteamID64(), community, (ERR, DATA) => {
  739. if (!ERR) {
  740. let s = DATA;
  741. Utils.getSets(s, allCards, (ERR, DDATA) => {
  742. if (!ERR) {
  743. sortSetsByAmountB(s, (DATA) => {
  744. let setsSent = {};
  745. firsttLoop: for (let i = 0; i < DATA.length; i++) {
  746. console.log(setsSent);
  747. console.log(DATA[i]);
  748. if (DDATA[DATA[i]]) {
  749. for (let j = 0; j < DDATA[DATA[i]].length; j++) {
  750. if (amountofB > 0) {
  751. if ((setsSent[DATA[i]] && setsSent[DATA[i]] < CONFIG.CARDS.CSGO.MAXSETSELL) || !setsSent[DATA[i]]) {
  752. t.addTheirItems(DDATA[DATA[i]][j]);
  753. console.log("DEBUG#LOOP #2 CONTINUE: ITEM ADD");
  754. amountofB--;
  755. if (!setsSent[DATA[i]]) {
  756. setsSent[DATA[i]] = 1;
  757. } else {
  758. setsSent[DATA[i]] += 1;
  759. }
  760. } else {
  761. console.log("DEBUG#LOOP #2 CONTINUE: RETURN");
  762. continue firsttLoop;
  763. }
  764. } else {
  765. console.log("DEBUG#LOOP #2 CONTINUE: RETURN");
  766. continue firsttLoop;
  767. }
  768. }
  769. } else {
  770. console.log("DEBUG#LOOP #2 CONTINUE: RETURN 2");
  771. continue firsttLoop;
  772. }
  773. }
  774. });
  775. if (amountofB > 0) {
  776. client.chatMessage(SENDER, "You do not have enough sets, (this bot only accepts " + CONFIG.CARDS.CSGO.MAXSETSELL + " sets per set type at a time). Please try again later.");
  777. } else {
  778. console.log("DEBUG#SENDING");
  779. t.addMyItems(botKeys);
  780. t.data("commandused", "Sell");
  781. t.data("amountofsets", amountofsets.toString());
  782. t.data("amountofkeys", n);
  783. t.send((ERR, STATUS) => {
  784. if (ERR) {
  785. client.chatMessage(SENDER, "An error occurred while sending your trade. Steam Trades could be down. Please try again later.");
  786. console.log("## An error occurred while sending trade: " + ERR);
  787. } else {
  788. client.chatMessage(SENDER, "Trade Sent! Confirming it...");
  789. console.log("## Trade offer sent!");
  790. }
  791. });
  792. }
  793. } else {
  794. console.log("## An error occurred while getting bot sets: " + ERR);
  795. }
  796. });
  797. } else {
  798. console.log("## An error occurred while getting user inventory: " + ERR);
  799. }
  800. });
  801. }
  802. }
  803. });
  804. } else {
  805. client.chatMessage(SENDER, "Please make sure you don't have a trade hold!");
  806. }
  807. });
  808. } else {
  809. client.chatMessage(SENDER, "Please try a lower amount of keys.");
  810. }
  811. } else {
  812. client.chatMessage(SENDER, "Please enter a valid amount of keys!");
  813. }
  814. } else {
  815. client.chatMessage(SENDER, "Please try again later.");
  816. }
  817. }
  818.  
  819. else if (MSG.toUpperCase().indexOf("!BUYTF") >= 0) {
  820. if (botSets) {
  821. let n = MSG.toUpperCase().replace("!BUYTF ", "");
  822. let amountofsets = parseInt(n) * CONFIG.CARDS.TF2.buy_sets_by_one;
  823. if (!isNaN(n) && parseInt(n) > 0) {
  824. if (n <= CONFIG.MESSAGES.MAXBUY) {
  825. let t = manager.createOffer(SENDER.getSteamID64());
  826. t.getUserDetails((ERR, ME, THEM) => {
  827. if (ERR) {
  828. console.log("## An error occurred while getting trade holds: " + ERR);
  829. client.chatMessage(SENDER, "An error occurred while getting your trade holds. Please try again");
  830. } else if (ME.escrowDays === 0 && THEM.escrowDays === 0) {
  831. n = parseInt(n);
  832. let theirKeys = [];
  833. client.chatMessage(SENDER, "Processing your request.");
  834. manager.getUserInventoryContents(SENDER.getSteamID64(), CONFIG.TFGAME, 2, true, (ERR, INV, CURR) => {
  835. if (ERR) {
  836. console.log("## An error occurred while getting inventory: " + ERR);
  837. client.chatMessage(SENDER, "An error occurred while loading your inventory. Please try later");
  838. } else {
  839. console.log("DEBUG#INV LOADED");
  840. if (!ERR) {
  841. console.log("DEBUG#INV LOADED NOERR");
  842. for (let i = 0; i < INV.length; i++) {
  843. if (theirKeys.length < n && CONFIG.TFACCEPTEDKEYS.indexOf(INV[i].market_hash_name) >= 0) {
  844. theirKeys.push(INV[i]);
  845. }
  846. }
  847. if (theirKeys.length !== n) {
  848. client.chatMessage(SENDER, "You do not have enough keys.");
  849. } else {
  850. Utils.getBadges(SENDER.getSteamID64(), (ERR, DATA) => {
  851. if (!ERR) {
  852. console.log("DEBUG#BADGE LOADED");
  853. if (!ERR) {
  854. let b = {}; // List with badges that CAN still be crafted
  855. if (DATA) {
  856. for (let i = 0; i < Object.keys(DATA).length; i++) {
  857. if (DATA[Object.keys(DATA)[i]] < 6) {
  858. b[Object.keys(DATA)[i]] = 5 - DATA[Object.keys(DATA)[i]];
  859. }
  860. }
  861. } else {
  862. client.chatMessage(SENDER.getSteamID64(), "Your badges are empty, sending an offer without checking badges.");
  863. }
  864. console.log(DATA);
  865. console.log(b);
  866. // TODO: COUNT AMOUNT OF SETS BOT CAN GIVE HIM
  867. // 1: GET BOTS CARDS. DONE
  868. // 2: GET PLAYER's BADGES. DONE
  869. // 3: MAGIC
  870. let hisMaxSets = 0,
  871. botNSets = 0;
  872. // Loop for sets he has partially completed
  873. for (let i = 0; i < Object.keys(b).length; i++) {
  874. if (botSets[Object.keys(b)[i]] && botSets[Object.keys(b)[i]].length >= 5 - b[Object.keys(b)[i]].length) {
  875. hisMaxSets += 5 - b[Object.keys(b)[i]].length;
  876. }
  877. }
  878. console.log("DEBUG#LOOP 1 DONE");
  879. // Loop for sets he has never crafted
  880. for (let i = 0; i < Object.keys(botSets).length; i++) {
  881. if (Object.keys(b).indexOf(Object.keys(botSets)[i]) < 0) {
  882. if (botSets[Object.keys(botSets)[i]].length >= 5) {
  883. hisMaxSets += 5;
  884. } else {
  885. hisMaxSets += botSets[Object.keys(botSets)[i]].length;
  886. }
  887. }
  888. botNSets += botSets[Object.keys(botSets)[i]].length;
  889. }
  890. console.log("DEBUG#LOOP 2 DONE");
  891. // HERE
  892. if (amountofsets <= hisMaxSets) {
  893. hisMaxSets = amountofsets;
  894. console.log("DEBUG#TRADE CREATED");
  895. sortSetsByAmount(botSets, (DATA) => {
  896. console.log("DEBUG#" + DATA);
  897. console.log("DEBUG#SETS SORTED");
  898. firstLoop: for (let i = 0; i < DATA.length; i++) {
  899. if (b[DATA[i]] === 0) {
  900. continue firstLoop;
  901. } else {
  902. console.log("DEBUG#" + i);
  903. console.log("DEBUG#FOR LOOP ITEMS");
  904. if (hisMaxSets > 0) {
  905. console.log("DEBUG#MAXSETSMORETHAN1");
  906. if (b[DATA[i]] && botSets[DATA[i]].length >= b[DATA[i]]) {
  907. // BOT HAS ENOUGH SETS OF THIS KIND
  908. console.log("DEBUG#LOOP #1");
  909. sLoop: for (let j = 0; j < 5 - b[DATA[i]]; j++) {
  910. if (j + 1 < b[DATA[i]] && hisMaxSets > 0) {
  911. console.log("DEBUG#LOOP #1: ITEM ADD");
  912. console.log("DEBUG#LOOP #1: " + botSets[DATA[i]][j]);
  913. t.addMyItems(botSets[DATA[i]][j]);
  914. hisMaxSets--;
  915. console.log(hisMaxSets);
  916. } else {
  917. console.log("DEBUG#LOOP #1: RETURN");
  918. continue firstLoop;
  919. }
  920. }
  921. } else if (b[DATA[i]] && botSets[DATA[i]].length < b[DATA[i]]) {
  922. // BOT DOESNT HAVE ENOUGH SETS OF THIS KIND
  923. console.log("DEBUG#LOOP #1 CONTINUE");
  924. continue; // *
  925. } else if (!b[DATA[i]] && botSets[DATA[i]].length < 5 && botSets[DATA[i]].length - b[DATA[i]] > 0) { // TODO NOT FOR LOOP WITH BOTSETS. IT SENDS ALL
  926. // BOT HAS ENOUGH SETS AND USER NEVER CRAFTED THIS
  927. bLoop: for (let j = 0; j < botSets[DATA[i]].length - b[DATA[i]]; j++) {
  928. if (botSets[DATA[i]][j] && hisMaxSets > 0) {
  929. t.addMyItems(botSets[DATA[i]][j]);
  930. console.log("DEBUG#LOOP #2 CONTINUE: ITEM ADD");
  931. hisMaxSets--;
  932. } else {
  933. console.log("DEBUG#LOOP #2 CONTINUE: RETURN");
  934. continue firstLoop;
  935. }
  936. }
  937. }
  938. else if (hisMaxSets < 5) {
  939. // BOT DOESNT HAVE CARDS USER AREADY CRAFTED, IF USER STILL NEEDS 5 SETS:
  940. console.log("DEBUG#LOOP #2");
  941. tLoop: for (let j = 0; j !== hisMaxSets; j++) {
  942. if (botSets[DATA[i]][j] && hisMaxSets > 0) {
  943. t.addMyItems(botSets[DATA[i]][j]);
  944. console.log("DEBUG#LOOP #2: ITEM ADD");
  945. hisMaxSets--;
  946. console.log(hisMaxSets);
  947. } else {
  948. console.log("DEBUG#LOOP #2: RETURN");
  949. continue firstLoop;
  950. }
  951. }
  952. } else {
  953. // BOT DOESNT HAVE CARDS USER AREADY CRAFTED, IF USER STILL NEEDS LESS THAN 5 SETS:
  954. console.log("DEBUG#LOOP #2");
  955. xLoop: for (let j = 0; j !== 5; j++ && hisMaxSets > 0) {
  956. if (botSets[DATA[i]][j] && hisMaxSets > 0) {
  957. t.addMyItems(botSets[DATA[i]][j]);
  958. console.log("DEBUG#LOOP #2: ITEM ADD");
  959. hisMaxSets--;
  960. console.log(hisMaxSets);
  961. } else {
  962. console.log("DEBUG#LOOP #2: RETURN");
  963. continue firstLoop;
  964. }
  965. }
  966. }
  967. } else {
  968. console.log("DEBUG#RETURN");
  969. break firstLoop;
  970. }
  971. }
  972. }
  973. if (hisMaxSets > 0) {
  974. client.chatMessage(SENDER, "There are not enough sets. Please try again later.");
  975. } else {
  976. console.log("DEBUG#SENDING");
  977. t.addTheirItems(theirKeys);
  978. t.data("commandused", "Buy");
  979. t.data("amountofkeys", n);
  980. t.data("amountofsets", amountofsets.toString());
  981. t.send((ERR, STATUS) => {
  982. if (ERR) {
  983. client.chatMessage(SENDER, "An error occurred while sending your trade. Steam Trades could be down. Please try again later.");
  984. console.log("## An error occurred while sending trade: " + ERR);
  985. } else {
  986. client.chatMessage(SENDER, "Trade Sent! Confirming it...");
  987. console.log("## Trade offer sent");
  988. }
  989. });
  990. }
  991. });
  992. } else {
  993. client.chatMessage(SENDER, "There are currently not enough sets that you have not used in stock for this amount of keys. Please try again later. If you want the bot to ignore your current badges use !buyany.");
  994. }
  995. // TO HERE
  996. } else {
  997. console.log("An error occurred while getting badges: " + ERR);
  998. }
  999. } else {
  1000. client.chatMessage(SENDER, "An error occurred while getting your badges. Please try again.");
  1001. console.log(SENDER, "## An error occurred while loading badges: " + ERR);
  1002. }
  1003. });
  1004. }
  1005. } else {
  1006. console.log("## An error occurred while getting inventory: " + ERR);
  1007. client.chatMessage(SENDER, "An error occurred while loading your inventory, please make sure it's set to public.");
  1008. }
  1009. }
  1010. });
  1011. } else {
  1012. client.chatMessage(SENDER, "Please make sure you don't have a trade hold!");
  1013. }
  1014. });
  1015. } else {
  1016. client.chatMessage(SENDER, "Please try a lower amount of keys.");
  1017. }
  1018. } else {
  1019. client.chatMessage(SENDER, "Please provide a valid amount of keys.");
  1020. }
  1021. } else {
  1022. client.chatMessage(SENDER, "Please try again later.");
  1023. }
  1024. }
  1025. else if (MSG.toUpperCase().indexOf("!BUYGEMS") >= 0) {
  1026. if (botSets) {
  1027. let n = MSG.toUpperCase().replace("!BUYGEMS ", ""),
  1028. amountofsets = parseInt(n);
  1029. if (!isNaN(n) && parseInt(n) > 0) {
  1030. if (n <= CONFIG.MESSAGES.MAXBUY) {
  1031. let t = manager.createOffer(SENDER.getSteamID64());
  1032. t.getUserDetails((ERR, ME, THEM) => {
  1033. if (ERR) {
  1034. console.log("## An error occurred while getting trade holds: " + ERR);
  1035. client.chatMessage(SENDER, "An error occurred while getting your trade holds. Please try again");
  1036. } else if (ME.escrowDays === 0 && THEM.escrowDays === 0) {
  1037. n = parseInt(n);
  1038. let theirGems = [];
  1039. let amountTheirGems = 0;
  1040. client.chatMessage(SENDER, "Processing your request.");
  1041. manager.getUserInventoryContents(SENDER.getSteamID64(), CONFIG.STEAMGAME, 6, true, (ERR, INV, CURR) => {
  1042. if (ERR) {
  1043. console.log("## An error occurred while getting inventory: " + ERR);
  1044. client.chatMessage(SENDER, "An error occurred while loading your inventory. Please try later");
  1045. } else {
  1046. console.log("DEBUG#INV LOADED");
  1047. if (!ERR) {
  1048. console.log("DEBUG#INV LOADED NOERR");
  1049. for (let i = 0; i < INV.length; i++) {
  1050. if (CONFIG.STEAMGEMS.indexOf(INV[i].market_hash_name) >= 0) {
  1051. amountTheirGems = INV[i].amount;
  1052. INV[i].amount = (n * CONFIG.CARDS.GEMS.buy_one_set_for);
  1053. theirGems.push(INV[i]);
  1054. break;
  1055. }
  1056. }
  1057. if (amountTheirGems < ( n * CONFIG.CARDS.GEMS.buy_one_set_for )) {
  1058. client.chatMessage(SENDER, "You do not have enough Gems.");
  1059. } else {
  1060. Utils.getBadges(SENDER.getSteamID64(), (ERR, DATA) => {
  1061. if (!ERR) {
  1062. console.log("DEBUG#BADGE LOADED");
  1063. if (!ERR) {
  1064. let b = {}; // List with badges that CAN still be crafted
  1065. if (DATA) {
  1066. for (let i = 0; i < Object.keys(DATA).length; i++) {
  1067. if (DATA[Object.keys(DATA)[i]] < 6) {
  1068. b[Object.keys(DATA)[i]] = 5 - DATA[Object.keys(DATA)[i]];
  1069. }
  1070. }
  1071. } else {
  1072. client.chatMessage(SENDER.getSteamID64(), "Your badges are empty, sending an offer without checking badges.");
  1073. }
  1074. // TODO: COUNT AMOUNT OF SETS BOT CAN GIVE HIM
  1075. // 1: GET BOTS CARDS. DONE
  1076. // 2: GET PLAYER's BADGES. DONE
  1077. // 3: MAGIC
  1078. let hisMaxSets = 0,
  1079. botNSets = 0;
  1080. // Loop for sets he has partially completed
  1081. for (let i = 0; i < Object.keys(b).length; i++) {
  1082. if (botSets[Object.keys(b)[i]] && botSets[Object.keys(b)[i]].length >= 5 - b[Object.keys(b)[i]].length) {
  1083. hisMaxSets += 5 - b[Object.keys(b)[i]].length;
  1084. }
  1085. }
  1086. console.log("DEBUG#LOOP 1 DONE");
  1087. // Loop for sets he has never crafted
  1088. for (let i = 0; i < Object.keys(botSets).length; i++) {
  1089. if (Object.keys(b).indexOf(Object.keys(botSets)[i]) < 0) {
  1090. if (botSets[Object.keys(botSets)[i]].length >= 5) {
  1091. hisMaxSets += 5;
  1092. } else {
  1093. hisMaxSets += botSets[Object.keys(botSets)[i]].length;
  1094. }
  1095. }
  1096. botNSets += botSets[Object.keys(botSets)[i]].length;
  1097. }
  1098. console.log("DEBUG#LOOP 2 DONE");
  1099. // HERE
  1100. if (amountofsets <= hisMaxSets) {
  1101. hisMaxSets = amountofsets;
  1102. console.log("DEBUG#TRADE CREATED");
  1103. sortSetsByAmount(botSets, (DATA) => {
  1104. console.log("DEBUG#" + DATA);
  1105. console.log("DEBUG#SETS SORTED");
  1106. firstLoop: for (let i = 0; i < DATA.length; i++) {
  1107. if (b[DATA[i]] === 0) {
  1108. continue firstLoop;
  1109. } else {
  1110. console.log("DEBUG#" + i);
  1111. console.log("DEBUG#FOR LOOP ITEMS");
  1112. if (hisMaxSets > 0) {
  1113. console.log("DEBUG#MAXSETSMORETHAN1");
  1114. if (b[DATA[i]] && botSets[DATA[i]].length >= b[DATA[i]]) {
  1115. // BOT HAS ENOUGH SETS OF THIS KIND
  1116. console.log("DEBUG#LOOP #1");
  1117. sLoop: for (let j = 0; j < 5 - b[DATA[i]]; j++) {
  1118. if (j + 1 < b[DATA[i]] && hisMaxSets > 0) {
  1119. console.log("DEBUG#LOOP #1: ITEM ADD");
  1120. console.log("DEBUG#LOOP #1: " + botSets[DATA[i]][j]);
  1121. t.addMyItems(botSets[DATA[i]][j]);
  1122. hisMaxSets--;
  1123. console.log(hisMaxSets);
  1124. } else {
  1125. console.log("DEBUG#LOOP #1: RETURN");
  1126. continue firstLoop;
  1127. }
  1128. }
  1129. } else if (b[DATA[i]] && botSets[DATA[i]].length < b[DATA[i]]) {
  1130. // BOT DOESNT HAVE ENOUGH SETS OF THIS KIND
  1131. console.log("DEBUG#LOOP #1 CONTINUE");
  1132. continue; // *
  1133. } else if (!b[DATA[i]] && botSets[DATA[i]].length < 5 && botSets[DATA[i]].length - b[DATA[i]] > 0) { // TODO NOT FOR LOOP WITH BOTSETS. IT SENDS ALL
  1134. // BOT HAS ENOUGH SETS AND USER NEVER CRAFTED THIS
  1135. bLoop: for (let j = 0; j < botSets[DATA[i]].length - b[DATA[i]]; j++) {
  1136. if (botSets[DATA[i]][j] && hisMaxSets > 0) {
  1137. t.addMyItems(botSets[DATA[i]][j]);
  1138. console.log("DEBUG#LOOP #2 CONTINUE: ITEM ADD");
  1139. hisMaxSets--;
  1140. } else {
  1141. console.log("DEBUG#LOOP #2 CONTINUE: RETURN");
  1142. continue firstLoop;
  1143. }
  1144. }
  1145. }
  1146. else if (hisMaxSets < 5) {
  1147. // BOT DOESNT HAVE CARDS USER AREADY CRAFTED, IF USER STILL NEEDS 5 SETS:
  1148. console.log("DEBUG#LOOP #2");
  1149. tLoop: for (let j = 0; j !== hisMaxSets; j++) {
  1150. if (botSets[DATA[i]][j] && hisMaxSets > 0) {
  1151. t.addMyItems(botSets[DATA[i]][j]);
  1152. console.log("DEBUG#LOOP #2: ITEM ADD");
  1153. hisMaxSets--;
  1154. console.log(hisMaxSets);
  1155. } else {
  1156. console.log("DEBUG#LOOP #2: RETURN");
  1157. continue firstLoop;
  1158. }
  1159. }
  1160. } else {
  1161. // BOT DOESNT HAVE CARDS USER AREADY CRAFTED, IF USER STILL NEEDS LESS THAN 5 SETS:
  1162. console.log("DEBUG#LOOP #2");
  1163. xLoop: for (let j = 0; j !== 5; j++ && hisMaxSets > 0) {
  1164. if (botSets[DATA[i]][j] && hisMaxSets > 0) {
  1165. t.addMyItems(botSets[DATA[i]][j]);
  1166. console.log("DEBUG#LOOP #2: ITEM ADD");
  1167. hisMaxSets--;
  1168. console.log(hisMaxSets);
  1169. } else {
  1170. console.log("DEBUG#LOOP #2: RETURN");
  1171. continue firstLoop;
  1172. }
  1173. }
  1174. }
  1175. } else {
  1176. console.log("DEBUG#RETURN");
  1177. break firstLoop;
  1178. }
  1179. }
  1180. }
  1181. if (hisMaxSets > 0) {
  1182. client.chatMessage(SENDER, "There are not enough sets. Please try again later.");
  1183. } else {
  1184. console.log("DEBUG#SENDING");
  1185. t.addTheirItems(theirGems);
  1186. t.data("commandused", "Buy");
  1187. //t.data("amountofkeys", n);
  1188. //t.data("amountofsets", amountofsets.toString());
  1189. t.send((ERR, STATUS) => {
  1190. if (ERR) {
  1191. client.chatMessage(SENDER, "An error occurred while sending your trade. Steam Trades could be down. Please try again later.");
  1192. console.log("## An error occurred while sending trade: " + ERR);
  1193. } else {
  1194. client.chatMessage(SENDER, "Trade Sent! Confirming it...");
  1195. console.log("## Trade offer sent");
  1196. }
  1197. });
  1198. }
  1199. });
  1200. } else {
  1201. client.chatMessage(SENDER, "There are currently not enough sets that you have not used in stock for this amount of keys. Please try again later. If you want the bot to ignore your current badges use !buyany.");
  1202. }
  1203. // TO HERE
  1204. } else {
  1205. console.log("An error occurred while getting badges: " + ERR);
  1206. }
  1207. } else {
  1208. client.chatMessage(SENDER, "An error occurred while getting your badges. Please try again.");
  1209. console.log(SENDER, "## An error occurred while loading badges: " + ERR);
  1210. }
  1211. });
  1212. }
  1213. } else {
  1214. console.log("## An error occurred while getting inventory: " + ERR);
  1215. client.chatMessage(SENDER, "An error occurred while loading your inventory, please make sure it's set to public.");
  1216. }
  1217. }
  1218. });
  1219. } else {
  1220. client.chatMessage(SENDER, "Please make sure you don't have a trade hold!");
  1221. }
  1222. });
  1223. } else {
  1224. client.chatMessage(SENDER, "Please try a lower amount of sets.");
  1225. }
  1226. } else {
  1227. client.chatMessage(SENDER, "Please provide a valid amount of sets.");
  1228. }
  1229. } else {
  1230. client.chatMessage(SENDER, "Please try again later.");
  1231. }
  1232. }
  1233. else if (MSG.toUpperCase().indexOf("!BUY") >= 0) {
  1234. if (botSets) {
  1235. let n = MSG.toUpperCase().replace("!BUY ", ""),
  1236. amountofsets = parseInt(n) * CONFIG.CARDS.CSGO.buy_sets_by_one;
  1237. if (!isNaN(n) && parseInt(n) > 0) {
  1238. if (n <= CONFIG.MESSAGES.MAXBUY) {
  1239. let t = manager.createOffer(SENDER.getSteamID64());
  1240. t.getUserDetails((ERR, ME, THEM) => {
  1241. if (ERR) {
  1242. console.log("## An error occurred while getting trade holds: " + ERR);
  1243. client.chatMessage(SENDER, "An error occurred while getting your trade holds. Please try again");
  1244. } else if (ME.escrowDays === 0 && THEM.escrowDays === 0) {
  1245. n = parseInt(n);
  1246. let theirKeys = [];
  1247. client.chatMessage(SENDER, "Processing your request.");
  1248. manager.getUserInventoryContents(SENDER.getSteamID64(), CONFIG.CSGOGAME, 2, true, (ERR, INV, CURR) => {
  1249. if (ERR) {
  1250. console.log("## An error occurred while getting inventory: " + ERR);
  1251. client.chatMessage(SENDER, "An error occurred while loading your inventory. Please try later");
  1252. } else {
  1253. console.log("DEBUG#INV LOADED");
  1254. if (!ERR) {
  1255. console.log("DEBUG#INV LOADED NOERR");
  1256. for (let i = 0; i < INV.length; i++) {
  1257. if (theirKeys.length < n && CONFIG.ACCEPTEDKEYS.indexOf(INV[i].market_hash_name) >= 0) {
  1258. theirKeys.push(INV[i]);
  1259. }
  1260. }
  1261. if (theirKeys.length !== n) {
  1262. client.chatMessage(SENDER, "You do not have enough keys.");
  1263. } else {
  1264. Utils.getBadges(SENDER.getSteamID64(), (ERR, DATA) => {
  1265. if (!ERR) {
  1266. console.log("DEBUG#BADGE LOADED");
  1267. if (!ERR) {
  1268. let b = {}; // List with badges that CAN still be crafted
  1269. if (DATA) {
  1270. for (let i = 0; i < Object.keys(DATA).length; i++) {
  1271. if (DATA[Object.keys(DATA)[i]] < 6) {
  1272. b[Object.keys(DATA)[i]] = 5 - DATA[Object.keys(DATA)[i]];
  1273. }
  1274. }
  1275. } else {
  1276. client.chatMessage(SENDER.getSteamID64(), "Your badges are empty, sending an offer without checking badges.");
  1277. }
  1278. console.log(DATA);
  1279. console.log(b);
  1280. // TODO: COUNT AMOUNT OF SETS BOT CAN GIVE HIM
  1281. // 1: GET BOTS CARDS. DONE
  1282. // 2: GET PLAYER's BADGES. DONE
  1283. // 3: MAGIC
  1284. let hisMaxSets = 0,
  1285. botNSets = 0;
  1286. // Loop for sets he has partially completed
  1287. for (let i = 0; i < Object.keys(b).length; i++) {
  1288. if (botSets[Object.keys(b)[i]] && botSets[Object.keys(b)[i]].length >= 5 - b[Object.keys(b)[i]].length) {
  1289. hisMaxSets += 5 - b[Object.keys(b)[i]].length;
  1290. }
  1291. }
  1292. console.log("DEBUG#LOOP 1 DONE");
  1293. // Loop for sets he has never crafted
  1294. for (let i = 0; i < Object.keys(botSets).length; i++) {
  1295. if (Object.keys(b).indexOf(Object.keys(botSets)[i]) < 0) {
  1296. if (botSets[Object.keys(botSets)[i]].length >= 5) {
  1297. hisMaxSets += 5;
  1298. } else {
  1299. hisMaxSets += botSets[Object.keys(botSets)[i]].length;
  1300. }
  1301. }
  1302. botNSets += botSets[Object.keys(botSets)[i]].length;
  1303. }
  1304. console.log("DEBUG#LOOP 2 DONE");
  1305. // HERE
  1306. if (amountofsets <= hisMaxSets) {
  1307. hisMaxSets = amountofsets;
  1308. console.log("DEBUG#TRADE CREATED");
  1309. sortSetsByAmount(botSets, (DATA) => {
  1310. console.log("DEBUG#" + DATA);
  1311. console.log("DEBUG#SETS SORTED");
  1312. firstLoop: for (let i = 0; i < DATA.length; i++) {
  1313. if (b[DATA[i]] === 0) {
  1314. continue firstLoop;
  1315. } else {
  1316. console.log("DEBUG#" + i);
  1317. console.log("DEBUG#FOR LOOP ITEMS");
  1318. if (hisMaxSets > 0) {
  1319. console.log("DEBUG#MAXSETSMORETHAN1");
  1320. if (b[DATA[i]] && botSets[DATA[i]].length >= b[DATA[i]]) {
  1321. // BOT HAS ENOUGH SETS OF THIS KIND
  1322. console.log("DEBUG#LOOP #1");
  1323. sLoop: for (let j = 0; j < 5 - b[DATA[i]]; j++) {
  1324. if (j + 1 < b[DATA[i]] && hisMaxSets > 0) {
  1325. console.log("DEBUG#LOOP #1: ITEM ADD");
  1326. console.log("DEBUG#LOOP #1: " + botSets[DATA[i]][j]);
  1327. t.addMyItems(botSets[DATA[i]][j]);
  1328. hisMaxSets--;
  1329. console.log(hisMaxSets);
  1330. } else {
  1331. console.log("DEBUG#LOOP #1: RETURN");
  1332. continue firstLoop;
  1333. }
  1334. }
  1335. } else if (b[DATA[i]] && botSets[DATA[i]].length < b[DATA[i]]) {
  1336. // BOT DOESNT HAVE ENOUGH SETS OF THIS KIND
  1337. console.log("DEBUG#LOOP #1 CONTINUE");
  1338. continue; // *
  1339. } else if (!b[DATA[i]] && botSets[DATA[i]].length < 5 && botSets[DATA[i]].length - b[DATA[i]] > 0) { // TODO NOT FOR LOOP WITH BOTSETS. IT SENDS ALL
  1340. // BOT HAS ENOUGH SETS AND USER NEVER CRAFTED THIS
  1341. bLoop: for (let j = 0; j < botSets[DATA[i]].length - b[DATA[i]]; j++) {
  1342. if (botSets[DATA[i]][j] && hisMaxSets > 0) {
  1343. t.addMyItems(botSets[DATA[i]][j]);
  1344. console.log("DEBUG#LOOP #2 CONTINUE: ITEM ADD");
  1345. hisMaxSets--;
  1346. } else {
  1347. console.log("DEBUG#LOOP #2 CONTINUE: RETURN");
  1348. continue firstLoop;
  1349. }
  1350. }
  1351. }
  1352. else if (hisMaxSets < 5) {
  1353. // BOT DOESNT HAVE CARDS USER AREADY CRAFTED, IF USER STILL NEEDS 5 SETS:
  1354. console.log("DEBUG#LOOP #2");
  1355. tLoop: for (let j = 0; j !== hisMaxSets; j++) {
  1356. if (botSets[DATA[i]][j] && hisMaxSets > 0) {
  1357. t.addMyItems(botSets[DATA[i]][j]);
  1358. console.log("DEBUG#LOOP #2: ITEM ADD");
  1359. hisMaxSets--;
  1360. console.log(hisMaxSets);
  1361. } else {
  1362. console.log("DEBUG#LOOP #2: RETURN");
  1363. continue firstLoop;
  1364. }
  1365. }
  1366. } else {
  1367. // BOT DOESNT HAVE CARDS USER AREADY CRAFTED, IF USER STILL NEEDS LESS THAN 5 SETS:
  1368. console.log("DEBUG#LOOP #2");
  1369. xLoop: for (let j = 0; j !== 5; j++ && hisMaxSets > 0) {
  1370. if (botSets[DATA[i]][j] && hisMaxSets > 0) {
  1371. t.addMyItems(botSets[DATA[i]][j]);
  1372. console.log("DEBUG#LOOP #2: ITEM ADD");
  1373. hisMaxSets--;
  1374. console.log(hisMaxSets);
  1375. } else {
  1376. console.log("DEBUG#LOOP #2: RETURN");
  1377. continue firstLoop;
  1378. }
  1379. }
  1380. }
  1381. } else {
  1382. console.log("DEBUG#RETURN");
  1383. break firstLoop;
  1384. }
  1385. }
  1386. }
  1387. if (hisMaxSets > 0) {
  1388. client.chatMessage(SENDER, "There are not enough sets. Please try again later.");
  1389. } else {
  1390. console.log("DEBUG#SENDING");
  1391. t.addTheirItems(theirKeys);
  1392. t.data("commandused", "Buy");
  1393. t.data("amountofkeys", n);
  1394. t.data("amountofsets", amountofsets.toString());
  1395. t.send((ERR, STATUS) => {
  1396. if (ERR) {
  1397. client.chatMessage(SENDER, "An error occurred while sending your trade. Steam Trades could be down. Please try again later.");
  1398. console.log("## An error occurred while sending trade: " + ERR);
  1399. } else {
  1400. client.chatMessage(SENDER, "Trade Sent! Confirming it...");
  1401. console.log("## Trade offer sent");
  1402. }
  1403. });
  1404. }
  1405. });
  1406. } else {
  1407. client.chatMessage(SENDER, "There are currently not enough sets that you have not used in stock for this amount of keys. Please try again later. If you want the bot to ignore your current badges use !buyany.");
  1408. }
  1409. // TO HERE
  1410. } else {
  1411. console.log("An error occurred while getting badges: " + ERR);
  1412. }
  1413. } else {
  1414. client.chatMessage(SENDER, "An error occurred while getting your badges. Please try again.");
  1415. console.log(SENDER, "## An error occurred while loading badges: " + ERR);
  1416. }
  1417. });
  1418. }
  1419. } else {
  1420. console.log("## An error occurred while getting inventory: " + ERR);
  1421. client.chatMessage(SENDER, "An error occurred while loading your inventory, please make sure it's set to public.");
  1422. }
  1423. }
  1424. });
  1425. } else {
  1426. client.chatMessage(SENDER, "Please make sure you don't have a trade hold!");
  1427. }
  1428. });
  1429. } else {
  1430. client.chatMessage(SENDER, "Please try a lower amount of keys.");
  1431. }
  1432. } else {
  1433. client.chatMessage(SENDER, "Please provide a valid amount of keys.");
  1434. }
  1435. } else {
  1436. client.chatMessage(SENDER, "Please try again later.");
  1437. }
  1438. }
  1439.  
  1440. else if (MSG.toUpperCase().indexOf("!BUYONETF") >= 0) {
  1441. if (botSets) {
  1442. let n = MSG.toUpperCase().replace("!BUYONETF ", ""),
  1443. amountofsets = parseInt(n) * CONFIG.CARDS.TF2.buy_sets_by_one;
  1444. if (!isNaN(n) && parseInt(n) > 0) {
  1445. if (n <= CONFIG.MESSAGES.MAXBUY) {
  1446. let t = manager.createOffer(SENDER.getSteamID64());
  1447. t.getUserDetails((ERR, ME, THEM) => {
  1448. if (ERR) {
  1449. console.log("## An error occurred while getting trade holds: " + ERR);
  1450. client.chatMessage(SENDER, "An error occurred while getting your trade holds. Please try again");
  1451. } else if (ME.escrowDays === 0 && THEM.escrowDays === 0) {
  1452. n = parseInt(n);
  1453. let theirKeys = [];
  1454. client.chatMessage(SENDER, "Processing your request.");
  1455. manager.getUserInventoryContents(SENDER.getSteamID64(), CONFIG.TFGAME, 2, true, (ERR, INV, CURR) => {
  1456. if (ERR) {
  1457. console.log("## An error occurred while getting inventory: " + ERR);
  1458. client.chatMessage(SENDER, "An error occurred while loading your inventory. Please try later");
  1459. } else {
  1460. console.log("DEBUG#INV LOADED");
  1461. if (!ERR) {
  1462. console.log("DEBUG#INV LOADED NOERR");
  1463. for (let i = 0; i < INV.length; i++) {
  1464. if (theirKeys.length < n && CONFIG.TFACCEPTEDKEYS.indexOf(INV[i].market_hash_name) >= 0) {
  1465. theirKeys.push(INV[i]);
  1466. }
  1467. }
  1468. if (theirKeys.length !== n) {
  1469. client.chatMessage(SENDER, "You do not have enough keys.");
  1470. } else {
  1471. Utils.getBadges(SENDER.getSteamID64(), (ERR, DATA) => {
  1472. if (!ERR) {
  1473. console.log("DEBUG#BADGE LOADED");
  1474. if (!ERR) {
  1475. let b = {}; // List with badges that CAN still be crafted
  1476. if (DATA) {
  1477. for (let i = 0; i < Object.keys(DATA).length; i++) {
  1478. if (DATA[Object.keys(DATA)[i]] < 6) {
  1479. b[Object.keys(DATA)[i]] = 5 - DATA[Object.keys(DATA)[i]];
  1480. }
  1481. }
  1482. } else {
  1483. client.chatMessage(SENDER.getSteamID64(), "Your badges are empty, sending an offer without checking badges.");
  1484. }
  1485. console.log(DATA);
  1486. console.log(b);
  1487. // TODO: COUNT AMOUNT OF SETS BOT CAN GIVE HIM
  1488. // 1: GET BOTS CARDS. DONE
  1489. // 2: GET PLAYER's BADGES. DONE
  1490. // 3: MAGIC
  1491. let hisMaxSets = 0,
  1492. botNSets = 0;
  1493. // Loop for sets he has partially completed
  1494. for (let i = 0; i < Object.keys(b).length; i++) {
  1495. if (botSets[Object.keys(b)[i]] && botSets[Object.keys(b)[i]].length >= 5 - b[Object.keys(b)[i]].length) {
  1496. hisMaxSets += 5 - b[Object.keys(b)[i]].length;
  1497. }
  1498. }
  1499. console.log("DEBUG#LOOP 1 DONE");
  1500. // Loop for sets he has never crafted
  1501. for (let i = 0; i < Object.keys(botSets).length; i++) {
  1502. if (Object.keys(b).indexOf(Object.keys(botSets)[i]) < 0) {
  1503. if (botSets[Object.keys(botSets)[i]].length >= 5) {
  1504. hisMaxSets += 5;
  1505. } else {
  1506. hisMaxSets += botSets[Object.keys(botSets)[i]].length;
  1507. }
  1508. }
  1509. botNSets += botSets[Object.keys(botSets)[i]].length;
  1510. }
  1511. totalBotSets = botNSets;
  1512. let playThis = CONFIG.PLAYGAMES;
  1513. if (CONFIG.PLAYGAMES && typeof(CONFIG.PLAYGAMES[0]) === "string") {
  1514. playThis[0] = parseString(playThis[0], totalBotSets);
  1515. }
  1516. client.gamesPlayed(playThis);
  1517. console.log("DEBUG#LOOP 2 DONE");
  1518. // HERE
  1519. if (amountofsets <= hisMaxSets) {
  1520. hisMaxSets = amountofsets;
  1521. console.log("DEBUG#TRADE CREATED");
  1522. sortSetsByAmount(botSets, (DATA) => {
  1523. console.log("DEBUG#" + DATA);
  1524. console.log("DEBUG#SETS SORTED");
  1525. firstLoop: for (let i = 0; i < DATA.length; i++) {
  1526. if (b[DATA[i]] === 0) {
  1527. continue firstLoop;
  1528. } else {
  1529. console.log("DEBUG#" + i);
  1530. console.log("DEBUG#FOR LOOP ITEMS");
  1531. if (hisMaxSets > 0) {
  1532. console.log("DEBUG#MAXSETSMORETHAN1");
  1533. if (!b[DATA[i]] && botSets[DATA[i]].length > 0) { // TODO NOT FOR LOOP WITH BOTSETS. IT SENDS ALL
  1534. // BOT HAS ENOUGH SETS AND USER NEVER CRAFTED THIS
  1535. bLoop: for (let j = 0; j < botSets[DATA[i]].length; j++) {
  1536. if (botSets[DATA[i]][j] && hisMaxSets > 0) {
  1537. t.addMyItems(botSets[DATA[i]][j]);
  1538. console.log("DEBUG#LOOP #2 CONTINUE: ITEM ADD");
  1539. hisMaxSets--;
  1540. continue firstLoop;
  1541. } else {
  1542. console.log("DEBUG#LOOP #2 CONTINUE: RETURN");
  1543. continue firstLoop;
  1544. }
  1545. }
  1546. }
  1547. } else {
  1548. console.log("DEBUG#RETURN");
  1549. break firstLoop;
  1550. }
  1551. }
  1552. }
  1553. if (hisMaxSets > 0) {
  1554. client.chatMessage(SENDER, "There are not enough sets. Please try again later.");
  1555. } else {
  1556. console.log("DEBUG#SENDING");
  1557. t.addTheirItems(theirKeys);
  1558. t.data("commandused", "BuyOne");
  1559. t.data("amountofkeys", n);
  1560. t.data("amountofsets", amountofsets.toString());
  1561. t.send((ERR, STATUS) => {
  1562. if (ERR) {
  1563. client.chatMessage(SENDER, "An error occurred while sending your trade. Steam Trades could be down. Please try again later.");
  1564. console.log("## An error occurred while sending trade: " + ERR);
  1565. } else {
  1566. client.chatMessage(SENDER, "Trade Sent! Confirming it...");
  1567. console.log("## Trade offer sent");
  1568. }
  1569. });
  1570. }
  1571. });
  1572. } else {
  1573. client.chatMessage(SENDER, "There are currently not enough sets that you have not used in stock for this amount of keys. Please try again later. If you want the bot to ignore your current badges use !buyany.");
  1574. }
  1575. // TO HERE
  1576. } else {
  1577. console.log("An error occurred while getting badges: " + ERR);
  1578. }
  1579. } else {
  1580. client.chatMessage(SENDER, "An error occurred while getting your badges. Please try again.");
  1581. console.log(SENDER, "## An error occurred while loading badges: " + ERR);
  1582. }
  1583. });
  1584. }
  1585. } else {
  1586. console.log("## An error occurred while getting inventory: " + ERR);
  1587. client.chatMessage(SENDER, "An error occurred while loading your inventory, please make sure it's set to public.");
  1588. }
  1589. }
  1590. });
  1591. } else {
  1592. client.chatMessage(SENDER, "Please make sure you don't have a trade hold!");
  1593. }
  1594. });
  1595. } else {
  1596. client.chatMessage(SENDER, "Please try a lower amount of keys.");
  1597. }
  1598. } else {
  1599. client.chatMessage(SENDER, "Please provide a valid amount of keys.");
  1600. }
  1601. } else {
  1602. client.chatMessage(SENDER, "Please try again later.");
  1603. }
  1604. }
  1605. else if (MSG.toUpperCase().indexOf("!BUYONEGEMS") >= 0) {
  1606. if (botSets) {
  1607. let n = MSG.toUpperCase().replace("!BUYONEGEMS ", ""),
  1608. amountofsets = parseInt(n);
  1609. if (!isNaN(n) && parseInt(n) > 0) {
  1610. if (n <= CONFIG.MESSAGES.MAXBUY) {
  1611. let t = manager.createOffer(SENDER.getSteamID64());
  1612. t.getUserDetails((ERR, ME, THEM) => {
  1613. if (ERR) {
  1614. console.log("## An error occurred while getting trade holds: " + ERR);
  1615. client.chatMessage(SENDER, "An error occurred while getting your trade holds. Please try again");
  1616. } else if (ME.escrowDays === 0 && THEM.escrowDays === 0) {
  1617. n = parseInt(n);
  1618. let theirGems = [];
  1619. let amountTheirGems = 0;
  1620. client.chatMessage(SENDER, "Processing your request.");
  1621. manager.getUserInventoryContents(SENDER.getSteamID64(), CONFIG.STEAMGAME, 6, true, (ERR, INV, CURR) => {
  1622. if (ERR) {
  1623. console.log("## An error occurred while getting inventory: " + ERR);
  1624. client.chatMessage(SENDER, "An error occurred while loading your inventory. Please try later");
  1625. } else {
  1626. console.log("DEBUG#INV LOADED");
  1627. if (!ERR) {
  1628. console.log("DEBUG#INV LOADED NOERR");
  1629. for (let i = 0; i < INV.length; i++) {
  1630. if (CONFIG.STEAMGEMS.indexOf(INV[i].market_hash_name) >= 0) {
  1631. amountTheirGems = INV[i].amount;
  1632. INV[i].amount = (n * CONFIG.CARDS.GEMS.buy_one_set_for);
  1633. theirGems.push(INV[i]);
  1634. break;
  1635. }
  1636. }
  1637. if (amountTheirGems < (n * CONFIG.CARDS.GEMS.buy_one_set_for)) {
  1638. client.chatMessage(SENDER, "You do not have enough Gems.");
  1639. } else {
  1640. Utils.getBadges(SENDER.getSteamID64(), (ERR, DATA) => {
  1641. if (!ERR) {
  1642. console.log("DEBUG#BADGE LOADED");
  1643. if (!ERR) {
  1644. let b = {}; // List with badges that CAN still be crafted
  1645. if (DATA) {
  1646. for (let i = 0; i < Object.keys(DATA).length; i++) {
  1647. if (DATA[Object.keys(DATA)[i]] < 6) {
  1648. b[Object.keys(DATA)[i]] = 5 - DATA[Object.keys(DATA)[i]];
  1649. }
  1650. }
  1651. } else {
  1652. client.chatMessage(SENDER.getSteamID64(), "Your badges are empty, sending an offer without checking badges.");
  1653. }
  1654. console.log(DATA);
  1655. console.log(b);
  1656. // TODO: COUNT AMOUNT OF SETS BOT CAN GIVE HIM
  1657. // 1: GET BOTS CARDS. DONE
  1658. // 2: GET PLAYER's BADGES. DONE
  1659. // 3: MAGIC
  1660. let hisMaxSets = 0,
  1661. botNSets = 0;
  1662. // Loop for sets he has partially completed
  1663. for (let i = 0; i < Object.keys(b).length; i++) {
  1664. if (botSets[Object.keys(b)[i]] && botSets[Object.keys(b)[i]].length >= 5 - b[Object.keys(b)[i]].length) {
  1665. hisMaxSets += 5 - b[Object.keys(b)[i]].length;
  1666. }
  1667. }
  1668. console.log("DEBUG#LOOP 1 DONE");
  1669. // Loop for sets he has never crafted
  1670. for (let i = 0; i < Object.keys(botSets).length; i++) {
  1671. if (Object.keys(b).indexOf(Object.keys(botSets)[i]) < 0) {
  1672. if (botSets[Object.keys(botSets)[i]].length >= 5) {
  1673. hisMaxSets += 5;
  1674. } else {
  1675. hisMaxSets += botSets[Object.keys(botSets)[i]].length;
  1676. }
  1677. }
  1678. botNSets += botSets[Object.keys(botSets)[i]].length;
  1679. }
  1680. totalBotSets = botNSets;
  1681. let playThis = CONFIG.PLAYGAMES;
  1682. if (CONFIG.PLAYGAMES && typeof(CONFIG.PLAYGAMES[0]) === "string") {
  1683. playThis[0] = parseString(playThis[0], totalBotSets);
  1684. }
  1685. client.gamesPlayed(playThis);
  1686. console.log("DEBUG#LOOP 2 DONE");
  1687. // HERE
  1688. if (amountofsets <= hisMaxSets) {
  1689. hisMaxSets = amountofsets;
  1690. console.log("DEBUG#TRADE CREATED");
  1691. sortSetsByAmount(botSets, (DATA) => {
  1692. console.log("DEBUG#" + DATA);
  1693. console.log("DEBUG#SETS SORTED");
  1694. firstLoop: for (let i = 0; i < DATA.length; i++) {
  1695. if (b[DATA[i]] === 0) {
  1696. continue firstLoop;
  1697. } else {
  1698. console.log("DEBUG#" + i);
  1699. console.log("DEBUG#FOR LOOP ITEMS");
  1700. if (hisMaxSets > 0) {
  1701. console.log("DEBUG#MAXSETSMORETHAN1");
  1702. if (!b[DATA[i]] && botSets[DATA[i]].length > 0) { // TODO NOT FOR LOOP WITH BOTSETS. IT SENDS ALL
  1703. // BOT HAS ENOUGH SETS AND USER NEVER CRAFTED THIS
  1704. bLoop: for (let j = 0; j < botSets[DATA[i]].length; j++) {
  1705. if (botSets[DATA[i]][j] && hisMaxSets > 0) {
  1706. t.addMyItems(botSets[DATA[i]][j]);
  1707. console.log("DEBUG#LOOP #2 CONTINUE: ITEM ADD");
  1708. hisMaxSets--;
  1709. continue firstLoop;
  1710. } else {
  1711. console.log("DEBUG#LOOP #2 CONTINUE: RETURN");
  1712. continue firstLoop;
  1713. }
  1714. }
  1715. }
  1716. } else {
  1717. console.log("DEBUG#RETURN");
  1718. break firstLoop;
  1719. }
  1720. }
  1721. }
  1722. if (hisMaxSets > 0) {
  1723. client.chatMessage(SENDER, "There are not enough sets. Please try again later.");
  1724. } else {
  1725. console.log("DEBUG#SENDING");
  1726. t.addTheirItems(theirGems);
  1727. t.data("commandused", "BuyOne");
  1728. t.data("amountofkeys", n);
  1729. t.data("amountofsets", amountofsets.toString());
  1730. t.send((ERR, STATUS) => {
  1731. if (ERR) {
  1732. client.chatMessage(SENDER, "An error occurred while sending your trade. Steam Trades could be down. Please try again later.");
  1733. console.log("## An error occurred while sending trade: " + ERR);
  1734. } else {
  1735. client.chatMessage(SENDER, "Trade Sent! Confirming it...");
  1736. console.log("## Trade offer sent");
  1737. }
  1738. });
  1739. }
  1740. });
  1741. } else {
  1742. client.chatMessage(SENDER, "There are currently not enough sets that you have not used in stock for this amount of keys. Please try again later. If you want the bot to ignore your current badges use !buyany.");
  1743. }
  1744. // TO HERE
  1745. } else {
  1746. console.log("An error occurred while getting badges: " + ERR);
  1747. }
  1748. } else {
  1749. client.chatMessage(SENDER, "An error occurred while getting your badges. Please try again.");
  1750. console.log(SENDER, "## An error occurred while loading badges: " + ERR);
  1751. }
  1752. });
  1753. }
  1754. } else {
  1755. console.log("## An error occurred while getting inventory: " + ERR);
  1756. client.chatMessage(SENDER, "An error occurred while loading your inventory, please make sure it's set to public.");
  1757. }
  1758. }
  1759. });
  1760. } else {
  1761. client.chatMessage(SENDER, "Please make sure you don't have a trade hold!");
  1762. }
  1763. });
  1764. } else {
  1765. client.chatMessage(SENDER, "Please try a lower amount of keys.");
  1766. }
  1767. } else {
  1768. client.chatMessage(SENDER, "Please provide a valid amount of keys.");
  1769. }
  1770. } else {
  1771. client.chatMessage(SENDER, "Please try again later.");
  1772. }
  1773. }
  1774. else if (MSG.toUpperCase().indexOf("!BUYONE") >= 0) {
  1775. if (botSets) {
  1776. let n = MSG.toUpperCase().replace("!BUYONE ", ""),
  1777. amountofsets = parseInt(n) * CONFIG.CARDS.CSGO.buy_sets_by_one;
  1778. if (!isNaN(n) && parseInt(n) > 0) {
  1779. if (n <= CONFIG.MESSAGES.MAXBUY) {
  1780. let t = manager.createOffer(SENDER.getSteamID64());
  1781. t.getUserDetails((ERR, ME, THEM) => {
  1782. if (ERR) {
  1783. console.log("## An error occurred while getting trade holds: " + ERR);
  1784. client.chatMessage(SENDER, "An error occurred while getting your trade holds. Please try again");
  1785. } else if (ME.escrowDays === 0 && THEM.escrowDays === 0) {
  1786. n = parseInt(n);
  1787. let theirKeys = [];
  1788. client.chatMessage(SENDER, "Processing your request.");
  1789. manager.getUserInventoryContents(SENDER.getSteamID64(), CONFIG.CSGOGAME, 2, true, (ERR, INV, CURR) => {
  1790. if (ERR) {
  1791. console.log("## An error occurred while getting inventory: " + ERR);
  1792. client.chatMessage(SENDER, "An error occurred while loading your inventory. Please try later");
  1793. } else {
  1794. console.log("DEBUG#INV LOADED");
  1795. if (!ERR) {
  1796. console.log("DEBUG#INV LOADED NOERR");
  1797. for (let i = 0; i < INV.length; i++) {
  1798. if (theirKeys.length < n && CONFIG.ACCEPTEDKEYS.indexOf(INV[i].market_hash_name) >= 0) {
  1799. theirKeys.push(INV[i]);
  1800. }
  1801. }
  1802. if (theirKeys.length !== n) {
  1803. client.chatMessage(SENDER, "You do not have enough keys.");
  1804. } else {
  1805. Utils.getBadges(SENDER.getSteamID64(), (ERR, DATA) => {
  1806. if (!ERR) {
  1807. console.log("DEBUG#BADGE LOADED");
  1808. if (!ERR) {
  1809. let b = {}; // List with badges that CAN still be crafted
  1810. if (DATA) {
  1811. for (let i = 0; i < Object.keys(DATA).length; i++) {
  1812. if (DATA[Object.keys(DATA)[i]] < 6) {
  1813. b[Object.keys(DATA)[i]] = 5 - DATA[Object.keys(DATA)[i]];
  1814. }
  1815. }
  1816. } else {
  1817. client.chatMessage(SENDER.getSteamID64(), "Your badges are empty, sending an offer without checking badges.");
  1818. }
  1819. console.log(DATA);
  1820. console.log(b);
  1821. // TODO: COUNT AMOUNT OF SETS BOT CAN GIVE HIM
  1822. // 1: GET BOTS CARDS. DONE
  1823. // 2: GET PLAYER's BADGES. DONE
  1824. // 3: MAGIC
  1825. let hisMaxSets = 0,
  1826. botNSets = 0;
  1827. // Loop for sets he has partially completed
  1828. for (let i = 0; i < Object.keys(b).length; i++) {
  1829. if (botSets[Object.keys(b)[i]] && botSets[Object.keys(b)[i]].length >= 5 - b[Object.keys(b)[i]].length) {
  1830. hisMaxSets += 5 - b[Object.keys(b)[i]].length;
  1831. }
  1832. }
  1833. console.log("DEBUG#LOOP 1 DONE");
  1834. // Loop for sets he has never crafted
  1835. for (let i = 0; i < Object.keys(botSets).length; i++) {
  1836. if (Object.keys(b).indexOf(Object.keys(botSets)[i]) < 0) {
  1837. if (botSets[Object.keys(botSets)[i]].length >= 5) {
  1838. hisMaxSets += 5;
  1839. } else {
  1840. hisMaxSets += botSets[Object.keys(botSets)[i]].length;
  1841. }
  1842. }
  1843. botNSets += botSets[Object.keys(botSets)[i]].length;
  1844. }
  1845. totalBotSets = botNSets;
  1846. let playThis = CONFIG.PLAYGAMES;
  1847. if (CONFIG.PLAYGAMES && typeof(CONFIG.PLAYGAMES[0]) === "string") {
  1848. playThis[0] = parseString(playThis[0], totalBotSets);
  1849. }
  1850. client.gamesPlayed(playThis);
  1851. console.log("DEBUG#LOOP 2 DONE");
  1852. // HERE
  1853. if (amountofsets <= hisMaxSets) {
  1854. hisMaxSets = amountofsets;
  1855. console.log("DEBUG#TRADE CREATED");
  1856. sortSetsByAmount(botSets, (DATA) => {
  1857. console.log("DEBUG#" + DATA);
  1858. console.log("DEBUG#SETS SORTED");
  1859. firstLoop: for (let i = 0; i < DATA.length; i++) {
  1860. if (b[DATA[i]] === 0) {
  1861. continue firstLoop;
  1862. } else {
  1863. console.log("DEBUG#" + i);
  1864. console.log("DEBUG#FOR LOOP ITEMS");
  1865. if (hisMaxSets > 0) {
  1866. console.log("DEBUG#MAXSETSMORETHAN1");
  1867. if (!b[DATA[i]] && botSets[DATA[i]].length > 0) { // TODO NOT FOR LOOP WITH BOTSETS. IT SENDS ALL
  1868. // BOT HAS ENOUGH SETS AND USER NEVER CRAFTED THIS
  1869. bLoop: for (let j = 0; j < botSets[DATA[i]].length; j++) {
  1870. if (botSets[DATA[i]][j] && hisMaxSets > 0) {
  1871. t.addMyItems(botSets[DATA[i]][j]);
  1872. console.log("DEBUG#LOOP #2 CONTINUE: ITEM ADD");
  1873. hisMaxSets--;
  1874. continue firstLoop;
  1875. } else {
  1876. console.log("DEBUG#LOOP #2 CONTINUE: RETURN");
  1877. continue firstLoop;
  1878. }
  1879. }
  1880. }
  1881. } else {
  1882. console.log("DEBUG#RETURN");
  1883. break firstLoop;
  1884. }
  1885. }
  1886. }
  1887. if (hisMaxSets > 0) {
  1888. client.chatMessage(SENDER, "There are not enough sets. Please try again later.");
  1889. } else {
  1890. console.log("DEBUG#SENDING");
  1891. t.addTheirItems(theirKeys);
  1892. t.data("commandused", "BuyOne");
  1893. t.data("amountofkeys", n);
  1894. t.data("amountofsets", amountofsets.toString());
  1895. t.send((ERR, STATUS) => {
  1896. if (ERR) {
  1897. client.chatMessage(SENDER, "An error occurred while sending your trade. Steam Trades could be down. Please try again later.");
  1898. console.log("## An error occurred while sending trade: " + ERR);
  1899. } else {
  1900. client.chatMessage(SENDER, "Trade Sent! Confirming it...");
  1901. console.log("## Trade offer sent");
  1902. }
  1903. });
  1904. }
  1905. });
  1906. } else {
  1907. client.chatMessage(SENDER, "There are currently not enough sets that you have not used in stock for this amount of keys. Please try again later. If you want the bot to ignore your current badges use !buyany.");
  1908. }
  1909. // TO HERE
  1910. } else {
  1911. console.log("An error occurred while getting badges: " + ERR);
  1912. }
  1913. } else {
  1914. client.chatMessage(SENDER, "An error occurred while getting your badges. Please try again.");
  1915. console.log(SENDER, "## An error occurred while loading badges: " + ERR);
  1916. }
  1917. });
  1918. }
  1919. } else {
  1920. console.log("## An error occurred while getting inventory: " + ERR);
  1921. client.chatMessage(SENDER, "An error occurred while loading your inventory, please make sure it's set to public.");
  1922. }
  1923. }
  1924. });
  1925. } else {
  1926. client.chatMessage(SENDER, "Please make sure you don't have a trade hold!");
  1927. }
  1928. });
  1929. } else {
  1930. client.chatMessage(SENDER, "Please try a lower amount of keys.");
  1931. }
  1932. } else {
  1933. client.chatMessage(SENDER, "Please provide a valid amount of keys.");
  1934. }
  1935. } else {
  1936. client.chatMessage(SENDER, "Please try again later.");
  1937. }
  1938. }
  1939.  
  1940. else if (MSG.toUpperCase().indexOf("!BUYANYTF") >= 0) {
  1941. if (botSets) {
  1942. let n = MSG.toUpperCase().replace("!BUYANYTF ", ""),
  1943. amountofsets = parseInt(n) * CONFIG.CARDS.TF2.buy_sets_by_one;
  1944. if (!isNaN(n) && parseInt(n) > 0) {
  1945. if (n <= CONFIG.MESSAGES.MAXBUY) {
  1946. let t = manager.createOffer(SENDER.getSteamID64());
  1947. n = parseInt(n);
  1948. let theirKeys = [];
  1949. t.getUserDetails((ERR, ME, THEM) => {
  1950. if (ERR) {
  1951. console.log("## An error occurred while getting trade holds: " + ERR);
  1952. client.chatMessage(SENDER, "An error occurred while getting your trade holds. Please try again");
  1953. } else if (ME.escrowDays === 0 && THEM.escrowDays === 0) {
  1954. client.chatMessage(SENDER, "Processing your request.");
  1955. manager.getUserInventoryContents(SENDER.getSteamID64(), CONFIG.TFGAME, 2, true, (ERR, INV, CURR) => {
  1956. if (ERR) {
  1957. console.log("## An error occurred while getting inventory: " + ERR);
  1958. client.chatMessage(SENDER, "An error occurred while loading your inventory. Please try later");
  1959. } else {
  1960. let amountofB = amountofsets;
  1961. for (let i = 0; i < INV.length; i++) {
  1962. if (theirKeys.length < n && CONFIG.TFACCEPTEDKEYS.indexOf(INV[i].market_hash_name) >= 0) {
  1963. theirKeys.push(INV[i]);
  1964. }
  1965. }
  1966. if (theirKeys.length !== n) {
  1967. client.chatMessage(SENDER, "You do not have enough keys.");
  1968. } else {
  1969. sortSetsByAmount(botSets, (DATA) => {
  1970. let setsSent = {};
  1971. firstLoop: for (let i = 0; i < DATA.length; i++) {
  1972. console.log(setsSent);
  1973. console.log(DATA[i]);
  1974. if (botSets[DATA[i]]) {
  1975. for (let j = 0; j < botSets[DATA[i]].length; j++) {
  1976. if (amountofB > 0) {
  1977. if ((setsSent[DATA[i]] && setsSent[DATA[i]] < 5) || !setsSent[DATA[i]]) {
  1978. t.addMyItems(botSets[DATA[i]][j]);
  1979. console.log("DEBUG#LOOP #2 CONTINUE: ITEM ADD");
  1980. amountofB--;
  1981. if (!setsSent[DATA[i]]) {
  1982. setsSent[DATA[i]] = 1;
  1983. } else {
  1984. setsSent[DATA[i]] += 1;
  1985. }
  1986. } else {
  1987. console.log("DEBUG#LOOP #2 CONTINUE: RETURN");
  1988. continue firstLoop;
  1989. }
  1990. } else {
  1991. console.log("DEBUG#LOOP #2 CONTINUE: RETURN");
  1992. continue firstLoop;
  1993. }
  1994. }
  1995. } else {
  1996. console.log("DEBUG#LOOP #2 CONTINUE: RETURN 2");
  1997. continue firstLoop;
  1998. }
  1999. }
  2000. });
  2001. }
  2002. if (amountofB > 0) {
  2003. client.chatMessage(SENDER, "There are not enough sets. Please try again later.");
  2004. } else {
  2005. console.log("DEBUG#SENDING");
  2006. t.addTheirItems(theirKeys);
  2007. t.data("commandused", "BuyAny");
  2008. t.data("amountofsets", amountofsets.toString());
  2009. t.data("amountofkeys", n);
  2010. t.send((ERR, STATUS) => {
  2011. if (ERR) {
  2012. client.chatMessage(SENDER, "An error occurred while sending your trade. Steam Trades could be down. Please try again later.");
  2013. console.log("## An error occurred while sending trade: " + ERR);
  2014. } else {
  2015. client.chatMessage(SENDER, "Trade Sent! Confirming it...");
  2016. console.log("## Trade offer sent!");
  2017. }
  2018. });
  2019. }
  2020. }
  2021. });
  2022. } else {
  2023. client.chatMessage(SENDER, "Please make sure you don't have a trade hold!");
  2024. }
  2025. });
  2026. } else {
  2027. client.chatMessage(SENDER, "Please try a lower amount of keys");
  2028. }
  2029. } else {
  2030. client.chatMessage(SENDER, "Please provide a valid amount of keys.");
  2031. }
  2032. } else {
  2033. client.chatMessage(SENDER, "Please try again later.");
  2034. }
  2035. }
  2036. else if (MSG.toUpperCase().indexOf("!BUYANYGEMS") >= 0) {
  2037. if (botSets) {
  2038. let n = MSG.toUpperCase().replace("!BUYANYGEMS ", ""),
  2039. amountofsets = parseInt(n);
  2040. if (!isNaN(n) && parseInt(n) > 0) {
  2041. if (n <= CONFIG.MESSAGES.MAXBUY) {
  2042. let t = manager.createOffer(SENDER.getSteamID64());
  2043. n = parseInt(n);
  2044. let theirGems = [];
  2045. let amountTheirGems = 0;
  2046. t.getUserDetails((ERR, ME, THEM) => {
  2047. if (ERR) {
  2048. console.log("## An error occurred while getting trade holds: " + ERR);
  2049. client.chatMessage(SENDER, "An error occurred while getting your trade holds. Please try again");
  2050. } else if (ME.escrowDays === 0 && THEM.escrowDays === 0) {
  2051. client.chatMessage(SENDER, "Processing your request.");
  2052. manager.getUserInventoryContents(SENDER.getSteamID64(), CONFIG.STEAMGAME, 6, true, (ERR, INV, CURR) => {
  2053. if (ERR) {
  2054. console.log("## An error occurred while getting inventory: " + ERR);
  2055. client.chatMessage(SENDER, "An error occurred while loading your inventory. Please try later");
  2056. } else {
  2057. let amountofB = amountofsets;
  2058. for (let i = 0; i < INV.length; i++) {
  2059. if (CONFIG.STEAMGEMS.indexOf(INV[i].market_hash_name) >= 0) {
  2060. amountTheirGems = INV[i].amount;
  2061. INV[i].amount = (n * CONFIG.CARDS.GEMS.buy_one_set_for);
  2062. theirGems.push(INV[i]);
  2063. break;
  2064. }
  2065. }
  2066. if (amountTheirGems < ( n * CONFIG.CARDS.GEMS.buy_one_set_for )) {
  2067. client.chatMessage(SENDER, "You do not have enough Gems.");
  2068. } else {
  2069. sortSetsByAmount(botSets, (DATA) => {
  2070. let setsSent = {};
  2071. firstLoop: for (let i = 0; i < DATA.length; i++) {
  2072. console.log(setsSent);
  2073. console.log(DATA[i]);
  2074. if (botSets[DATA[i]]) {
  2075. for (let j = 0; j < botSets[DATA[i]].length; j++) {
  2076. if (amountofB > 0) {
  2077. if ((setsSent[DATA[i]] && setsSent[DATA[i]] < 5) || !setsSent[DATA[i]]) {
  2078. t.addMyItems(botSets[DATA[i]][j]);
  2079. console.log("DEBUG#LOOP #2 CONTINUE: ITEM ADD");
  2080. amountofB--;
  2081. if (!setsSent[DATA[i]]) {
  2082. setsSent[DATA[i]] = 1;
  2083. } else {
  2084. setsSent[DATA[i]] += 1;
  2085. }
  2086. } else {
  2087. console.log("DEBUG#LOOP #2 CONTINUE: RETURN");
  2088. continue firstLoop;
  2089. }
  2090. } else {
  2091. console.log("DEBUG#LOOP #2 CONTINUE: RETURN");
  2092. continue firstLoop;
  2093. }
  2094. }
  2095. } else {
  2096. console.log("DEBUG#LOOP #2 CONTINUE: RETURN 2");
  2097. continue firstLoop;
  2098. }
  2099. }
  2100. });
  2101. }
  2102. if (amountofB > 0) {
  2103. client.chatMessage(SENDER, "There are not enough sets. Please try again later.");
  2104. } else {
  2105. console.log("DEBUG#SENDING");
  2106. t.addTheirItems(theirGems);
  2107. t.data("commandused", "BuyAny");
  2108. t.data("amountofsets", amountofsets.toString());
  2109. t.data("amountofkeys", n);
  2110. t.send((ERR, STATUS) => {
  2111. if (ERR) {
  2112. client.chatMessage(SENDER, "An error occurred while sending your trade. Steam Trades could be down. Please try again later.");
  2113. console.log("## An error occurred while sending trade: " + ERR);
  2114. } else {
  2115. client.chatMessage(SENDER, "Trade Sent! Confirming it...");
  2116. console.log("## Trade offer sent!");
  2117. }
  2118. });
  2119. }
  2120. }
  2121. });
  2122. } else {
  2123. client.chatMessage(SENDER, "Please make sure you don't have a trade hold!");
  2124. }
  2125. });
  2126. } else {
  2127. client.chatMessage(SENDER, "Please try a lower amount of keys");
  2128. }
  2129. } else {
  2130. client.chatMessage(SENDER, "Please provide a valid amount of keys.");
  2131. }
  2132. } else {
  2133. client.chatMessage(SENDER, "Please try again later.");
  2134. }
  2135. }
  2136. else if (MSG.toUpperCase().indexOf("!BUYANY") >= 0) {
  2137. if (botSets) {
  2138. let n = MSG.toUpperCase().replace("!BUYANY ", ""),
  2139. amountofsets = parseInt(n) * CONFIG.CARDS.CSGO.buy_sets_by_one;
  2140. if (!isNaN(n) && parseInt(n) > 0) {
  2141. if (n <= CONFIG.MESSAGES.MAXBUY) {
  2142. let t = manager.createOffer(SENDER.getSteamID64());
  2143. n = parseInt(n);
  2144. let theirKeys = [];
  2145. t.getUserDetails((ERR, ME, THEM) => {
  2146. if (ERR) {
  2147. console.log("## An error occurred while getting trade holds: " + ERR);
  2148. client.chatMessage(SENDER, "An error occurred while getting your trade holds. Please try again");
  2149. } else if (ME.escrowDays === 0 && THEM.escrowDays === 0) {
  2150. client.chatMessage(SENDER, "Processing your request.");
  2151. manager.getUserInventoryContents(SENDER.getSteamID64(), CONFIG.CSGOGAME, 2, true, (ERR, INV, CURR) => {
  2152. if (ERR) {
  2153. console.log("## An error occurred while getting inventory: " + ERR);
  2154. client.chatMessage(SENDER, "An error occurred while loading your inventory. Please try later");
  2155. } else {
  2156. let amountofB = amountofsets;
  2157. for (let i = 0; i < INV.length; i++) {
  2158. if (theirKeys.length < n && CONFIG.ACCEPTEDKEYS.indexOf(INV[i].market_hash_name) >= 0) {
  2159. theirKeys.push(INV[i]);
  2160. }
  2161. }
  2162. if (theirKeys.length !== n) {
  2163. client.chatMessage(SENDER, "You do not have enough keys.");
  2164. } else {
  2165. sortSetsByAmount(botSets, (DATA) => {
  2166. let setsSent = {};
  2167. firstLoop: for (let i = 0; i < DATA.length; i++) {
  2168. console.log(setsSent);
  2169. console.log(DATA[i]);
  2170. if (botSets[DATA[i]]) {
  2171. for (let j = 0; j < botSets[DATA[i]].length; j++) {
  2172. if (amountofB > 0) {
  2173. if ((setsSent[DATA[i]] && setsSent[DATA[i]] < 5) || !setsSent[DATA[i]]) {
  2174. t.addMyItems(botSets[DATA[i]][j]);
  2175. console.log("DEBUG#LOOP #2 CONTINUE: ITEM ADD");
  2176. amountofB--;
  2177. if (!setsSent[DATA[i]]) {
  2178. setsSent[DATA[i]] = 1;
  2179. } else {
  2180. setsSent[DATA[i]] += 1;
  2181. }
  2182. } else {
  2183. console.log("DEBUG#LOOP #2 CONTINUE: RETURN");
  2184. continue firstLoop;
  2185. }
  2186. } else {
  2187. console.log("DEBUG#LOOP #2 CONTINUE: RETURN");
  2188. continue firstLoop;
  2189. }
  2190. }
  2191. } else {
  2192. console.log("DEBUG#LOOP #2 CONTINUE: RETURN 2");
  2193. continue firstLoop;
  2194. }
  2195. }
  2196. });
  2197. }
  2198. if (amountofB > 0) {
  2199. client.chatMessage(SENDER, "There are not enough sets. Please try again later.");
  2200. } else {
  2201. console.log("DEBUG#SENDING");
  2202. t.addTheirItems(theirKeys);
  2203. t.data("commandused", "BuyAny");
  2204. t.data("amountofsets", amountofsets.toString());
  2205. t.data("amountofkeys", n);
  2206. t.send((ERR, STATUS) => {
  2207. if (ERR) {
  2208. client.chatMessage(SENDER, "An error occurred while sending your trade. Steam Trades could be down. Please try again later.");
  2209. console.log("## An error occurred while sending trade: " + ERR);
  2210. } else {
  2211. client.chatMessage(SENDER, "Trade Sent! Confirming it...");
  2212. console.log("## Trade offer sent!");
  2213. }
  2214. });
  2215. }
  2216. }
  2217. });
  2218. } else {
  2219. client.chatMessage(SENDER, "Please make sure you don't have a trade hold!");
  2220. }
  2221. });
  2222. } else {
  2223. client.chatMessage(SENDER, "Please try a lower amount of keys");
  2224. }
  2225. } else {
  2226. client.chatMessage(SENDER, "Please provide a valid amount of keys.");
  2227. }
  2228. } else {
  2229. client.chatMessage(SENDER, "Please try again later.");
  2230. }
  2231. }
  2232.  
  2233. else if (MSG.toUpperCase() === "!OWNER") {
  2234. client.chatMessage(SENDER, "My steam account: \r\n" + CONFIG.OWNERLINK);
  2235. }
  2236. else if (MSG.toUpperCase() === "!CREDITS") {
  2237. client.chatMessage(SENDER, CONFIG.CREDITS);
  2238. }
  2239. else if (CONFIG.ADMINS.indexOf(SENDER.getSteamID64()) >= 0 || CONFIG.ADMINS.indexOf(parseInt(SENDER.getSteamID64())) >= 0) {
  2240. // Admin commands.
  2241. if (MSG.toUpperCase() === "!ADMIN") {
  2242. client.chatMessage(SENDER, "List of admin commands:\n" +
  2243. "!withdraw - withdraw x amount of CS:GO keys\n" +
  2244. "!tfwithdraw - withdraw x amount of TFkeys\n" +
  2245. "!gemswithdraw - withdraw x amount of Gems\n" +
  2246. "!deposit - deposit x amount of CS:GO keys\n" +
  2247. "!tfdeposit - deposit x amount of TF keys\n" +
  2248. "!gemsdeposit - deposit x amount of Gems\n" +
  2249. "!shutdown - logoff bot account and close application\n" +
  2250. "!restart - logoff and login account\n" +
  2251. "!block - block desired steam user\n" +
  2252. "!unblock - unblock desired steam user\n" +
  2253. "!stock - send a trade offer to owner requesting all available sets to trade\n" +
  2254. "!profit - show information about bot sells/buy");
  2255. }
  2256. else if (MSG.toUpperCase().indexOf("!WITHDRAW") >= 0) {
  2257. let amountkeys = parseInt(MSG.toUpperCase().replace("!WITHDRAW ", ""))
  2258. if (!isNaN(amountkeys) && parseInt(amountkeys) > 0) {
  2259. manager.getInventoryContents(CONFIG.CSGOGAME, 2, true, (ERR, INV, CURR) => {
  2260. if (ERR) {
  2261. client.chatMessage(SENDER, "An error occurred while loading the bot's inventory.");
  2262. console.log("## An error occurred while getting inventory: " + ERR);
  2263. } else {
  2264. let botkeys = 0;
  2265. let t = manager.createOffer(SENDER.getSteamID64());
  2266. let added = 0;
  2267. for (let i = 0; i < INV.length; i++) {
  2268. if (CONFIG.ACCEPTEDKEYS.indexOf(INV[i].market_hash_name) >= 0) {
  2269. botkeys++;
  2270. if (added < amountkeys) {
  2271. t.addMyItem(INV[i]);
  2272. added++;
  2273. }
  2274. }
  2275. }
  2276. if (botkeys < amountkeys)
  2277. client.chatMessage(SENDER, "Bot don't have enough keys to send. (He has " + botkeys + " keys)");
  2278. else {
  2279. t.send();
  2280. client.chatMessage(SENDER, amountkeys + " keys sent to your account.");
  2281. }
  2282. }
  2283. });
  2284. } else {
  2285. client.chatMessage(SENDER, "Please enter a valid amount of keys!");
  2286. }
  2287. }
  2288. else if (MSG.toUpperCase().indexOf("!ADDGAME") >= 0) {
  2289. let appid = parseInt(MSG.toUpperCase().replace("!ADDGAME ", ""));
  2290. if (!isNaN(appid) && parseInt(appid) > 0) {
  2291. let key = MSG.toUpperCase().replace("!ADDGAME " + appid + " ", "");
  2292. if (key !== "") {
  2293. let gamesStock = jsonfile.readFileSync(gamesstockfilename);
  2294. if (typeof gamesStock.stock[appid] !== 'undefined') {
  2295. gamesStock.stock[appid].keys.push(key);
  2296. fs.writeFile(gamesstockfilename, JSON.stringify(gamesStock, null, "\t"), function (err) {
  2297. if (err) return console.log(err);
  2298. client.chatMessage(SENDER, "Key successfully added to database!");
  2299. });
  2300. } else {
  2301. client.chatMessage(SENDER, "Game with this AppID is not in database. Please make it before adding key.");
  2302. }
  2303. } else
  2304. client.chatMessage(SENDER, "Please enter a valid key!");
  2305. } else
  2306. client.chatMessage(SENDER, "Please enter a valid AppID!");
  2307. }
  2308. else if (MSG.toUpperCase().indexOf("!TFWITHDRAW") >= 0) {
  2309. let amountkeys = parseInt(MSG.toUpperCase().replace("!TFWITHDRAW ", ""))
  2310. if (!isNaN(amountkeys) && parseInt(amountkeys) > 0) {
  2311. manager.getInventoryContents(CONFIG.TFGAME, 2, true, (ERR, INV, CURR) => {
  2312. if (ERR) {
  2313. client.chatMessage(SENDER, "An error occurred while loading the bot's inventory.");
  2314. console.log("## An error occurred while getting inventory: " + ERR);
  2315. } else {
  2316. let botkeys = 0;
  2317. let t = manager.createOffer(SENDER.getSteamID64());
  2318. let added = 0;
  2319. for (let i = 0; i < INV.length; i++) {
  2320. if (CONFIG.TFACCEPTEDKEYS.indexOf(INV[i].market_hash_name) >= 0) {
  2321. botkeys++;
  2322. if (added < amountkeys) {
  2323. t.addMyItem(INV[i]);
  2324. added++;
  2325. }
  2326. }
  2327. }
  2328. if (botkeys < amountkeys)
  2329. client.chatMessage(SENDER, "Bot don't have enough keys to send. (He has " + botkeys + " keys)");
  2330. else {
  2331. t.send();
  2332. client.chatMessage(SENDER, amountkeys + " keys sent to your account.");
  2333. }
  2334. }
  2335. });
  2336. } else {
  2337. client.chatMessage(SENDER, "Please enter a valid amount of keys!");
  2338. }
  2339. }
  2340. else if (MSG.toUpperCase().indexOf("!GEMSWITHDRAW") >= 0) {
  2341. let amountgems = parseInt(MSG.toUpperCase().replace("!GEMSWITHDRAW ", ""))
  2342. if (!isNaN(amountgems) && parseInt(amountgems) > 0) {
  2343. manager.getInventoryContents(CONFIG.STEAMGAME, 6, true, (ERR, INV, CURR) => {
  2344. if (ERR) {
  2345. client.chatMessage(SENDER, "An error occurred while loading the bot's inventory.");
  2346. console.log("## An error occurred while getting inventory: " + ERR);
  2347. } else {
  2348. let botgems = 0;
  2349. let t = manager.createOffer(SENDER.getSteamID64());
  2350. for (let i = 0; i < INV.length; i++) {
  2351. if (CONFIG.STEAMGEMS.indexOf(INV[i].market_hash_name) >= 0) {
  2352. botgems = INV[i].amount;
  2353. if (INV[i].amount >= amountgems) {
  2354. INV[i].amount = amountgems;
  2355. t.addMyItem(INV[i]);
  2356. break;
  2357. }
  2358. }
  2359. }
  2360. if (botgems < amountgems)
  2361. client.chatMessage(SENDER, "Bot don't have enough gems to send. (He has " + botgems + " gems)");
  2362. else {
  2363. t.send();
  2364. client.chatMessage(SENDER, amountgems + " gems sent to your account.");
  2365. }
  2366. }
  2367. });
  2368. } else {
  2369. client.chatMessage(SENDER, "Please enter a valid amount of gems!");
  2370. }
  2371. }
  2372. else if (MSG.toUpperCase().indexOf("!DEPOSIT") >= 0) {
  2373. let amountkeys = parseInt(MSG.toUpperCase().replace("!DEPOSIT ", ""));
  2374. if (!isNaN(amountkeys) && parseInt(amountkeys) > 0) {
  2375. let t = manager.createOffer(SENDER.getSteamID64());
  2376. t.getUserDetails((ERR, ME, THEM) => {
  2377. if (ERR) {
  2378. console.log("## An error occurred while getting trade holds: " + ERR);
  2379. client.chatMessage(SENDER, "An error occurred while getting your trade holds. Please try again");
  2380. } else if (ME.escrowDays === 0 && THEM.escrowDays === 0) {
  2381. let theirKeys = [];
  2382. client.chatMessage(SENDER, "Processing your request.");
  2383. manager.getUserInventoryContents(SENDER.getSteamID64(), CONFIG.CSGOGAME, 2, true, (ERR, INV, CURR) => {
  2384. if (ERR) {
  2385. console.log("## An error occurred while getting inventory: " + ERR);
  2386. client.chatMessage(SENDER, "An error occurred while loading your inventory. Please try later");
  2387. } else {
  2388. let theirKeys = [];
  2389. for (let i = 0; i < INV.length; i++) {
  2390. if (CONFIG.ACCEPTEDKEYS.indexOf(INV[i].market_hash_name) >= 0) {
  2391. theirKeys.push(INV[i]);
  2392. }
  2393. }
  2394. if (theirKeys.length < amountkeys) {
  2395. client.chatMessage(SENDER, "You don't have enough keys to send. (You have " + theirKeys.length + " keys)");
  2396. } else {
  2397. t.addTheirItems(theirKeys);
  2398. t.send();
  2399. client.chatMessage(SENDER, "You sent me " + amountkeys + " keys.");
  2400. }
  2401. }
  2402. });
  2403. } else {
  2404. client.chatMessage(SENDER, "Please make sure you don't have a trade hold!");
  2405. }
  2406. });
  2407. } else {
  2408. client.chatMessage(SENDER, "Please enter a valid amount of keys!");
  2409. }
  2410. }
  2411. else if (MSG.toUpperCase().indexOf("!TFDEPOSIT") >= 0) {
  2412. let amountkeys = parseInt(MSG.toUpperCase().replace("!TFDEPOSIT ", ""));
  2413. if (!isNaN(amountkeys) && parseInt(amountkeys) > 0) {
  2414. let t = manager.createOffer(SENDER.getSteamID64());
  2415. t.getUserDetails((ERR, ME, THEM) => {
  2416. if (ERR) {
  2417. console.log("## An error occurred while getting trade holds: " + ERR);
  2418. client.chatMessage(SENDER, "An error occurred while getting your trade holds. Please try again");
  2419. } else if (ME.escrowDays === 0 && THEM.escrowDays === 0) {
  2420. let theirKeys = [];
  2421. client.chatMessage(SENDER, "Processing your request.");
  2422. manager.getUserInventoryContents(SENDER.getSteamID64(), CONFIG.TFGAME, 2, true, (ERR, INV, CURR) => {
  2423. if (ERR) {
  2424. console.log("## An error occurred while getting inventory: " + ERR);
  2425. client.chatMessage(SENDER, "An error occurred while loading your inventory. Please try later");
  2426. } else {
  2427. let theirKeys = [];
  2428. for (let i = 0; i < INV.length; i++) {
  2429. if (CONFIG.TFACCEPTEDKEYS.indexOf(INV[i].market_hash_name) >= 0) {
  2430. theirKeys.push(INV[i]);
  2431. }
  2432. }
  2433. if (theirKeys.length < amountkeys) {
  2434. client.chatMessage(SENDER, "You don't have enough keys to send. (You have " + theirKeys.length + " keys)");
  2435. } else {
  2436. t.addTheirItems(theirKeys);
  2437. t.send();
  2438. client.chatMessage(SENDER, "You sent me " + amountkeys + " keys.");
  2439. }
  2440. }
  2441. });
  2442. } else {
  2443. client.chatMessage(SENDER, "Please make sure you don't have a trade hold!");
  2444. }
  2445. });
  2446. } else {
  2447. client.chatMessage(SENDER, "Please enter a valid amount of keys!");
  2448. }
  2449. }
  2450. else if (MSG.toUpperCase().indexOf("!GEMSDEPOSIT") >= 0) {
  2451. let amountgems = parseInt(MSG.toUpperCase().replace("!GEMSDEPOSIT ", ""));
  2452. if (!isNaN(amountgems) && parseInt(amountgems) > 0) {
  2453. let t = manager.createOffer(SENDER.getSteamID64());
  2454. t.getUserDetails((ERR, ME, THEM) => {
  2455. if (ERR) {
  2456. console.log("## An error occurred while getting trade holds: " + ERR);
  2457. client.chatMessage(SENDER, "An error occurred while getting your trade holds. Please try again");
  2458. } else if (ME.escrowDays === 0 && THEM.escrowDays === 0) {
  2459. let theirKeys = [];
  2460. client.chatMessage(SENDER, "Processing your request.");
  2461. manager.getUserInventoryContents(SENDER.getSteamID64(), CONFIG.STEAMGAME, 6, true, (ERR, INV, CURR) => {
  2462. if (ERR) {
  2463. console.log("## An error occurred while getting inventory: " + ERR);
  2464. client.chatMessage(SENDER, "An error occurred while loading your inventory. Please try later");
  2465. } else {
  2466. let theirKeys = [];
  2467. let botgems = 0;
  2468. for (let i = 0; i < INV.length; i++) {
  2469. if (CONFIG.STEAMGEMS.indexOf(INV[i].market_hash_name) >= 0) {
  2470. botgems = INV[i].amount;
  2471. if (INV[i].amount >= amountgems) {
  2472. INV[i].amount = amountgems;
  2473. t.addTheirItem(INV[i]);
  2474. break;
  2475. }
  2476. }
  2477. }
  2478. if (botgems < amountgems) {
  2479. client.chatMessage(SENDER, "You don't have enough gems to send. (You have " + botgems + " gems)");
  2480. } else {
  2481. t.send();
  2482. client.chatMessage(SENDER, "You sent me " + amountgems + " gems.");
  2483. }
  2484. }
  2485. });
  2486. } else {
  2487. client.chatMessage(SENDER, "Please make sure you don't have a trade hold!");
  2488. }
  2489. });
  2490. } else {
  2491. client.chatMessage(SENDER, "Please enter a valid amount of keys!");
  2492. }
  2493. }
  2494. else if (MSG.toUpperCase() === "!SHUTDOWN") {
  2495. client.chatMessage(SENDER, "GoodBye!");
  2496. client.logOff();
  2497. process.exit(69)
  2498. }
  2499. else if (MSG.toUpperCase() === "!RESTART") {
  2500. client.chatMessage(SENDER, "Relogging!");
  2501. client.relog();
  2502. }
  2503. else if (MSG.toUpperCase().indexOf("!BLOCK") >= 0) {
  2504. let n = MSG.toUpperCase().replace("!BLOCK ", "").toString();
  2505. if (SID64REGEX.test(n)) {
  2506. client.chatMessage(SENDER, "User blocked.");
  2507. client.blockUser(n);
  2508. } else {
  2509. client.chatMessage(SENDER, "Please provide a valid SteamID64");
  2510. }
  2511. }
  2512. else if (MSG.toUpperCase().indexOf("!UNBLOCK") >= 0) {
  2513. let n = MSG.toUpperCase().replace("!UNBLOCK ", "").toString();
  2514. if (SID64REGEX.test(n)) {
  2515. client.chatMessage(SENDER, "User unblocked.");
  2516. client.unblockUser(n);
  2517. } else {
  2518. client.chatMessage(SENDER, "Please provide a valid SteamID64");
  2519. }
  2520. }
  2521. else if (MSG.toUpperCase().indexOf("!USERCHECK") >= 0) {
  2522. let n = MSG.toUpperCase().replace("!USERCHECK ", "").toString();
  2523. if (SID64REGEX.test(n)) {
  2524. if (Object.keys(botSets).length > 0) {
  2525. client.chatMessage(SENDER, "Loading badges...");
  2526. Utils.getBadges(n, (ERR, DATA) => {
  2527. if (!ERR) {
  2528. let b = {}; // List with badges that CAN still be crafted
  2529. if (DATA) {
  2530. for (let i = 0; i < Object.keys(DATA).length; i++) {
  2531. if (DATA[Object.keys(DATA)[i]] < 6) {
  2532. b[Object.keys(DATA)[i]] = 5 - DATA[Object.keys(DATA)[i]];
  2533. }
  2534. }
  2535. } else {
  2536. client.chatMessage(SENDER.getSteamID64(), n + "'s badges are empty, sending an offer without checking badges.");
  2537. }
  2538. console.log(b);
  2539. // TODO: COUNT AMOUNT OF SETS BOT CAN GIVE HIM
  2540. // 1: GET BOTS CARDS. DONE
  2541. // 2: GET PLAYER's BADGES. DONE
  2542. // 3: MAGIC
  2543. let hisMaxSets = 0,
  2544. botNSets = 0;
  2545. // Loop for sets he has partially completed
  2546. for (let i = 0; i < Object.keys(b).length; i++) {
  2547. if (botSets[Object.keys(b)[i]] && botSets[Object.keys(b)[i]].length >= 5 - b[Object.keys(b)[i]].length) {
  2548. hisMaxSets += 5 - b[Object.keys(b)[i]].length;
  2549. }
  2550. }
  2551. // Loop for sets he has never crafted
  2552. for (let i = 0; i < Object.keys(botSets).length; i++) {
  2553. if (Object.keys(b).indexOf(Object.keys(botSets)[i]) < 0) {
  2554. if (botSets[Object.keys(botSets)[i]].length >= 5) {
  2555. hisMaxSets += 5;
  2556. } else {
  2557. hisMaxSets += botSets[Object.keys(botSets)[i]].length;
  2558. }
  2559. }
  2560. botNSets += botSets[Object.keys(botSets)[i]].length;
  2561. }
  2562. client.chatMessage(SENDER, "There are currently " + hisMaxSets + "/" + botNSets + " sets available which " + n + " has not fully crafted yet. Buying all of them will cost " + parseInt(hisMaxSets / CONFIG.CARDS.CSGO.BUY1FORAMOUNTOFSETS * 100) / 100 + " keys.");
  2563. } else {
  2564. client.chatMessage(SENDER, "An error occurred while getting " + n + "'s badges. Please try again.");
  2565. console.log("An error occurred while getting badges: " + ERR);
  2566. }
  2567. });
  2568. } else {
  2569. client.chatMessage(SENDER, "Please try again later.");
  2570. }
  2571. } else {
  2572. client.chatMessage(SENDER, "Please provide a valid SteamID64.");
  2573. }
  2574. }
  2575. else if (MSG.toUpperCase() === "!STOCK") {
  2576. client.chatMessage(SENDER, "Loading inventory...");
  2577. Utils.getInventory(SENDER.getSteamID64(), community, (ERR, DATA) => {
  2578. console.log("DEBUG#INVLOADED");
  2579. if (!ERR) {
  2580. let s = DATA;
  2581. Utils.getSets(s, allCards, (ERR, DATA) => {
  2582. console.log("DEBUG#SETSLOADED");
  2583. if (!ERR) {
  2584. // console.log(b);
  2585. // TODO: COUNT AMOUNT OF SETS BOT CAN GIVE HIM
  2586. // 1: GET BOTS CARDS. DONE
  2587. // 2: GET PLAYER's BADGES. DONE
  2588. // 2: GET PLAYER's BADGES. DONE
  2589. // 3: MAGIC
  2590. let hisMaxSets = 0,
  2591. botNSets = 0;
  2592. // Loop for sets he has partially completed
  2593. // Loop for sets he has never crafted
  2594. for (let i = 0; i < Object.keys(DATA).length; i++) {
  2595. if (DATA[Object.keys(DATA)[i]].length >= 5) {
  2596. hisMaxSets += 5;
  2597. } else {
  2598. hisMaxSets += DATA[Object.keys(DATA)[i]].length;
  2599. }
  2600. botNSets += DATA[Object.keys(DATA)[i]].length;
  2601. }
  2602. totalBotSets = botNSets;
  2603. let playThis = CONFIG.PLAYGAMES;
  2604. if (CONFIG.PLAYGAMES && typeof(CONFIG.PLAYGAMES[0]) === "string") {
  2605. playThis[0] = parseString(playThis[0], totalBotSets);
  2606. }
  2607. if (botNSets === 0) {
  2608. client.chatMessage(SENDER, "You currently don't have any available set which the bot can stock.");
  2609. }
  2610. else {
  2611. client.gamesPlayed(playThis);
  2612. client.chatMessage(SENDER, "You currently have " + botNSets + " set(s) available which the bot can stock.\r\nSending offer...");
  2613. let t = manager.createOffer(SENDER.getSteamID64());
  2614. Utils.getSets(s, allCards, (ERR, DDATA) => {
  2615. if (!ERR) {
  2616. sortSetsByAmountB(s, (DATA) => {
  2617. let setsSent = {};
  2618. firsttLoop: for (let i = 0; i < DATA.length; i++) {
  2619. console.log(setsSent);
  2620. console.log(DATA[i]);
  2621. if (DDATA[DATA[i]]) {
  2622. for (let j = 0; j < DDATA[DATA[i]].length; j++) {
  2623. t.addTheirItems(DDATA[DATA[i]][j]);
  2624. console.log("DEBUG#LOOP #2 CONTINUE: ITEM ADD");
  2625. }
  2626. } else {
  2627. console.log("DEBUG#LOOP #2 CONTINUE: RETURN 2");
  2628. continue firsttLoop;
  2629. }
  2630. }
  2631. });
  2632. console.log("DEBUG#SENDING");
  2633. t.data("commandused", "Stock");
  2634. t.send((ERR, STATUS) => {
  2635. if (ERR) {
  2636. client.chatMessage(SENDER, "An error occurred while sending your trade. Steam Trades could be down. Please try again later.");
  2637. console.log("## An error occurred while sending trade: " + ERR);
  2638. } else {
  2639. client.chatMessage(SENDER, "Trade Sent! Confirming it...");
  2640. console.log("## Trade offer sent!");
  2641. }
  2642. });
  2643. } else {
  2644. console.log("## An error occurred while getting bot sets: " + ERR);
  2645. }
  2646. });
  2647. }
  2648. } else {
  2649. console.log("## An error occurred while getting user sets: " + ERR);
  2650. }
  2651. });
  2652. } else {
  2653. console.log("## An error occurred while getting user inventory: " + ERR);
  2654. }
  2655. });
  2656. }
  2657. else {
  2658. client.chatMessage(SENDER, "Command not recognized.");
  2659. }
  2660. }
  2661. else {
  2662. client.chatMessage(SENDER, "Command not recognized. Use !help to see how this bot works.");
  2663. }
  2664. });
  2665.  
  2666. client.on("friendRelationship", (SENDER, REL) => {
  2667. if (REL === 2) {
  2668. client.addFriend(SENDER);
  2669. } else if (REL === 3) {
  2670. if (CONFIG.INVITETOGROUPID) {
  2671. client.inviteToGroup(SENDER, CONFIG.INVITETOGROUPID);
  2672. }
  2673. client.chatMessage(SENDER, CONFIG.MESSAGES.WELCOME);
  2674. }
  2675. });
  2676.  
  2677. // manager.on("unknownOfferSent", (o, os) => {
  2678. // if (o.state === 9) {
  2679. // console.log("## OFFER SENT LIST");
  2680. // community.checkConfirmations();
  2681. // }
  2682. // });
  2683.  
  2684. // community.on("newConfirmation", (CONF) => {
  2685. // console.log("## New confirmation.");
  2686. // community.acceptConfirmationForObject(CONFIG.IDENTITYSECRET, CONF.id);
  2687. // });
  2688. manager.on("sentOfferChanged", (OFFER, OLDSTATE) => {
  2689. if (OFFER.state === 2) {
  2690. client.chatMessage(OFFER.partner, "Trade confirmed! Click here to accept it: https://www.steamcommunity.com/tradeoffer/" + OFFER.id);
  2691. } else if (OFFER.state === 3) {
  2692. Utils.getInventory(client.steamID.getSteamID64(), community, (ERR, DATA) => {
  2693. if (!ERR) {
  2694. let s = DATA;
  2695. Utils.getSets(s, allCards, (ERR, DATA) => {
  2696. if (!ERR) {
  2697. botSets = DATA;
  2698. console.log("## Bot's sets loaded.");
  2699. } else {
  2700. console.log("## An error occurred while getting bot sets: " + ERR);
  2701. }
  2702. let botNSets = 0;
  2703. for (let i = 0; i < Object.keys(botSets).length; i++) {
  2704. botNSets += botSets[Object.keys(botSets)[i]].length;
  2705. }
  2706. totalBotSets = botNSets;
  2707. let playThis = CONFIG.PLAYGAMES;
  2708. if (CONFIG.PLAYGAMES && typeof(CONFIG.PLAYGAMES[0]) === "string") {
  2709. playThis[0] = parseString(playThis[0], totalBotSets);
  2710. }
  2711. client.gamesPlayed(playThis);
  2712. });
  2713. } else {
  2714. console.log("## An error occurred while getting bot inventory: " + ERR);
  2715. }
  2716. });
  2717. if (CONFIG.INVITETOGROUPID) {
  2718. client.inviteToGroup(OFFER.partner, CONFIG.INVITETOGROUPID);
  2719. }
  2720. let d = "" + OFFER.data("commandused") + "";
  2721. d += "\nSets: " + OFFER.data("amountofsets");
  2722. d += "\nKeys: " + OFFER.data("amountofkeys");
  2723. d += "\nSteamID: " + OFFER.partner.getSteamID64();
  2724. fs.writeFile("./TradesAccepted/" + OFFER.id + "-" + OFFER.partner.getSteamID64() + ".txt", d, (ERR) => {
  2725. if (ERR) {
  2726. console.log("## An error occurred while writing trade file: " + ERR);
  2727. }
  2728. });
  2729. //IF TRADE WAS GAME GIVE PLAYER THE CODE
  2730. if (OFFER.data("commandused") === "BuyGame") {
  2731. let gamesStock = jsonfile.readFileSync(gamesstockfilename);
  2732. let cdkey = gamesStock.stock[OFFER.data("index")].keys[0];
  2733. let name = gamesStock.stock[OFFER.data("index")].name;
  2734. gamesStock.stock[OFFER.data("index")].keys.splice(0, 1);
  2735.  
  2736. fs.writeFile(gamesstockfilename, JSON.stringify(gamesStock, null, "\t"), function (err) {
  2737. if (err) return console.log(err);
  2738. console.log('Steam CD Key successfully removed.');
  2739. });
  2740. client.chatMessage(OFFER.partner.getSteamID64(), "\nYour Steam CD Key for game \"" + name + "\" is:\r\n" + cdkey);
  2741. client.chatMessage(OFFER.partner.getSteamID64(), "\nYour Steam CD Key for game \"" + name + "\" is:\r\n" + cdkey);
  2742. client.chatMessage(OFFER.partner.getSteamID64(), "\nYour Steam CD Key for game \"" + name + "\" is:\r\n" + cdkey);
  2743. }
  2744. /////////////////////////////////////////
  2745. community.getSteamUser(OFFER.partner, (ERR, USER) => {
  2746. if (ERR) {
  2747. console.log("## An error occurred while getting user profile: " + ERR);
  2748. client.chatMessage(USER.steamID, "An error occurred while getting your profile (to comment).");
  2749. } else {
  2750. USER.comment(CONFIG.COMMENTAFTERTRADE, (ERR) => {
  2751. if (ERR) {
  2752. console.log("## An error occurred while commenting on user profile: " + ERR);
  2753. client.chatMessage(USER.steamID, "An error occurred while getting commenting on your profile.");
  2754. } else {
  2755. client.chatMessage(USER.steamID, "Thanks for trading! :D");
  2756. }
  2757. });
  2758. }
  2759. });
  2760. } else if (OFFER.state === 6) {
  2761. client.chatMessage(OFFER.partner, "Hey, you did not accept the offer. Please try again if you wish to receive sets!");
  2762. }
  2763. /* else if (OFFER.state === 9) {
  2764. community.checkConfirmations();
  2765. }*/
  2766. });
  2767.  
  2768. manager.on("newOffer", (OFFER) => {
  2769. if (CONFIG.ADMINS.indexOf(OFFER.partner.getSteamID64()) >= 0 || CONFIG.ADMINS.indexOf(parseInt(OFFER.partner.getSteamID64())) >= 0) {
  2770. OFFER.getUserDetails((ERR, ME, THEM) => {
  2771. if (ERR) {
  2772. console.log("## An error occurred while getting trade holds: " + ERR);
  2773. client.chatMessage(OFFER.partner, "An error occurred while getting your trade holds. Please try again");
  2774. OFFER.decline((ERR) => {
  2775. if (ERR) {
  2776. console.log("## An error occurred while declining trade: " + ERR);
  2777. }
  2778. });
  2779. } else if (ME.escrowDays === 0 && THEM.escrowDays === 0) {
  2780. OFFER.accept((ERR) => {
  2781. if (ERR) {
  2782. console.log("## An error occurred while declining trade: " + ERR);
  2783. OFFER.decline((ERR) => {
  2784. if (ERR) {
  2785. console.log("## An error occurred while declining trade: " + ERR);
  2786. }
  2787. });
  2788. } else {
  2789. client.chatMessage(OFFER.partner, "Offer accepted!");
  2790. }
  2791. });
  2792. } else {
  2793. client.chatMessage(OFFER.partner, "Please make sure you don't have a trade hold!");
  2794. OFFER.decline((ERR) => {
  2795. if (ERR) {
  2796. console.log("## An error occurred while declining trade: " + ERR);
  2797. }
  2798. });
  2799. }
  2800. });
  2801. } else if (OFFER.itemsToGive.length === 0) {
  2802. let onlySteam = true;
  2803. for (let i = 0; i < OFFER.itemsToReceive.length; i++) {
  2804. if (OFFER.itemsToReceive[i].appid !== 753) {
  2805. onlySteam = false;
  2806. }
  2807. }
  2808. if (onlySteam) {
  2809. OFFER.accept((ERR) => {
  2810. if (ERR) {
  2811. console.log("## An error occurred while declining trade: " + ERR);
  2812. }
  2813. });
  2814. }
  2815. } else {
  2816. OFFER.decline((ERR) => {
  2817. if (ERR) {
  2818. console.log("## An error occurred while declining trade: " + ERR);
  2819. }
  2820. });
  2821. }
  2822. });
  2823.  
  2824. community.on("newConfirmation", (CONF) => {
  2825. console.log("## New confirmation.");
  2826. community.acceptConfirmationForObject(CONFIG.IDENTITYSECRET, CONF.id, (ERR) => {
  2827. if (ERR) {
  2828. console.log("## An error occurred while accepting confirmation: " + ERR);
  2829. } else {
  2830. console.log("## Confirmation accepted.");
  2831. }
  2832. });
  2833. });
  2834.  
  2835. function sortSetsByAmount(SETS, callback) {
  2836. callback(Object.keys(SETS).sort((k1, k2) => SETS[k1].length - SETS[k2].length).reverse());
  2837. }
  2838.  
  2839. function sortSetsByAmountB(SETS, callback) {
  2840. callback(Object.keys(SETS).sort((k1, k2) => SETS[k1].length - SETS[k2].length));
  2841. }
  2842.  
  2843. function parseString(INPUT, SETS) {
  2844. return INPUT.replace(":sets:", SETS);
  2845. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement