Advertisement
Guest User

Untitled

a guest
Aug 21st, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 16.27 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 connected email {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 3 sec...\n", user);
  175.  
  176.             Thread.Sleep(TimeSpan.FromSeconds(3));
  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.                             //ALL COMMAND REGIONS; ADD NEW COMMANDS IN SEPERATE REGIONS HERE.
  226.                             #region send
  227.                             case "!send":
  228.                                 if (!isBotAdmin(callback.Sender))
  229.                                     break;
  230.  
  231.  
  232.                                 args = seperate(2, ' ', callback.Message);
  233.                                 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
  234.                                 if (args[0] == "-1")
  235.                                 {
  236.                                     steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "Command syntax: !send [friend] [message]");
  237.                                     return;
  238.                                 }
  239.                                 for (int i = 0; i < steamFriends.GetFriendCount(); i++)
  240.                                 {
  241.                                     SteamID friend = steamFriends.GetFriendByIndex(i);
  242.                                     if (steamFriends.GetFriendPersonaName(friend).ToLower().Contains(args[1].ToLower()))
  243.                                     {
  244.                                         steamFriends.SendChatMessage(friend, EChatEntryType.ChatMsg, args[2]);
  245.                                     }
  246.                                 }
  247.                                 break;
  248.                             #endregion
  249.                             //for the !send command.
  250.                             #region friends
  251.                             case "!friends":
  252.                                 Console.WriteLine("!friends command recieved. User: " + steamFriends.GetFriendPersonaName(callback.Sender));
  253.                                 for (int i = 0; i < steamFriends.GetFriendCount(); i++)
  254.                                 {
  255.                                     SteamID friend = steamFriends.GetFriendByIndex(i);
  256.                                     steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "Friend: " + steamFriends.GetFriendPersonaName(friend) + "  State:  " + steamFriends.GetFriendPersonaState(friend));
  257.                                 }
  258.                                 break;
  259.                             #endregion
  260.                             //lists all the bots friends, aswell as status.
  261.                             #region friend
  262.  
  263.                             case "!friend":
  264.  
  265.                                 args = seperate(1, ' ', callback.Message);
  266.                                 Console.WriteLine("!friend " + args[1] + " | " + steamFriends.GetFriendPersonaName(Convert.ToUInt64(args[1])) + " command recieved. User: " + steamFriends.GetFriendPersonaName(callback.Sender));
  267.  
  268.                                 if (!isBotAdmin(callback.Sender))
  269.                                     return;
  270.  
  271.                                 if (args[0] == "-1")
  272.                                 {
  273.                                     steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "Command syntax: !friend [Steam ID]");
  274.                                     return;
  275.                                 }
  276.  
  277.                                 try
  278.                                 {
  279.                                     SteamID validSID = Convert.ToUInt64(args[1]);
  280.                                     if (!validSID.IsValid)
  281.                                     {
  282.                                         steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "Invalid SteamID");
  283.                                         break;
  284.                                     }
  285.                                     steamFriends.AddFriend(validSID);
  286.                                 }
  287.                                 catch (FormatException)
  288.                                 {
  289.                                     steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "Invalid SteamID64.");
  290.                                 }
  291.                                 break;
  292.                             #endregion
  293.                             //the !friend command which is used to get the bot to add a specific SID / steam64id
  294.                             #region changename
  295.                             case "!changename":
  296.  
  297.                                 args = seperate(1, ' ', callback.Message);
  298.                                 Console.WriteLine("!changename " + args[1] + " | " + steamFriends.GetFriendPersonaName(Convert.ToUInt64(args[1])) + " command recieved. User: " + steamFriends.GetFriendPersonaName(callback.Sender));
  299.  
  300.                                 if (!isBotAdmin(callback.Sender))
  301.                                     return;
  302.  
  303.                                 if (args[0] == "-1")
  304.                                 {
  305.                                     steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "Command syntax: !change [name]");
  306.                                     return;
  307.                                 }
  308.  
  309.                                 steamFriends.SetPersonaName(args[1]);
  310.                                 break;
  311.                                 #endregion
  312.                             // simple command to change the bots name instead of having to log in everytime, because that shit's annoying.
  313.                         }
  314.                     }
  315.                 }
  316.                 string rLine;
  317.                 string trimmed = callback.Message;
  318.  
  319.                 char[] trim = { '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '=', '+', '[', ']', '{', '}', '\\', '|', ';', ':', '"', '\'', ',', '<', '.', '>', '/', '?' };
  320.  
  321.                 for (int i = 0; i < 30; i++)
  322.                 {
  323.                     trimmed = trimmed.Replace(trim[i].ToString(), "");
  324.                 }
  325.  
  326.                 StreamReader sReader = new StreamReader("chat.txt");
  327.  
  328.                 while ((rLine = sReader.ReadLine()) != null)
  329.                 {
  330.                     string text = rLine.Remove(rLine.IndexOf('|') - 1);
  331.                     string response = rLine.Remove(0, rLine.IndexOf('|') + 2);
  332.  
  333.                     if (callback.Message.Contains(text))
  334.                     {
  335.                         steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, response);
  336.                         sReader.Close();
  337.                         return;
  338.                     }
  339.                 }
  340.  
  341.             }
  342.         }
  343.  
  344.         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.
  345.         {
  346.             try
  347.             {
  348.                 if (sid.ConvertToUInt64() == Convert.ToUInt64(File.ReadAllText("admin.txt")))
  349.                 {
  350.                     return true;
  351.                 }
  352.  
  353.                 steamFriends.SendChatMessage(sid, EChatEntryType.ChatMsg, "You are not a bot admin.");
  354.                 Console.WriteLine(steamFriends.GetFriendPersonaName(sid) + " attempted to use an administrator command without privledges.");
  355.                 return false;
  356.             }
  357.             catch (Exception e)
  358.             {
  359.                 Console.WriteLine(e.Message);
  360.                 return false;
  361.             }
  362.         }
  363.  
  364.         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
  365.         {
  366.             string[] returned = new string[4];
  367.  
  368.             int i = 0;
  369.  
  370.             int error = 0;
  371.  
  372.             int lenght = thestring.Length;
  373.  
  374.             foreach (char c in thestring)
  375.             {
  376.                 if (i != number)
  377.                 {
  378.                     if (error > lenght || number > 5)
  379.                     {
  380.                         returned[0] = "-1";
  381.                         return returned;
  382.                     }
  383.                     else if (c == seperator)
  384.                     {
  385.                         returned[i] = thestring.Remove(thestring.IndexOf(c));
  386.                         thestring = thestring.Remove(0, thestring.IndexOf(c) + 1);
  387.                         i++;
  388.                     }
  389.                     error++;
  390.  
  391.                     if (error > lenght && i != number)
  392.                     {
  393.                         returned[0] = "-1";
  394.                         return returned;
  395.                     }
  396.                 }
  397.                 else
  398.                 {
  399.                     returned[i] = thestring;
  400.                 }
  401.             }
  402.             return returned;
  403.         }
  404.     }
  405. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement