Advertisement
Guest User

KoGaMa.NET V0.1.5

a guest
Dec 22nd, 2017
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 24.14 KB | None | 0 0
  1. using System;
  2. using System.Net.Http;
  3. using System.Threading.Tasks;
  4. using Newtonsoft.Json;
  5. using System.Collections.Generic;
  6. using System.Timers;
  7. using System.Linq;
  8. using System.Text.RegularExpressions;
  9.  
  10. namespace KoGaMa.NET
  11. {
  12.     class Program
  13.     {
  14.         public static void MessageReceived(Bot.Message Message)
  15.         {
  16.             if (Message.content.ToLower() == "ping")
  17.             {
  18.                 Message.Reply("pong");
  19.             }
  20.             else if (Message.content.ToLower() == "kill me")
  21.             {
  22.                 Message.Reply(string.Concat(Enumerable.Repeat(":BunnyEmote51 ", 99999)));
  23.             }
  24.             else if (Message.content.ToLower().StartsWith("play ") && Message.author.name == "CaptainJens")
  25.             {
  26.                 Message.bot.SetStatus("playing", Message.content.Substring(5));
  27.                 Message.Reply("Now playing: " + Message.bot.GameID);
  28.             }
  29.             else if (Message.content.ToLower().StartsWith("build ") && Message.author.name == "CaptainJens")
  30.             {
  31.                 Message.bot.SetStatus("building", Message.content.Substring(6));
  32.                 Message.Reply("Now building: " + Message.bot.GameID);
  33.             }
  34.             else if (Message.content.ToLower() == "gooffline" && Message.author.name == "CaptainJens")
  35.             {
  36.                 Message.bot.SetStatus("offline");
  37.                 Message.Reply("I'm offline now!");
  38.             }
  39.             else if (Message.content.ToLower() == "goonline" && Message.author.name == "CaptainJens")
  40.             {
  41.                 Message.bot.SetStatus("online");
  42.                 Message.Reply("I'm online again!");
  43.             }
  44.         }
  45.  
  46.         static async Task AsyncBot()
  47.         {
  48.             Bot bot = new Bot();
  49.  
  50.             bot.MessageReceived += MessageReceived;
  51.  
  52.             bot.LoggedIn += async () =>
  53.             {
  54.                 await bot.SendMessage("Hello", "335181");
  55.             };
  56.  
  57.             bot.EnableLogging = true;
  58.             bot.SetServer("WWW");
  59.             bot.DebugColor = ConsoleColor.DarkCyan;
  60.             bot.SetStatus("playing","123");
  61.             await bot.Login("username", "password");
  62.  
  63.             //await bot.CheckFeed("335181");
  64.  
  65.             await Task.Delay(-1);
  66.         }
  67.  
  68.         static void Main(string[] args)
  69.         {
  70.             AsyncBot().Wait();
  71.         }
  72.     }
  73.  
  74.     class Bot
  75.     {
  76.         //VARIABLES
  77.         private HttpClient client { get; set; } = new HttpClient();
  78.         private Timer CheckChatInterval = new Timer() { Interval = 2000, AutoReset = true, Enabled = true };
  79.         private Timer SendPulseInterval = new Timer() { Interval = 10000, AutoReset = true, Enabled = true };
  80.  
  81.         public bool EnableLogging = false;
  82.         public string Server { get; private set; } = "server";
  83.         public ConsoleColor DebugColor = ConsoleColor.DarkCyan;
  84.         public string CSRFToken { get; private set; }
  85.         public string Notify_Token {get; private set;}
  86.  
  87.         public Action<Bot.Message> MessageReceived;
  88.         public Action LoggedIn;
  89.  
  90.         public string GameID { get; private set; } = "0";
  91.         public string Status { get; private set; } = "playing"; //"playing", "building", "online", "offline"
  92.         public string Username { get; private set; } = "username";
  93.         public string ID { get; private set; } = "id";
  94.         public string Gold { get; private set; } = "gold";
  95.         public string Silver { get; private set; } = "silver";
  96.         public string Friends_Limit { get; private set; } = "friends_limit";
  97.         public string Created { get; private set; } = "created";
  98.         public string Email { get; private set; } = "email";
  99.         public string Language { get; private set; } = "language";
  100.         public bool Email_Confirmed { get; private set; } = false;
  101.         public bool IsAdmin { get; private set; } = false;
  102.  
  103.         public string XP { get; private set; } = "XP";
  104.         public string Level_Progress { get; private set; } = "level_progress";
  105.         public string XP_To_Next_Level { get; private set; } = "xp_to_next_level";
  106.         public string Previous_Level_XP { get; private set; } = "previous_level_xp";
  107.         public string Next_Level_XP { get; private set; } = "next_level_xp";
  108.  
  109.         //GETS CALLED AFTER BOT GETS CREATED
  110.         public Bot()
  111.         {
  112.             try
  113.             {
  114.                 this.CheckChatInterval.Elapsed += this.CheckChat;
  115.                 this.SendPulseInterval.Elapsed += this.SendPulse;
  116.             }
  117.             catch (Exception e)
  118.             {
  119.                 Console.ForegroundColor = ConsoleColor.Red;
  120.                 Console.WriteLine("ERROR:");
  121.                 Console.ResetColor();
  122.                 Console.WriteLine(e.Message);
  123.                 Console.WriteLine("\n");
  124.             }
  125.         }
  126.  
  127.         //SET STATUS (Status = "playing", "building, "online" or "offline")
  128.         public void SetStatus(string Status, string GameID = "0")
  129.         {
  130.             try
  131.             {
  132.                 if (Status.ToLower() != "offline" && Status.ToLower() != "online" && Status.ToLower() != "playing" && Status.ToLower() != "building")
  133.                 {
  134.                     Console.ForegroundColor = ConsoleColor.Red;
  135.                     Console.WriteLine("ERROR:");
  136.                     Console.ResetColor();
  137.                     Console.WriteLine("Invalid status type. Status type has to be \"playing\", \"building\", \"online\" or \"offline\"");
  138.                     Console.WriteLine("\n");
  139.                 }
  140.                 else
  141.                 {
  142.                     this.GameID = GameID;
  143.                     this.Status = Status;
  144.                     if (Status.ToLower() == "offline")
  145.                         SendPulseInterval.Stop();
  146.                     else
  147.                         SendPulseInterval.Start();
  148.                 }
  149.             }
  150.             catch (Exception e)
  151.             {
  152.                 Console.ForegroundColor = ConsoleColor.Red;
  153.                 Console.WriteLine("ERROR:");
  154.                 Console.ResetColor();
  155.                 Console.WriteLine(e.Message);
  156.                 Console.WriteLine("\n");
  157.             }
  158.         }
  159.  
  160.         //SET SERVER
  161.         public void SetServer(string Server)
  162.         {
  163.             try
  164.             {
  165.                 while (Server.ToLower() != "www" && Server.ToLower() != "friends" && Server.ToLower() != "br")
  166.                 {
  167.                     Console.WriteLine("\nPlease enter the server of your Bot. It can be WWW, Friends or BR");
  168.                     Server = Console.ReadLine();
  169.                 }
  170.                 if (Server.ToLower() == "www")
  171.                 {
  172.                     this.client.BaseAddress = new Uri("http://www.kogama.com");
  173.                     this.Server = "WWW";
  174.                 }
  175.                 else if (Server.ToLower() == "br")
  176.                 {
  177.                     this.client.BaseAddress = new Uri("http://kogama.com.br/");
  178.                     this.Server = "BR";
  179.                 }
  180.                 else if (Server.ToLower() == "friends")
  181.                 {
  182.                     this.client.BaseAddress = new Uri("http://friends.kogama.com/");
  183.                     this.Server = "Friends";
  184.                 }
  185.             }
  186.             catch (Exception e)
  187.             {
  188.                 Console.ForegroundColor = ConsoleColor.Red;
  189.                 Console.WriteLine("ERROR:");
  190.                 Console.ResetColor();
  191.                 Console.WriteLine(e.Message);
  192.                 Console.WriteLine("\n");
  193.             }
  194.         }
  195.  
  196.         //GET STRING BETWEEN TWO STRINGS (START AND END)
  197.         private List<string> GetSubStrings(string input, string start, string end)
  198.         {
  199.             MatchCollection matches = Regex.Matches(input, start + "(.*?)" + end);
  200.             List<string> list = new List<string>();
  201.             foreach (Match match in matches)
  202.                 list.Add(match.Groups[1].Value);
  203.             return list;
  204.         }
  205.  
  206.         //CLASSES USED AS REQUEST DATA (PRIVATE)
  207.         private class LoginInfo
  208.         {
  209.             public string username = "Username";
  210.             public string password = "Password";
  211.         }
  212.  
  213.         private class MessageInfo
  214.         {
  215.             public string message = "message";
  216.             public string to_profile_id = "id";
  217.             public bool history = false;
  218.             public bool status = false;
  219.         }
  220.  
  221.         private class OnlineInfo
  222.         {
  223.             public string location = "location";
  224.             public string status = "status";
  225.         }
  226.  
  227.         private class CommentInfo
  228.         {
  229.             public string comment = "message";
  230.         }
  231.  
  232.         //CLASSES FOR OBJECTS (PUBLIC)
  233.         public class Message
  234.         {
  235.             public User author = new User();
  236.             public string content = "text";
  237.             public string created = "created";
  238.             public Bot bot;
  239.             public async void Reply(string Message)
  240.             {
  241.                 try
  242.                 {
  243.                     if (this.author.id == "id")
  244.                     {
  245.                         Console.WriteLine("Please provide the ID of the Author.");
  246.                     }
  247.                     else
  248.                     {
  249.                         await bot.SendMessage(Message, this.author.name, Log: false);
  250.                         Console.ForegroundColor = bot.DebugColor;
  251.                         Console.WriteLine("COMMAND:");
  252.                         Console.ResetColor();
  253.                         Console.WriteLine("Input: " + this.content + "\nAuthor: " + this.author.name + "\nOutput: " + Message + "\n");
  254.                     }
  255.                 }
  256.                 catch (Exception e)
  257.                 {
  258.                     Console.ForegroundColor = ConsoleColor.Red;
  259.                     Console.WriteLine("ERROR:");
  260.                     Console.ResetColor();
  261.                     Console.WriteLine(e.Message);
  262.                     Console.WriteLine("\n");
  263.                 }
  264.             }
  265.         }
  266.  
  267.         public class User
  268.         {
  269.             public string name = "username";
  270.             public string id = "id";
  271.         }
  272.  
  273.         //-----REQUESTS-----//
  274.         //LOGIN
  275.         public async Task Login(string username, string password)
  276.         {
  277.             try
  278.             {
  279.                 if(Server.ToLower() != "www" && Server.ToLower() != "friends" && Server.ToLower() != "br")
  280.                 {
  281.                     Console.ForegroundColor = ConsoleColor.Red;
  282.                     Console.WriteLine("ERROR:");
  283.                     Console.ResetColor();
  284.                     Console.WriteLine("Please provide a server before logging in. \nTo provide a server use: bot.SetServer(\"server\")\n\"server\" can be \"WWW\", \"Friends\" or \"BR\"");
  285.                     Console.WriteLine("\n");
  286.                     CheckChatInterval.Stop();
  287.                     SendPulseInterval.Stop();
  288.                     Console.ReadLine();
  289.                     return;
  290.                 }
  291.                 LoginInfo user = new LoginInfo() { username = username, password = password };
  292.                 HttpResponseMessage response = await this.client.PostAsJsonAsync("/auth/login/", user);
  293.                 response.EnsureSuccessStatusCode();
  294.                 Console.WriteLine("\nLogged in!\n");
  295.                 var DataObject = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(await response.Content.ReadAsStringAsync());
  296.                 this.Username = DataObject["data"]["username"];
  297.                 this.ID = DataObject["data"]["id"];
  298.                 this.Gold = DataObject["data"]["gold"];
  299.                 this.Silver = DataObject["data"]["silver"];
  300.                 this.Friends_Limit = DataObject["data"]["friends_limit"];
  301.                 this.Created = DataObject["data"]["created"];
  302.                 if (DataObject["data"]["email"] != "")
  303.                     this.Email = DataObject["data"]["email"];
  304.                 else
  305.                     this.Email = "none";
  306.                 this.Language = DataObject["data"]["language"];
  307.                 this.Email_Confirmed = DataObject["data"]["email_confirmed"];
  308.                 this.IsAdmin = DataObject["data"]["is_admin"];
  309.  
  310.                 this.XP = DataObject["data"]["xp"];
  311.                 this.Level_Progress = DataObject["data"]["level_progress"];
  312.                 this.Previous_Level_XP = DataObject["data"]["previous_level_xp"];
  313.                 this.Next_Level_XP = DataObject["data"]["next_level_xp"];
  314.                 this.XP_To_Next_Level = DataObject["data"]["xp_to_next_level"];
  315.  
  316.                 string HTMLData = await this.client.GetAsync("/auth/after-login/").Result.Content.ReadAsStringAsync();
  317.  
  318.                 this.CSRFToken = this.GetSubStrings(HTMLData, "<meta name=\"CSRFToken\" content=\"", "\">")[0];
  319.                 this.client.DefaultRequestHeaders.Add("X-Csrf-Token", this.CSRFToken);
  320.  
  321.                 this.Notify_Token = this.GetSubStrings(HTMLData, "<meta name=\"Notify-Token\" content=\"", "\">")[0];
  322.                 HTMLData = null;
  323.                 LoggedIn?.Invoke();
  324.             }
  325.             catch (Exception e)
  326.             {
  327.                 Console.ForegroundColor = ConsoleColor.Red;
  328.                 Console.WriteLine("ERROR:");
  329.                 Console.ResetColor();
  330.                 Console.WriteLine(e.Message);
  331.                 if (e.Message.Contains("400"))
  332.                     Console.WriteLine("\nThis error can be related to entering the wrong username or password. \nPlease close the application and check your login info.");
  333.                 Console.WriteLine("\n");
  334.             }
  335.         }
  336.  
  337.         //WIP-GET USER INFO
  338.         public async Task<string> GetUserInfo(string username)
  339.         {
  340.             try
  341.             {
  342.                 Console.ForegroundColor = this.DebugColor;
  343.                 Console.WriteLine("USER INFO:");
  344.                 Console.ResetColor();
  345.                 if (username == this.Username)
  346.                 {
  347.                     await Task.Delay(20);
  348.  
  349.                     Console.WriteLine("Username: " + this.Username);
  350.                     Console.WriteLine("ID: " + this.ID);
  351.                     Console.WriteLine("Gold: " + this.Gold);
  352.                     Console.WriteLine("Silver: " + this.Silver);
  353.                     Console.WriteLine("Friends limit: " + this.Friends_Limit);
  354.                     Console.WriteLine("Account created: " + this.Created);
  355.                     Console.WriteLine("Email address: " + this.Email);
  356.                     Console.WriteLine("Language: " + this.Language);
  357.                     Console.WriteLine("Email confirmed: " + this.Email_Confirmed);
  358.                     Console.WriteLine("Is administrator: " + this.IsAdmin);
  359.  
  360.                     Console.WriteLine("\nXP: " + this.XP);
  361.                     Console.WriteLine("Level progress to next level: " + this.Level_Progress + "%");
  362.                     Console.WriteLine("XP needed to reach next level: " + this.XP_To_Next_Level);
  363.                     Console.WriteLine("Previous Level XP: " + this.Previous_Level_XP);
  364.                     Console.WriteLine("Next Level XP: " + this.Next_Level_XP);
  365.                     Console.WriteLine("\n");
  366.                     return "";
  367.                 }
  368.                 else
  369.                 {
  370.                     return "YOUR INFO:\n\nUsername: nub\nGold: -200\nXP: 0.000001";
  371.                 }
  372.             }
  373.             catch (Exception e)
  374.             {
  375.                 Console.ForegroundColor = ConsoleColor.Red;
  376.                 Console.WriteLine("ERROR:");
  377.                 Console.ResetColor();
  378.                 Console.WriteLine(e.Message);
  379.                 Console.WriteLine("\n");
  380.                 return "An error occured.";
  381.             }
  382.         }
  383.  
  384.         //PURCHASE MODEL(S)
  385.         public async Task PurchaseModel(string modelID, int times)
  386.         {
  387.             try
  388.             {
  389.                 Console.ForegroundColor = this.DebugColor;
  390.                 Console.WriteLine("PURCHASING MODELS:");
  391.                 Console.ResetColor();
  392.                 int x = 0;
  393.                 while (x < times)
  394.                 {
  395.                     if ((Convert.ToInt32(this.Gold) >= 30 && (this.Server.ToLower() == "www" || this.Server.ToLower() == "br")) || (Convert.ToInt32(this.Gold) >= 50 && (this.Server.ToLower() == "friends")))
  396.                     {
  397.                         x += 1;
  398.                         Console.WriteLine("\nThe model will be purchased in 30 seconds!");
  399.                         await Task.Delay(30000);
  400.                         HttpResponseMessage response = await this.client.PostAsJsonAsync("/model/market/i-" + modelID + "/purchase/", "");
  401.                         response.EnsureSuccessStatusCode();
  402.                         Console.WriteLine("\nModel purchased!");
  403.                     }
  404.                     else
  405.                     {
  406.                         Console.ForegroundColor = ConsoleColor.Red;
  407.                         Console.Write("ERROR:");
  408.                         Console.ResetColor();
  409.                         Console.WriteLine(" you do not have enough gold to purchase the model.\n");
  410.                         return;
  411.                     }
  412.                 }
  413.                 await Task.Delay(500);
  414.                 Console.WriteLine("\nTransaction completed!\n");
  415.                 await Task.Delay(500);
  416.             }
  417.             catch (Exception e)
  418.             {
  419.                 Console.ForegroundColor = ConsoleColor.Red;
  420.                 Console.WriteLine("ERROR:");
  421.                 Console.ResetColor();
  422.                 Console.WriteLine(e.Message);
  423.                 Console.WriteLine("\n");
  424.             }
  425.         }
  426.  
  427.         //SEND MESSAGE
  428.         public async Task SendMessage(string Message, string To_ID, string To_Name = "", bool Log = true)
  429.         {
  430.             try
  431.             {
  432.                 MessageInfo MsgObject = new MessageInfo();
  433.                 MsgObject.message = Message;
  434.                 MsgObject.to_profile_id = To_ID;
  435.                 HttpResponseMessage response = await this.client.PostAsJsonAsync("/chat/" + this.ID + "/", MsgObject);
  436.                 if (response.IsSuccessStatusCode)
  437.                 {
  438.                     if (Log == true)
  439.                     {
  440.                         Console.ForegroundColor = this.DebugColor;
  441.                         Console.WriteLine("MESSAGE SENT:");
  442.                         Console.ResetColor();
  443.                         Console.WriteLine("From: me\nMessage: " + Message);
  444.                         if (To_Name != "")
  445.                             Console.WriteLine("To: " + To_Name);
  446.                         else
  447.                             Console.WriteLine("To: " + To_ID);
  448.                         Console.WriteLine("");
  449.                     }
  450.                 }
  451.                 else
  452.                 {
  453.                     Console.ForegroundColor = ConsoleColor.Red;
  454.                     Console.WriteLine("ERROR:");
  455.                     Console.ResetColor();
  456.                     Console.WriteLine("Failed to send the message.");
  457.                     Console.WriteLine("Server responded with: " + response.StatusCode);
  458.                 }
  459.             }
  460.             catch (Exception e)
  461.             {
  462.                 Console.ForegroundColor = ConsoleColor.Red;
  463.                 Console.WriteLine("ERROR:");
  464.                 Console.ResetColor();
  465.                 Console.WriteLine(e.Message);
  466.                 Console.WriteLine("\n");
  467.             }
  468.         }
  469.  
  470.         //WIP-CHECK FEED
  471.         public async Task CheckFeed(string ProfileID)
  472.         {
  473.             try
  474.             {
  475.                 HttpResponseMessage response = await this.client.GetAsync("/api/feed/" + ProfileID);
  476.                 var DataObject = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(await response.Content.ReadAsStringAsync());
  477.                 foreach (dynamic feedpost in DataObject["data"])
  478.                 {
  479.                     Console.WriteLine(feedpost);
  480.                     if (feedpost["_data"].ToString().Contains("test"))
  481.                     {
  482.                         CommentInfo RequestData = new CommentInfo()
  483.                         {
  484.                             comment = "Feedpost info: " + feedpost["_data"] + " Created: " + feedpost["created"] + " type: " + feedpost["feed_type"] + " gold: " + feedpost["gold"]
  485.                         };
  486.                         Console.WriteLine(feedpost["other_username"] + " ID" + feedpost["id"]);
  487.                         string ID2 = feedpost["id"];
  488.                         HttpResponseMessage response2 = await client.PostAsJsonAsync("/api/feed/" + ID2 + "/comment/", RequestData);
  489.                         Console.WriteLine(await response2.Content.ReadAsStringAsync());
  490.                     }
  491.                 }
  492.             }
  493.             catch (Exception e)
  494.             {
  495.                 Console.ForegroundColor = ConsoleColor.Red;
  496.                 Console.WriteLine("ERROR:");
  497.                 Console.ResetColor();
  498.                 Console.WriteLine(e.Message);
  499.                 Console.WriteLine("\n");
  500.             }
  501.         }
  502.  
  503.         //[TIMERS ONLY] SEND PULSE
  504.         private async void SendPulse(Object CheckChatObject, ElapsedEventArgs CheckChatArgs)
  505.         {
  506.             try
  507.             {
  508.                 if (this.Status.ToLower() == "building")
  509.                 {
  510.                     OnlineInfo RequestData = new OnlineInfo() { location = "/build/0/project/" + this.GameID + "/", status = "active" };
  511.                     await this.client.PostAsJsonAsync("/user/" + this.ID + "/pulse/", RequestData);
  512.                 }
  513.                 else if(this.Status.ToLower() == "playing")
  514.                 {
  515.                     OnlineInfo RequestData = new OnlineInfo() { location = "/games/play/" + this.GameID + "/", status = "active" };
  516.                     await this.client.PostAsJsonAsync("/user/" + this.ID + "/pulse/", RequestData);
  517.                 }
  518.                 else if(this.Status.ToLower() == "online")
  519.                 {
  520.                     OnlineInfo RequestData = new OnlineInfo() { location = "/", status = "active" };
  521.                     await this.client.PostAsJsonAsync("/user/" + this.ID + "/pulse/", RequestData);
  522.                 }
  523.             }
  524.             catch (Exception e)
  525.             {
  526.                 Console.ForegroundColor = ConsoleColor.Red;
  527.                 Console.WriteLine("ERROR:");
  528.                 Console.ResetColor();
  529.                 Console.WriteLine(e.Message);
  530.                 Console.WriteLine("\n");
  531.             }
  532.         }
  533.  
  534.         //[TIMERS ONLY] CHECK CHAT
  535.         private async void CheckChat(Object CheckChatObject, ElapsedEventArgs CheckChatArgs)
  536.         {
  537.             try
  538.             {
  539.                 HttpResponseMessage response = await this.client.GetAsync("/chat/" + this.ID + "/");
  540.                 if (response.IsSuccessStatusCode)
  541.                 {
  542.                     var DataObject = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(await response.Content.ReadAsStringAsync());
  543.  
  544.                     foreach (dynamic a in DataObject["data"])
  545.                     {
  546.                         foreach (dynamic b in a)
  547.                         {
  548.                             foreach (dynamic c in b)
  549.                             {
  550.                                 Message Message = new Message()
  551.                                 {
  552.                                     content = c["message"].ToString(),
  553.                                     created = c["created"].ToString(),
  554.                                     author = new User()
  555.                                     {
  556.                                         name = c["from_username"].ToString(),
  557.                                         id = c["from_profile_id"].ToString()
  558.                                     },
  559.                                     bot = this
  560.                                 };
  561.                                 MessageReceived?.Invoke(Message);
  562.                             }
  563.                         }
  564.                     }
  565.                 }
  566.             }
  567.             catch (Exception e)
  568.             {
  569.                 Console.ForegroundColor = ConsoleColor.Red;
  570.                 Console.WriteLine("ERROR:");
  571.                 Console.ResetColor();
  572.                 Console.WriteLine(e.Message);
  573.                 Console.WriteLine("\n");
  574.             }
  575.         }
  576.     }
  577. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement