Advertisement
Guest User

SteamBot.cs

a guest
Aug 20th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.68 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 SteamBot
  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. if (!File.Exists("admin.txt"))
  34. {
  35. File.Create("admin.txt").Close();
  36. File.WriteAllText("admin.txt", "76561198070576110");
  37. }
  38.  
  39.  
  40. Console.Title = "SteamBot Console";
  41. Console.WriteLine("CTRL+C quits the program.");
  42.  
  43. Console.Write("Username: ");
  44. user = Console.ReadLine();
  45.  
  46. Console.Write("Password: ");
  47. pass = Console.ReadLine();
  48.  
  49. SteamLogIn();
  50. }
  51.  
  52. static void SteamLogIn()
  53. {
  54. steamClient = new SteamClient();
  55.  
  56. manager = new CallbackManager(steamClient);
  57.  
  58. steamUser = steamClient.GetHandler<SteamUser>();
  59.  
  60. steamFriends = steamClient.GetHandler<SteamFriends>();
  61.  
  62. new Callback<SteamClient.ConnectedCallback>(OnConnected);
  63. new Callback<SteamClient.DisconnectedCallback>(OnDisconnected, manager);
  64.  
  65. new Callback<SteamUser.LoggedOnCallback>(OnLoggedOn, manager);
  66. new Callback<SteamUser.LoggedOffCallback>(OnLoggedOff, manager);
  67.  
  68. new Callback<SteamUser.AccountInfoCallback>(OnAccountInfo, manager);
  69. new Callback<SteamFriends.FriendMsgCallback>(OnChatMessage, manager);
  70.  
  71. new Callback<SteamFriends.FriendsListCallback>(OnFriendsList, manager);
  72.  
  73. new Callback<SteamUser.UpdateMachineAuthCallback>(OnMachineAuth, manager);
  74.  
  75. isRunning = true;
  76.  
  77. Console.WriteLine("Connecting to Steam...");
  78.  
  79. steamClient.Connect();
  80.  
  81.  
  82. while (isRunning)
  83. {
  84. manager.RunWaitCallbacks(TimeSpan.FromSeconds(1));
  85. }
  86. Console.ReadKey();
  87. }
  88.  
  89. static void OnConnected(SteamClient.ConnectedCallback callback)
  90. {
  91. if (callback.Result != EResult.OK)
  92. {
  93. Console.WriteLine("Unable to connect to steam: {0}", callback.Result);
  94.  
  95. isRunning = false;
  96. return;
  97. }
  98.  
  99. Console.WriteLine("Connected to Steam. \nLogging in {0}... \n", user);
  100.  
  101. byte[] sentryHash = null;
  102.  
  103. if(File.Exists("sentry.bin"))
  104. {
  105. byte[] sentryFile = File.ReadAllBytes("sentry.bin");
  106.  
  107. sentryHash = CryptoHelper.SHAHash(sentryFile);
  108. }
  109.  
  110. steamUser.LogOn(new SteamUser.LogOnDetails
  111. {
  112. Username = user,
  113. Password = pass,
  114.  
  115. AuthCode = authCode,
  116.  
  117. SentryFileHash = sentryHash,
  118. });
  119. }
  120.  
  121. static void OnLoggedOn(SteamUser.LoggedOnCallback callback)
  122. {
  123. if(callback.Result != EResult.AccountLogonDenied)
  124. {
  125. Console.WriteLine("Account is SteamGuard protected.");
  126.  
  127. Console.Write("Please enter the authentication code sent to the email at {0}: ", callback.EmailDomain);
  128.  
  129. authCode = Console.ReadLine();
  130.  
  131. return;
  132. }
  133.  
  134. if(callback.Result != EResult.OK)
  135. {
  136. Console.WriteLine("Unable to log in to Steam: {0}\n", callback.Result);
  137. isRunning = false;
  138. return;
  139. }
  140. Console.WriteLine("{0} succesfully logged in.", user);
  141.  
  142. }
  143.  
  144. static void OnMachineAuth(SteamUser.UpdateMachineAuthCallback callback)
  145. {
  146. Console.WriteLine("Updating sentry file...");
  147.  
  148. byte[] sentryHash = CryptoHelper.SHAHash(callback.Data);
  149.  
  150. File.WriteAllBytes("sentry.bin", callback.Data);
  151.  
  152. steamUser.SendMachineAuthResponse(new SteamUser.MachineAuthDetails
  153. {
  154. JobID = callback.JobID,
  155. FileName = callback.FileName,
  156. BytesWritten = callback.BytesToWrite,
  157. FileSize = callback.Data.Length,
  158. Offset = callback.Offset,
  159. Result = EResult.OK,
  160. LastError = 0,
  161. OneTimePassword = callback.OneTimePassword,
  162. SentryFileHash = sentryHash,
  163. });
  164.  
  165. Console.WriteLine("Done.");
  166. }
  167.  
  168. static void OnDisconnected(SteamClient.DisconnectedCallback callback)
  169. {
  170. Console.WriteLine("\n {0} disconnected from Steam, reconnecting in 5...\n", user);
  171.  
  172. Thread.Sleep(TimeSpan.FromSeconds(5));
  173.  
  174. steamClient.Connect();
  175. }
  176.  
  177. static void OnLoggedOff(SteamUser.LoggedOffCallback callback)
  178. {
  179. Console.WriteLine("Logged off from Steam: {0}", callback.Result);
  180. }
  181.  
  182. static void OnAccountInfo(SteamUser.AccountInfoCallback callback)
  183. {
  184. steamFriends.SetPersonaState(EPersonaState.Online);
  185.  
  186. }
  187.  
  188. static void OnFriendsList(SteamFriends.FriendsListCallback callback)
  189. {
  190. Thread.Sleep(TimeSpan.FromSeconds(3));
  191.  
  192. foreach(var friend in callback.FriendList)
  193. {
  194. if(friend.Relationship == EFriendRelationship.RequestRecipient)
  195. {
  196. steamFriends.AddFriend(friend.SteamID);
  197. Thread.Sleep(500);
  198. steamFriends.SendChatMessage(friend.SteamID, EChatEntryType.ChatMsg, "Hello, I'm a bot.");
  199. }
  200. }
  201. }
  202.  
  203. static void OnChatMessage(SteamFriends.FriendMsgCallback callback)
  204. {
  205. string[] args;
  206.  
  207. if (callback.EntryType == EChatEntryType.ChatMsg)
  208. {
  209. if (callback.Message.Length > 1)
  210. {
  211. if(callback.Message.Remove(1) == "!")
  212. {
  213. string command = callback.Message;
  214. if(callback.Message.Contains(" "))
  215. {
  216. command = callback.Message.Remove(callback.Message.IndexOf(' '));
  217. }
  218.  
  219. switch(command)
  220. {
  221. #region send
  222. case "!send":
  223. if (!isBotAdmin(callback.Sender))
  224. break;
  225.  
  226.  
  227. args = seperate(2, ' ', callback.Message);
  228. Console.WriteLine("!send " + args[1] + args[2] + " command recieved. User: " + steamFriends.GetFriendPersonaName(callback.Sender));
  229. if(args[0] == "-1")
  230. {
  231. steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "Command syntax: !send [friend] [message]");
  232. return;
  233. }
  234. for (int i = 0; i < steamFriends.GetFriendCount(); i++)
  235. {
  236. SteamID friend = steamFriends.GetFriendByIndex(i);
  237. if(steamFriends.GetFriendPersonaName(friend).ToLower().Contains(args[1].ToLower()))
  238. {
  239. steamFriends.SendChatMessage(friend, EChatEntryType.ChatMsg, args[2]);
  240. }
  241. }
  242. break;
  243. #endregion
  244.  
  245. #region friends
  246. case "!friends":
  247. Console.WriteLine("!friends command recieved. User: " + steamFriends.GetFriendPersonaName(callback.Sender));
  248. for (int i = 0; i < steamFriends.GetFriendCount(); i++)
  249. {
  250. SteamID friend = steamFriends.GetFriendByIndex(i);
  251. steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "Friend: " + steamFriends.GetFriendPersonaName(friend) + " State: " + steamFriends.GetFriendPersonaState(friend));
  252. }
  253. break;
  254. #endregion
  255.  
  256. #region friend
  257.  
  258. case "!friend":
  259.  
  260. args = seperate(1, ' ', callback.Message);
  261. Console.WriteLine("!friend " + args[1] + " | " + steamFriends.GetFriendPersonaName(Convert.ToUInt64(args[1])) + " command recieved. User: " + steamFriends.GetFriendPersonaName(callback.Sender));
  262.  
  263. if (!isBotAdmin(callback.Sender))
  264. return;
  265.  
  266. if(args[0] == "-1")
  267. {
  268. steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "Command syntax: !friend [Steam ID]");
  269. return;
  270. }
  271.  
  272. try
  273. {
  274. SteamID validSID = Convert.ToUInt64(args[1]);
  275. if(!validSID.IsValid)
  276. {
  277. steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "Invalid SteamID");
  278. break;
  279. }
  280. steamFriends.AddFriend(validSID);
  281. }
  282. catch(FormatException)
  283. {
  284. steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "Invalid SteamID64.");
  285. }
  286. break;
  287. #endregion
  288.  
  289. #region changename
  290. case "!changename":
  291.  
  292. args = seperate(1, ' ', callback.Message);
  293. Console.WriteLine("!changename " + args[1] + " | " + steamFriends.GetFriendPersonaName(Convert.ToUInt64(args[1])) + " command recieved. User: " + steamFriends.GetFriendPersonaName(callback.Sender));
  294.  
  295. if (!isBotAdmin(callback.Sender))
  296. return;
  297.  
  298. if (args[0] == "-1")
  299. {
  300. steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, "Command syntax: !change [name]");
  301. return;
  302. }
  303.  
  304. steamFriends.SetPersonaName(args[1]);
  305. break;
  306. #endregion
  307. }
  308. }
  309. }
  310. string rLine;
  311. string trimmed = callback.Message;
  312.  
  313. char[] trim = { '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '_', '=', '+', '[', ']', '{', '}', '\\', '|', ';', ':', '"', '\'', ',', '<', '.', '>', '/', '?' };
  314.  
  315. for(int i = 0; i < 30; i++)
  316. {
  317. trimmed = trimmed.Replace(trim[i].ToString(), "");
  318. }
  319.  
  320. StreamReader sReader = new StreamReader("chat.txt");
  321.  
  322. while((rLine = sReader.ReadLine()) != null)
  323. {
  324. string text = rLine.Remove(rLine.IndexOf('|') - 1);
  325. string response = rLine.Remove(0, rLine.IndexOf('|') + 2);
  326.  
  327. if(callback.Message.Contains(text))
  328. {
  329. steamFriends.SendChatMessage(callback.Sender, EChatEntryType.ChatMsg, response);
  330. sReader.Close();
  331. return;
  332. }
  333. }
  334.  
  335. }
  336. }
  337.  
  338. public static bool isBotAdmin(SteamID sid)
  339. {
  340. try
  341. {
  342. if (sid.ConvertToUInt64() == Convert.ToUInt64(File.ReadAllText("admin.txt")))
  343. {
  344. return true;
  345. }
  346.  
  347. steamFriends.SendChatMessage(sid, EChatEntryType.ChatMsg, "You are not a bot admin.");
  348. Console.WriteLine(steamFriends.GetFriendPersonaName(sid) + " attempted to use an administrator command without privledges.");
  349. return false;
  350. }
  351. catch(Exception e)
  352. {
  353. Console.WriteLine(e.Message);
  354. return false;
  355. }
  356. }
  357.  
  358. public static string[] seperate(int number,char seperator,string thestring)
  359. {
  360. string[] returned = new string[4];
  361.  
  362. int i = 0;
  363.  
  364. int error = 0;
  365.  
  366. int lenght = thestring.Length;
  367.  
  368. foreach(char c in thestring)
  369. {
  370. if(i != number)
  371. {
  372. if(error > lenght || number > 5)
  373. {
  374. returned[0] = "-1";
  375. return returned;
  376. }
  377. else if(c == seperator)
  378. {
  379. returned[i] = thestring.Remove(thestring.IndexOf(c));
  380. thestring = thestring.Remove(0, thestring.IndexOf(c) + 1);
  381. i++;
  382. }
  383. error++;
  384.  
  385. if(error > lenght && i != number)
  386. {
  387. returned[0] = "-1";
  388. return returned;
  389. }
  390. }
  391. else
  392. {
  393. returned[i] = thestring;
  394. }
  395. }
  396. return returned;
  397. }
  398. }
  399. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement