Advertisement
Guest User

Source

a guest
Dec 29th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 15.23 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 _1703_Bot
  11. {
  12.     class Program
  13.     {
  14.         static bool is_running = false;
  15.         static string user_name, pass;
  16.         static string auth_code;
  17.  
  18.         static SteamClient steam_client;
  19.         static CallbackManager callback_manager;
  20.         static SteamUser steam_user;
  21.         static SteamFriends steam_friends;
  22.  
  23.         static void Main(string[] args)
  24.         {
  25.             if (!File.Exists("chat.txt"))
  26.             {
  27.                 File.Create("chat.txt").Close();
  28.                 File.WriteAllText("chat.txt", "Hello | <MSG> Hello. Use '!help' to view commands.");
  29.             }
  30.             if (!File.Exists("admin.txt"))
  31.             {
  32.                 File.Create("admin.txt").Close();
  33.                 File.WriteAllText("admin.txt", "76561198053054477");
  34.             }
  35.  
  36.             Console.Title = "1703 Bot Console";
  37.             Console.Write("Username: ");
  38.             user_name = Console.ReadLine();
  39.             Console.Write("Password: ");
  40.             pass = Console.ReadLine();
  41.  
  42.             SteamLogIn();
  43.         }
  44.  
  45.         static void SteamLogIn()
  46.         {
  47.             steam_client = new SteamClient();
  48.             callback_manager = new CallbackManager(steam_client);
  49.             steam_user = steam_client.GetHandler<SteamUser>();
  50.             steam_friends = steam_client.GetHandler<SteamFriends>();
  51.  
  52.             callback_manager.Subscribe<SteamClient.ConnectedCallback>(OnConnected);
  53.             callback_manager.Subscribe<SteamClient.DisconnectedCallback>(OnDisconnected);
  54.             callback_manager.Subscribe<SteamUser.LoggedOnCallback>(OnLoggedOn);
  55.             callback_manager.Subscribe<SteamUser.LoggedOffCallback>(OnLoggedOff);
  56.             callback_manager.Subscribe<SteamUser.AccountInfoCallback>(OnAccountInfo);
  57.             callback_manager.Subscribe<SteamFriends.FriendMsgCallback>(OnChatMessage);
  58.             callback_manager.Subscribe<SteamFriends.FriendsListCallback>(OnFriendsList);
  59.             callback_manager.Subscribe<SteamUser.UpdateMachineAuthCallback>(OnMachineAuth);
  60.  
  61.             is_running = true;
  62.  
  63.             Console.Write("Connecting to Steam...");
  64.             steam_client.Connect();
  65.  
  66.             while (is_running)
  67.             {
  68.                 callback_manager.RunWaitCallbacks(TimeSpan.FromSeconds(1));
  69.             }
  70.             Console.ReadKey();
  71.         }
  72.  
  73.         static void OnConnected(SteamClient.ConnectedCallback callback)
  74.         {
  75.             if (callback.Result != EResult.OK)
  76.             {
  77.                 Console.WriteLine("Unable to connect to Steam: {0}", callback.Result);
  78.                 is_running = false;
  79.                 return;
  80.             }
  81.             Console.WriteLine("Connected to Steam! Logging in '{0}'", callback.Result);
  82.  
  83.             byte[] sentry_hash = null;
  84.             if (File.Exists("sentry.bin"))
  85.             {
  86.                 byte[] sentry_file = File.ReadAllBytes("sentry.bin");
  87.                 sentry_hash = CryptoHelper.SHAHash(sentry_file);
  88.             }
  89.  
  90.             steam_user.LogOn(new SteamUser.LogOnDetails
  91.             {
  92.                 Username = user_name,
  93.                 Password = pass,
  94.                 AuthCode = auth_code,
  95.                 SentryFileHash = sentry_hash,
  96.             });
  97.         }
  98.  
  99.         static void OnLoggedOn(SteamUser.LoggedOnCallback callback)
  100.         {
  101.             if (callback.Result == EResult.AccountLogonDenied)
  102.             {
  103.                 Console.WriteLine("This account is SteamGuard protected.");
  104.                 Console.WriteLine("Please enter the authorization code sent ot the email at {0}: ", callback.EmailDomain);
  105.                 auth_code = Console.ReadLine();
  106.                 return;
  107.             }
  108.  
  109.             if (callback.Result != EResult.OK)
  110.             {
  111.                 Console.WriteLine("Unable to log in to Steam: {0}\n", callback.Result);
  112.                 is_running = false;
  113.                 return;
  114.             }
  115.             Console.WriteLine("{0} Successfully Logged in to Steam!", user_name);
  116.         }
  117.  
  118.         static void OnMachineAuth(SteamUser.UpdateMachineAuthCallback callback)
  119.         {
  120.             Console.WriteLine("Updating Sentryfile...");
  121.             byte[] sentry_hash = CryptoHelper.SHAHash(callback.Data);
  122.             File.WriteAllBytes("sentry.bin", callback.Data);
  123.             steam_user.SendMachineAuthResponse(new SteamUser.MachineAuthDetails
  124.             {
  125.                 JobID = callback.JobID,
  126.                 FileName = callback.FileName,
  127.                 BytesWritten = callback.BytesToWrite,
  128.                 FileSize = callback.Data.Length,
  129.                 Offset = callback.Offset,
  130.                 Result = EResult.OK,
  131.                 LastError = 0,
  132.                 OneTimePassword = callback.OneTimePassword,
  133.                 SentryFileHash = sentry_hash,
  134.             });
  135.             Console.WriteLine("Done!");
  136.         }
  137.  
  138.         static void OnDisconnected(SteamClient.DisconnectedCallback callback)
  139.         {
  140.             Console.WriteLine("\n{0} Disconnected from Steam, reconnecting...\n", user_name);
  141.             Thread.Sleep(TimeSpan.FromSeconds(5));
  142.             steam_client.Connect();
  143.         }
  144.  
  145.         static void OnLoggedOff(SteamUser.LoggedOffCallback callback)
  146.         {
  147.             Console.WriteLine("Logged off of Steam: {0}", callback.Result);
  148.         }
  149.  
  150.         static void OnAccountInfo(SteamUser.AccountInfoCallback callback)
  151.         {
  152.             steam_friends.SetPersonaState(EPersonaState.Online);
  153.         }
  154.  
  155.         static void OnFriendsList(SteamFriends.FriendsListCallback callback)
  156.         {
  157.             Thread.Sleep(2500);
  158.             foreach (var friend in callback.FriendList)
  159.             {
  160.                 if (friend.Relationship == EFriendRelationship.RequestRecipient)
  161.                 {
  162.                     steam_friends.AddFriend(friend.SteamID);
  163.                     Thread.Sleep(500);
  164.                     steam_friends.SendChatMessage(friend.SteamID, EChatEntryType.ChatMsg, "<MSG> hello, I am 1703 bot. I will notify you of any 1703 outfit events. Use '!help' to view my commands.");
  165.                 }
  166.             }
  167.         }
  168.  
  169.         static void OnChatMessage(SteamFriends.FriendMsgCallback callback)
  170.         {
  171.             string[] args;
  172.             if (callback.EntryType == EChatEntryType.ChatMsg)
  173.             {
  174.                 if (callback.Message.Length > 1)
  175.                 {
  176.                     if (callback.Message.Remove(1) == "!")
  177.                     {
  178.                         string command = callback.Message;
  179.  
  180.                         if (callback.Message.Contains(" "))
  181.                         {
  182.                             command = callback.Message.Remove(callback.Message.IndexOf(' '));
  183.                         }
  184.  
  185.                         switch (command)
  186.                         {
  187.                             #region send
  188.                             case "!send":
  189.                                 if (!isBotAdmin(callback.Sender))
  190.                                     break;
  191.  
  192.                                 args = seperate(2, ' ', callback.Message);
  193.                                 Console.WriteLine("!send '" + args[1] + "' '" + args[2] + "'Command Recieved: User: " + steam_friends.GetFriendPersonaName(callback.Sender));
  194.                                 if (args[0] == "-1")
  195.                                 {
  196.                                     steam_friends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "<ERROR> Incorrect Syntax. Use '!send [friend] [message]'.");
  197.                                     return;
  198.                                 }
  199.                                 for (int i = 0; i < steam_friends.GetFriendCount(); i++)
  200.                                 {
  201.                                     SteamID friend = steam_friends.GetFriendByIndex(i);
  202.                                     if (steam_friends.GetFriendPersonaName(friend).ToLower().Contains(args[1].ToLower()))
  203.                                     {
  204.                                         steam_friends.SendChatMessage(friend, EChatEntryType.ChatMsg, "<MSG> " + args[2]);
  205.                                     }
  206.                                 }
  207.                                 break;
  208.                             #endregion
  209.                             #region broadcast
  210.                             case "!broadcast":
  211.                                 if (!isBotAdmin(callback.Sender))
  212.                                     break;
  213.  
  214.                                 args = seperate(1, ' ', callback.Message);
  215.                                 Console.WriteLine("!broadcast '" + args[1] + "' Command Recieved: User: " + steam_friends.GetFriendPersonaName(callback.Sender));
  216.                                 if (args[0] == "-1")
  217.                                 {
  218.                                     steam_friends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "<ERROR> Inccorect syntax. Use '!broadcast [message]'.");
  219.                                 }
  220.                                 for (int i = 0; i < steam_friends.GetFriendCount(); i++)
  221.                                 {
  222.                                     SteamID friend = steam_friends.GetFriendByIndex(i);
  223.                                     if (friend > 0)
  224.                                     {
  225.                                         steam_friends.SendChatMessage(friend, EChatEntryType.ChatMsg, "<BROADCAST> " + args[1]);
  226.                                     }
  227.                                 }
  228.                                 break;
  229.                             #endregion
  230.                             #region friends
  231.                             case "!friends":
  232.                                 Console.WriteLine("!friends Command Recieved: User: " + steam_friends.GetFriendPersonaName(callback.Sender));
  233.                                 steam_friends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "<INFO> List of bot friends: ");
  234.                                 for (int i = 0; i < steam_friends.GetFriendCount(); i++)
  235.                                 {
  236.                                     SteamID friend = steam_friends.GetFriendByIndex(i);
  237.                                     steam_friends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "<INFO> Friend: " + steam_friends.GetFriendPersonaName(friend) + " State: " + steam_friends.GetFriendPersonaState(friend));
  238.                                 }
  239.                                 break;
  240.                             #endregion
  241.                             #region help
  242.                             case "!help":
  243.                                 Console.Write("!help Command Recieved: User: " + steam_friends.GetFriendPersonaName(callback.Sender));
  244.                                 steam_friends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "<INFO> List of User Commands:");
  245.                                 steam_friends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "<INFO> '!help' : Lists all Available Commands");
  246.                                 steam_friends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "<INFO> '!friends' : Lists all the friends of the 1703 Bot");
  247.                                 steam_friends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "<INFO> List of Admin Commands:");
  248.                                 steam_friends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "<INFO> '!broadcast [message]' : Broadcasts a message to all 1703 Bot Friends");
  249.                                 steam_friends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "<INFO> 'send [friend] [message]' : sends a message to specified 1703 Bot Friend");
  250.                                 break;
  251.                             #endregion
  252.  
  253.                         }
  254.                     }
  255.                 }
  256.                 string rLine;
  257.                 string trimmed = callback.Message;
  258.                 char[] trim = { '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '=', '+', '[', ']', '{', '}', '\\', '|', ';', ':', '"', '\'', ',', '<', '.', '>', '/', '?' };
  259.                 for (int i = 0; i < 30; i++)
  260.                 {
  261.                     trimmed = trimmed.Replace(trim[i].ToString(), "");
  262.                 }
  263.  
  264.                 StreamReader stream_reader = new StreamReader("chat.txt");
  265.  
  266.                 while ((rLine = stream_reader.ReadLine()) != null)
  267.                 {
  268.                     string text = rLine.Remove(rLine.IndexOf('|') - 1);
  269.                     string response = rLine.Remove(0, rLine.IndexOf('|') + 2);
  270.  
  271.                     if (callback.Message.Contains(text))
  272.                     {
  273.                         Console.WriteLine("Cat reply Sent: User: " + steam_friends.GetFriendPersonaName(callback.Sender));
  274.                         steam_friends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, response);
  275.                         stream_reader.Close();
  276.                         return;
  277.                     }
  278.                 }
  279.             }
  280.         }
  281.  
  282.         public static bool isBotAdmin(SteamID sid)
  283.         {
  284.             string[] lines = null;
  285.             try
  286.             {
  287.                 lines = File.ReadAllLines("admin.txt");
  288.                 foreach (string i in lines)
  289.                 {
  290.                     if (sid.ConvertToUInt64() == Convert.ToUInt64(i))
  291.                     {
  292.                         return true;
  293.                     }
  294.                 }
  295.                 steam_friends.SendChatMessage(sid, EChatEntryType.ChatMsg, "<ERROR> You do not have sufficient permission to use that command. If this is an error, and you are an admin, please contact D3i (Dei) or Infilze (Koolbade)");
  296.                 Console.WriteLine(steam_friends.GetFriendPersonaName(sid) + " attemted to use an administrator command while not an administrator.");
  297.                 return false;
  298.             }
  299.  
  300.                 catch (Exception e)
  301.             {
  302.                 Console.WriteLine(e.Message);
  303.                 return false;
  304.             }
  305.         }
  306.  
  307.         public static string[] seperate(int number, char seperator, string thestring)
  308.         {
  309.             string[] returned = new string[4];
  310.             int i = 0;
  311.             int error = 0;
  312.             int length = thestring.Length;
  313.  
  314.             foreach (char c in thestring)
  315.             {
  316.                 if (i != number)
  317.                 {
  318.                     if (error > length || number > 5)
  319.                     {
  320.                         returned[0] = "-1";
  321.                         return returned;
  322.                     }
  323.                     else if (c == seperator)
  324.                     {
  325.                         returned[i] = thestring.Remove(thestring.IndexOf(c));
  326.                         thestring = thestring.Remove(0, thestring.IndexOf(c) + 1);
  327.                         i++;
  328.                     }
  329.                     error++;
  330.                     if (error == length && i != number)
  331.                     {
  332.                         returned[0] = "-1";
  333.                         return returned;
  334.                     }
  335.                 }
  336.                 else
  337.                 {
  338.                     returned[i] = thestring;
  339.                 }
  340.             }
  341.             return returned;
  342.         }
  343.     }
  344. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement