Skip to content

Instantly share code, notes, and snippets.

@The-Synthax
Forked from waylaidwanderer/KeyUserHandler.cs
Last active August 29, 2015 14:27
Show Gist options
  • Save The-Synthax/7a382f570485d3a3d8f7 to your computer and use it in GitHub Desktop.
Save The-Synthax/7a382f570485d3a3d8f7 to your computer and use it in GitHub Desktop.
Keybanking bot written by http://steamcommunity.com/id/waylaidwanderer/ - coded using SteamBot wrapper (https://github.com/Jessecar96/SteamBot) Buys and sells keys as well as handling donations with Donations Control integration.
using SteamKit2;
using System.Collections.Generic;
using SteamTrade;
using System;
using System.Timers;
using SteamTrade.TradeWebAPI;
namespace SteamBot {
public class KeyUserHandler : UserHandler {
static string BotVersion = "2.6.0m";
static int SellPricePerKey = 67;
static int BuyPricePerKey = 66;
static int InviteTimerInterval = 2000;
static int TimerInterval = 170000;
int UserMetalAdded, UserScrapAdded, UserRecAdded, UserRefAdded, UserKeysAdded, BotKeysAdded, BotMetalAdded, BotScrapAdded, BotRecAdded, BotRefAdded, InventoryMetal, InventoryScrap, InventoryRec, InventoryRef, InventoryKeys, OverpayNumKeys, ExcessInScrap, PreviousKeys, WhileLoop, InvalidItem = 0;
double ExcessRefined = 0.0;
bool InGroupChat, TimerEnabled, HasRun, HasErrorRun, ChooseDonate, AskOverpay, IsOverpaying, HasCounted, AdminRequest, AdminRequestReady = false;
bool TimerDisabled = true;
ulong uid;
SteamID currentSID;
Timer inviteMsgTimer = new System.Timers.Timer(InviteTimerInterval);
Timer adTimer = new System.Timers.Timer(TimerInterval);
// Changed: Initialize inventories when the KeyUserHandler is created
// So we'll actually have access to SteamWeb.
private readonly GenericInventory mySteamInventory;
private readonly GenericInventory OtherSteamInventory;
public KeyUserHandler(Bot bot, SteamID sid) : base(bot, sid) {
mySteamInventory = new GenericInventory(SteamWeb);
OtherSteamInventory = new GenericInventory(SteamWeb);
}
public override bool OnFriendAdd() {
Bot.Log.Success(Bot.SteamFriends.GetFriendPersonaName(OtherSID) + " (" + OtherSID.ToString() + ") added me!");
inviteMsgTimer.Interval = InviteTimerInterval;
inviteMsgTimer.Elapsed += (sender, e) => OnInviteTimerElapsed(sender, e, EChatEntryType.ChatMsg);
inviteMsgTimer.Enabled = true;
return true;
}
public override bool OnGroupAdd() {
Bot.Log.Warn("Someone tried to invite me to a group! I declined it!");
return false;
}
public override void OnLoginCompleted() {
Bot.SteamFriends.SetPersonaState(EPersonaState.LookingToTrade);
}
public override void OnFriendRemove() {
Bot.Log.Success(Bot.SteamFriends.GetFriendPersonaName(OtherSID) + " (" + OtherSID.ToString() + ") removed me!");
}
public override void OnChatRoomMessage(SteamID chatID, SteamID sender, string message) {
Log.Info(Bot.SteamFriends.GetFriendPersonaName(sender) + ": " + message);
}
public override void OnMessage(string message, EChatEntryType type) {
message = message.ToLower();
if (message.Contains("buying") || message.Contains("what") || message.Contains("how many") || message.Contains("how much") || message.Contains("price") || message.Contains("selling")) {
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.");
} else if (message.Contains("what's up") || message.Contains("sup")) {
Bot.SteamFriends.SendChatMessage(OtherSID, type, "Not much I can even do. Master never lets me out of my cage.");
} else if ((message.Contains("stock"))) {
if (InventoryMetal == 0 && InventoryKeys == 0) {
Bot.SteamFriends.SendChatMessage(OtherSID, type, "Error! Either I am broke or my inventory could not be retrieved!");
} else {
Bot.SteamFriends.SendChatMessage(OtherSID, type, "I have: " + InventoryKeys + " keys and " + InventoryMetal + "metal!");
}
} else if ((((message.Contains("love") || message.Contains("luv") || message.Contains("<3")) && (message.Contains("y") || message.Contains("u")))) || message.Contains("ily") || message.Contains("ilu")) {
if (message.Contains("do")) {
Bot.SteamFriends.SendChatMessage(OtherSID, type, "I love you lots. <3");
} else {
Bot.SteamFriends.SendChatMessage(OtherSID, type, "I love you too!");
}
} else if (message.Contains("<3")) {
Bot.SteamFriends.SendChatMessage(OtherSID, type, "<3");
} 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")) {
Bot.SteamFriends.SendChatMessage(OtherSID, type, "My master would be better suited for your sexual desires.");
} else if (message.Contains("thank") || message == "ty") {
Bot.SteamFriends.SendChatMessage(OtherSID, type, "You're welcome!");
} else if (message.Contains("give me")) {
Bot.SteamFriends.SendChatMessage(OtherSID, type, "No, you give it to me!");
} else if (message.Contains("tsunami")) {
Bot.SteamFriends.SendChatMessage(OtherSID, type, "I love my master too!");
} else if (message == "donate") {
Bot.SteamFriends.SendChatMessage(OtherSID, type, "Please type that command into the TRADE WINDOW, not here! And thanks. <3");
} else if ((message == "buy") || (message == "sell")) {
Bot.SteamFriends.SendChatMessage(OtherSID, type, "That's an old command, and is unnecessary. Just trade me to begin!");
} else if (message.Contains("help")) {
Bot.SteamFriends.SendChatMessage(OtherSID, EChatEntryType.ChatMsg, "Hi. Thanks for using tsunami'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!");
} else if (IsAdmin) {
if (message.StartsWith(".join")) {
if (message.Length >= 7) {
if (message.Substring(6) == "tf2") {
uid = 103582791430075519;
} else {
ulong.TryParse(message.Substring(6), out uid);
}
var chatid = new SteamID(uid);
Bot.SteamFriends.JoinChat(chatid);
Bot.SteamFriends.SendChatMessage(OtherSID, type, "Joining chat: " + chatid.ConvertToUInt64().ToString());
InGroupChat = true;
Bot.SteamFriends.SetPersonaState(EPersonaState.LookingToTrade);
Bot.Log.Success("Joining chat: " + chatid.ConvertToUInt64().ToString());
}
} else if (message.StartsWith(".leave")) {
if (message.Length >= 8) {
if (message.Substring(7) == "tf2") {
uid = 103582791430075519;
} else {
ulong.TryParse(message.Substring(7), out uid);
}
var chatid = new SteamID(uid);
Bot.SteamFriends.LeaveChat(chatid);
Bot.SteamFriends.SendChatMessage(OtherSID, type, "Leaving chat: " + chatid.ConvertToUInt64().ToString());
InGroupChat = false;
Bot.Log.Success("Leaving chat: " + chatid.ConvertToUInt64().ToString());
}
} else if (message.StartsWith(".cashout")) {
AdminRequest = true;
Bot.OpenTrade(OtherSID);
Bot.Log.Warn(OtherSID + " has requested to cashout!");
} else if (message.StartsWith(".advertise")) {
if (InGroupChat) {
if (TimerDisabled == true) {
TimerEnabled = true;
Bot.SteamFriends.SendChatMessage(OtherSID, type, "Beginning advertisements.");
Bot.Log.Success("Beginning advertisements.");
if (!HasRun) {
adTimer.Interval = TimerInterval;
adTimer.Elapsed += (sender, e) => OnTimerElapsed(sender, e, type);
HasRun = true;
}
} else if (TimerDisabled == false) {
TimerEnabled = false;
Bot.SteamFriends.SendChatMessage(OtherSID, type, "Stopping advertisements.");
Bot.Log.Warn("Stopping advertisements.");
HasRun = false;
}
Advertise(type);
}
} else if (message.StartsWith(".play")) {
if (message.Length >= 7) {
if (message.Substring(6) == "tf2") {
Bot.SetGamePlaying(440);
Bot.Log.Success("Successfully simulated in-game status for TF2.");
}
if (message.Substring(6) == "dota2") {
Bot.SetGamePlaying(570);
Bot.Log.Success("Successfully simulated in-game status for Dota 2.");
} else {
Bot.SetGamePlaying(0);
Bot.Log.Success("Exited game simulation.");
}
}
} else if (message.StartsWith(".sell")) {
int NewSellPrice = 0;
if (message.Length >= 6) {
Bot.SteamFriends.SendChatMessage(OtherSID, type, "Current selling price: " + SellPricePerKey + " scrap.");
int.TryParse(message.Substring(5), out NewSellPrice);
Bot.Log.Success("Admin has requested that I set the new selling price from " + SellPricePerKey + " scrap to " + NewSellPrice + " scrap.");
SellPricePerKey = NewSellPrice;
Bot.SteamFriends.SendChatMessage(OtherSID, type, "Setting new selling price to: " + SellPricePerKey + " scrap.");
Bot.Log.Success("Successfully set new price.");
} else {
Bot.SteamFriends.SendChatMessage(OtherSID, type, "I need more arguments. Current selling price: " + SellPricePerKey + " scrap.");
}
} else if (message.StartsWith(".buy")) {
int NewBuyPrice = 0;
if (message.Length >= 5) {
Bot.SteamFriends.SendChatMessage(OtherSID, type, "Current buying price: " + BuyPricePerKey + " scrap.");
int.TryParse(message.Substring(4), out NewBuyPrice);
Bot.Log.Success("Admin has requested that I set the new selling price from " + BuyPricePerKey + " scrap to " + NewBuyPrice + " scrap.");
BuyPricePerKey = NewBuyPrice;
Bot.SteamFriends.SendChatMessage(OtherSID, type, "Setting new buying price to: " + BuyPricePerKey + " scrap.");
Bot.Log.Success("Successfully set new price.");
} else {
Bot.SteamFriends.SendChatMessage(OtherSID, type, "I need more arguments. Current buying price: " + BuyPricePerKey + " scrap.");
}
} else if (message.StartsWith(".gmessage")) {
if (message.Length >= 10) {
if (InGroupChat) {
var chatid = new SteamID(uid);
string gmessage = message.Substring(10);
Bot.SteamFriends.SendChatRoomMessage(chatid, type, gmessage);
Bot.Log.Success("Said into group chat: " + gmessage);
} else {
Bot.Log.Warn("Cannot send message because I am not in a group chatroom!");
}
}
} else if (message == ".canceltrade") {
Trade.CancelTrade();
Bot.SteamFriends.SendChatMessage(currentSID, EChatEntryType.ChatMsg, "Trade forcefully closed.");
} else if (message == ".removeall") {
RemoveAllFriends();
Bot.Log.Warn("Removed all friends from my friends list.");
}
} else {
Bot.SteamFriends.SendChatMessage(OtherSID, type, Bot.ChatResponse);
}
}
public override bool OnTradeRequest() {
Bot.Log.Success(Bot.SteamFriends.GetFriendPersonaName(OtherSID) + " (" + OtherSID.ToString() + ") has requested to trade with me!");
if (Bot.SteamFriends.GetPersonaState().Equals(EPersonaState.Busy)) {
Bot.Log.Error("Declied trade, I am already trading with someone else!");
Bot.SteamFriends.SendChatMessage(OtherSID, EChatEntryType.ChatMsg, "I am already in a trade! Please be patient!");
return false;
} else {
Bot.Log.Error("Accepted trade!");
return true;
}
}
public override void OnTradeError(string error) {
Bot.SteamFriends.SendChatMessage(OtherSID,
EChatEntryType.ChatMsg,
"Error: " + error + "."
);
Bot.Log.Warn(error);
if (!HasErrorRun) {
Bot.SteamFriends.SendChatMessage(OtherSID, EChatEntryType.ChatMsg, "There was an error! Please trade me again if I am not trading with anyone else!");
HasErrorRun = true;
}
Bot.SteamFriends.SetPersonaState(EPersonaState.LookingToTrade);
}
public override void OnTradeTimeout() {
Bot.SteamFriends.SendChatMessage(OtherSID, EChatEntryType.ChatMsg, "Sorry, but you were either AFK or took too long and the trade was canceled.");
Bot.Log.Info("User was kicked because they were AFK.");
Bot.SteamFriends.SetPersonaState(EPersonaState.LookingToTrade);
}
public override void OnTradeInit() {
List<long> contextId = new List<long>();
contextId.Add(1);
contextId.Add(6);
mySteamInventory.load(753, contextId, Bot.SteamClient.SteamID);
OtherSteamInventory.load(753, contextId, OtherSID);
if(!mySteamInventory.isLoaded || !OtherSteamInventory.isLoaded) {
SendTradeMessage("Couldn't open an inventory!");
}
ReInit();
TradeCountInventory(true);
Trade.SendMessage("Welcome to tsunami's keybanking bot (v" + BotVersion + "). Just add your metal or keys, and I will automatically add keys or metal when you have put up enough.");
if (InventoryKeys == 0) {
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.");
} else if (InventoryMetal < BuyPricePerKey) {
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.");
} else {
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.");
}
Bot.SteamFriends.SetPersonaState(EPersonaState.Busy);
if (IsAdmin && AdminRequest == true) {
Trade.SendMessage("Hello master! Here are my earnings!");
Bot.Log.Warn("Adding my earnings to the trade!");
Bot.GetInventory();
Inventory.Item[] inventory = Bot.MyInventory.Items;
foreach (Inventory.Item item in inventory) {
if (item != null && !item.IsNotTradeable) {
Trade.AddItem(item.Id);
}
}
AdminRequest = false;
AdminRequestReady = true;
}
}
public override void OnTradeAddItem(Schema.Item schemaItem, Inventory.Item inventoryItem) {
var item = Trade.CurrentSchema.GetItem(schemaItem.Defindex);
if (inventoryItem.AppId == 440) {
if (!HasCounted) {
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.");
} else if (InvalidItem >= 4) {
Trade.CancelTrade();
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.");
Bot.Log.Warn("Booted user for messing around.");
Bot.SteamFriends.SetPersonaState(EPersonaState.LookingToTrade);
} else if (item.Defindex == 5000) {
UserMetalAdded++;
UserScrapAdded++;
Bot.Log.Success("User added: " + item.ItemName);
} else if (item.Defindex == 5001) {
UserMetalAdded += 3;
UserRecAdded++;
Bot.Log.Success("User added: " + item.ItemName);
} else if (item.Defindex == 5002) {
UserMetalAdded += 9;
UserRefAdded++;
Bot.Log.Success("User added: " + item.ItemName);
} else if (schemaItem.ItemName == "Mann Co. Supply Crate Key" || schemaItem.ItemName == "#TF_Tool_DecoderRing") {
UserKeysAdded++;
Bot.Log.Success("User added: " + item.ItemName);
if (!ChooseDonate) {
int KeysToScrap = UserKeysAdded * BuyPricePerKey;
if (InventoryMetal < KeysToScrap) {
Trade.SendMessage("I only have " + InventoryMetal + " scrap. You need to remove some keys.");
Bot.Log.Warn("I don't have enough metal for the user.");
} else {
Trade.SendMessage("You have given me " + UserKeysAdded + " key(s). I will give you " + KeysToScrap + " scrap.");
Bot.Log.Success("User gave me " + UserKeysAdded + " key(s). I will now give him " + KeysToScrap + " scrap.");
bool DoneAddingMetal = false;
while (!DoneAddingMetal) {
if (InventoryRef > 0 && BotMetalAdded + 9 <= KeysToScrap) {
Trade.AddItemByDefindex(5002);
Bot.Log.Warn("I added Refined Metal.");
BotMetalAdded += 9;
BotRefAdded++;
InventoryRef--;
} else if (InventoryRec > 0 && BotMetalAdded + 3 <= KeysToScrap) {
Trade.AddItemByDefindex(5001);
Bot.Log.Warn("I added Reclaimed Metal.");
BotMetalAdded += 3;
BotRecAdded++;
InventoryRec--;
} else if (InventoryScrap > 0 && BotMetalAdded + 1 <= KeysToScrap) {
Trade.AddItemByDefindex(5000);
Bot.Log.Warn("I added Scrap Metal.");
BotMetalAdded++;
BotScrapAdded++;
InventoryScrap--;
} else if (InventoryScrap == 0 && BotMetalAdded + 2 == KeysToScrap) {
Trade.SendMessage("Sorry, but I don't have enough scrap metal to give you! Please remove some keys or add two keys.");
Bot.Log.Warn("Couldn't add enough metal for the user!");
DoneAddingMetal = true;
} else if (InventoryScrap == 0 && BotMetalAdded + 1 == KeysToScrap) {
Trade.SendMessage("Sorry, but I don't have enough scrap metal to give you! Please remove some keys or add a key.");
Bot.Log.Warn("Couldn't add enough metal for the user!");
DoneAddingMetal = true;
} else if (BotMetalAdded == KeysToScrap) {
Trade.SendMessage("Added enough metal. " + BotRefAdded + " ref, " + BotRecAdded + " rec, " + BotScrapAdded + " scrap.");
Bot.Log.Success("Gave user enough metal!");
DoneAddingMetal = true;
}
}
}
}
} else {
Trade.SendMessage("Sorry, I don't accept " + item.ItemName + "! I only accept metal/keys! Please remove it from the trade to continue.");
Bot.Log.Warn("User added: " + item.ItemName);
InvalidItem++;
}
if (!ChooseDonate) {
if (UserMetalAdded % SellPricePerKey >= 0 && UserMetalAdded > 0) {
int NumKeys = UserMetalAdded / SellPricePerKey;
if (NumKeys > 0 && NumKeys != PreviousKeys) {
Trade.SendMessage("You put up enough metal for " + NumKeys + " key(s). Adding your keys now...");
Bot.Log.Success("User put up enough metal for " + NumKeys + " key(s).");
if (NumKeys > InventoryKeys) {
double excess = ((NumKeys - BotKeysAdded) * SellPricePerKey) / 9.0;
string refined = string.Format("{0:N2}", excess);
Trade.SendMessage("I only have " + InventoryKeys + " in my backpack. :(");
Bot.Log.Warn("User wanted to buy " + NumKeys + " key(s), but I only have " + InventoryKeys + " key(s).");
Trade.SendMessage("Please remove " + refined + " ref.");
NumKeys = InventoryKeys;
}
for (int count = BotKeysAdded; count < NumKeys; count++) {
if (AddKey()) {
Bot.Log.Warn("I am adding Mann Co. Supply Crate Key.");
BotKeysAdded++;
}
}
Trade.SendMessage("I have added " + BotKeysAdded + " key(s) for you.");
Bot.Log.Success("I have added " + BotKeysAdded + " key(s) for the user.");
PreviousKeys = NumKeys;
}
}
}
} else {
Trade.SendMessage("That's an item from the wrong game!");
}
}
public override void OnTradeRemoveItem(Schema.Item schemaItem, Inventory.Item inventoryItem) {
var item = Trade.CurrentSchema.GetItem(schemaItem.Defindex);
if (item.Defindex == 5000) {
UserMetalAdded--;
UserScrapAdded--;
Bot.Log.Success("User removed: " + item.ItemName);
} else if (item.Defindex == 5001) {
UserMetalAdded -= 3;
UserRecAdded--;
Bot.Log.Success("User removed: " + item.ItemName);
} else if (item.Defindex == 5002) {
UserMetalAdded -= 9;
UserRefAdded--;
Bot.Log.Success("User removed: " + item.ItemName);
} else if (schemaItem.ItemName == "Mann Co. Supply Crate Key" || schemaItem.ItemName == "#TF_Tool_DecoderRing") {
UserKeysAdded--;
Bot.Log.Success("User removed: " + item.ItemName);
} else {
Bot.Log.Warn("User removed: " + item.ItemName);
}
if (UserKeysAdded < (float)BotMetalAdded / BuyPricePerKey) {
int KeysToScrap = UserKeysAdded * BuyPricePerKey;
bool DoneAddingMetal = false;
while (!DoneAddingMetal) {
WhileLoop++;
if (BotRefAdded > 0 && BotMetalAdded - 9 >= KeysToScrap) {
Trade.RemoveItemByDefindex(5002);
Bot.Log.Warn("I removed Refined Metal.");
BotMetalAdded -= 9;
BotRefAdded--;
InventoryRef++;
} else if (BotRecAdded > 0 && BotMetalAdded - 3 >= KeysToScrap) {
Trade.RemoveItemByDefindex(5001);
Bot.Log.Warn("I removed Reclaimed Metal.");
BotMetalAdded -= 3;
BotRecAdded--;
InventoryRec++;
} else if (BotScrapAdded > 0 && BotMetalAdded - 1 >= KeysToScrap) {
Trade.RemoveItemByDefindex(5000);
Bot.Log.Warn("I removed Scrap Metal.");
BotMetalAdded--;
BotScrapAdded--;
InventoryScrap++;
} else if (BotMetalAdded == KeysToScrap) {
DoneAddingMetal = true;
} else if (WhileLoop > 50) {
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.");
WhileLoop = 0;
DoneAddingMetal = true;
break;
}
}
}
while ((float)UserMetalAdded / SellPricePerKey < BotKeysAdded) {
if (RemoveKey()) {
Bot.Log.Warn("I removed Mann Co. Supply Crate Key.");
BotKeysAdded--;
InventoryKeys++;
PreviousKeys = BotKeysAdded;
IsOverpaying = false;
}
}
}
public override void OnTradeMessage(string message) {
Bot.Log.Info("[TRADE MESSAGE] " + message);
message = message.ToLower();
if (message == "donate") {
ChooseDonate = true;
Trade.SendMessage("Thank you so much! Just 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!");
Bot.Log.Success("User wants to donate!");
} else if (message == "continue") {
if (AskOverpay) {
IsOverpaying = true;
Trade.SendMessage("You have chosen to continue overpaying. Click \"Ready to Trade\" again to complete the trade.");
Bot.Log.Warn("User has chosen to continue overpaying!");
} else {
Trade.SendMessage("You cannot use this command right now!");
Bot.Log.Warn("User typed \"continue\" for no reason.");
}
}
}
public override void OnTradeReady(bool ready) {
if (!ready) {
Trade.SetReady(false);
} else if (IsAdmin && AdminRequestReady == true) {
Trade.SetReady(true);
} else {
Bot.Log.Success("User is ready to trade!");
if (Validate()) {
Trade.SetReady(true);
} else {
if (AskOverpay && OverpayNumKeys != 0 && !ChooseDonate) {
double AdditionalRefined = (SellPricePerKey / 9.0) - ExcessRefined;
string addRef = string.Format("{0:N2}", AdditionalRefined);
string refined = string.Format("{0:N2}", ExcessRefined);
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.");
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.");
} else {
ResetTrade(false);
}
}
}
}
public override void OnTradeAccept() {
if (AdminRequestReady == true) {
Trade.AcceptTrade();
AdminRequestReady = false;
Log.Success("Successfully cashed out!");
Bot.SteamFriends.SetPersonaState(EPersonaState.LookingToTrade);
}
if (Validate() || IsAdmin) {
bool success = Trade.AcceptTrade();
if (success) {
Log.Success("Trade was successful!");
Bot.SteamFriends.SendChatMessage(OtherSID, EChatEntryType.ChatMsg, "Thanks for a successful trade!");
Bot.SteamFriends.SetPersonaState(EPersonaState.LookingToTrade);
} else {
Log.Warn("Trade might have failed.");
Bot.SteamFriends.SetPersonaState(EPersonaState.LookingToTrade);
}
}
List<ulong> scraps = new List<ulong>();
Bot.GetInventory();
foreach (var item in Bot.MyInventory.Items) {
if (scraps.Count == 3)
break;
if (item.Defindex == 5000 && !scraps.Contains(item.Id))
scraps.Add(item.Id);
}
if (scraps.Count == 3) {
Bot.SetGamePlaying(440);
SteamBot.TF2GC.Crafting.CraftItems(Bot, scraps.ToArray());
Bot.GetInventory();
}
OnTradeClose();
}
public override void OnTradeSuccess() {
}
public override void OnTradeClose() {
Bot.SteamFriends.SetPersonaState(EPersonaState.LookingToTrade);
base.OnTradeClose();
}
public bool Validate() {
int ScrapCount = 0;
int KeyCount = 0;
List<string> errors = new List<string>();
foreach (TradeUserAssets asset in Trade.OtherOfferedItems) {
var item = Trade.OtherInventory.GetItem(asset.assetid);
var schemaItem = Trade.CurrentSchema.GetItem(item.Defindex);
if (item.Defindex == 5000) {
ScrapCount++;
} else if (item.Defindex == 5001) {
ScrapCount += 3;
} else if (item.Defindex == 5002) {
ScrapCount += 9;
} else if (schemaItem.ItemName == "Mann Co. Supply Crate Key" || schemaItem.ItemName == "#TF_Tool_DecoderRing") {
KeyCount++;
} else {
errors.Add("I can't accept " + schemaItem.ItemName + "!");
}
}
if (ChooseDonate) {
foreach (TradeUserAssets asset in Trade.OtherOfferedItems) {
var item = Trade.OtherInventory.GetItem(asset.assetid);
var schemaItem = Trade.CurrentSchema.GetItem(item.Defindex);
if (schemaItem.ItemName != "Mann Co. Supply Crate Key" && schemaItem.ItemName != "#TF_Tool_DecoderRing" && item.Defindex != 5000 && item.Defindex != 5001 && item.Defindex != 5002) {
errors.Add("I'm sorry, but I cannot accept " + schemaItem.ItemName + "!");
}
}
if (BotMetalAdded > 0 || BotKeysAdded > 0) {
errors.Add("You can't do that :( I still have items put up!");
}
} else if (UserKeysAdded > 0) {
Bot.Log.Warn("User has " + KeyCount + " key(s) put up. Verifying if " + (float)BotMetalAdded / BuyPricePerKey + " == " + KeyCount + ".");
if (KeyCount != (float)BotMetalAdded / BuyPricePerKey) {
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.");
}
} else if (ScrapCount % SellPricePerKey != 0 && !IsOverpaying) {
OverpayNumKeys = ScrapCount / SellPricePerKey;
ExcessInScrap = ScrapCount - (OverpayNumKeys * SellPricePerKey);
ExcessRefined = (ExcessInScrap / 9.0);
string refined = string.Format("{0:N2}", ExcessRefined);
Trade.SendMessage("You put up enough metal for " + OverpayNumKeys + " key(s), with " + refined + " ref extra.");
Bot.Log.Success("User put up enough metal for " + OverpayNumKeys + " key(s), with " + refined + " ref extra.");
if (OverpayNumKeys == 0) {
double AdditionalRefined = (SellPricePerKey / 9.0) - ExcessRefined;
string addRef = string.Format("{0:N2}", AdditionalRefined);
errors.Add("ERROR: You need to add " + addRef + " ref for a key.");
Bot.Log.Warn("User doesn't have enough metal added, and needs add " + addRef + " ref for a key.");
} else if (OverpayNumKeys >= 1) {
errors.Add("You have put up more metal than what I'm asking.");
AskOverpay = true;
}
} else if (ScrapCount > 0 && !IsOverpaying) {
if (ScrapCount < BotKeysAdded * SellPricePerKey || (ScrapCount > BotKeysAdded * SellPricePerKey)) {
errors.Add("You must put up exactly " + String.Format("{0:0.00}", (SellPricePerKey / 9.0)) + " ref per key.");
}
}
if (errors.Count != 0)
Trade.SendMessage("There were errors in your trade: ");
foreach (string error in errors) {
Trade.SendMessage(error);
}
return errors.Count == 0;
}
public void TradeCountInventory(bool message) {
Inventory.Item[] inventory = Trade.MyInventory.Items;
InventoryMetal = 0;
InventoryKeys = 0;
InventoryRef = 0;
InventoryRec = 0;
InventoryScrap = 0;
foreach (Inventory.Item item in inventory) {
var schemaItem = Trade.CurrentSchema.GetItem(item.Defindex);
if (item.Defindex == 5000) {
InventoryMetal++;
InventoryScrap++;
} else if (item.Defindex == 5001) {
InventoryMetal += 3;
InventoryRec++;
} else if (item.Defindex == 5002) {
InventoryMetal += 9;
InventoryRef++;
} else if (schemaItem.ItemName == "Mann Co. Supply Crate Key" || schemaItem.ItemName == "#TF_Tool_DecoderRing") {
InventoryKeys++;
}
}
if (message) {
double MetalToRef = (InventoryMetal / 9.0) - 0.01;
string refined = string.Format("{0:N2}", MetalToRef);
Trade.SendMessage("Current stock: I have " + refined + " ref (" + InventoryRef + " ref, " + InventoryRec + " rec, and " + InventoryScrap + " scrap) and " + InventoryKeys + " key(s) in my backpack.");
Bot.Log.Success("Current stock: I have " + refined + " ref (" + InventoryRef + " ref, " + InventoryRec + " rec, and " + InventoryScrap + " scrap) and " + InventoryKeys + " key(s) in my backpack.");
}
HasCounted = true;
}
public void ReInit() {
UserMetalAdded = 0;
UserRefAdded = 0;
UserRecAdded = 0;
UserScrapAdded = 0;
UserKeysAdded = 0;
BotKeysAdded = 0;
BotMetalAdded = 0;
BotRefAdded = 0;
BotRecAdded = 0;
BotScrapAdded = 0;
OverpayNumKeys = 0;
PreviousKeys = 0;
ExcessInScrap = 0;
ExcessRefined = 0.0;
WhileLoop = 0;
InvalidItem = 0;
HasErrorRun = false;
ChooseDonate = false;
AskOverpay = false;
IsOverpaying = false;
HasCounted = false;
currentSID = OtherSID;
}
public bool AddKey() {
bool added = false;
while (!added) {
foreach (var item in Trade.MyInventory.Items) {
var schemaItem = Trade.CurrentSchema.GetItem(item.Defindex);
if (schemaItem.ItemName == "Mann Co. Supply Crate Key" || schemaItem.ItemName == "#TF_Tool_DecoderRing") {
added = Trade.AddItem(item.Id);
if (added) break;
}
}
break;
}
return added;
}
public bool RemoveKey() {
bool removed = false;
while (!removed) {
foreach (var item in Trade.MyInventory.Items) {
var schemaItem = Trade.CurrentSchema.GetItem(item.Defindex);
if (schemaItem.ItemName == "Mann Co. Supply Crate Key" || schemaItem.ItemName == "#TF_Tool_DecoderRing") {
removed = Trade.RemoveItem(item.Id);
if (removed) break;
}
}
break;
}
return removed;
}
private void OnInviteTimerElapsed(object source, ElapsedEventArgs e, EChatEntryType type) {
Bot.SteamFriends.SendChatMessage(OtherSID, EChatEntryType.ChatMsg, "Hello!" + Bot.SteamFriends.GetFriendPersonaName(OtherSID) + "(" + OtherSID.ToString() + "). I am tsunami's personal bot. Feel free to use and abuse me! Just trade me to start a trade!");
Bot.Log.Success("Sent welcome message to: " + Bot.SteamFriends.GetFriendPersonaName(OtherSID) + "(" + OtherSID.ToString() + ").");
inviteMsgTimer.Enabled = false;
inviteMsgTimer.Stop();
}
public void ResetTrade(bool message) {
foreach (var item in Trade.MyInventory.Items) {
Trade.RemoveItem(item.Id);
}
BotKeysAdded = 0;
BotMetalAdded = 0;
BotRefAdded = 0;
BotRecAdded = 0;
BotScrapAdded = 0;
ChooseDonate = false;
TradeCountInventory(message);
Trade.SendMessage("Something went wrong! Scroll up to read the errors.");
Bot.Log.Warn("Something went wrong! I am resetting the trade.");
Trade.SendMessage("I have reset the trade. Please try again. (If you chose to donate, you will need to type \"donate\" again)");
Bot.Log.Success("Reset trade.");
}
public void Advertise(EChatEntryType type) {
if (TimerEnabled == true) {
adTimer.Enabled = true;
TimerDisabled = false;
}
if (TimerEnabled == false) {
adTimer.Enabled = false;
adTimer.Stop();
TimerDisabled = true;
}
}
private void OnTimerElapsed(object source, ElapsedEventArgs e, EChatEntryType type) {
var chatid = new SteamID(uid);
string adMessage;
if (InventoryMetal > BuyPricePerKey && InventoryKeys > 0) {
adMessage = "Hello! I am 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. Send me a trade!";
Bot.SteamFriends.SendChatRoomMessage(chatid, type, adMessage);
Bot.Log.Success("Advertised into group chat: " + adMessage);
} else if (InventoryMetal < BuyPricePerKey && InventoryKeys > 0) {
adMessage = "Hello! I am only selling keys for " + String.Format("{0:0.00}", (SellPricePerKey / 9.0)) + " ref at the moment. Send me a trade!";
Bot.SteamFriends.SendChatRoomMessage(chatid, type, adMessage);
Bot.Log.Success("Advertised into group chat: " + adMessage);
} else if (InventoryMetal > BuyPricePerKey && InventoryKeys == 0) {
adMessage = "Hello! I am only buying keys for " + String.Format("{0:0.00}", (BuyPricePerKey / 9.0)) + " ref at the moment. Send me a trade!";
Bot.SteamFriends.SendChatRoomMessage(chatid, type, adMessage);
Bot.Log.Success("Advertised into group chat: " + adMessage);
} else {
Bot.Log.Error("I can't buy or sell anything at the moment! Either there is an inventory error or I'm broke!");
}
}
public void RemoveAllFriends() {
int numFriends = Bot.SteamFriends.GetFriendCount();
for (int count = 0; count < numFriends; count++) {
SteamID friend = Bot.SteamFriends.GetFriendByIndex(count);
ulong friendID = friend.ConvertToUInt64();
bool isAdmin = false;
// Changed: fixed admin enumerator
foreach (SteamID admin in Bot.Admins) {
if (admin == friendID) {
isAdmin = true;
break;
}
}
if (isAdmin) {
continue;
} else {
Bot.SteamFriends.RemoveFriend(friend);
System.Threading.Thread.Sleep(500);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment