Advertisement
Guest User

Untitled

a guest
Dec 14th, 2018
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 16.65 KB | None | 0 0
  1. //to do:
  2. //try to do !weather, getting all the IDs for the cities in a json/txt file
  3. //new webclient timezone!!
  4.  
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using SteamKit2;
  11. using System.IO;
  12. using System.Threading;
  13. using OpenWeatherMap;
  14. using System.Net;
  15. using System.Xml;
  16.  
  17.  
  18. namespace steamBot
  19. {
  20.     class Program
  21.     {
  22.         static string user, pass;
  23.         static string authCode;
  24.  
  25.         static SteamClient steamClient;
  26.         static CallbackManager manager;
  27.         static SteamUser steamUser;
  28.         static SteamFriends steamFriends;
  29.  
  30.  
  31.         static bool isRunning = false;
  32.  
  33.         static void Main(string[] args)
  34.         {
  35.             if (!File.Exists("chat.txt"))
  36.             {
  37.                 File.Create("chat.txt").Close();
  38.                 File.WriteAllText("chat.txt", "abc | 123");
  39.             }
  40.             Console.Title = "Steam Bot";
  41.  
  42.             Console.Write("To initiate steam bot, enter your credentials\n\n");
  43.             Thread.Sleep(750);
  44.             Console.Write("Username: ");
  45.             user = Console.ReadLine();
  46.  
  47.             Console.Write("Password: ");
  48.             pass = Console.ReadLine();
  49.  
  50.             SteamLogIn();
  51.         }
  52.  
  53.         private static DateTime startTime = DateTime.Now;
  54.  
  55.         static void SteamLogIn()
  56.         {
  57.             steamClient = new SteamClient();
  58.             manager = new CallbackManager(steamClient);
  59.             steamUser = steamClient.GetHandler<SteamUser>();
  60.             steamFriends = steamClient.GetHandler<SteamFriends>();
  61.             manager.Subscribe<SteamClient.ConnectedCallback>(OnConnected);
  62.             manager.Subscribe<SteamClient.DisconnectedCallback>(OnDisconnected);
  63.             manager.Subscribe<SteamUser.LoggedOnCallback>(OnLoggedOn);
  64.             manager.Subscribe<SteamUser.UpdateMachineAuthCallback>(UpdateAuthMachineCallback);
  65.             manager.Subscribe<SteamUser.AccountInfoCallback>(OnAccountInfo);
  66.             manager.Subscribe<SteamFriends.FriendMsgCallback>(OnChatMessage);
  67.             manager.Subscribe<SteamFriends.FriendsListCallback>(OnFriendsList);
  68.  
  69.  
  70.             isRunning = true;
  71.  
  72.             Console.WriteLine("\nConnecting to Steam..\n");
  73.  
  74.             steamClient.Connect();
  75.  
  76.             while (isRunning)
  77.             {
  78.                 manager.RunWaitCallbacks(TimeSpan.FromSeconds(1));
  79.             }
  80.  
  81.         }
  82.  
  83.         static void OnConnected(SteamClient.ConnectedCallback callback)
  84.         {
  85.             Console.WriteLine("Connected to steam server \nLogging in user: {0}", user);
  86.             byte[] sentryHash = null;
  87.             if (File.Exists("sentry.bin"))
  88.             {
  89.                 byte[] sentryfile = File.ReadAllBytes("sentry.bin");
  90.  
  91.                 sentryHash = CryptoHelper.SHAHash(sentryfile);
  92.  
  93.  
  94.             }
  95.             steamUser.LogOn(new SteamUser.LogOnDetails
  96.             {
  97.                 Username = user,
  98.                 Password = pass,
  99.  
  100.                 AuthCode = authCode,
  101.  
  102.                 SentryFileHash = sentryHash
  103.  
  104.             });
  105.         }
  106.         static void OnLoggedOn(SteamUser.LoggedOnCallback callback)
  107.         {
  108.             if (callback.Result == EResult.AccountLogonDenied)
  109.             {
  110.                 Console.WriteLine("This account is Steam Guard protected");
  111.                 Console.WriteLine("Please enter the code sent to your email at {0}: ", callback.EmailDomain);
  112.                 authCode = Console.ReadLine();
  113.                 return;
  114.             }
  115.             if (callback.Result != EResult.OK)
  116.             {
  117.                 Console.WriteLine("Unable to log in to Steam: {0}\n", callback.Result);
  118.                 Console.WriteLine("Closing in 5 seconds");
  119.                 Thread.Sleep(1000);
  120.                 Console.WriteLine("Closing in 4 seconds");
  121.                 Thread.Sleep(1000);
  122.                 Console.WriteLine("Closing in 3 seconds");
  123.                 Thread.Sleep(1000);
  124.                 Console.WriteLine("Closing in 2 seconds");
  125.                 Thread.Sleep(1000);
  126.                 Console.WriteLine("Closing..");
  127.                 Thread.Sleep(250);
  128.                 isRunning = false;
  129.                 return;
  130.             }
  131.             Console.Clear();
  132.             Console.WriteLine("Sucessfully logged in as: {0}", user);
  133.         }
  134.  
  135.         static void UpdateAuthMachineCallback(SteamUser.UpdateMachineAuthCallback callback)
  136.         {
  137.             Console.WriteLine("Updating sentry file..");
  138.  
  139.             byte[] sentryHash = CryptoHelper.SHAHash(callback.Data);
  140.  
  141.             File.WriteAllBytes("sentry.bin", callback.Data);
  142.  
  143.             steamUser.SendMachineAuthResponse(new SteamUser.MachineAuthDetails
  144.             {
  145.                 JobID = callback.JobID,
  146.                 FileName = callback.FileName,
  147.                 BytesWritten = callback.BytesToWrite,
  148.                 FileSize = callback.Data.Length,
  149.                 Offset = callback.Offset,
  150.                 Result = EResult.OK,
  151.                 LastError = 0,
  152.                 OneTimePassword = callback.OneTimePassword,
  153.                 SentryFileHash = sentryHash,
  154.             });
  155.             Console.WriteLine("Done!");
  156.         }
  157.  
  158.         static void OnDisconnected(SteamClient.DisconnectedCallback callback)
  159.         {
  160.             Console.WriteLine("\n {0} Disconnected from steam servers , reconnecting in 5...\n", user);
  161.             Thread.Sleep(5000);
  162.             steamClient.Connect();
  163.             Console.Clear();
  164.         }
  165.  
  166.         static void OnAccountInfo(SteamUser.AccountInfoCallback callback)
  167.         {
  168.             steamFriends.SetPersonaState(EPersonaState.Online);
  169.         }
  170.  
  171.         static void OnFriendsList(SteamFriends.FriendsListCallback callback)
  172.         {
  173.             Thread.Sleep(2500);
  174.  
  175.             foreach (var friend in callback.FriendList)
  176.             {
  177.                 if (friend.Relationship == EFriendRelationship.RequestRecipient)
  178.                 {
  179.                     Console.WriteLine("Friend Request: " + steamFriends.GetFriendPersonaName(friend.SteamID) + " sent a friend request. Accepting..");
  180.                     steamFriends.AddFriend(friend.SteamID);
  181.                     Thread.Sleep(500);
  182.                     steamFriends.SendChatMessage(friend.SteamID, EChatEntryType.ChatMsg, "Hello I am a bot, for help type !help or !commands");
  183.                 }
  184.             }
  185.         }
  186.  
  187.         static void OnChatMessage(SteamFriends.FriendMsgCallback callback)
  188.         {
  189.             bool mSent = false;
  190.             string command = callback.Message;
  191.             string command2 = callback.Message;
  192.             TimeZoneInfo localZone = TimeZoneInfo.Local;
  193.             string[] args;
  194.             if (callback.EntryType == EChatEntryType.ChatMsg)
  195.             {
  196.                 if (callback.Message.Length > 1)
  197.                 {
  198.                     if (callback.Message.Remove(1) == "!")
  199.                     {
  200.                         if (callback.Message.Contains(" "))
  201.                         {
  202.                             command = callback.Message.Remove(callback.Message.IndexOf(' '));
  203.                         }
  204.  
  205.                         switch (command)
  206.                         {
  207.                             #region !send
  208.                             case "!send":
  209.                                 args = separate(2, ' ', callback.Message);
  210.                                 Console.WriteLine("!send " + args[1] + " '" + args[2] + "' command recieved. User: " + steamFriends.GetFriendPersonaName(callback.Sender));
  211.                                 if (args[0] == "-1")
  212.                                 {
  213.                                     steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "Syntax: !send [friend] [message]");
  214.                                     return;
  215.                                 }
  216.                                 for (int i = 0; i < steamFriends.GetFriendCount(); i++)
  217.                                 {
  218.                                     SteamID friend = steamFriends.GetFriendByIndex(i);
  219.                                     if (steamFriends.GetFriendPersonaName(friend).ToLower().Contains(args[1].ToLower()))
  220.                                     {
  221.                                         steamFriends.SendChatMessage(friend, EChatEntryType.ChatMsg, args[2]);
  222.                                         steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "Message with the text '" + args[2] + "' sent to " + args[1]);
  223.                                     }
  224.                                 }
  225.                                 mSent = true;
  226.                                 return;
  227.                             #endregion
  228.                             #region !friends
  229.                             case "!friends":
  230.                                 Console.WriteLine("!friends command recieved. User: " + steamFriends.GetFriendPersonaName(callback.Sender));
  231.                                 steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "This command might not show all steam friends because of steam's API problems");
  232.                                 Thread.Sleep(4000);
  233.                                 for (int i = 1; i < steamFriends.GetFriendCount() + 1; i++)
  234.                                 {
  235.                                     SteamID friend = steamFriends.GetFriendByIndex(i);
  236.                                     steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "Friend: " + steamFriends.GetFriendPersonaName(friend) + " - State: " + steamFriends.GetFriendPersonaState(friend));
  237.                                     if (i % 5 == 0)
  238.                                     {
  239.                                         Thread.Sleep(5000);
  240.                                     }
  241.                                 }
  242.                                 mSent = true;
  243.                                 return;
  244.                             #endregion
  245.                             #region !commands
  246.                             case "!commands":
  247.                                 Console.WriteLine("!commands command recieved. User: " + steamFriends.GetFriendPersonaName(callback.Sender));
  248.                                 steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "Available commands: !help, !send; !friends, !twitter, !time, !mytimezone, !uptime");
  249.                                 mSent = true;
  250.                                 return;
  251.                             #endregion
  252.                             #region !twitter
  253.                             case "!twitter":
  254.                                 Console.WriteLine("!twitter command recieved. User: " + steamFriends.GetFriendPersonaName(callback.Sender));
  255.                                 steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "https://twitter.com/_diogoo19");
  256.                                 mSent = true;
  257.                                 return;
  258.                             #endregion
  259.                             #region !time
  260.                             case "!time":
  261.                                 args = separate(1, ' ', callback.Message);
  262.                                 if (args[0] == "-1")
  263.                                 {
  264.                                     Console.WriteLine("!time command recieved. User: " + steamFriends.GetFriendPersonaName(callback.Sender));
  265.                                     steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "Current UTC time: " + System.DateTime.UtcNow.ToString());
  266.                                     mSent = true;
  267.                                     return;
  268.                                 }
  269.                                 return;
  270.                             #endregion
  271.                             #region !mytimezone
  272.                             case "!mytimezone":
  273.                                 Console.WriteLine("!mytimezone command recieved. User: " + steamFriends.GetFriendPersonaName(callback.Sender));
  274.                                 steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "Your timezone is " + localZone.DaylightName);
  275.                                 mSent = true;
  276.                                 return;
  277.                             #endregion
  278.                             case "!help":
  279.                                 Console.WriteLine("!help command recieved. User: " + steamFriends.GetFriendPersonaName(callback.Sender));
  280.                                 steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "I am a bot and I will answer if you greet me. For commands do !commands");
  281.                                 mSent = true;
  282.                                 return;
  283.                             case "!uptime":
  284.                                 TimeSpan time = DateTime.Now - startTime;
  285.                                 Console.WriteLine("!uptime command recieved. User: " + steamFriends.GetFriendPersonaName(callback.Sender) + " Response: " + time);
  286.                                 steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "I have been online for: " + time + " (hh:mm:ss:ms)");
  287.                                 mSent = true;
  288.                                 return;
  289.                                 //http://api.openweathermap.org/data/2.5/forecast?id={CITYID}&APPID={APIKEY}
  290.                         }
  291.                         string rLine;
  292.                         string trimmed = callback.Message;
  293.                         char[] trim = { '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '=', '+', '[', ']', '{', '}', '\\', '|', ';', ':', '"', '\'', ',', '<', '.', '>', '/', '?' };
  294.  
  295.                         for (int i = 0; i < 30; i++)
  296.                         {
  297.                             trimmed = trimmed.Replace(trim[i].ToString(), "");
  298.                         }
  299.                         StreamReader sReader = new StreamReader("chat.txt");
  300.  
  301.                         while ((rLine = sReader.ReadLine()) != null)
  302.                         {
  303.                             string text = rLine.Remove(rLine.IndexOf('|') - 1);
  304.                             string response = rLine.Remove(0, rLine.IndexOf('|') + 2);
  305.  
  306.                             if (callback.Message.ToLower().Contains(text))
  307.                             {
  308.                                 Console.WriteLine("Message: " + steamFriends.GetFriendPersonaName(callback.Sender) + ": " + callback.Message + " -- BOT Response: " + response);
  309.                                 steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, response);
  310.                                 sReader.Close();
  311.                                 mSent = true;
  312.                                 return;
  313.                             }
  314.                             if (!callback.Message.ToLower().Contains(text))
  315.                             {
  316.                                 mSent = false;
  317.                             }
  318.                         }
  319.                         if (mSent == false)
  320.                         {
  321.                             Console.WriteLine("Message not recognized. User: " + steamFriends.GetFriendPersonaName(callback.Sender) + " with the message: " + callback.Message);
  322.                             steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "Sorry, I didn't understand that");
  323.                         }
  324.                     }
  325.                 }
  326.             }
  327.         }
  328.  
  329. #pragma warning disable IDE1006 // Naming Styles
  330.         public static string[] separate(int number, char separator, string thestring)
  331. #pragma warning restore IDE1006 // Naming Styles
  332. #pragma warning restore IDE1006 // Naming Styles
  333.         {
  334.             string[] returned = new string[4];
  335.  
  336.             int i = 0;
  337.  
  338.             int error = 0;
  339.  
  340.             int lenght = thestring.Length;
  341.  
  342.             foreach (char c in thestring)
  343.             {
  344.                 if (i != number)
  345.                 {
  346.                     if (error > lenght || number > 5)
  347.                     {
  348.                         returned[0] = "-1";
  349.                         return returned;
  350.                     }
  351.                     else if (c == separator)
  352.                     {
  353.                         returned[i] = thestring.Remove(thestring.IndexOf(c));
  354.                         thestring = thestring.Remove(0, thestring.IndexOf(c) + 1);
  355.                         i++;
  356.                     }
  357.                     error++;
  358.  
  359.                     if (error == lenght && i != number)
  360.                     {
  361.                         returned[0] = "-1";
  362.                         return returned;
  363.                     }
  364.                 }
  365.                 else
  366.                 {
  367.                     returned[i] = thestring;
  368.                 }
  369.             }
  370.             return returned;
  371.         }
  372.     }
  373. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement