Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using SteamKit2;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Net.Sockets;
- using System.Runtime.CompilerServices;
- using System.Text;
- using System.Threading;
- using System.Web.Script.Serialization;
- using System.Runtime.InteropServices;
- using System.Linq;
- namespace L3dg3ndBot___Steam_bot
- {
- internal class Program
- {
- static string path = @"C:\Program Files (x86)\Steam\steamapps\common\Unturned\";
- private const int APPID = 0x4a722;
- private static string authCode;
- private static RootObject config;
- private static bool isRunning = false;
- private static CallbackManager manager;
- private static string pass;
- private static SteamClient steamClient;
- private static SteamFriends steamFriends;
- private static SteamUser steamUser;
- private static string user;
- private static string inputPass()
- {
- string str = "";
- bool flag = true;
- while (flag)
- {
- char keyChar = Console.ReadKey(true).KeyChar;
- if (keyChar == '\r')
- {
- flag = false;
- Console.WriteLine();
- }
- else if ((keyChar == '\b') && (str.Length > 0))
- {
- Console.CursorLeft--;
- Console.Write(' ');
- Console.CursorLeft--;
- str = str.Substring(0, str.Length - 1);
- }
- else
- {
- str = str + keyChar.ToString();
- Console.Write("*");
- }
- }
- return str;
- }
- public static bool isBotAdmin(SteamID sid)
- {
- try
- {
- foreach (ulong num in config.Admins)
- {
- if (num == sid)
- {
- return true;
- }
- }
- steamFriends.SendChatMessage(sid, EChatEntryType.ChatMsg, "ATTENTION - YOU ARE NOT AN ADMIN");
- Console.WriteLine(steamFriends.GetFriendPersonaName(sid) + " attempted to use an Administrator command while not an Administrator, your actions have been saved for Admins to see!");
- }
- catch (Exception exception)
- {
- Console.WriteLine(exception.Message);
- return false;
- }
- return false;
- }
- public static bool isBotMod1(SteamID sid)
- {
- try
- {
- foreach (ulong num in config.Moderators)
- {
- if (num == sid)
- {
- return true;
- }
- }
- foreach (ulong num2 in config.Admins)
- {
- if (num2 == sid)
- {
- return true;
- }
- }
- steamFriends.SendChatMessage(sid, EChatEntryType.ChatMsg, "ATTENTION - YOU ARE NOT AN ADMIN / MOD");
- Console.WriteLine(steamFriends.GetFriendPersonaName(sid) + " attempted to use an Administrator command while not an Administrator, your actions have been saved for Admins to see!");
- }
- catch (Exception exception)
- {
- Console.WriteLine(exception.Message);
- return false;
- }
- return false;
- }
- public static bool isGroupBotAdmin(SteamFriends.ChatMsgCallback sid)
- {
- try
- {
- if (sid.ChatterID.ConvertToUInt64() == Convert.ToUInt64(System.IO.File.ReadAllText("admin.cfg")))
- {
- return true;
- }
- steamFriends.SendChatRoomMessage(sid.ChatterID.ConvertToUInt64(), EChatEntryType.ChatMsg, "ATTENTION - YOU ARE NOT AN ADMIN");
- Console.WriteLine(steamFriends.GetFriendPersonaName(sid.ChatterID.ConvertToUInt64()) + " attempted to use an Administrator command while not an Administrator, your actions have been saved for Admins to see!");
- }
- catch (Exception exception)
- {
- Console.WriteLine(exception.Message);
- return false;
- }
- return false;
- }
- private static void Main(string[] args)
- {
- Console.Title = "L3dg3ndBOT by Alexr03 (SteamBOT)";
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.WriteLine(string.Concat(new object[] { "[ ", DateTime.Now.Hour, ":", DateTime.Now.Minute, " ] L3dg3ndBOT was made by Alexr03 and doesn't collect ANY of information." }));
- Console.ForegroundColor = ConsoleColor.Cyan;
- Console.WriteLine("");
- Console.WriteLine(string.Concat(new object[] { "[ ", DateTime.Now.Hour, ":", DateTime.Now.Minute, " ] Loading... Please wait..." }));
- Thread.Sleep(0xdac);
- Console.WriteLine(string.Concat(new object[] { "[ ", DateTime.Now.Hour, ":", DateTime.Now.Minute, " ] Reloading configuration..." }));
- reloadConfig();
- Thread.Sleep(0x5dc);
- Console.Clear();
- Console.WriteLine("");
- Thread.Sleep(100);
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("Do note you are running a beta version of this software, so be greatful twats!");
- Console.ForegroundColor = ConsoleColor.Green;
- Console.WriteLine("");
- Console.WriteLine("");
- Console.WriteLine(".-. .----. .-. .----. .-. .-- -. .--. .---- -.");
- Thread.Sleep(100);
- Console.WriteLine(": : `-- ; : : `-- ; : : : .; :: ,. :`-. .-'");
- Thread.Sleep(100);
- Console.WriteLine(": : .' ' .-' : .--. .' ' ,-.,-. .-' : : .': :: : : : ");
- Thread.Sleep(100);
- Console.WriteLine(": :__ _`,`.' .; :' .; : _`,`.: ,. :' .; : : .; :: :; : : : ");
- Thread.Sleep(100);
- Console.WriteLine(":___.'`.__.'`.__.'`._. ;`.__.':_;:_;`.__.' :___.'`.__.' :_; ");
- Thread.Sleep(100);
- Console.WriteLine(" .-. : ");
- Thread.Sleep(100);
- Console.WriteLine(" `._.' ");
- Thread.Sleep(100);
- Console.WriteLine("");
- Thread.Sleep(100);
- Console.WriteLine(" SolarSenHosting Servers - Alexr03 2016 ");
- Thread.Sleep(100);
- Console.WriteLine("");
- Thread.Sleep(100);
- Console.WriteLine("");
- Console.ForegroundColor = ConsoleColor.Yellow;
- Thread.Sleep(100);
- Console.WriteLine(string.Concat(new object[] { "[ ", DateTime.Now.Hour, ":", DateTime.Now.Minute, " ] L3dg3ndBOT was made by Alexr03 and doesn't collect ANY of information. :D" }));
- Thread.Sleep(100);
- Console.ForegroundColor = ConsoleColor.White;
- Console.WriteLine(string.Concat(new object[] { "[ ", DateTime.Now.Hour, ":", DateTime.Now.Minute, " ] [L3dg3ndBOT Says: ] You need to login to Steam." }));
- Console.WriteLine(string.Concat(new object[] { "[ ", DateTime.Now.Hour, ":", DateTime.Now.Minute, " ] [L3dg3ndBOT Says: ] What is your Steam Username?" }));
- Console.ForegroundColor = ConsoleColor.Green;
- Console.Write("Username: ");
- user = Console.ReadLine();
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.WriteLine(string.Concat(new object[] { "[ ", DateTime.Now.Hour, ":", DateTime.Now.Minute, " ] [L3dg3ndBOT Says: ] Great! Now lets move on to the next part." }));
- Thread.Sleep(200);
- Console.WriteLine(string.Concat(new object[] { "[ ", DateTime.Now.Hour, ":", DateTime.Now.Minute, " ] [L3dg3ndBOT Says: ] What is your Steam Password?" }));
- Console.ForegroundColor = ConsoleColor.Green;
- Console.Write("Password: ");
- pass = inputPass();
- SteamLogIn();
- }
- private static void OnAccountInfo(SteamUser.AccountInfoCallback callback)
- {
- steamFriends.SetPersonaState(EPersonaState.Online);
- }
- private static void OnChatEnter(SteamFriends.ChatEnterCallback callback)
- {
- steamFriends.SendChatRoomMessage(callback.ChatID, EChatEntryType.ChatMsg, "What a nice chatroom! :steamhappy:");
- }
- private static void OnChatInvite(SteamFriends.ChatInviteCallback callback)
- {
- steamFriends.JoinChat(callback.ChatRoomID);
- Console.WriteLine(string.Concat(new object[] { user, " has been invited to ", callback.ChatRoomName, "'s group chat! (", callback.ChatRoomID, ")" }));
- }
- static void OnChatMessage(SteamFriends.FriendMsgCallback callback)
- {
- //This allows us to send a callback.Message, callback.Sender is the steam ID
- //of the friend who sent the callback.Message, therefore we can use it
- //for the SendChatMessage function which requires a steam ID
- //Then, chat entry type is set to chat callback.Message in order to
- //inform steam we are sending a text based callback.Message
- //and not a "x user is typing..." or something like that
- //this if statement prevents the bot from saqying something every while you're typing
- //because the "x user is typing..." is interperated as a steam callback.Message, so
- //we need to check if it's an actual chat callback.Message
- //we're going to create a switch statement to mass-check the arguements.
- //but first we need to check if the chat callback.Message has a valid length to keep the .Remove() from crashing
- if (callback.Message.Length > 1)
- {
- //we check the first character of the callback.Message for the valid command prefix
- if (callback.Message.Remove(1) == "/")
- {
- //we set a string equal to the callback.Message so we can manipulate it without screwing up the original callback.Message
- string command = callback.Message;
- //check to see if the callback.Message contains a space so we can then remove everything after it, so we can check the command. For example: !friend [sid64] becomes !friend.
- if (callback.Message.Contains(' '))
- {
- //set the string command equal to its stripped counterpart. !friend [sid64] becomes !friend for the switch statement later on
- command = callback.Message.Remove(callback.Message.IndexOf(' '));
- }
- /*now we use a switch statement, which is basically like a cascaded if statement. Instead of writing
- * if(command == "!send")
- * { stuff; }
- *
- * else if (command == "!friend")
- * { stuff; }
- *
- * we write
- *
- * switch(command)
- * {
- * case "!send":
- * stuff;
- * break;
- *
- * case "!friend":
- * stuff;
- * break;
- * }
- *
- * Which is much clearer and easier to write
- */
- string[] strArray;
- string[] args; //set up an array string for our future args
- switch (command)
- {
- //check the command to see if it is "!send"
- case "/start":
- //first we need to create a seperation function to seperate the args. so !friend [sid64] becomes args[0] = "!friend", args[1] = "sid64"
- //okay, now that we've got that, we need to keep people that aren't allowed to use the command from using it. See: isBotAdmin
- if (!isBotAdmin(callback.Sender)) //is the person not a bot admin?
- return; //exit function without executing commands.
- //seperate our arguments into 3 parts using our seperate function
- args = Seperate(2, ' ', callback.Message); //args[0] = !send, args[1] = friend to send to, args[2] = callback.Message
- Console.WriteLine("/start " + args[1] + " " + args[2] + " command received. User: " + steamFriends.GetFriendPersonaName(callback.Sender));
- if (args[0] == "-1") //see if the number of expected arguments was not received
- {
- steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "Processing your request for startup!");
- start();
- }
- break;
- case "/changename":
- if (!isBotAdmin(callback.Sender))
- {
- return;
- }
- strArray = seperate(1, ' ', callback.Message);
- Console.WriteLine("/changename " + strArray[1] + " command received. User: " + steamFriends.GetFriendPersonaName(callback.Sender));
- if (strArray[0] == "-1")
- {
- steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "Syntax: /changename [name]");
- return;
- }
- steamFriends.SetPersonaName(strArray[1]);
- break;
- case "/botstatus":
- if (!isBotAdmin(callback.Sender))
- {
- return;
- }
- strArray = seperate(1, ' ', callback.Message);
- Console.WriteLine("/botstatus " + strArray[1] + " command received. User: " + steamFriends.GetFriendPersonaName(callback.Sender));
- if (strArray[0] == "-1")
- {
- steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "Syntax: /botstatus [status]");
- return;
- }
- if (strArray[1] == "online")
- {
- steamFriends.SetPersonaState(EPersonaState.Online);
- steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "Status changed to Online");
- }
- if (strArray[1] == "busy")
- {
- steamFriends.SetPersonaState(EPersonaState.Busy);
- steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "Status changed to Busy");
- }
- if (strArray[1] == "away")
- {
- steamFriends.SetPersonaState(EPersonaState.Away);
- steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "Status changed to Away");
- }
- break;
- case "/shutdown":
- if (!isBotAdmin(callback.Sender))
- return;
- args = Seperate(2, ' ', callback.Message);
- Console.WriteLine("/shutdown " + args[1] + " " + args[2] + " command received. User: " + steamFriends.GetFriendPersonaName(callback.Sender));
- if (args[0] == "-1")
- {
- steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "Processing your request for shutdown!");
- shutdown();
- }
- break;
- }
- return;
- }
- }
- //now that we've got a config and a "Chatty" variable, let's make it so you can tell the bot to talk or not
- if (!config.Chatty)
- return;
- //if the bot is not chatty, exit.
- //before we start off, go to main() and work with the chat.txt
- //we'll need two string variables, one to hold the trimed version of the callback.Message, and another to hold the read line.
- string trimmed = callback.Message; //we can just set it here for now
- //we also need an array of the characters we need to trim from the callback.Message
- char[] trim = { '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '=', '+', '[', ']', '{', '}', '\\', '|', ';', ':', '"', '\'', ',', '<', '.', '>', '/', '?' };
- //now we need to create a stream reader to read the file that contains all of our chat lines
- }
- //we create a seperation function to make checking and using arguements much easier
- // number of arguements -1 v seperation char the string to seperate | the number of arguements - 1 is due to the fact that each arguement is preceded by one space, so "!friend [sid64]" has two args, but only one space.
- public static string[] Seperate(int number, char seperator, string thestring)
- {
- //we create a return array, set v to the max number of arguements ANY of your commands will take.
- string[] returned = new string[4];
- //create a counter variable
- int i = 0;
- //create an error variable to check for errors in the string
- int error = 0;
- //Get the length of the command so we can check it against the error variable to see if the number of arguements expected equaled the number of arguements recieved. for example "!friend" takes two arguements
- //if it were to only recieve one, once "error" equaled the string length and "i" was not equal to the expected number of arguements, we would return an error and handle it inside the switch statement
- //such as sending a chat callback.Message of the command's syntax
- int length = thestring.Length;
- //check each char in a foreach loop
- foreach (char s in thestring)
- {
- //check to see if the number of arguements expected has been reached yet
- if (i != number)
- {
- //check to see if the number of arguements expected wasn't reached, or if too many were recieved
- if (error > length || number > 5)
- {
- //return an error code
- returned[0] = "-1";
- return returned;
- }
- //check to see if the current character is equal to our seperator, so we can then isolate it and move it into the appropriate args[x]
- else if (s == seperator)
- {
- returned[i] = thestring.Remove(thestring.IndexOf(s));
- thestring = thestring.Remove(0, thestring.IndexOf(s) + 1);
- i++;
- }
- //increment error after each iteration
- error++;
- //check to see if the # of arguements expected was not reached
- if (error == length && i != number)
- {
- returned[0] = "-1";
- return returned;
- }
- }
- else
- {
- //return the last part of the string
- returned[i] = thestring;
- }
- }
- //return our array
- return returned;
- }
- private static void OnConnected(SteamClient.ConnectedCallback callback)
- {
- if (callback.Result != EResult.OK)
- {
- Console.WriteLine("L3dg3ndBOT has failed to connect!");
- Console.WriteLine("Error:" + callback.Result);
- isRunning = false;
- }
- else
- {
- Console.WriteLine("Succesfully connected to Steam! \nLogging in {0}!\n", user);
- byte[] buffer = null;
- if (System.IO.File.Exists("sentry.bin"))
- {
- buffer = CryptoHelper.SHAHash(System.IO.File.ReadAllBytes("sentry.bin"));
- }
- SteamUser.LogOnDetails details = new SteamUser.LogOnDetails
- {
- Username = user,
- Password = pass,
- AuthCode = authCode,
- SentryFileHash = buffer
- };
- steamUser.LogOn(details);
- Console.WriteLine("Connected!");
- if (System.IO.File.Exists("sentry.bin"))
- {
- buffer = CryptoHelper.SHAHash(System.IO.File.ReadAllBytes("sentry.bin"));
- }
- }
- }
- private static void OnDisconnected(SteamClient.DisconnectedCallback obj)
- {
- Console.WriteLine("\n{0} disconnected from steam, attempting reconnect in 5 seconds.", user);
- Thread.Sleep(0x1388);
- steamClient.Connect(null);
- }
- private static void onFriendAdded(SteamFriends.FriendsListCallback callback)
- {
- Thread.Sleep(0x9c4);
- foreach (SteamFriends.FriendsListCallback.Friend friend in callback.FriendList)
- {
- if (friend.Relationship == EFriendRelationship.RequestRecipient)
- {
- steamFriends.AddFriend(friend.SteamID);
- Thread.Sleep(500);
- steamFriends.SendChatMessage(friend.SteamID, EChatEntryType.ChatMsg, "Hello " + friend.SteamID + "! Succesfully added you as a friend!");
- }
- }
- }
- private static void OnLoggedOn(SteamUser.LoggedOnCallback callback)
- {
- if (callback.Result == EResult.AccountLogonDenied)
- {
- Console.WriteLine("This account is Guarded by steam.");
- Console.Write("Enter auth code sent to your {0} account.", callback.EmailDomain);
- authCode = Console.ReadLine();
- }
- else if (callback.Result != EResult.OK)
- {
- Console.WriteLine("Unable to login: {0}\n", callback.Result);
- isRunning = false;
- }
- else
- {
- Console.WriteLine("{0} logged in successfully.", user);
- }
- }
- public static void reloadConfig()
- {
- if (!System.IO.File.Exists("config.cfg"))
- {
- StringBuilder builder = new StringBuilder();
- builder.Append("{\r\n");
- builder.Append("\"Admins\":[76561198071072872],\r\n");
- builder.Append("\"Moderators\":[],\r\n");
- builder.Append("\"Server1Path\":@ C:/Program Files(x86)/Steam/steamapps/common/Unturned/Unturned.exe\r\n");
- builder.Append("\"Chatty\":true\r\n");
- builder.Append("}\r\n");
- System.IO.File.WriteAllText("config.cfg", builder.ToString());
- }
- try
- {
- config = new JavaScriptSerializer().Deserialize<RootObject>(System.IO.File.ReadAllText("config.cfg"));
- }
- catch (Exception exception)
- {
- Console.WriteLine("Error appeared : " + exception.Message);
- Console.ReadKey();
- reloadConfig();
- }
- }
- public static string[] seperate(int number, char seperator, string thestring)
- {
- string[] strArray = new string[4];
- int index = 0;
- int num2 = 0;
- int length = thestring.Length;
- foreach (char ch in thestring)
- {
- if (index != number)
- {
- int num5;
- if ((num2 > length) || (number > 5))
- {
- strArray[0] = "-1";
- return strArray;
- }
- if (ch == seperator)
- {
- strArray[index] = thestring.Remove(thestring.IndexOf(ch));
- thestring = thestring.Remove(0, thestring.IndexOf(ch) + 1);
- num5 = index;
- index = num5 + 1;
- }
- num5 = num2;
- num2 = num5 + 1;
- if ((num2 == length) && (index != number))
- {
- strArray[0] = "-1";
- return strArray;
- }
- }
- else
- {
- strArray[index] = thestring;
- }
- }
- return strArray;
- }
- private static void SteamLogIn()
- {
- steamClient = new SteamClient(ProtocolType.Tcp);
- manager = new CallbackManager(steamClient);
- steamUser = steamClient.GetHandler<SteamUser>();
- steamFriends = steamClient.GetHandler<SteamFriends>();
- new Callback<SteamClient.ConnectedCallback>(new Action<SteamClient.ConnectedCallback>(Program.OnConnected), manager);
- new Callback<SteamUser.LoggedOnCallback>(new Action<SteamUser.LoggedOnCallback>(Program.OnLoggedOn), manager);
- new Callback<SteamClient.DisconnectedCallback>(new Action<SteamClient.DisconnectedCallback>(Program.OnDisconnected), manager);
- new Callback<SteamKit2.SteamUser.UpdateMachineAuthCallback>(new Action<SteamKit2.SteamUser.UpdateMachineAuthCallback>(Program.UpdateMachineAuthCallback), manager);
- new Callback<SteamUser.AccountInfoCallback>(new Action<SteamUser.AccountInfoCallback>(Program.OnAccountInfo), manager);
- new Callback<SteamFriends.FriendMsgCallback>(new Action<SteamFriends.FriendMsgCallback>(Program.OnChatMessage), manager);
- new Callback<SteamFriends.FriendsListCallback>(new Action<SteamFriends.FriendsListCallback>(Program.onFriendAdded), manager);
- new Callback<SteamFriends.ChatInviteCallback>(new Action<SteamFriends.ChatInviteCallback>(Program.OnChatInvite), manager);
- new Callback<SteamFriends.ChatEnterCallback>(new Action<SteamFriends.ChatEnterCallback>(Program.OnChatEnter), manager);
- isRunning = true;
- steamClient.Connect(null);
- Console.WriteLine("Trying to connect to {0}\n", user);
- while (isRunning)
- {
- manager.RunWaitCallbacks(TimeSpan.FromSeconds(1.0));
- }
- }
- private static void UpdateMachineAuthCallback(SteamKit2.SteamUser.UpdateMachineAuthCallback callback)
- {
- Console.WriteLine("Updating sentry file...");
- byte[] buffer = CryptoHelper.SHAHash(callback.Data);
- System.IO.File.WriteAllBytes("sentry.bin", callback.Data);
- SteamUser.MachineAuthDetails details = new SteamUser.MachineAuthDetails
- {
- FileName = callback.FileName,
- BytesWritten = callback.BytesToWrite,
- FileSize = callback.Data.Length,
- Offset = callback.Offset,
- Result = EResult.OK,
- LastError = 0,
- OneTimePassword = (SteamUser.MachineAuthDetails.OTPDetails)callback.OneTimePassword
- };
- steamUser.SendMachineAuthResponse(details);
- Console.WriteLine("Done!");
- }
- static void start()
- {
- try
- {
- Console.WriteLine("Unturned is starting now.");
- Console.WriteLine("Starting...");
- System.Threading.Thread.Sleep(500);
- Process Unturned = new Process();
- Unturned.StartInfo.FileName = RootObject.Server1Path;
- Unturned.EnableRaisingEvents = true;
- Unturned.Start();
- }
- catch (Exception e)
- {
- Console.WriteLine(e.Message);
- }
- }
- static void shutdown()
- {
- try
- {
- foreach (Process proc in Process.GetProcessesByName("Unturned"))
- {
- proc.Kill();
- }
- }
- catch (Exception ex)
- {
- Console.Write(ex.Message);
- }
- }
- public class RootObject
- {
- public List<ulong> Admins { get; set; }
- public bool Chatty { get; set; }
- public List<ulong> Moderators { get; set; }
- public static string Server1Path { get; set; }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement