C# Steam Trade Bot


SUBMITTED BY: Guest

DATE: March 14, 2015, 9:09 p.m.

FORMAT: Text only

SIZE: 44.3 kB

HITS: 2085

  1. using SteamKit2;
  2. using System.Collections.Generic;
  3. using SteamTrade;
  4. using System;
  5. using System.Timers;
  6. namespace SteamBot
  7. {
  8. public class KeyUserHandler : UserHandler
  9. {
  10. static string BotVersion = "2.5.2";
  11. static int SellPricePerKey = 31; // price in scrap, e.g. 31 / 9 = 3.55 ref
  12. static int BuyPricePerKey = 29; // price in scrap, e.g. 29 / 9 = 3.33 ref
  13. static int InviteTimerInterval = 2000;
  14. int UserMetalAdded, UserScrapAdded, UserRecAdded, UserRefAdded, UserKeysAdded, BotKeysAdded, BotMetalAdded, BotScrapAdded, BotRecAdded, BotRefAdded, InventoryMetal, InventoryScrap, InventoryRec, InventoryRef, InventoryKeys, OverpayNumKeys, ExcessInScrap, PreviousKeys, WhileLoop, InvalidItem = 0;
  15. double ExcessRefined = 0.0;
  16. bool InGroupChat, TimerEnabled, HasRun, HasErrorRun, ChooseDonate, AskOverpay, IsOverpaying, HasCounted = false;
  17. bool TimerDisabled = true;
  18. ulong uid;
  19. SteamID currentSID;
  20. Timer inviteMsgTimer = new System.Timers.Timer(InviteTimerInterval);
  21. public KeyUserHandler(Bot bot, SteamID sid)
  22. : base(bot, sid)
  23. {
  24. }
  25. public override bool OnFriendAdd()
  26. {
  27. Bot.log.Success(Bot.SteamFriends.GetFriendPersonaName(OtherSID) + " (" + OtherSID.ToString() + ") added me!");
  28. // Using a timer here because the message will fail to send if you do it too quickly
  29. inviteMsgTimer.Interval = InviteTimerInterval;
  30. inviteMsgTimer.Elapsed += (sender, e) => OnInviteTimerElapsed(sender, e, EChatEntryType.ChatMsg);
  31. inviteMsgTimer.Enabled = true;
  32. return true;
  33. }
  34. public override void OnFriendRemove()
  35. {
  36. Bot.log.Success(Bot.SteamFriends.GetFriendPersonaName(OtherSID) + " (" + OtherSID.ToString() + ") removed me!");
  37. }
  38. public override void OnMessage(string message, EChatEntryType type)
  39. {
  40. message = message.ToLower();
  41. //REGULAR chat commands
  42. if (message.Contains("buying") || message.Contains("what") || message.Contains("how many") || message.Contains("how much") || message.Contains("price") || message.Contains("selling"))
  43. {
  44. Bot.SteamFriends.SendChatMessage(OtherSID, type, "I buy keys for " + String.Format("{0:0.00}", (BuyPricePerKey / 9.0)) + " ref, and sell keys for " + String.Format("{0:0.00}", (SellPricePerKey / 9.0)) + " ref.");
  45. }
  46. else if ((message.Contains("love") || message.Contains("luv") || message.Contains("<3")) && (message.Contains("y") || message.Contains("u")))
  47. {
  48. if (message.Contains("do"))
  49. {
  50. Bot.SteamFriends.SendChatMessage(OtherSID, type, "I love you lots. <3");
  51. }
  52. else
  53. {
  54. Bot.SteamFriends.SendChatMessage(OtherSID, type, "I love you too!");
  55. }
  56. }
  57. else if (message.Contains("<3"))
  58. {
  59. Bot.SteamFriends.SendChatMessage(OtherSID, type, "<3");
  60. }
  61. else if (message.Contains("fuck") || message.Contains("suck") || message.Contains("dick") || message.Contains("cock") || message.Contains("tit") || message.Contains("boob") || message.Contains("pussy") || message.Contains("vagina") || message.Contains("cunt") || message.Contains("penis"))
  62. {
  63. Bot.SteamFriends.SendChatMessage(OtherSID, type, "Sorry, but as a robot I cannot perform sexual functions.");
  64. }
  65. else if (message.Contains("thank"))
  66. {
  67. Bot.SteamFriends.SendChatMessage(OtherSID, type, "You're welcome!");
  68. }
  69. else if (message == "donate")
  70. {
  71. Bot.SteamFriends.SendChatMessage(OtherSID, type, "Please type that command into the TRADE WINDOW, not here! And thanks. <3");
  72. }
  73. else if (message == "buy")
  74. {
  75. Bot.SteamFriends.SendChatMessage(OtherSID, type, "That's an old command, and is unnecessary. Just trade me to begin!");
  76. }
  77. else if (message == "sell")
  78. {
  79. Bot.SteamFriends.SendChatMessage(OtherSID, type, "That's an old command, and is unnecessary. Just trade me to begin!");
  80. }
  81. else if (message.Contains("help"))
  82. {
  83. Bot.SteamFriends.SendChatMessage(OtherSID, EChatEntryType.ChatMsg, "Hi. Thanks for using The CTS Community's keybanking bot! Trade me, then simply put up your keys or metal and I will add my keys or metal automatically. I also accept donations of either keys or metal. To donate, type \"donate\" in the trade window!");
  84. }
  85. // ADMIN commands
  86. else if (IsAdmin)
  87. {
  88. if (message.StartsWith(".join"))
  89. {
  90. // Usage: .join GroupID - e.g. ".join 103582791433582049" or ".join cts" - this will allow the bot to join a group's chatroom
  91. if (message.Length >= 7)
  92. {
  93. if (message.Substring(6) == "tf2")
  94. {
  95. uid = 103582791430075519;
  96. }
  97. else
  98. {
  99. ulong.TryParse(message.Substring(6), out uid);
  100. }
  101. var chatid = new SteamID(uid);
  102. Bot.SteamFriends.JoinChat(chatid);
  103. Bot.SteamFriends.SendChatMessage(OtherSID, type, "Joining chat: " + chatid.ConvertToUInt64().ToString());
  104. InGroupChat = true;
  105. Bot.SteamFriends.SetPersonaState(EPersonaState.Online);
  106. Bot.log.Success("Joining chat: " + chatid.ConvertToUInt64().ToString());
  107. }
  108. }
  109. else if (message.StartsWith(".leave"))
  110. {
  111. // Usage: .leave GroupID, same concept as joining
  112. if (message.Length >= 8)
  113. {
  114. if (message.Substring(7) == "tf2")
  115. {
  116. uid = 103582791430075519;
  117. }
  118. else
  119. {
  120. ulong.TryParse(message.Substring(7), out uid);
  121. }
  122. var chatid = new SteamID(uid);
  123. Bot.SteamFriends.LeaveChat(chatid);
  124. Bot.SteamFriends.SendChatMessage(OtherSID, type, "Leaving chat: " + chatid.ConvertToUInt64().ToString());
  125. InGroupChat = false;
  126. Bot.log.Success("Leaving chat: " + chatid.ConvertToUInt64().ToString());
  127. }
  128. }
  129. else if (message.StartsWith(".sell"))
  130. {
  131. // Usage: .sell newprice "e.g. sell 26"
  132. int NewSellPrice = 0;
  133. if (message.Length >= 6)
  134. {
  135. Bot.SteamFriends.SendChatMessage(OtherSID, type, "Current selling price: " + SellPricePerKey + " scrap.");
  136. int.TryParse(message.Substring(5), out NewSellPrice);
  137. Bot.log.Success("Admin has requested that I set the new selling price from " + SellPricePerKey + " scrap to " + NewSellPrice + " scrap.");
  138. SellPricePerKey = NewSellPrice;
  139. Bot.SteamFriends.SendChatMessage(OtherSID, type, "Setting new selling price to: " + SellPricePerKey + " scrap.");
  140. Bot.log.Success("Successfully set new price.");
  141. }
  142. else
  143. {
  144. Bot.SteamFriends.SendChatMessage(OtherSID, type, "I need more arguments. Current selling price: " + SellPricePerKey + " scrap.");
  145. }
  146. }
  147. else if (message.StartsWith(".buy"))
  148. {
  149. // Usage: .buy newprice "e.g. .buy 24"
  150. int NewBuyPrice = 0;
  151. if (message.Length >= 5)
  152. {
  153. Bot.SteamFriends.SendChatMessage(OtherSID, type, "Current buying price: " + BuyPricePerKey + " scrap.");
  154. int.TryParse(message.Substring(4), out NewBuyPrice);
  155. Bot.log.Success("Admin has requested that I set the new selling price from " + BuyPricePerKey + " scrap to " + NewBuyPrice + " scrap.");
  156. BuyPricePerKey = NewBuyPrice;
  157. Bot.SteamFriends.SendChatMessage(OtherSID, type, "Setting new buying price to: " + BuyPricePerKey + " scrap.");
  158. Bot.log.Success("Successfully set new price.");
  159. }
  160. else
  161. {
  162. Bot.SteamFriends.SendChatMessage(OtherSID, type, "I need more arguments. Current buying price: " + BuyPricePerKey + " scrap.");
  163. }
  164. }
  165. else if (message.StartsWith(".gmessage"))
  166. {
  167. // usage: say ".gmessage Hello!" to the bot will send "Hello!" into group chat
  168. if (message.Length >= 10)
  169. {
  170. if (InGroupChat)
  171. {
  172. var chatid = new SteamID(uid);
  173. string gmessage = message.Substring(10);
  174. Bot.SteamFriends.SendChatRoomMessage(chatid, type, gmessage);
  175. Bot.log.Success("Said into group chat: " + gmessage);
  176. }
  177. else
  178. {
  179. Bot.log.Warn("Cannot send message because I am not in a group chatroom!");
  180. }
  181. }
  182. }
  183. else if (message == ".canceltrade")
  184. {
  185. // Cancels the trade. Occasionally the message will be sent to YOU instead of the current user. Oops.
  186. Trade.CancelTrade();
  187. Bot.SteamFriends.SendChatMessage(currentSID, EChatEntryType.ChatMsg, "My creator has forcefully cancelled the trade. Whatever you were doing, he probably wants you to stop.");
  188. }
  189. }
  190. else
  191. {
  192. Bot.SteamFriends.SendChatMessage(OtherSID, type, Bot.ChatResponse);
  193. }
  194. }
  195. public override bool OnTradeRequest()
  196. {
  197. Bot.log.Success(Bot.SteamFriends.GetFriendPersonaName(OtherSID) + " (" + OtherSID.ToString() + ") has requested to trade with me!");
  198. return true;
  199. }
  200. public override void OnTradeError(string error)
  201. {
  202. Bot.SteamFriends.SendChatMessage(OtherSID,
  203. EChatEntryType.ChatMsg,
  204. "Error: " + error + "."
  205. );
  206. Bot.log.Warn(error);
  207. if (!HasErrorRun)
  208. {
  209. Bot.SteamFriends.SendChatMessage(OtherSID, EChatEntryType.ChatMsg, "Did something go horribly wrong? If you have found a bug or something that you think wasn't supposed to happen, please leave a message on my owner's profile!");
  210. HasErrorRun = true;
  211. }
  212. Bot.SteamFriends.SetPersonaState(EPersonaState.Online);
  213. }
  214. public override void OnTradeTimeout()
  215. {
  216. Bot.SteamFriends.SendChatMessage(OtherSID, EChatEntryType.ChatMsg,
  217. "Sorry, but you were either AFK or took too long and the trade was canceled.");
  218. Bot.log.Info("User was kicked because he was AFK.");
  219. Bot.SteamFriends.SetPersonaState(EPersonaState.Online);
  220. }
  221. public override void OnTradeInit()
  222. {
  223. ReInit();
  224. TradeCountInventory(true);
  225. Trade.SendMessage("Welcome to ScrapBank.Me's public keybanking bot (v" + BotVersion + "). This bot was coded by http://steamcommunity.com/id/waylaidwanderer. To use this bot, just add your metal or keys, and the bot will automatically add keys or metal when you have put up enough.");
  226. if (InventoryKeys == 0)
  227. {
  228. Trade.SendMessage("I don't have any keys to sell right now! I am currently buying keys for " + String.Format("{0:0.00}", (BuyPricePerKey / 9.0)) + " ref.");
  229. }
  230. else if (InventoryMetal < BuyPricePerKey)
  231. {
  232. Trade.SendMessage("I don't have enough metal to buy keys! I am selling keys for " + String.Format("{0:0.00}", (SellPricePerKey / 9.0)) + " ref.");
  233. }
  234. else
  235. {
  236. Trade.SendMessage("I am currently buying keys for " + String.Format("{0:0.00}", (BuyPricePerKey / 9.0)) + " ref, and selling keys for " + String.Format("{0:0.00}", (SellPricePerKey / 9.0)) + " ref.");
  237. }
  238. Bot.SteamFriends.SetPersonaState(EPersonaState.Busy);
  239. }
  240. public override void OnTradeAddItem(Schema.Item schemaItem, Inventory.Item inventoryItem)
  241. {
  242. var item = Trade.CurrentSchema.GetItem(schemaItem.Defindex);
  243. if (!HasCounted)
  244. {
  245. Trade.SendMessage("ERROR: I haven't finished counting my inventory yet! Please remove any items you added, and then re-add them or there could be errors.");
  246. }
  247. else if (InvalidItem >= 4)
  248. {
  249. Trade.CancelTrade();
  250. Bot.SteamFriends.SendChatMessage(OtherSID, EChatEntryType.ChatMsg, "Please stop messing around. I am used for buying and selling keys only. I can only accept metal or keys as payment.");
  251. Bot.log.Warn("Booted user for messing around.");
  252. Bot.SteamFriends.SetPersonaState(EPersonaState.Online);
  253. }
  254. else if (item.Defindex == 5000)
  255. {
  256. // Put up scrap metal
  257. UserMetalAdded++;
  258. UserScrapAdded++;
  259. Bot.log.Success("User added: " + item.ItemName);
  260. }
  261. else if (item.Defindex == 5001)
  262. {
  263. // Put up reclaimed metal
  264. UserMetalAdded += 3;
  265. UserRecAdded++;
  266. Bot.log.Success("User added: " + item.ItemName);
  267. }
  268. else if (item.Defindex == 5002)
  269. {
  270. // Put up refined metal
  271. UserMetalAdded += 9;
  272. UserRefAdded++;
  273. Bot.log.Success("User added: " + item.ItemName);
  274. }
  275. else if (schemaItem.ItemName == "Mann Co. Supply Crate Key" || schemaItem.ItemName == "#TF_Tool_DecoderRing")
  276. {
  277. // Put up keys
  278. UserKeysAdded++;
  279. Bot.log.Success("User added: " + item.ItemName);
  280. // USER IS SELLING KEYS
  281. if (!ChooseDonate)
  282. {
  283. // BOT ADDS METAL
  284. int KeysToScrap = UserKeysAdded * BuyPricePerKey;
  285. if (InventoryMetal < KeysToScrap)
  286. {
  287. Trade.SendMessage("I only have " + InventoryMetal + " scrap. You need to remove some keys.");
  288. Bot.log.Warn("I don't have enough metal for the user.");
  289. }
  290. else
  291. {
  292. Trade.SendMessage("You have given me " + UserKeysAdded + " key(s). I will give you " + KeysToScrap + " scrap.");
  293. Bot.log.Success("User gave me " + UserKeysAdded + " key(s). I will now give him " + KeysToScrap + " scrap.");
  294. // Put up required metal
  295. bool DoneAddingMetal = false;
  296. while (!DoneAddingMetal)
  297. {
  298. if (InventoryRef > 0 && BotMetalAdded + 9 <= KeysToScrap)
  299. {
  300. Trade.AddItemByDefindex(5002);
  301. Bot.log.Warn("I added Refined Metal.");
  302. BotMetalAdded += 9;
  303. BotRefAdded++;
  304. InventoryRef--;
  305. }
  306. else if (InventoryRec > 0 && BotMetalAdded + 3 <= KeysToScrap)
  307. {
  308. Trade.AddItemByDefindex(5001);
  309. Bot.log.Warn("I added Reclaimed Metal.");
  310. BotMetalAdded += 3;
  311. BotRecAdded++;
  312. InventoryRec--;
  313. }
  314. else if (InventoryScrap > 0 && BotMetalAdded + 1 <= KeysToScrap)
  315. {
  316. Trade.AddItemByDefindex(5000);
  317. Bot.log.Warn("I added Scrap Metal.");
  318. BotMetalAdded++;
  319. BotScrapAdded++;
  320. InventoryScrap--;
  321. }
  322. else if (InventoryScrap == 0 && BotMetalAdded + 2 == KeysToScrap)
  323. {
  324. Trade.SendMessage("Sorry, but I don't have enough scrap metal to give you! Please remove some keys or add two keys.");
  325. Bot.log.Warn("Couldn't add enough metal for the user!");
  326. DoneAddingMetal = true;
  327. }
  328. else if (InventoryScrap == 0 && BotMetalAdded + 1 == KeysToScrap)
  329. {
  330. Trade.SendMessage("Sorry, but I don't have enough scrap metal to give you! Please remove some keys or add a key.");
  331. Bot.log.Warn("Couldn't add enough metal for the user!");
  332. DoneAddingMetal = true;
  333. }
  334. else if (BotMetalAdded == KeysToScrap)
  335. {
  336. Trade.SendMessage("Added enough metal. " + BotRefAdded + " ref, " + BotRecAdded + " rec, " + BotScrapAdded + " scrap.");
  337. Bot.log.Success("Gave user enough metal!");
  338. DoneAddingMetal = true;
  339. }
  340. }
  341. }
  342. }
  343. }
  344. else
  345. {
  346. // Put up other items
  347. Trade.SendMessage("Sorry, I don't accept " + item.ItemName + "! I only accept metal/keys. Please remove it from the trade to continue.");
  348. Bot.log.Warn("User added: " + item.ItemName);
  349. InvalidItem++;
  350. }
  351. // USER IS BUYING KEYS
  352. if (!ChooseDonate)
  353. {
  354. if (UserMetalAdded % SellPricePerKey >= 0 && UserMetalAdded > 0)
  355. {
  356. // Count refined and convert to keys -- X scrap per key
  357. int NumKeys = UserMetalAdded / SellPricePerKey;
  358. if (NumKeys > 0 && NumKeys != PreviousKeys)
  359. {
  360. Trade.SendMessage("You put up enough metal for " + NumKeys + " key(s). Adding your keys now...");
  361. Bot.log.Success("User put up enough metal for " + NumKeys + " key(s).");
  362. if (NumKeys > InventoryKeys)
  363. {
  364. double excess = ((NumKeys - BotKeysAdded) * SellPricePerKey) / 9.0;
  365. string refined = string.Format("{0:N2}", excess);
  366. Trade.SendMessage("I only have " + InventoryKeys + " in my backpack. :(");
  367. Bot.log.Warn("User wanted to buy " + NumKeys + " key(s), but I only have " + InventoryKeys + " key(s).");
  368. Trade.SendMessage("Please remove " + refined + " ref.");
  369. NumKeys = InventoryKeys;
  370. }
  371. // Add the keys to the trade window
  372. for (int count = BotKeysAdded; count < NumKeys; count++)
  373. {
  374. if (AddKey())
  375. {
  376. Bot.log.Warn("I am adding Mann Co. Supply Crate Key.");
  377. BotKeysAdded++;
  378. }
  379. }
  380. Trade.SendMessage("I have added " + BotKeysAdded + " key(s) for you.");
  381. Bot.log.Success("I have added " + BotKeysAdded + " key(s) for the user.");
  382. PreviousKeys = NumKeys;
  383. }
  384. }
  385. }
  386. }
  387. public override void OnTradeRemoveItem(Schema.Item schemaItem, Inventory.Item inventoryItem)
  388. {
  389. var item = Trade.CurrentSchema.GetItem(schemaItem.Defindex);
  390. if (item.Defindex == 5000)
  391. {
  392. // Removed scrap metal
  393. UserMetalAdded--;
  394. UserScrapAdded--;
  395. Bot.log.Success("User removed: " + item.ItemName);
  396. }
  397. else if (item.Defindex == 5001)
  398. {
  399. // Removed reclaimed metal
  400. UserMetalAdded -= 3;
  401. UserRecAdded--;
  402. Bot.log.Success("User removed: " + item.ItemName);
  403. }
  404. else if (item.Defindex == 5002)
  405. {
  406. // Removed refined metal
  407. UserMetalAdded -= 9;
  408. UserRefAdded--;
  409. Bot.log.Success("User removed: " + item.ItemName);
  410. }
  411. else if (schemaItem.ItemName == "Mann Co. Supply Crate Key" || schemaItem.ItemName == "#TF_Tool_DecoderRing")
  412. {
  413. // Removed keys
  414. UserKeysAdded--;
  415. Bot.log.Success("User removed: " + item.ItemName);
  416. }
  417. else
  418. {
  419. // Removed other items
  420. Bot.log.Warn("User removed: " + item.ItemName);
  421. }
  422. // User removes key from trade
  423. if (UserKeysAdded < (float)BotMetalAdded / BuyPricePerKey)
  424. {
  425. int KeysToScrap = UserKeysAdded * BuyPricePerKey;
  426. bool DoneAddingMetal = false;
  427. while (!DoneAddingMetal)
  428. {
  429. WhileLoop++;
  430. if (BotRefAdded > 0 && BotMetalAdded - 9 >= KeysToScrap)
  431. {
  432. Trade.RemoveItemByDefindex(5002);
  433. Bot.log.Warn("I removed Refined Metal.");
  434. BotMetalAdded -= 9;
  435. BotRefAdded--;
  436. InventoryRef++;
  437. }
  438. else if (BotRecAdded > 0 && BotMetalAdded - 3 >= KeysToScrap)
  439. {
  440. Trade.RemoveItemByDefindex(5001);
  441. Bot.log.Warn("I removed Reclaimed Metal.");
  442. BotMetalAdded -= 3;
  443. BotRecAdded--;
  444. InventoryRec++;
  445. }
  446. else if (BotScrapAdded > 0 && BotMetalAdded - 1 >= KeysToScrap)
  447. {
  448. Trade.RemoveItemByDefindex(5000);
  449. Bot.log.Warn("I removed Scrap Metal.");
  450. BotMetalAdded--;
  451. BotScrapAdded--;
  452. InventoryScrap++;
  453. }
  454. else if (BotMetalAdded == KeysToScrap)
  455. {
  456. DoneAddingMetal = true;
  457. }
  458. else if (WhileLoop > 50)
  459. {
  460. Trade.SendMessage("Error: I could not remove the proper amounts of metal from the trade. I might be out of scrap metal - try adding more keys if possible, or remove a few keys.");
  461. WhileLoop = 0;
  462. DoneAddingMetal = true;
  463. break;
  464. }
  465. }
  466. }
  467. // User removes metal from trade
  468. while ((float)UserMetalAdded / SellPricePerKey < BotKeysAdded)
  469. {
  470. if (RemoveKey())
  471. {
  472. Bot.log.Warn("I removed Mann Co. Supply Crate Key.");
  473. BotKeysAdded--;
  474. InventoryKeys++;
  475. PreviousKeys = BotKeysAdded;
  476. IsOverpaying = false;
  477. }
  478. }
  479. }
  480. public override void OnTradeMessage(string message)
  481. {
  482. Bot.log.Info("[TRADE MESSAGE] " + message);
  483. message = message.ToLower();
  484. if (message == "donate")
  485. {
  486. ChooseDonate = true;
  487. Trade.SendMessage("Oh, you want to donate metal or keys? Thank you so much! Please put up your items and simply click \"Ready to Trade\" when done! If you want to buy or sell keys again you need to start a new trade with me.");
  488. Bot.log.Success("User wants to donate!");
  489. }
  490. else if (message == "continue")
  491. {
  492. if (AskOverpay)
  493. {
  494. IsOverpaying = true;
  495. Trade.SendMessage("You have chosen to continue overpaying. Click \"Ready to Trade\" again to complete the trade.");
  496. Bot.log.Warn("User has chosen to continue overpaying!");
  497. }
  498. else
  499. {
  500. Trade.SendMessage("You cannot use this command right now!");
  501. Bot.log.Warn("User typed \"continue\" for no reason.");
  502. }
  503. }
  504. }
  505. public override void OnTradeReady(bool ready)
  506. {
  507. if (!ready)
  508. {
  509. Trade.SetReady(false);
  510. }
  511. else
  512. {
  513. Bot.log.Success("User is ready to trade!");
  514. if (Validate())
  515. {
  516. Trade.SetReady(true);
  517. }
  518. else
  519. {
  520. if (AskOverpay && OverpayNumKeys != 0 && !ChooseDonate)
  521. {
  522. double AdditionalRefined = (SellPricePerKey / 9.0) - ExcessRefined;
  523. string addRef = string.Format("{0:N2}", AdditionalRefined);
  524. string refined = string.Format("{0:N2}", ExcessRefined);
  525. Trade.SendMessage("WARNING: You will be overpaying. If you'd like to continue, type \"continue\", otherwise remove " + refined + " ref, or add " + addRef + " ref. You cannot complete the trade unless you do so.");
  526. Bot.log.Warn("User has added an excess of " + refined + " ref. He can add " + addRef + " ref for another key. Asking user if they want to continue.");
  527. }
  528. else
  529. {
  530. ResetTrade(false);
  531. }
  532. }
  533. }
  534. }
  535. public override void OnTradeAccept()
  536. {
  537. if (Validate() || IsAdmin)
  538. {
  539. bool success = Trade.AcceptTrade();
  540. if (success)
  541. {
  542. Log.Success("Trade was successful!");
  543. Bot.SteamFriends.SendChatMessage(OtherSID, EChatEntryType.ChatMsg, "Thanks for a successful trade!");
  544. Bot.SteamFriends.SendChatMessage(OtherSID, EChatEntryType.ChatMsg, "This bot was coded by http://steamcommunity.com/id/waylaidwanderer . Bugs or problems? Please report them to the owner of the bot instead.");
  545. Bot.SteamFriends.SetPersonaState(EPersonaState.Online);
  546. }
  547. else
  548. {
  549. Log.Warn("Trade might have failed.");
  550. Bot.SteamFriends.SetPersonaState(EPersonaState.Online);
  551. }
  552. }
  553. OnTradeClose();
  554. }
  555. public override void OnTradeClose()
  556. {
  557. Bot.SteamFriends.SetPersonaState(EPersonaState.Online);
  558. base.OnTradeClose();
  559. }
  560. public bool Validate()
  561. {
  562. int ScrapCount = 0;
  563. int KeyCount = 0;
  564. List<string> errors = new List<string>();
  565. foreach (ulong id in Trade.OtherOfferedItems)
  566. {
  567. var item = Trade.OtherInventory.GetItem(id);
  568. var schemaItem = Trade.CurrentSchema.GetItem(item.Defindex);
  569. if (item.Defindex == 5000)
  570. {
  571. ScrapCount++;
  572. }
  573. else if (item.Defindex == 5001)
  574. {
  575. ScrapCount += 3;
  576. }
  577. else if (item.Defindex == 5002)
  578. {
  579. ScrapCount += 9;
  580. }
  581. else if (schemaItem.ItemName == "Mann Co. Supply Crate Key" || schemaItem.ItemName == "#TF_Tool_DecoderRing")
  582. {
  583. KeyCount++;
  584. }
  585. else
  586. {
  587. errors.Add("I can't accept " + schemaItem.ItemName + "!");
  588. }
  589. }
  590. if (ChooseDonate)
  591. {
  592. foreach (ulong id in Trade.OtherOfferedItems)
  593. {
  594. var item = Trade.OtherInventory.GetItem(id);
  595. var schemaItem = Trade.CurrentSchema.GetItem(item.Defindex);
  596. if (schemaItem.ItemName != "Mann Co. Supply Crate Key" && schemaItem.ItemName != "#TF_Tool_DecoderRing" && item.Defindex != 5000 && item.Defindex != 5001 && item.Defindex != 5002)
  597. {
  598. errors.Add("I'm sorry, but I cannot accept " + schemaItem.ItemName + "!");
  599. }
  600. }
  601. if (BotMetalAdded > 0 || BotKeysAdded > 0)
  602. {
  603. errors.Add("You can't do that :( I still have items put up!");
  604. }
  605. }
  606. else if (UserKeysAdded > 0)
  607. {
  608. Bot.log.Warn("User has " + KeyCount + " key(s) put up. Verifying if " + (float)BotMetalAdded / BuyPricePerKey + " == " + KeyCount + ".");
  609. if (KeyCount != (float)BotMetalAdded / BuyPricePerKey)
  610. {
  611. errors.Add("Something went wrong. Either you do not have the correct amount of keys or I don't have the correct amount of metal.");
  612. }
  613. }
  614. else if (ScrapCount % SellPricePerKey != 0 && !IsOverpaying)
  615. {
  616. // Count refined and convert to keys -- X scrap per key
  617. OverpayNumKeys = ScrapCount / SellPricePerKey;
  618. ExcessInScrap = ScrapCount - (OverpayNumKeys * SellPricePerKey);
  619. ExcessRefined = (ExcessInScrap / 9.0);
  620. string refined = string.Format("{0:N2}", ExcessRefined);
  621. Trade.SendMessage("You put up enough metal for " + OverpayNumKeys + " key(s), with " + refined + " ref extra.");
  622. Bot.log.Success("User put up enough metal for " + OverpayNumKeys + " key(s), with " + refined + " ref extra.");
  623. if (OverpayNumKeys == 0)
  624. {
  625. double AdditionalRefined = (SellPricePerKey / 9.0) - ExcessRefined;
  626. string addRef = string.Format("{0:N2}", AdditionalRefined);
  627. errors.Add("ERROR: You need to add " + addRef + " ref for a key.");
  628. Bot.log.Warn("User doesn't have enough metal added, and needs add " + addRef + " ref for a key.");
  629. }
  630. else if (OverpayNumKeys >= 1)
  631. {
  632. errors.Add("You have put up more metal than what I'm asking.");
  633. AskOverpay = true;
  634. }
  635. }
  636. else if (ScrapCount > 0 && !IsOverpaying)
  637. {
  638. if (ScrapCount < BotKeysAdded * SellPricePerKey || (ScrapCount > BotKeysAdded * SellPricePerKey))
  639. {
  640. errors.Add("You must put up exactly " + String.Format("{0:0.00}", (SellPricePerKey / 9.0)) + " ref per key.");
  641. }
  642. }
  643. // send the errors
  644. if (errors.Count != 0)
  645. Trade.SendMessage("There were errors in your trade: ");
  646. foreach (string error in errors)
  647. {
  648. Trade.SendMessage(error);
  649. }
  650. return errors.Count == 0;
  651. }
  652. public void TradeCountInventory(bool message)
  653. {
  654. // Let's count our inventory
  655. Inventory.Item[] inventory = Trade.MyInventory.Items;
  656. InventoryMetal = 0;
  657. InventoryKeys = 0;
  658. InventoryRef = 0;
  659. InventoryRec = 0;
  660. InventoryScrap = 0;
  661. foreach (Inventory.Item item in inventory)
  662. {
  663. var schemaItem = Trade.CurrentSchema.GetItem(item.Defindex);
  664. if (item.Defindex == 5000)
  665. {
  666. InventoryMetal++;
  667. InventoryScrap++;
  668. }
  669. else if (item.Defindex == 5001)
  670. {
  671. InventoryMetal += 3;
  672. InventoryRec++;
  673. }
  674. else if (item.Defindex == 5002)
  675. {
  676. InventoryMetal += 9;
  677. InventoryRef++;
  678. }
  679. else if (schemaItem.ItemName == "Mann Co. Supply Crate Key" || schemaItem.ItemName == "#TF_Tool_DecoderRing")
  680. {
  681. InventoryKeys++;
  682. }
  683. }
  684. if (message)
  685. {
  686. double MetalToRef = (InventoryMetal / 9.0) - 0.01;
  687. string refined = string.Format("{0:N2}", MetalToRef);
  688. Trade.SendMessage("Current stock: I have " + refined + " ref (" + InventoryRef + " ref, " + InventoryRec + " rec, and " + InventoryScrap + " scrap) and " + InventoryKeys + " key(s) in my backpack.");
  689. Bot.log.Success("Current stock: I have " + refined + " ref (" + InventoryRef + " ref, " + InventoryRec + " rec, and " + InventoryScrap + " scrap) and " + InventoryKeys + " key(s) in my backpack.");
  690. }
  691. HasCounted = true;
  692. }
  693. public void ReInit()
  694. {
  695. UserMetalAdded = 0;
  696. UserRefAdded = 0;
  697. UserRecAdded = 0;
  698. UserScrapAdded = 0;
  699. UserKeysAdded = 0;
  700. BotKeysAdded = 0;
  701. BotMetalAdded = 0;
  702. BotRefAdded = 0;
  703. BotRecAdded = 0;
  704. BotScrapAdded = 0;
  705. OverpayNumKeys = 0;
  706. PreviousKeys = 0;
  707. ExcessInScrap = 0;
  708. ExcessRefined = 0.0;
  709. WhileLoop = 0;
  710. InvalidItem = 0;
  711. HasErrorRun = false;
  712. ChooseDonate = false;
  713. AskOverpay = false;
  714. IsOverpaying = false;
  715. HasCounted = false;
  716. currentSID = OtherSID;
  717. }
  718. public bool AddKey()
  719. {
  720. bool added = false;
  721. while (!added)
  722. {
  723. foreach (var item in Trade.MyInventory.Items)
  724. {
  725. var schemaItem = Trade.CurrentSchema.GetItem(item.Defindex);
  726. if (schemaItem.ItemName == "Mann Co. Supply Crate Key" || schemaItem.ItemName == "#TF_Tool_DecoderRing")
  727. {
  728. added = Trade.AddItem(item.Id);
  729. if (added) break;
  730. }
  731. }
  732. break;
  733. }
  734. return added;
  735. }
  736. public bool RemoveKey()
  737. {
  738. bool removed = false;
  739. while (!removed)
  740. {
  741. foreach (var item in Trade.MyInventory.Items)
  742. {
  743. var schemaItem = Trade.CurrentSchema.GetItem(item.Defindex);
  744. if (schemaItem.ItemName == "Mann Co. Supply Crate Key" || schemaItem.ItemName == "#TF_Tool_DecoderRing")
  745. {
  746. removed = Trade.RemoveItem(item.Id);
  747. if (removed) break;
  748. }
  749. }
  750. break;
  751. }
  752. return removed;
  753. }
  754. private void OnInviteTimerElapsed(object source, ElapsedEventArgs e, EChatEntryType type)
  755. {
  756. Bot.SteamFriends.SendChatMessage(OtherSID, EChatEntryType.ChatMsg, "Hi. You have added ScrapBank.Me's public keybanking bot! Just trade me, and add your keys or metal to begin! This bot also accept donations of either keys or metal. To donate, type \"donate\" in the trade window!");
  757. Bot.log.Success("Sent welcome message.");
  758. inviteMsgTimer.Enabled = false;
  759. inviteMsgTimer.Stop();
  760. }
  761. public void ResetTrade(bool message)
  762. {
  763. foreach (var item in Trade.MyInventory.Items)
  764. {
  765. Trade.RemoveItem(item.Id);
  766. }
  767. BotKeysAdded = 0;
  768. BotMetalAdded = 0;
  769. BotRefAdded = 0;
  770. BotRecAdded = 0;
  771. BotScrapAdded = 0;
  772. ChooseDonate = false;
  773. TradeCountInventory(message);
  774. Trade.SendMessage("Something went wrong! Scroll up to read the errors.");
  775. Bot.log.Warn("Something went wrong! I am resetting the trade.");
  776. Trade.SendMessage("I have reset the trade. Please try again. (If you chose to donate, you will need to type \"donate\" again)");
  777. Bot.log.Success("Reset trade.");
  778. }
  779. }
  780. }
  781. Keybanking Bot Instructions
  782. This keybanking bot is the first of its kind. I finished coding it on 12/12/12 (before scrap.tf) for my Steam Community group http://steamcommunity.com/groups/CTSCommunity and since then it has only gotten better. I am releasing the source here for anyone to use due to the fact that another group (who had previously agreed not to share it) has taken my code and bot and is redistributing it without permission or proper credit.
  783. Setup
  784. You will need to add a new code file to the SteamBot solution, named KeyUserHandler.cs. Paste the contents of this code into the file and save.
  785. In settings.json you will need to change the BotControlClass parameter to KeyUserHandler instead of the default SimpleUserHandler, if it has not been already done.
  786. Commands
  787. I've written some commands for the bot so that I wouldn't have to keep recompiling and run the bot with new parameters. If you need to change other parameters like certain things the bot says, you will need to recompile the bot yourself.
  788. These commands are all typed into chat, unless otherwise specified.
  789. .buy xx
  790. You can change the price that the bot buys keys for with this command. The prices are calculated in scraps, so .buy 29 would be 29 / 9 = 3.22 ref.
  791. .sell xx
  792. Change the price the bot sells keys at. The prices are calculated in scraps, so .sell 30 would be 30 / 9 = 3.33 ref.
  793. .join groupID
  794. Tell the bot to join a group's chatroom. e.g. .join 103582791433582049 or .join cts. The latter is coded into the bot so you will need to change it if you want your own shortcuts.
  795. .leave groupID
  796. Tell the bot to leave a group's chatroom. e.g. .leave 103582791433582049 or .leave cts. The latter is coded into the bot so you will need to change it if you want your own shortcuts.
  797. .gmessage string
  798. Send a message to the last chatroom that the bot joined. For example typing .gmessage Hello! into your bot's chat will make it say Hello! into the group's chatroom.
  799. .canceltrade
  800. Cancels the bot's trade and sends the user a warning message. Occasionally the message will be sent to YOU instead of the current user. I haven't fixed that yet.
  801. .removeall
  802. Removes everyone from the bot's friends list. Useful if it is full.
  803. Trade Commands
  804. These commands must be typed into the trade window and not the chat window.
  805. donate
  806. This tells the bot you want to donate keys/metal. Useful for replenishing its stock.
  807. The bot will also allow you to overpay for keys with metal - it will first tell you how much you will be overpaying by, and then tell you how much you need to add for another key. If you choose to type continue, it will note that you are overpaying.
  808. This was originally a whole repo but since all that was necessary is the KeyUserHandler, I've deleted the repo and moved the code to a Gist here.

comments powered by Disqus