Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Net.Http;
- using System.Threading.Tasks;
- using Newtonsoft.Json;
- using System.Collections.Generic;
- using System.Timers;
- using System.Linq;
- using System.Text.RegularExpressions;
- namespace KoGaMa.NET
- {
- class Program
- {
- public static void MessageReceived(Bot.Message Message)
- {
- if (Message.content.ToLower() == "ping")
- {
- Message.Reply("pong");
- }
- else if (Message.content.ToLower() == "kill me")
- {
- Message.Reply(string.Concat(Enumerable.Repeat(":BunnyEmote51 ", 99999)));
- }
- else if (Message.content.ToLower().StartsWith("play ") && Message.author.name == "CaptainJens")
- {
- Message.bot.SetStatus("playing", Message.content.Substring(5));
- Message.Reply("Now playing: " + Message.bot.GameID);
- }
- else if (Message.content.ToLower().StartsWith("build ") && Message.author.name == "CaptainJens")
- {
- Message.bot.SetStatus("building", Message.content.Substring(6));
- Message.Reply("Now building: " + Message.bot.GameID);
- }
- else if (Message.content.ToLower() == "gooffline" && Message.author.name == "CaptainJens")
- {
- Message.bot.SetStatus("offline");
- Message.Reply("I'm offline now!");
- }
- else if (Message.content.ToLower() == "goonline" && Message.author.name == "CaptainJens")
- {
- Message.bot.SetStatus("online");
- Message.Reply("I'm online again!");
- }
- }
- static async Task AsyncBot()
- {
- Bot bot = new Bot();
- bot.MessageReceived += MessageReceived;
- bot.LoggedIn += async () =>
- {
- await bot.SendMessage("Hello", "335181");
- };
- bot.EnableLogging = true;
- bot.SetServer("WWW");
- bot.DebugColor = ConsoleColor.DarkCyan;
- bot.SetStatus("playing","123");
- await bot.Login("username", "password");
- //await bot.CheckFeed("335181");
- await Task.Delay(-1);
- }
- static void Main(string[] args)
- {
- AsyncBot().Wait();
- }
- }
- class Bot
- {
- //VARIABLES
- private HttpClient client { get; set; } = new HttpClient();
- private Timer CheckChatInterval = new Timer() { Interval = 2000, AutoReset = true, Enabled = true };
- private Timer SendPulseInterval = new Timer() { Interval = 10000, AutoReset = true, Enabled = true };
- public bool EnableLogging = false;
- public string Server { get; private set; } = "server";
- public ConsoleColor DebugColor = ConsoleColor.DarkCyan;
- public string CSRFToken { get; private set; }
- public string Notify_Token {get; private set;}
- public Action<Bot.Message> MessageReceived;
- public Action LoggedIn;
- public string GameID { get; private set; } = "0";
- public string Status { get; private set; } = "playing"; //"playing", "building", "online", "offline"
- public string Username { get; private set; } = "username";
- public string ID { get; private set; } = "id";
- public string Gold { get; private set; } = "gold";
- public string Silver { get; private set; } = "silver";
- public string Friends_Limit { get; private set; } = "friends_limit";
- public string Created { get; private set; } = "created";
- public string Email { get; private set; } = "email";
- public string Language { get; private set; } = "language";
- public bool Email_Confirmed { get; private set; } = false;
- public bool IsAdmin { get; private set; } = false;
- public string XP { get; private set; } = "XP";
- public string Level_Progress { get; private set; } = "level_progress";
- public string XP_To_Next_Level { get; private set; } = "xp_to_next_level";
- public string Previous_Level_XP { get; private set; } = "previous_level_xp";
- public string Next_Level_XP { get; private set; } = "next_level_xp";
- //GETS CALLED AFTER BOT GETS CREATED
- public Bot()
- {
- try
- {
- this.CheckChatInterval.Elapsed += this.CheckChat;
- this.SendPulseInterval.Elapsed += this.SendPulse;
- }
- catch (Exception e)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("ERROR:");
- Console.ResetColor();
- Console.WriteLine(e.Message);
- Console.WriteLine("\n");
- }
- }
- //SET STATUS (Status = "playing", "building, "online" or "offline")
- public void SetStatus(string Status, string GameID = "0")
- {
- try
- {
- if (Status.ToLower() != "offline" && Status.ToLower() != "online" && Status.ToLower() != "playing" && Status.ToLower() != "building")
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("ERROR:");
- Console.ResetColor();
- Console.WriteLine("Invalid status type. Status type has to be \"playing\", \"building\", \"online\" or \"offline\"");
- Console.WriteLine("\n");
- }
- else
- {
- this.GameID = GameID;
- this.Status = Status;
- if (Status.ToLower() == "offline")
- SendPulseInterval.Stop();
- else
- SendPulseInterval.Start();
- }
- }
- catch (Exception e)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("ERROR:");
- Console.ResetColor();
- Console.WriteLine(e.Message);
- Console.WriteLine("\n");
- }
- }
- //SET SERVER
- public void SetServer(string Server)
- {
- try
- {
- while (Server.ToLower() != "www" && Server.ToLower() != "friends" && Server.ToLower() != "br")
- {
- Console.WriteLine("\nPlease enter the server of your Bot. It can be WWW, Friends or BR");
- Server = Console.ReadLine();
- }
- if (Server.ToLower() == "www")
- {
- this.client.BaseAddress = new Uri("http://www.kogama.com");
- this.Server = "WWW";
- }
- else if (Server.ToLower() == "br")
- {
- this.client.BaseAddress = new Uri("http://kogama.com.br/");
- this.Server = "BR";
- }
- else if (Server.ToLower() == "friends")
- {
- this.client.BaseAddress = new Uri("http://friends.kogama.com/");
- this.Server = "Friends";
- }
- }
- catch (Exception e)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("ERROR:");
- Console.ResetColor();
- Console.WriteLine(e.Message);
- Console.WriteLine("\n");
- }
- }
- //GET STRING BETWEEN TWO STRINGS (START AND END)
- private List<string> GetSubStrings(string input, string start, string end)
- {
- MatchCollection matches = Regex.Matches(input, start + "(.*?)" + end);
- List<string> list = new List<string>();
- foreach (Match match in matches)
- list.Add(match.Groups[1].Value);
- return list;
- }
- //CLASSES USED AS REQUEST DATA (PRIVATE)
- private class LoginInfo
- {
- public string username = "Username";
- public string password = "Password";
- }
- private class MessageInfo
- {
- public string message = "message";
- public string to_profile_id = "id";
- public bool history = false;
- public bool status = false;
- }
- private class OnlineInfo
- {
- public string location = "location";
- public string status = "status";
- }
- private class CommentInfo
- {
- public string comment = "message";
- }
- //CLASSES FOR OBJECTS (PUBLIC)
- public class Message
- {
- public User author = new User();
- public string content = "text";
- public string created = "created";
- public Bot bot;
- public async void Reply(string Message)
- {
- try
- {
- if (this.author.id == "id")
- {
- Console.WriteLine("Please provide the ID of the Author.");
- }
- else
- {
- await bot.SendMessage(Message, this.author.name, Log: false);
- Console.ForegroundColor = bot.DebugColor;
- Console.WriteLine("COMMAND:");
- Console.ResetColor();
- Console.WriteLine("Input: " + this.content + "\nAuthor: " + this.author.name + "\nOutput: " + Message + "\n");
- }
- }
- catch (Exception e)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("ERROR:");
- Console.ResetColor();
- Console.WriteLine(e.Message);
- Console.WriteLine("\n");
- }
- }
- }
- public class User
- {
- public string name = "username";
- public string id = "id";
- }
- //-----REQUESTS-----//
- //LOGIN
- public async Task Login(string username, string password)
- {
- try
- {
- if(Server.ToLower() != "www" && Server.ToLower() != "friends" && Server.ToLower() != "br")
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("ERROR:");
- Console.ResetColor();
- 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\"");
- Console.WriteLine("\n");
- CheckChatInterval.Stop();
- SendPulseInterval.Stop();
- Console.ReadLine();
- return;
- }
- LoginInfo user = new LoginInfo() { username = username, password = password };
- HttpResponseMessage response = await this.client.PostAsJsonAsync("/auth/login/", user);
- response.EnsureSuccessStatusCode();
- Console.WriteLine("\nLogged in!\n");
- var DataObject = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(await response.Content.ReadAsStringAsync());
- this.Username = DataObject["data"]["username"];
- this.ID = DataObject["data"]["id"];
- this.Gold = DataObject["data"]["gold"];
- this.Silver = DataObject["data"]["silver"];
- this.Friends_Limit = DataObject["data"]["friends_limit"];
- this.Created = DataObject["data"]["created"];
- if (DataObject["data"]["email"] != "")
- this.Email = DataObject["data"]["email"];
- else
- this.Email = "none";
- this.Language = DataObject["data"]["language"];
- this.Email_Confirmed = DataObject["data"]["email_confirmed"];
- this.IsAdmin = DataObject["data"]["is_admin"];
- this.XP = DataObject["data"]["xp"];
- this.Level_Progress = DataObject["data"]["level_progress"];
- this.Previous_Level_XP = DataObject["data"]["previous_level_xp"];
- this.Next_Level_XP = DataObject["data"]["next_level_xp"];
- this.XP_To_Next_Level = DataObject["data"]["xp_to_next_level"];
- string HTMLData = await this.client.GetAsync("/auth/after-login/").Result.Content.ReadAsStringAsync();
- this.CSRFToken = this.GetSubStrings(HTMLData, "<meta name=\"CSRFToken\" content=\"", "\">")[0];
- this.client.DefaultRequestHeaders.Add("X-Csrf-Token", this.CSRFToken);
- this.Notify_Token = this.GetSubStrings(HTMLData, "<meta name=\"Notify-Token\" content=\"", "\">")[0];
- HTMLData = null;
- LoggedIn?.Invoke();
- }
- catch (Exception e)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("ERROR:");
- Console.ResetColor();
- Console.WriteLine(e.Message);
- if (e.Message.Contains("400"))
- Console.WriteLine("\nThis error can be related to entering the wrong username or password. \nPlease close the application and check your login info.");
- Console.WriteLine("\n");
- }
- }
- //WIP-GET USER INFO
- public async Task<string> GetUserInfo(string username)
- {
- try
- {
- Console.ForegroundColor = this.DebugColor;
- Console.WriteLine("USER INFO:");
- Console.ResetColor();
- if (username == this.Username)
- {
- await Task.Delay(20);
- Console.WriteLine("Username: " + this.Username);
- Console.WriteLine("ID: " + this.ID);
- Console.WriteLine("Gold: " + this.Gold);
- Console.WriteLine("Silver: " + this.Silver);
- Console.WriteLine("Friends limit: " + this.Friends_Limit);
- Console.WriteLine("Account created: " + this.Created);
- Console.WriteLine("Email address: " + this.Email);
- Console.WriteLine("Language: " + this.Language);
- Console.WriteLine("Email confirmed: " + this.Email_Confirmed);
- Console.WriteLine("Is administrator: " + this.IsAdmin);
- Console.WriteLine("\nXP: " + this.XP);
- Console.WriteLine("Level progress to next level: " + this.Level_Progress + "%");
- Console.WriteLine("XP needed to reach next level: " + this.XP_To_Next_Level);
- Console.WriteLine("Previous Level XP: " + this.Previous_Level_XP);
- Console.WriteLine("Next Level XP: " + this.Next_Level_XP);
- Console.WriteLine("\n");
- return "";
- }
- else
- {
- return "YOUR INFO:\n\nUsername: nub\nGold: -200\nXP: 0.000001";
- }
- }
- catch (Exception e)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("ERROR:");
- Console.ResetColor();
- Console.WriteLine(e.Message);
- Console.WriteLine("\n");
- return "An error occured.";
- }
- }
- //PURCHASE MODEL(S)
- public async Task PurchaseModel(string modelID, int times)
- {
- try
- {
- Console.ForegroundColor = this.DebugColor;
- Console.WriteLine("PURCHASING MODELS:");
- Console.ResetColor();
- int x = 0;
- while (x < times)
- {
- if ((Convert.ToInt32(this.Gold) >= 30 && (this.Server.ToLower() == "www" || this.Server.ToLower() == "br")) || (Convert.ToInt32(this.Gold) >= 50 && (this.Server.ToLower() == "friends")))
- {
- x += 1;
- Console.WriteLine("\nThe model will be purchased in 30 seconds!");
- await Task.Delay(30000);
- HttpResponseMessage response = await this.client.PostAsJsonAsync("/model/market/i-" + modelID + "/purchase/", "");
- response.EnsureSuccessStatusCode();
- Console.WriteLine("\nModel purchased!");
- }
- else
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.Write("ERROR:");
- Console.ResetColor();
- Console.WriteLine(" you do not have enough gold to purchase the model.\n");
- return;
- }
- }
- await Task.Delay(500);
- Console.WriteLine("\nTransaction completed!\n");
- await Task.Delay(500);
- }
- catch (Exception e)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("ERROR:");
- Console.ResetColor();
- Console.WriteLine(e.Message);
- Console.WriteLine("\n");
- }
- }
- //SEND MESSAGE
- public async Task SendMessage(string Message, string To_ID, string To_Name = "", bool Log = true)
- {
- try
- {
- MessageInfo MsgObject = new MessageInfo();
- MsgObject.message = Message;
- MsgObject.to_profile_id = To_ID;
- HttpResponseMessage response = await this.client.PostAsJsonAsync("/chat/" + this.ID + "/", MsgObject);
- if (response.IsSuccessStatusCode)
- {
- if (Log == true)
- {
- Console.ForegroundColor = this.DebugColor;
- Console.WriteLine("MESSAGE SENT:");
- Console.ResetColor();
- Console.WriteLine("From: me\nMessage: " + Message);
- if (To_Name != "")
- Console.WriteLine("To: " + To_Name);
- else
- Console.WriteLine("To: " + To_ID);
- Console.WriteLine("");
- }
- }
- else
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("ERROR:");
- Console.ResetColor();
- Console.WriteLine("Failed to send the message.");
- Console.WriteLine("Server responded with: " + response.StatusCode);
- }
- }
- catch (Exception e)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("ERROR:");
- Console.ResetColor();
- Console.WriteLine(e.Message);
- Console.WriteLine("\n");
- }
- }
- //WIP-CHECK FEED
- public async Task CheckFeed(string ProfileID)
- {
- try
- {
- HttpResponseMessage response = await this.client.GetAsync("/api/feed/" + ProfileID);
- var DataObject = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(await response.Content.ReadAsStringAsync());
- foreach (dynamic feedpost in DataObject["data"])
- {
- Console.WriteLine(feedpost);
- if (feedpost["_data"].ToString().Contains("test"))
- {
- CommentInfo RequestData = new CommentInfo()
- {
- comment = "Feedpost info: " + feedpost["_data"] + " Created: " + feedpost["created"] + " type: " + feedpost["feed_type"] + " gold: " + feedpost["gold"]
- };
- Console.WriteLine(feedpost["other_username"] + " ID" + feedpost["id"]);
- string ID2 = feedpost["id"];
- HttpResponseMessage response2 = await client.PostAsJsonAsync("/api/feed/" + ID2 + "/comment/", RequestData);
- Console.WriteLine(await response2.Content.ReadAsStringAsync());
- }
- }
- }
- catch (Exception e)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("ERROR:");
- Console.ResetColor();
- Console.WriteLine(e.Message);
- Console.WriteLine("\n");
- }
- }
- //[TIMERS ONLY] SEND PULSE
- private async void SendPulse(Object CheckChatObject, ElapsedEventArgs CheckChatArgs)
- {
- try
- {
- if (this.Status.ToLower() == "building")
- {
- OnlineInfo RequestData = new OnlineInfo() { location = "/build/0/project/" + this.GameID + "/", status = "active" };
- await this.client.PostAsJsonAsync("/user/" + this.ID + "/pulse/", RequestData);
- }
- else if(this.Status.ToLower() == "playing")
- {
- OnlineInfo RequestData = new OnlineInfo() { location = "/games/play/" + this.GameID + "/", status = "active" };
- await this.client.PostAsJsonAsync("/user/" + this.ID + "/pulse/", RequestData);
- }
- else if(this.Status.ToLower() == "online")
- {
- OnlineInfo RequestData = new OnlineInfo() { location = "/", status = "active" };
- await this.client.PostAsJsonAsync("/user/" + this.ID + "/pulse/", RequestData);
- }
- }
- catch (Exception e)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("ERROR:");
- Console.ResetColor();
- Console.WriteLine(e.Message);
- Console.WriteLine("\n");
- }
- }
- //[TIMERS ONLY] CHECK CHAT
- private async void CheckChat(Object CheckChatObject, ElapsedEventArgs CheckChatArgs)
- {
- try
- {
- HttpResponseMessage response = await this.client.GetAsync("/chat/" + this.ID + "/");
- if (response.IsSuccessStatusCode)
- {
- var DataObject = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(await response.Content.ReadAsStringAsync());
- foreach (dynamic a in DataObject["data"])
- {
- foreach (dynamic b in a)
- {
- foreach (dynamic c in b)
- {
- Message Message = new Message()
- {
- content = c["message"].ToString(),
- created = c["created"].ToString(),
- author = new User()
- {
- name = c["from_username"].ToString(),
- id = c["from_profile_id"].ToString()
- },
- bot = this
- };
- MessageReceived?.Invoke(Message);
- }
- }
- }
- }
- }
- catch (Exception e)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("ERROR:");
- Console.ResetColor();
- Console.WriteLine(e.Message);
- Console.WriteLine("\n");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement