Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //to do:
- //try to do !weather, getting all the IDs for the cities in a json/txt file
- //new webclient timezone!!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using SteamKit2;
- using System.IO;
- using System.Threading;
- using OpenWeatherMap;
- using System.Net;
- using System.Xml;
- namespace steamBot
- {
- class Program
- {
- static string user, pass;
- static string authCode;
- static SteamClient steamClient;
- static CallbackManager manager;
- static SteamUser steamUser;
- static SteamFriends steamFriends;
- static bool isRunning = false;
- static void Main(string[] args)
- {
- if (!File.Exists("chat.txt"))
- {
- File.Create("chat.txt").Close();
- File.WriteAllText("chat.txt", "abc | 123");
- }
- Console.Title = "Steam Bot";
- Console.Write("To initiate steam bot, enter your credentials\n\n");
- Thread.Sleep(750);
- Console.Write("Username: ");
- user = Console.ReadLine();
- Console.Write("Password: ");
- pass = Console.ReadLine();
- SteamLogIn();
- }
- private static DateTime startTime = DateTime.Now;
- static void SteamLogIn()
- {
- steamClient = new SteamClient();
- manager = new CallbackManager(steamClient);
- steamUser = steamClient.GetHandler<SteamUser>();
- steamFriends = steamClient.GetHandler<SteamFriends>();
- manager.Subscribe<SteamClient.ConnectedCallback>(OnConnected);
- manager.Subscribe<SteamClient.DisconnectedCallback>(OnDisconnected);
- manager.Subscribe<SteamUser.LoggedOnCallback>(OnLoggedOn);
- manager.Subscribe<SteamUser.UpdateMachineAuthCallback>(UpdateAuthMachineCallback);
- manager.Subscribe<SteamUser.AccountInfoCallback>(OnAccountInfo);
- manager.Subscribe<SteamFriends.FriendMsgCallback>(OnChatMessage);
- manager.Subscribe<SteamFriends.FriendsListCallback>(OnFriendsList);
- isRunning = true;
- Console.WriteLine("\nConnecting to Steam..\n");
- steamClient.Connect();
- while (isRunning)
- {
- manager.RunWaitCallbacks(TimeSpan.FromSeconds(1));
- }
- }
- static void OnConnected(SteamClient.ConnectedCallback callback)
- {
- Console.WriteLine("Connected to steam server \nLogging in user: {0}", user);
- 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)
- {
- if (callback.Result == EResult.AccountLogonDenied)
- {
- Console.WriteLine("This account is Steam Guard protected");
- Console.WriteLine("Please enter the code sent to your email at {0}: ", callback.EmailDomain);
- authCode = Console.ReadLine();
- return;
- }
- if (callback.Result != EResult.OK)
- {
- Console.WriteLine("Unable to log in to Steam: {0}\n", callback.Result);
- Console.WriteLine("Closing in 5 seconds");
- Thread.Sleep(1000);
- Console.WriteLine("Closing in 4 seconds");
- Thread.Sleep(1000);
- Console.WriteLine("Closing in 3 seconds");
- Thread.Sleep(1000);
- Console.WriteLine("Closing in 2 seconds");
- Thread.Sleep(1000);
- Console.WriteLine("Closing..");
- Thread.Sleep(250);
- isRunning = false;
- return;
- }
- Console.Clear();
- Console.WriteLine("Sucessfully logged in as: {0}", user);
- }
- static void UpdateAuthMachineCallback(SteamUser.UpdateMachineAuthCallback callback)
- {
- Console.WriteLine("Updating sentry file..");
- 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.WriteLine("Done!");
- }
- static void OnDisconnected(SteamClient.DisconnectedCallback callback)
- {
- Console.WriteLine("\n {0} Disconnected from steam servers , reconnecting in 5...\n", user);
- Thread.Sleep(5000);
- steamClient.Connect();
- Console.Clear();
- }
- static void OnAccountInfo(SteamUser.AccountInfoCallback callback)
- {
- steamFriends.SetPersonaState(EPersonaState.Online);
- }
- static void OnFriendsList(SteamFriends.FriendsListCallback callback)
- {
- Thread.Sleep(2500);
- foreach (var friend in callback.FriendList)
- {
- if (friend.Relationship == EFriendRelationship.RequestRecipient)
- {
- Console.WriteLine("Friend Request: " + steamFriends.GetFriendPersonaName(friend.SteamID) + " sent a friend request. Accepting..");
- steamFriends.AddFriend(friend.SteamID);
- Thread.Sleep(500);
- steamFriends.SendChatMessage(friend.SteamID, EChatEntryType.ChatMsg, "Hello I am a bot, for help type !help or !commands");
- }
- }
- }
- static void OnChatMessage(SteamFriends.FriendMsgCallback callback)
- {
- bool mSent = false;
- string command = callback.Message;
- string command2 = callback.Message;
- TimeZoneInfo localZone = TimeZoneInfo.Local;
- string[] args;
- if (callback.EntryType == EChatEntryType.ChatMsg)
- {
- if (callback.Message.Length > 1)
- {
- if (callback.Message.Remove(1) == "!")
- {
- if (callback.Message.Contains(" "))
- {
- command = callback.Message.Remove(callback.Message.IndexOf(' '));
- }
- switch (command)
- {
- #region !send
- case "!send":
- args = separate(2, ' ', callback.Message);
- Console.WriteLine("!send " + args[1] + " '" + args[2] + "' command recieved. User: " + steamFriends.GetFriendPersonaName(callback.Sender));
- if (args[0] == "-1")
- {
- steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "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]);
- steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "Message with the text '" + args[2] + "' sent to " + args[1]);
- }
- }
- mSent = true;
- return;
- #endregion
- #region !friends
- case "!friends":
- Console.WriteLine("!friends command recieved. User: " + steamFriends.GetFriendPersonaName(callback.Sender));
- steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "This command might not show all steam friends because of steam's API problems");
- Thread.Sleep(4000);
- for (int i = 1; i < steamFriends.GetFriendCount() + 1; i++)
- {
- SteamID friend = steamFriends.GetFriendByIndex(i);
- steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "Friend: " + steamFriends.GetFriendPersonaName(friend) + " - State: " + steamFriends.GetFriendPersonaState(friend));
- if (i % 5 == 0)
- {
- Thread.Sleep(5000);
- }
- }
- mSent = true;
- return;
- #endregion
- #region !commands
- case "!commands":
- Console.WriteLine("!commands command recieved. User: " + steamFriends.GetFriendPersonaName(callback.Sender));
- steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "Available commands: !help, !send; !friends, !twitter, !time, !mytimezone, !uptime");
- mSent = true;
- return;
- #endregion
- #region !twitter
- case "!twitter":
- Console.WriteLine("!twitter command recieved. User: " + steamFriends.GetFriendPersonaName(callback.Sender));
- steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "https://twitter.com/_diogoo19");
- mSent = true;
- return;
- #endregion
- #region !time
- case "!time":
- args = separate(1, ' ', callback.Message);
- if (args[0] == "-1")
- {
- Console.WriteLine("!time command recieved. User: " + steamFriends.GetFriendPersonaName(callback.Sender));
- steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "Current UTC time: " + System.DateTime.UtcNow.ToString());
- mSent = true;
- return;
- }
- return;
- #endregion
- #region !mytimezone
- case "!mytimezone":
- Console.WriteLine("!mytimezone command recieved. User: " + steamFriends.GetFriendPersonaName(callback.Sender));
- steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "Your timezone is " + localZone.DaylightName);
- mSent = true;
- return;
- #endregion
- case "!help":
- Console.WriteLine("!help command recieved. User: " + steamFriends.GetFriendPersonaName(callback.Sender));
- steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "I am a bot and I will answer if you greet me. For commands do !commands");
- mSent = true;
- return;
- case "!uptime":
- TimeSpan time = DateTime.Now - startTime;
- Console.WriteLine("!uptime command recieved. User: " + steamFriends.GetFriendPersonaName(callback.Sender) + " Response: " + time);
- steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "I have been online for: " + time + " (hh:mm:ss:ms)");
- mSent = true;
- return;
- //http://api.openweathermap.org/data/2.5/forecast?id={CITYID}&APPID={APIKEY}
- }
- 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");
- while ((rLine = sReader.ReadLine()) != null)
- {
- string text = rLine.Remove(rLine.IndexOf('|') - 1);
- string response = rLine.Remove(0, rLine.IndexOf('|') + 2);
- if (callback.Message.ToLower().Contains(text))
- {
- Console.WriteLine("Message: " + steamFriends.GetFriendPersonaName(callback.Sender) + ": " + callback.Message + " -- BOT Response: " + response);
- steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, response);
- sReader.Close();
- mSent = true;
- return;
- }
- if (!callback.Message.ToLower().Contains(text))
- {
- mSent = false;
- }
- }
- if (mSent == false)
- {
- Console.WriteLine("Message not recognized. User: " + steamFriends.GetFriendPersonaName(callback.Sender) + " with the message: " + callback.Message);
- steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "Sorry, I didn't understand that");
- }
- }
- }
- }
- }
- #pragma warning disable IDE1006 // Naming Styles
- public static string[] separate(int number, char separator, string thestring)
- #pragma warning restore IDE1006 // Naming Styles
- #pragma warning restore IDE1006 // Naming Styles
- {
- 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 == separator)
- {
- 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