Advertisement
Guest User

Untitled

a guest
Aug 21st, 2017
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 15.74 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using SteamKit2;
  7. using System.IO;
  8. using System.Threading;
  9.  
  10. namespace SteamBot
  11. {
  12.     class Program
  13.     {
  14.        
  15.  
  16.        
  17.  
  18.         static string user, pass;
  19.  
  20.         static SteamClient steamClient;
  21.         static CallbackManager manager;
  22.         static SteamUser steamUser;
  23.         static SteamFriends steamFriends;
  24.  
  25.         static bool isRunning = false;
  26.  
  27.         static string authCode;
  28.  
  29.         static void Main(string[] args) // main string creating the console and any required files.
  30.         {
  31.             if (!File.Exists("chat.txt"))
  32.             {
  33.                 File.Create("chat.txt").Close();
  34.                 File.WriteAllText("chat.txt", "abc | 123");
  35.             }
  36.  
  37.             if (!File.Exists("admin.txt"))
  38.             {
  39.                 File.Create("admin.txt").Close();
  40.                 File.WriteAllText("admin.txt", "76561198070576110");
  41.             }
  42.  
  43.  
  44.             Console.Title = "SteamBot Console";
  45.             Console.WriteLine("Broke the ESC / CTRL+C quitting method... Use the 'X' or ALT + F4 -Dev");
  46.  
  47.             Console.Write("Username: ");
  48.             user = Console.ReadLine();
  49.  
  50.             Console.Write("Password: ");
  51.             pass = Console.ReadLine();
  52.  
  53.             SteamLogIn();
  54.         }
  55.  
  56.         static void SteamLogIn() //originates the managers + is the logging in func.
  57.         {
  58.             steamClient = new SteamClient();
  59.  
  60.             manager = new CallbackManager(steamClient);
  61.  
  62.             steamUser = steamClient.GetHandler<SteamUser>();
  63.  
  64.             steamFriends = steamClient.GetHandler<SteamFriends>();
  65.  
  66.             new Callback<SteamClient.ConnectedCallback>(OnConnected);
  67.             new Callback<SteamClient.DisconnectedCallback>(OnDisconnected, manager);
  68.  
  69.             new Callback<SteamUser.LoggedOnCallback>(OnLoggedOn, manager);
  70.             new Callback<SteamUser.LoggedOffCallback>(OnLoggedOff, manager);
  71.  
  72.             new Callback<SteamUser.AccountInfoCallback>(OnAccountInfo, manager);
  73.             new Callback<SteamFriends.FriendMsgCallback>(OnChatMessage, manager);
  74.  
  75.             new Callback<SteamFriends.FriendsListCallback>(OnFriendsList, manager);
  76.  
  77.             new Callback<SteamUser.UpdateMachineAuthCallback>(OnMachineAuth, manager);
  78.  
  79.             isRunning = true;
  80.  
  81.             Console.WriteLine("Connecting to Steam...");
  82.  
  83.             steamClient.Connect();
  84.  
  85.  
  86.             while (isRunning)
  87.             {
  88.                 manager.RunWaitCallbacks(TimeSpan.FromSeconds(1));
  89.             }
  90.             Console.ReadKey();
  91.         }
  92.  
  93.         static void OnConnected(SteamClient.ConnectedCallback callback) //Handles the connection, if it was successful or it was refused etc.
  94.         {
  95.             if (callback.Result != EResult.OK)
  96.             {
  97.                 Console.WriteLine("Unable to connect to steam: {0}", callback.Result);
  98.  
  99.                 isRunning = false;
  100.                 return;
  101.             }
  102.  
  103.             Console.WriteLine("Connected to Steam. \nLogging in {0}... \n", user);
  104.  
  105.             byte[] sentryHash = null;
  106.  
  107.             if (File.Exists("sentry.bin"))
  108.             {
  109.                 byte[] sentryFile = File.ReadAllBytes("sentry.bin");
  110.  
  111.                 sentryHash = CryptoHelper.SHAHash(sentryFile);
  112.             }
  113.  
  114.             steamUser.LogOn(new SteamUser.LogOnDetails
  115.             {
  116.                 Username = user,
  117.                 Password = pass,
  118.  
  119.                 AuthCode = authCode,
  120.  
  121.                 SentryFileHash = sentryHash,
  122.             });
  123.         }
  124.  
  125.         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.
  126.         {
  127.             if (callback.Result != EResult.AccountLogonDenied)
  128.             {
  129.                 Console.WriteLine("Account is SteamGuard protected.");
  130.  
  131.                 Console.Write("Please enter the authentication code sent to the email at {0}: ", callback.EmailDomain);
  132.  
  133.                 authCode = Console.ReadLine();
  134.  
  135.                 return;
  136.             }
  137.  
  138.             if (callback.Result != EResult.OK)
  139.             {
  140.                 Console.WriteLine("Unable to log in to Steam: {0}\n", callback.Result);
  141.                 isRunning = false;
  142.                 return;
  143.             }
  144.             Console.WriteLine("{0} succesfully logged in.", user);
  145.  
  146.         }
  147.  
  148.         static void OnMachineAuth(SteamUser.UpdateMachineAuthCallback callback) //handles the sentryHash file and subsequently updates sentry.bin
  149.         {
  150.             Console.WriteLine("Updating sentry file...");
  151.  
  152.             byte[] sentryHash = CryptoHelper.SHAHash(callback.Data);
  153.  
  154.             File.WriteAllBytes("sentry.bin", callback.Data);
  155.  
  156.             steamUser.SendMachineAuthResponse(new SteamUser.MachineAuthDetails
  157.             {
  158.                 JobID = callback.JobID,
  159.                 FileName = callback.FileName,
  160.                 BytesWritten = callback.BytesToWrite,
  161.                 FileSize = callback.Data.Length,
  162.                 Offset = callback.Offset,
  163.                 Result = EResult.OK,
  164.                 LastError = 0,
  165.                 OneTimePassword = callback.OneTimePassword,
  166.                 SentryFileHash = sentryHash,
  167.             });
  168.  
  169.             Console.WriteLine("Done.");
  170.         }
  171.  
  172.         static void OnDisconnected(SteamClient.DisconnectedCallback callback) //handles reconnecting to steam once steam disconnects and reconnnects to check the bin or loses connection.
  173.         {
  174.             Console.WriteLine("\n {0} disconnected from Steam, reconnecting in 5...\n", user);
  175.  
  176.             Thread.Sleep(TimeSpan.FromSeconds(5));
  177.  
  178.             steamClient.Connect();
  179.         }
  180.  
  181.         static void OnLoggedOff(SteamUser.LoggedOffCallback callback) //informs the user they logged the bot off using simple callbacks
  182.         {
  183.             Console.WriteLine("Logged off from Steam: {0}", callback.Result);
  184.         }
  185.  
  186.         static void OnAccountInfo(SteamUser.AccountInfoCallback callback) //handles setting the steam status to 'online' in the friends submenu
  187.         {
  188.             steamFriends.SetPersonaState(EPersonaState.Online);
  189.  
  190.         }
  191.  
  192.         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."
  193.         {
  194.             Thread.Sleep(TimeSpan.FromSeconds(3));
  195.  
  196.             foreach (var friend in callback.FriendList)
  197.             {
  198.                 if (friend.Relationship == EFriendRelationship.RequestRecipient)
  199.                 {
  200.                     steamFriends.AddFriend(friend.SteamID);
  201.                     Thread.Sleep(500);
  202.                     steamFriends.SendChatMessage(friend.SteamID, EChatEntryType.ChatMsg, "Hello, I'm a bot.");
  203.                 }
  204.             }
  205.         }
  206.  
  207.         static void OnChatMessage(SteamFriends.FriendMsgCallback callback)
  208.         {
  209.             string[] args;
  210.  
  211.             if (callback.EntryType == EChatEntryType.ChatMsg)
  212.             {
  213.                 if (callback.Message.Length > 1)
  214.                 {
  215.                     if (callback.Message.Remove(1) == "!")
  216.                     {
  217.                         string command = callback.Message;
  218.                         if (callback.Message.Contains(" "))
  219.                         {
  220.                             command = callback.Message.Remove(callback.Message.IndexOf(' '));
  221.                         }
  222.  
  223.                         switch (command)
  224.                         {
  225.                             #region send
  226.                             case "!send":
  227.                                 if (!isBotAdmin(callback.Sender))
  228.                                     break;
  229.  
  230.  
  231.                                 args = seperate(2, ' ', callback.Message);
  232.                                 Console.WriteLine("!send " + args[1] + args[2] + " command recieved. User: " + steamFriends.GetFriendPersonaName(callback.Sender));
  233.                                 if (args[0] == "-1")
  234.                                 {
  235.                                     steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "Command syntax: !send [friend] [message]");
  236.                                     return;
  237.                                 }
  238.                                 for (int i = 0; i < steamFriends.GetFriendCount(); i++)
  239.                                 {
  240.                                     SteamID friend = steamFriends.GetFriendByIndex(i);
  241.                                     if (steamFriends.GetFriendPersonaName(friend).ToLower().Contains(args[1].ToLower()))
  242.                                     {
  243.                                         steamFriends.SendChatMessage(friend, EChatEntryType.ChatMsg, args[2]);
  244.                                     }
  245.                                 }
  246.                                 break;
  247.                             #endregion
  248.  
  249.                             #region friends
  250.                             case "!friends":
  251.                                 Console.WriteLine("!friends command recieved. User: " + steamFriends.GetFriendPersonaName(callback.Sender));
  252.                                 for (int i = 0; i < steamFriends.GetFriendCount(); i++)
  253.                                 {
  254.                                     SteamID friend = steamFriends.GetFriendByIndex(i);
  255.                                     steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "Friend: " + steamFriends.GetFriendPersonaName(friend) + "  State:  " + steamFriends.GetFriendPersonaState(friend));
  256.                                 }
  257.                                 break;
  258.                             #endregion
  259.  
  260.                             #region friend
  261.  
  262.                             case "!friend":
  263.  
  264.                                 args = seperate(1, ' ', callback.Message);
  265.                                 Console.WriteLine("!friend " + args[1] + " | " + steamFriends.GetFriendPersonaName(Convert.ToUInt64(args[1])) + " command recieved. User: " + steamFriends.GetFriendPersonaName(callback.Sender));
  266.  
  267.                                 if (!isBotAdmin(callback.Sender))
  268.                                     return;
  269.  
  270.                                 if (args[0] == "-1")
  271.                                 {
  272.                                     steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "Command syntax: !friend [Steam ID]");
  273.                                     return;
  274.                                 }
  275.  
  276.                                 try
  277.                                 {
  278.                                     SteamID validSID = Convert.ToUInt64(args[1]);
  279.                                     if (!validSID.IsValid)
  280.                                     {
  281.                                         steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "Invalid SteamID");
  282.                                         break;
  283.                                     }
  284.                                     steamFriends.AddFriend(validSID);
  285.                                 }
  286.                                 catch (FormatException)
  287.                                 {
  288.                                     steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "Invalid SteamID64.");
  289.                                 }
  290.                                 break;
  291.                             #endregion
  292.  
  293.                             #region changename
  294.                             case "!changename":
  295.  
  296.                                 args = seperate(1, ' ', callback.Message);
  297.                                 Console.WriteLine("!changename " + args[1] + " | " + steamFriends.GetFriendPersonaName(Convert.ToUInt64(args[1])) + " command recieved. User: " + steamFriends.GetFriendPersonaName(callback.Sender));
  298.  
  299.                                 if (!isBotAdmin(callback.Sender))
  300.                                     return;
  301.  
  302.                                 if (args[0] == "-1")
  303.                                 {
  304.                                     steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "Command syntax: !change [name]");
  305.                                     return;
  306.                                 }
  307.  
  308.                                 steamFriends.SetPersonaName(args[1]);
  309.                                 break;
  310.                                 #endregion
  311.                         }
  312.                     }
  313.                 }
  314.                 string rLine;
  315.                 string trimmed = callback.Message;
  316.  
  317.                 char[] trim = { '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '=', '+', '[', ']', '{', '}', '\\', '|', ';', ':', '"', '\'', ',', '<', '.', '>', '/', '?' };
  318.  
  319.                 for (int i = 0; i < 30; i++)
  320.                 {
  321.                     trimmed = trimmed.Replace(trim[i].ToString(), "");
  322.                 }
  323.  
  324.                 StreamReader sReader = new StreamReader("chat.txt");
  325.  
  326.                 while ((rLine = sReader.ReadLine()) != null)
  327.                 {
  328.                     string text = rLine.Remove(rLine.IndexOf('|') - 1);
  329.                     string response = rLine.Remove(0, rLine.IndexOf('|') + 2);
  330.  
  331.                     if (callback.Message.Contains(text))
  332.                     {
  333.                         steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, response);
  334.                         sReader.Close();
  335.                         return;
  336.                     }
  337.                 }
  338.  
  339.             }
  340.         }
  341.  
  342.         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.
  343.         {
  344.             try
  345.             {
  346.                 if (sid.ConvertToUInt64() == Convert.ToUInt64(File.ReadAllText("admin.txt")))
  347.                 {
  348.                     return true;
  349.                 }
  350.  
  351.                 steamFriends.SendChatMessage(sid, EChatEntryType.ChatMsg, "You are not a bot admin.");
  352.                 Console.WriteLine(steamFriends.GetFriendPersonaName(sid) + " attempted to use an administrator command without privledges.");
  353.                 return false;
  354.             }
  355.             catch (Exception e)
  356.             {
  357.                 Console.WriteLine(e.Message);
  358.                 return false;
  359.             }
  360.         }
  361.  
  362.         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
  363.         {
  364.             string[] returned = new string[4];
  365.  
  366.             int i = 0;
  367.  
  368.             int error = 0;
  369.  
  370.             int lenght = thestring.Length;
  371.  
  372.             foreach (char c in thestring)
  373.             {
  374.                 if (i != number)
  375.                 {
  376.                     if (error > lenght || number > 5)
  377.                     {
  378.                         returned[0] = "-1";
  379.                         return returned;
  380.                     }
  381.                     else if (c == seperator)
  382.                     {
  383.                         returned[i] = thestring.Remove(thestring.IndexOf(c));
  384.                         thestring = thestring.Remove(0, thestring.IndexOf(c) + 1);
  385.                         i++;
  386.                     }
  387.                     error++;
  388.  
  389.                     if (error > lenght && i != number)
  390.                     {
  391.                         returned[0] = "-1";
  392.                         return returned;
  393.                     }
  394.                 }
  395.                 else
  396.                 {
  397.                     returned[i] = thestring;
  398.                 }
  399.             }
  400.             return returned;
  401.         }
  402.     }
  403. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement