Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using SteamKit2;
- using System.IO;
- using System.Threading;
- namespace SteamBot
- {
- class Program
- {
- static string user, pass;
- static SteamClient steamClient;
- static CallbackManager manager;
- static SteamUser steamUser;
- static SteamFriends steamFriends;
- static bool isRunning = false;
- static string authCode;
- static void Main(string[] args) // main string creating the console and any required files.
- {
- if (!File.Exists("chat.txt"))
- {
- File.Create("chat.txt").Close();
- File.WriteAllText("chat.txt", "abc | 123");
- }
- if (!File.Exists("admin.txt"))
- {
- File.Create("admin.txt").Close();
- File.WriteAllText("admin.txt", "76561198070576110");
- }
- Console.Title = "SteamBot Console";
- Console.ForegroundColor = ConsoleColor.White;
- Console.BackgroundColor = ConsoleColor.Blue;
- Console.WriteLine("Broke the ESC / CTRL+C quitting method... Use the 'X' or ALT + F4 -Dev");
- Console.ResetColor();
- Console.ForegroundColor = ConsoleColor.Green;
- Console.Write("Username: ");
- Console.ResetColor();
- Console.ForegroundColor = ConsoleColor.White;
- user = Console.ReadLine();
- Console.ResetColor();
- Console.ForegroundColor = ConsoleColor.Green;
- Console.Write("Password: ");
- Console.ResetColor();
- Console.ForegroundColor = ConsoleColor.White;
- pass = Console.ReadLine();
- Console.ResetColor();
- SteamLogIn();
- }
- static void SteamLogIn() //originates the managers + is the logging in func.
- {
- steamClient = new SteamClient();
- manager = new CallbackManager(steamClient);
- steamUser = steamClient.GetHandler<SteamUser>();
- steamFriends = steamClient.GetHandler<SteamFriends>();
- new Callback<SteamClient.ConnectedCallback>(OnConnected);
- new Callback<SteamClient.DisconnectedCallback>(OnDisconnected, manager);
- new Callback<SteamUser.LoggedOnCallback>(OnLoggedOn, manager);
- new Callback<SteamUser.LoggedOffCallback>(OnLoggedOff, manager);
- new Callback<SteamUser.AccountInfoCallback>(OnAccountInfo, manager);
- new Callback<SteamFriends.FriendMsgCallback>(OnChatMessage, manager);
- new Callback<SteamFriends.FriendsListCallback>(OnFriendsList, manager);
- new Callback<SteamUser.UpdateMachineAuthCallback>(OnMachineAuth, manager);
- isRunning = true;
- Console.ForegroundColor = ConsoleColor.Green;
- Console.WriteLine("Connecting to Steam...");
- Console.ResetColor();
- steamClient.Connect();
- while (isRunning)
- {
- manager.RunWaitCallbacks(TimeSpan.FromSeconds(1));
- }
- Console.ReadKey();
- }
- static void OnConnected(SteamClient.ConnectedCallback callback) //Handles the connection, if it was successful or it was refused etc.
- {
- if (callback.Result != EResult.OK)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("Unable to connect to steam: {0}", callback.Result);
- Console.ResetColor();
- isRunning = false;
- return;
- }
- Console.ForegroundColor = ConsoleColor.Green;
- Console.WriteLine("Connected to Steam. \nLogging in {0}... \n", user);
- Console.ResetColor();
- byte[] sentryHash = null;
- if (File.Exists("sentry.bin"))
- {
- byte[] sentryFile = File.ReadAllBytes("sentry.bin");
- sentryHash = CryptoHelper.SHAHash(sentryFile);
- }
- steamUser.LogOn(new SteamUser.LogOnDetails
- {
- Username = user,
- Password = pass,
- AuthCode = authCode,
- SentryFileHash = sentryHash,
- });
- }
- static void OnLoggedOn(SteamUser.LoggedOnCallback callback) //handles the steamguard email part- and it checks callbacks to give the user the go ahead signal of being logged in.
- {
- if (callback.Result != EResult.AccountLogonDenied)
- {
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.WriteLine("Account is SteamGuard protected.");
- Console.ResetColor();
- Console.ForegroundColor = ConsoleColor.Green;
- Console.Write("Please enter the authentication code sent to the connected email {0}: ", callback.EmailDomain);
- Console.ResetColor();
- Console.ForegroundColor = ConsoleColor.White;
- authCode = Console.ReadLine();
- Console.ResetColor();
- return;
- }
- if (callback.Result != EResult.OK)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("Unable to log in to Steam: {0}\n", callback.Result);
- Console.ResetColor();
- isRunning = false;
- return;
- }
- Console.ForegroundColor = ConsoleColor.Green;
- Console.WriteLine("{0} succesfully logged in.", user);
- Console.ResetColor();
- }
- static void OnMachineAuth(SteamUser.UpdateMachineAuthCallback callback) //handles the sentryHash file and subsequently updates sentry.bin
- {
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.WriteLine("Updating sentry file...");
- Console.ResetColor();
- byte[] sentryHash = CryptoHelper.SHAHash(callback.Data);
- File.WriteAllBytes("sentry.bin", callback.Data);
- steamUser.SendMachineAuthResponse(new SteamUser.MachineAuthDetails
- {
- JobID = callback.JobID,
- FileName = callback.FileName,
- BytesWritten = callback.BytesToWrite,
- FileSize = callback.Data.Length,
- Offset = callback.Offset,
- Result = EResult.OK,
- LastError = 0,
- OneTimePassword = callback.OneTimePassword,
- SentryFileHash = sentryHash,
- });
- Console.ForegroundColor = ConsoleColor.Green;
- Console.WriteLine("Done.");
- Console.ResetColor();
- }
- static void OnDisconnected(SteamClient.DisconnectedCallback callback) //handles reconnecting to steam once steam disconnects and reconnnects to check the bin or loses connection.
- {
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.WriteLine("\n {0} disconnected from Steam, reconnecting in 3 sec...\n", user);
- Console.ResetColor();
- Thread.Sleep(TimeSpan.FromSeconds(3));
- steamClient.Connect();
- }
- static void OnLoggedOff(SteamUser.LoggedOffCallback callback) //informs the user they logged the bot off using simple callbacks
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("Logged off from Steam: {0}", callback.Result);
- Console.ResetColor();
- }
- static void OnAccountInfo(SteamUser.AccountInfoCallback callback) //handles setting the steam status to 'online' in the friends submenu
- {
- steamFriends.SetPersonaState(EPersonaState.Online);
- }
- static void OnFriendsList(SteamFriends.FriendsListCallback callback) //Automatically adds someon if they send a request; also sends a standard greeting, I.E "Hello, I'm a bot."
- {
- Thread.Sleep(TimeSpan.FromSeconds(3));
- foreach (var friend in callback.FriendList)
- {
- if (friend.Relationship == EFriendRelationship.RequestRecipient)
- {
- steamFriends.AddFriend(friend.SteamID);
- Thread.Sleep(500);
- steamFriends.SendChatMessage(friend.SteamID, EChatEntryType.ChatMsg, "Hello, I'm a bot.");
- }
- }
- }
- static void OnChatMessage(SteamFriends.FriendMsgCallback callback)
- {
- string[] args;
- if (callback.EntryType == EChatEntryType.ChatMsg)
- {
- if (callback.Message.Length > 1)
- {
- if (callback.Message.Remove(1) == "!")
- {
- string command = callback.Message;
- if (callback.Message.Contains(" "))
- {
- command = callback.Message.Remove(callback.Message.IndexOf(' '));
- }
- switch (command)
- {
- //ALL COMMAND REGIONS; ADD NEW COMMANDS IN SEPERATE REGIONS HERE. for organization mostly, make it look pretty.
- #region send
- case "!send":
- if (!isBotAdmin(callback.Sender))
- break;
- args = seperate(2, ' ', callback.Message);
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.WriteLine("!send " + args[1] + args[2] + " command recieved. User: " + steamFriends.GetFriendPersonaName(callback.Sender)); // writes to the console that the bot has recived a command
- Console.ResetColor();
- if (args[0] == "-1")
- {
- steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "Command syntax: !send [friend] [message]");
- return;
- }
- for (int i = 0; i < steamFriends.GetFriendCount(); i++)
- {
- SteamID friend = steamFriends.GetFriendByIndex(i);
- if (steamFriends.GetFriendPersonaName(friend).ToLower().Contains(args[1].ToLower()))
- {
- steamFriends.SendChatMessage(friend, EChatEntryType.ChatMsg, args[2]);
- }
- }
- break;
- #endregion
- //for the !send command.
- #region friends
- case "!friends":
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.WriteLine("!friends command recieved. User: " + steamFriends.GetFriendPersonaName(callback.Sender));
- Console.ResetColor();
- for (int i = 0; i < steamFriends.GetFriendCount(); i++)
- {
- SteamID friend = steamFriends.GetFriendByIndex(i);
- steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "Friend: " + steamFriends.GetFriendPersonaName(friend) + " State: " + steamFriends.GetFriendPersonaState(friend));
- }
- break;
- #endregion
- //lists all the bots friends, aswell as status.
- #region friend
- case "!friend":
- args = seperate(1, ' ', callback.Message);
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.WriteLine("!friend " + args[1] + " | " + steamFriends.GetFriendPersonaName(Convert.ToUInt64(args[1])) + " command recieved. User: " + steamFriends.GetFriendPersonaName(callback.Sender));
- Console.ResetColor();
- if (!isBotAdmin(callback.Sender))
- return;
- if (args[0] == "-1")
- {
- steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "Command syntax: !friend [Steam ID]");
- return;
- }
- try
- {
- SteamID validSID = Convert.ToUInt64(args[1]);
- if (!validSID.IsValid)
- {
- steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "Invalid SteamID");
- break;
- }
- steamFriends.AddFriend(validSID);
- }
- catch (FormatException)
- {
- steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "Invalid SteamID64.");
- }
- break;
- #endregion
- //the !friend command which is used to get the bot to add a specific SID / steam64id
- #region changename
- case "!changename":
- args = seperate(1, ' ', callback.Message);
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.WriteLine("!changename " + args[1] + " | " + steamFriends.GetFriendPersonaName(Convert.ToUInt64(args[1])) + " command recieved. User: " + steamFriends.GetFriendPersonaName(callback.Sender));
- Console.ResetColor();
- if (!isBotAdmin(callback.Sender)) // simple check to see if the user who initiated the command is listed as an admin in "admin.txt"
- return;
- if (args[0] == "-1")
- {
- steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "Command syntax: !change [name]"); // appears if the !change command fails, it'll loop back into a false state and spew out the syntax for the command.
- return;
- }
- steamFriends.SetPersonaName(args[1]);
- break;
- #endregion
- // simple command to change the bots name instead of having to log in everytime, because that shit's annoying.
- }
- }
- }
- string rLine;
- string trimmed = callback.Message;
- char[] trim = { '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '=', '+', '[', ']', '{', '}', '\\', '|', ';', ':', '"', '\'', ',', '<', '.', '>', '/', '?' };
- for (int i = 0; i < 30; i++)
- {
- trimmed = trimmed.Replace(trim[i].ToString(), "");
- }
- StreamReader sReader = new StreamReader("chat.txt"); // reads the chat.txt, scanning one line every runthrough of a cycle as it looks for a command and or an exception to catch.
- while ((rLine = sReader.ReadLine()) != null)
- {
- string text = rLine.Remove(rLine.IndexOf('|') - 1);
- string response = rLine.Remove(0, rLine.IndexOf('|') + 2);
- if (callback.Message.Contains(text))
- {
- steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, response);
- sReader.Close(); // remember to always close the StreamReader like a good person you twat.
- return;
- }
- }
- }
- }
- public static bool isBotAdmin(SteamID sid) // checks to see if the users SID / steam64id matchs those found in admin.txt, if not it returns false, updating the console and catching all errors by returning them false if someone tries to exploit the admin func.
- {
- try
- {
- if (sid.ConvertToUInt64() == Convert.ToUInt64(File.ReadAllText("admin.txt")))
- {
- return true;
- }
- steamFriends.SendChatMessage(sid, EChatEntryType.ChatMsg, "You are not a bot admin."); // returns to the user stating no admin privledges were found for said user, also posts to the bot console window that a user attempted an admin command.
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine(steamFriends.GetFriendPersonaName(sid) + " attempted to use an administrator command without privledges.");
- Console.ResetColor();
- return false;
- }
- catch (Exception e)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine(e.Message);
- Console.ResetColor();
- return false;
- }
- }
- public static string[] seperate(int number, char seperator, string thestring) // variables determining the number of seperators and strings / commands and arguments sent to the bot, I think. Don't qoute me -Dev
- {
- string[] returned = new string[4];
- int i = 0;
- int error = 0;
- int lenght = thestring.Length;
- foreach (char c in thestring)
- {
- if (i != number)
- {
- if (error > lenght || number > 5)
- {
- returned[0] = "-1";
- return returned;
- }
- else if (c == seperator)
- {
- returned[i] = thestring.Remove(thestring.IndexOf(c));
- thestring = thestring.Remove(0, thestring.IndexOf(c) + 1);
- i++;
- }
- error++;
- if (error > lenght && i != number)
- {
- returned[0] = "-1";
- return returned;
- }
- }
- else
- {
- returned[i] = thestring;
- }
- }
- return returned;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement