Advertisement
Guest User

Untitled

a guest
Mar 12th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.21 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 TutorialBot
  11. {
  12. class Program
  13. {
  14. static string user, pass;
  15.  
  16. static SteamClient steamClient;
  17. static CallbackManager manager;
  18. static SteamUser steamUser;
  19. static SteamFriends steamFriends;
  20.  
  21. static bool isRunning = false;
  22.  
  23. static string authCode;
  24.  
  25. static void Main(string[] args)
  26. {
  27. if(!File.Exists("chat.txt"))
  28. {
  29. File.Create("chat.txt").Close();
  30. File.WriteAllText("chat.txt", "abc | 123");
  31. }
  32.  
  33. Console.Title = "A bot";
  34. Console.WriteLine("CTRL+C quits the program.");
  35.  
  36. Console.Write("Username: ");
  37. user = Console.ReadLine();
  38.  
  39. Console.Write("Password: ");
  40. pass = Console.ReadLine();
  41.  
  42. SteamLogIn();
  43. }
  44.  
  45. static void SteamLogIn()
  46. {
  47. steamClient = new SteamClient();
  48.  
  49. manager = new CallbackManager(steamClient);
  50.  
  51. steamUser = steamClient.GetHandler<SteamUser>();
  52.  
  53. steamFriends = steamClient.GetHandler<SteamFriends>();
  54.  
  55. new Callback<SteamClient.ConnectedCallback>(OnConnected, manager);
  56. new Callback<SteamClient.DisconnectedCallback>(OnDisconnected, manager);
  57.  
  58. new Callback<SteamUser.LoggedOnCallback>(OnLoggedOn, manager);
  59. new Callback<SteamUser.LoggedOffCallback>(OnLoggedOff, manager);
  60.  
  61. new Callback<SteamUser.AccountInfoCallback>(OnAccountInfo, manager);
  62. new Callback<SteamFriends.FriendMsgCallback>(OnChatMessage, manager);
  63.  
  64. new Callback<SteamFriends.FriendsListCallback>(OnFriendsList, manager);
  65.  
  66. new JobCallback<SteamUser.UpdateMachineAuthCallback>(OnMachineAuth, manager);
  67.  
  68. isRunning = true;
  69.  
  70. Console.WriteLine("Connecting to Steam...");
  71.  
  72. steamClient.Connect();
  73.  
  74.  
  75. while (isRunning)
  76. {
  77. manager.RunWaitCallbacks(TimeSpan.FromSeconds(1));
  78. }
  79. Console.ReadKey();
  80. }
  81.  
  82. static void OnConnected(SteamClient.ConnectedCallback callback)
  83. {
  84. if (callback.Result != EResult.OK)
  85. {
  86. Console.WriteLine("Unable to connect to Steam: {0}", callback.Result);
  87.  
  88. isRunning = false;
  89. return;
  90. }
  91.  
  92. Console.WriteLine("Connected to Steam! Logging in '{0}'...", user);
  93.  
  94. byte[] sentryHash = null;
  95. if (File.Exists("sentry.bin"))
  96. {
  97. byte[] sentryFile = File.ReadAllBytes("sentry.bin");
  98. sentryHash = CryptoHelper.SHAHash(sentryFile);
  99. }
  100.  
  101. steamUser.LogOn(new SteamUser.LogOnDetails
  102. {
  103. Username = user,
  104. Password = pass,
  105.  
  106. AuthCode = authCode,
  107.  
  108. SentryFileHash = sentryHash,
  109. });
  110. }
  111.  
  112. static void OnLoggedOn(SteamUser.LoggedOnCallback callback)
  113. {
  114. if (callback.Result == EResult.AccountLogonDenied)
  115. {
  116. Console.WriteLine("This account is SteamGuard protected.");
  117.  
  118. Console.Write("Please enter the auth code sent to the email at {0}: ", callback.EmailDomain);
  119.  
  120. authCode = Console.ReadLine();
  121.  
  122. return;
  123. }
  124. if (callback.Result != EResult.OK)
  125. {
  126. Console.WriteLine("Unable to log in to Steam: {0}\n", callback.Result);
  127. isRunning = false;
  128. return;
  129. }
  130. Console.WriteLine("{0} succesfully logged in!", user);
  131. }
  132.  
  133. static void OnMachineAuth(SteamUser.UpdateMachineAuthCallback callback, JobID jobId)
  134. {
  135. Console.WriteLine("Updating sentryfile...");
  136. byte[] sentryHash = CryptoHelper.SHAHash(callback.Data);
  137. File.WriteAllBytes("sentry.bin", callback.Data);
  138. steamUser.SendMachineAuthResponse(new SteamUser.MachineAuthDetails
  139. {
  140. JobID = jobId,
  141. FileName = callback.FileName,
  142. BytesWritten = callback.BytesToWrite,
  143. FileSize = callback.Data.Length,
  144. Offset = callback.Offset,
  145. Result = EResult.OK,
  146. LastError = 0,
  147. OneTimePassword = callback.OneTimePassword,
  148. SentryFileHash = sentryHash,
  149. });
  150. Console.WriteLine("Done!");
  151. }
  152.  
  153. static void OnDisconnected(SteamClient.DisconnectedCallback callback)
  154. {
  155. Console.WriteLine("\n{0} disconnected from Steam, reconnecting in 5...\n", user);
  156.  
  157. Thread.Sleep(TimeSpan.FromSeconds(5));
  158.  
  159. steamClient.Connect();
  160. }
  161.  
  162. static void OnLoggedOff(SteamUser.LoggedOffCallback callback)
  163. {
  164. Console.WriteLine("Logged off of Steam: {0}", callback.Result);
  165. }
  166.  
  167. static void OnAccountInfo(SteamUser.AccountInfoCallback callback)
  168. {
  169. steamFriends.SetPersonaState(EPersonaState.Online);
  170.  
  171. }
  172.  
  173. static void OnFriendsList(SteamFriends.FriendsListCallback callback)
  174. {
  175. Thread.Sleep(2500);
  176.  
  177. foreach(var friend in callback.FriendList)
  178. {
  179. if(friend.Relationship == EFriendRelationship.RequestRecipient)
  180. {
  181. steamFriends.AddFriend(friend.SteamID);
  182. Thread.Sleep(500);
  183. steamFriends.SendChatMessage(friend.SteamID, EChatEntryType.ChatMsg, "Hello, This is Svensis bot, his steam link is http://steamcommunity.com/id/svensis/ If you have any questions, write !send Svensis (text)");
  184. }
  185. }
  186. }
  187.  
  188. static void OnChatMessage(SteamFriends.FriendMsgCallback callback)
  189. {
  190. string[] args;
  191.  
  192. if (callback.EntryType == EChatEntryType.ChatMsg)
  193. {
  194. if (callback.Message.Length > 1)
  195. {
  196. //"!"
  197. if (callback.Message.Remove(1) == "!")
  198. {
  199. string command = callback.Message;
  200. if (callback.Message.Contains(" ")) //!help
  201. {
  202. command = callback.Message.Remove(callback.Message.IndexOf(' '));
  203. }
  204.  
  205. switch (command)
  206. {
  207. #region send
  208. case "!send": //!send friendname message
  209. args = seperate(2, ' ', callback.Message); //args[0] = !send, args[1] = friendname, args[2] = message, args[3-4] = null;
  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, "Command 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())) //bob dylan !send bob message or !send dylan message
  220. {
  221. steamFriends.SendChatMessage(friend, EChatEntryType.ChatMsg, args[2]);
  222. }
  223. }
  224. break;
  225. #endregion
  226. case "!friends":
  227. Console.WriteLine("!friends command recieved. User: " + steamFriends.GetFriendPersonaName(callback.Sender));
  228. for (int i = 0; i < steamFriends.GetFriendCount(); i++)
  229. {
  230. SteamID friend = steamFriends.GetFriendByIndex(i);
  231. steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "Friend: " + steamFriends.GetFriendPersonaName(friend) + " State: " + steamFriends.GetFriendPersonaState(friend));
  232. }
  233. break;
  234. }
  235. }
  236. }
  237. string rLine;
  238. string trimmed = callback.Message;
  239. char[] trim = { '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '=', '+', '[', ']', '{', '}', '\\', '|', ';', ':', '"', '\'', ',', '<', '.', '>', '/', '?'};
  240.  
  241. for(int i = 0; i < 30; i++)
  242. {
  243. trimmed = trimmed.Replace(trim[i].ToString(), "");
  244. }
  245.  
  246. StreamReader sReader = new StreamReader("chat.txt");
  247.  
  248. while((rLine = sReader.ReadLine()) != null)
  249. {
  250. string text = rLine.Remove(rLine.IndexOf('|') - 1);
  251. string response = rLine.Remove(0, rLine.IndexOf('|') + 2);
  252.  
  253. if(callback.Message.Contains(text))
  254. {
  255. steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, response);
  256. sReader.Close();
  257. return;
  258. }
  259. }
  260.  
  261. }
  262. }
  263.  
  264. public static string[] seperate(int number, char seperator, string thestring)
  265. {
  266. string[] returned = new string[4];
  267.  
  268. int i = 0;
  269.  
  270. int error = 0;
  271.  
  272. int length = thestring.Length;
  273.  
  274. foreach (char c in thestring) //!friend
  275. {
  276. if (i != number)
  277. {
  278. if (error > length || number > 5)
  279. {
  280. returned[0] = "-1";
  281. return returned;
  282. }
  283. else if (c == seperator)
  284. {
  285. //returned[0] = !friend
  286. returned[i] = thestring.Remove(thestring.IndexOf(c));
  287. thestring = thestring.Remove(0, thestring.IndexOf(c) + 1);
  288. i++;
  289. }
  290. error++;
  291.  
  292. if (error == length && i != number)
  293. {
  294. returned[0] = "-1";
  295. return returned;
  296. }
  297. }
  298. else
  299. {
  300. returned[i] = thestring;
  301. }
  302. }
  303. return returned;
  304. }
  305. }
  306. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement