Exemple #1
0
        public static ResponseObject Handle(StandardisedRequestObject Context)
        {
            bool ErrorOccured = false;

            Backend.Data.Objects.Bot CorrespondingBot = AuthCheck(Context);

            if (Context.Headers.AllKeys.Contains("TwitchID"))
            {
                if (!Checks.IsValidID(Context.Headers["TwitchID"]))
                {
                    Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, TwitchID contains invalid characters"; return(Context.ResponseObject);
                }
            }
            if (Context.Headers.AllKeys.Contains("DiscordID"))
            {
                if (!Checks.IsValidID(Context.Headers["DiscordID"]))
                {
                    Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, DiscordID contains invalid characters"; return(Context.ResponseObject);
                }
            }

            if (Context.URLSegments[1] == "viewer")
            {
                if ((Context.Headers.AllKeys.Contains("TwitchID") || Context.Headers.AllKeys.Contains("DiscordID") || Context.Headers.AllKeys.Contains("Notifications") || Context.Headers.AllKeys.Contains("WatchTime") || Context.Headers.AllKeys.Contains("DontReward")) && Context.Headers.AllKeys.Contains("ID"))
                {
                    if (CorrespondingBot != null)
                    {
                        Data.Objects.Viewer B = Data.Objects.Viewer.FromID(int.Parse(Context.Headers["ID"]));
                        if (B == null)
                        {
                            ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, ID does not correspond to an existing viewer"; return(Context.ResponseObject);
                        }
                        if (B.Currency.ID == CorrespondingBot.Currency.ID || CorrespondingBot.IsSuperBot)
                        {
                            if (Context.Headers["DiscordID"] != null)
                            {
                                B.DiscordID = Context.Headers["DiscordID"];
                            }
                            if (Context.Headers["TwitchID"] != null)
                            {
                                B.TwitchID = Context.Headers["TwitchID"];
                            }
                            if (Context.Headers["Notifications"] != null)
                            {
                                B.LiveNotifcations = Context.Headers["Notifications"] == "True";
                            }
                            if (Context.Headers["WatchTime"] != null)
                            {
                                B.WatchTime = int.Parse(Context.Headers["WatchTime"]);
                            }
                            if (Context.Headers["DontReward"] != null)
                            {
                                B.DontReward = Context.Headers["DontReward"] == "True";
                            }
                            B.Update();
                        }
                        else
                        {
                            ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, This bot does not have permission to edit that Bank";
                        }
                    }
                    else
                    {
                        ErrorOccured = true;
                        //Context.ResponseObject.Code = 403; Context.ResponseObject.Message = "Invalid AuthToken";
                    }
                }
                else if (Context.Headers.AllKeys.Contains("TwitchID") || Context.Headers.AllKeys.Contains("DiscordID"))
                {
                    if (CorrespondingBot != null)
                    {
                        Data.Objects.Viewer B = new Data.Objects.Viewer();
                        if (B == null)
                        {
                            ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, ID does not correspond to an existing viewer"; return(Context.ResponseObject);
                        }
                        B.DiscordID = Context.Headers["DiscordID"];
                        B.TwitchID  = Context.Headers["TwitchID"];
                        if (Context.Headers.AllKeys.Contains("CurrencyID"))
                        {
                            try { int.Parse(Context.Headers["CurrencyID"]); } catch { Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Malformed CurrencyID"; return(Context.ResponseObject); }
                            if (int.Parse(Context.Headers["CurrencyID"]) == CorrespondingBot.Currency.ID || CorrespondingBot.IsSuperBot)
                            {
                                B.Currency = Data.Objects.Currency.FromID(int.Parse(Context.Headers["CurrencyID"]));
                            }
                            else
                            {
                                ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, This bot does not have permission to edit that Currency"; return(Context.ResponseObject);
                            }
                        }
                        else
                        {
                            B.Currency = CorrespondingBot.Currency;
                        }
                        B.Balance = int.Parse(CorrespondingBot.Currency.CommandConfig["InititalBalance"].ToString());
                        if (B.Currency != null)
                        {
                            if (!B.Save())
                            {
                                ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, a Viewer already exists in this currency witht that Discord and/or Twitch ID";
                            }
                        }
                        else
                        {
                            ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, was unable to set Currency, try explicitly setting Currency with CurrencyID header";
                        }
                    }
                    else
                    {
                        ErrorOccured = true;
                        //Context.ResponseObject.Code = 403; Context.ResponseObject.Message = "Invalid AuthToken";
                    }
                }
                else if (Context.Headers.AllKeys.Contains("ID") && Context.Headers.AllKeys.Contains("Operator") && Context.Headers.AllKeys.Contains("Value"))
                {
                    if (CorrespondingBot != null)
                    {
                        try { int.Parse(Context.Headers["ID"]); int.Parse(Context.Headers["Value"]); } catch { Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Malformed ID and/or Value"; return(Context.ResponseObject); }
                        Data.Objects.Viewer B = Data.Objects.Viewer.FromID(int.Parse(Context.Headers["ID"]));
                        if (B == null)
                        {
                            ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, ID does not correspond to an existing viewer"; return(Context.ResponseObject);
                        }
                        if (B.Currency.ID == CorrespondingBot.Currency.ID || CorrespondingBot.IsSuperBot)
                        {
                            if (Context.Headers["Operator"].ToString() == "+")
                            {
                                B.Balance += int.Parse(Context.Headers["Value"]);
                                if (B.Balance >= 0)
                                {
                                    B.Update();
                                }
                                else
                                {
                                    ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Cannot set balance as negative";
                                }
                            }
                            else if (Context.Headers["Operator"].ToString() == "-")
                            {
                                B.Balance -= int.Parse(Context.Headers["Value"]);
                                if (B.Balance >= 0)
                                {
                                    B.Update();
                                }
                                else
                                {
                                    ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Cannot set balance as negative";
                                }
                            }
                            else
                            {
                                ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Operator must be + or -";
                            }
                        }
                        else
                        {
                            ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, This bot does not have permission to edit that Bank";
                        }
                    }
                    else
                    {
                        ErrorOccured = true;
                        //Context.ResponseObject.Code = 403; Context.ResponseObject.Message = "Invalid AuthToken";
                    }
                }
                else if ((Context.Headers.AllKeys.Contains("BalanceIncrement") || Context.Headers.AllKeys.Contains("WatchTimeIncrement")) && Context.Headers.AllKeys.Contains("CurrencyID") && Context.RequestData != null)
                {
                    if (CorrespondingBot != null)
                    {
                        int BalanceIncrement = 0, WatchTimeIncrement = 0;
                        if (Context.Headers.AllKeys.Contains("BalanceIncrement"))
                        {
                            try { BalanceIncrement = int.Parse(Context.Headers["BalanceIncrement"]); } catch { Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Malformed BalanceIncrement"; return(Context.ResponseObject); }
                        }
                        if (Context.Headers.AllKeys.Contains("WatchTimeIncrement"))
                        {
                            try { WatchTimeIncrement = int.Parse(Context.Headers["WatchTimeIncrement"]); } catch { Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Malformed WatchTimeIncrement"; return(Context.ResponseObject); }
                        }
                        try { int.Parse(Context.Headers["CurrencyID"]); }
                        catch { Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Malformed CurrencyID"; return(Context.ResponseObject); }
                        Data.Objects.Currency C = Data.Objects.Currency.FromID(int.Parse(Context.Headers["CurrencyID"]));
                        if (C != null)
                        {
                            if (C.ID == CorrespondingBot.Currency.ID || CorrespondingBot.IsSuperBot)
                            {
                                List <string> DiscordIDs = new List <string> {
                                }, TwitchIDs = new List <string> {
                                };
                                if (Context.RequestData["DiscordIDs"] != null)
                                {
                                    DiscordIDs = Context.RequestData["DiscordIDs"].ToObject <List <string> >();
                                }
                                if (Context.RequestData["TwitchIDs"] != null)
                                {
                                    TwitchIDs = Context.RequestData["TwitchIDs"].ToObject <List <string> >();
                                }
                                Data.Objects.Viewer.Increment(DiscordIDs, TwitchIDs, BalanceIncrement, WatchTimeIncrement, C.ID);
                            }
                            else
                            {
                                ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, This bot does not have permission to edit that Bank";
                            }
                        }
                        else
                        {
                            ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, CurrencyID does not correspond to an existing Currency";
                        }
                    }
                    else
                    {
                        ErrorOccured = true;
                        //Context.ResponseObject.Code = 403; Context.ResponseObject.Message = "Invalid AuthToken";
                    }
                }
                else if (Context.Headers.AllKeys.Contains("ID"))
                {
                    if (CorrespondingBot != null)
                    {
                        try { int.Parse(Context.Headers["ID"]); } catch { Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Malformed ID"; return(Context.ResponseObject); }
                        Data.Objects.Viewer B = Data.Objects.Viewer.FromID(int.Parse(Context.Headers["ID"]));
                        if (B == null)
                        {
                            ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, ID does not correspond to an existing viewer"; return(Context.ResponseObject);
                        }
                        if (B.Currency.ID == CorrespondingBot.Currency.ID || CorrespondingBot.IsSuperBot)
                        {
                            B.Delete();
                        }
                        else
                        {
                            ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, This bot does not have permission to edit that Bank";
                        }
                    }
                    else
                    {
                        ErrorOccured = true;
                        //Context.ResponseObject.Code = 403; Context.ResponseObject.Message = "Invalid AuthToken";
                    }
                }
                else
                {
                    ErrorOccured = true;
                    Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, No operable Headers provided";
                }
            }
            else if (Context.URLSegments[1] == "login")
            {
                if ((Context.Headers.AllKeys.Contains("UserName") || Context.Headers.AllKeys.Contains("Email") || Context.Headers.AllKeys.Contains("Password")) && Context.Headers.AllKeys.Contains("AccessToken") && Context.Headers.AllKeys.Contains("ID"))
                {
                    try { int.Parse(Context.Headers["ID"]); } catch { Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Malformed ID"; return(Context.ResponseObject); }
                    Data.Objects.Login L = Data.Objects.Login.FromID(int.Parse(Context.Headers["ID"]), true);
                    if (L != null)
                    {
                        if (Backend.Init.ScryptEncoder.Compare(Context.Headers["AccessToken"], L.AccessToken))
                        {
                            if (Context.Headers["Email"] != null)
                            {
                                if (!Checks.IsValidEmail(Context.Headers["Email"]))
                                {
                                    Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Email is not valid"; return(Context.ResponseObject);
                                }
                                L.Email = Context.Headers["Email"];
                            }
                            if (Context.Headers["UserName"] != null)
                            {
                                if (!Checks.IsAlphaNumericString(Context.Headers["UserName"]))
                                {
                                    Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Username is not AlphaNumeric"; return(Context.ResponseObject);
                                }
                                L.UserName = Context.Headers["UserName"];
                            }
                            if (Context.Headers["Password"] != null)
                            {
                                if (Context.Headers["Password"].Length < 8)
                                {
                                    Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Password too short"; return(Context.ResponseObject);
                                }
                                if (!Checks.IsValidPassword(Context.Headers["Password"]))
                                {
                                    Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Password requires at least 1 Capital, 1 Number, 1 Special"; return(Context.ResponseObject);
                                }
                                L.HashedPassword = new Scrypt.ScryptEncoder().Encode(Context.Headers["Password"]);
                            }
                            if (!L.UpdateUserNameEmailPassword())
                            {
                                ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, That UserName or Email may be in use by another account";
                            }
                        }
                        else
                        {
                            ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, AccessToken is invalid";
                        }
                    }
                    else
                    {
                        ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, ID does not correspond to an existing user";
                    }
                }
                else if (Context.Headers.AllKeys.Contains("AccessToken") && Context.Headers.AllKeys.Contains("ID"))
                {
                    try { int.Parse(Context.Headers["ID"]); } catch { Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Malformed ID"; return(Context.ResponseObject); }
                    Data.Objects.Login L = Data.Objects.Login.FromID(int.Parse(Context.Headers["ID"]), true);
                    if (L != null)
                    {
                        if (Backend.Init.ScryptEncoder.Compare(Context.Headers["AccessToken"], L.AccessToken))
                        {
                            if (!L.UpdateToken())
                            {
                                Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Was unable to refresh"; ErrorOccured = true;
                            }
                            else
                            {
                                Context.ResponseObject.Data = L.ToJson();
                            }
                        }
                        else
                        {
                            ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, AccessToken is invalid";
                        }
                    }
                    else
                    {
                        ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, ID does not correspond to an existing user";
                    }
                }
                else if (Context.Headers.AllKeys.Contains("Password"))
                {
                    if (Context.Headers.AllKeys.Contains("UserName"))
                    {
                        Data.Objects.Login L = Data.Objects.Login.FromUserName(Context.Headers["UserName"], true);
                        if (L != null)
                        {
                            if (Context.Headers["Password"] == null)
                            {
                                ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Password is null";
                            }
                            else
                            {
                                if (Backend.Init.ScryptEncoder.Compare(Context.Headers["Password"], L.HashedPassword))
                                {
                                    L.UpdateToken(); L.HashedPassword = null; Context.ResponseObject.Data = L.ToJson();
                                }
                                else
                                {
                                    ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Password does not match";
                                }
                            }
                        }
                        else
                        {
                            ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, UserName does not correspond to an existing user";
                        }
                    }
                    else if (Context.Headers.AllKeys.Contains("Email"))
                    {
                        Data.Objects.Login L = Data.Objects.Login.FromEmail(Context.Headers["Email"], true);
                        if (L != null)
                        {
                            if (Context.Headers["Password"] == null)
                            {
                                ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Password is null";
                            }
                            else
                            {
                                if (Backend.Init.ScryptEncoder.Compare(Context.Headers["Password"], L.HashedPassword))
                                {
                                    L.UpdateToken(); L.HashedPassword = null; Context.ResponseObject.Data = L.ToJson();
                                }
                                else
                                {
                                    ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Password does not match";
                                }
                            }
                        }
                        else
                        {
                            ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Email does not correspond to an existing user";
                        }
                    }
                    else
                    {
                        ErrorOccured = true;
                        Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Email or UserName header is required";
                    }
                }
                else if (Context.URLSegments.Length == 3)
                {
                    try { int.Parse(Context.Headers["ID"]); } catch { Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Malformed ID"; return(Context.ResponseObject); }
                    if (Context.Headers.AllKeys.Contains("AccessToken") && Context.Headers.AllKeys.Contains("ID") && Context.URLSegments[2] == "delete")
                    {
                        Data.Objects.Login L = Data.Objects.Login.FromID(int.Parse(Context.Headers["ID"]), true);
                        if (L != null)
                        {
                            if (Backend.Init.ScryptEncoder.Compare(Context.Headers["AccessToken"], L.AccessToken))
                            {
                                L.Delete();
                            }
                            else
                            {
                                ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, AccessToken is invalid";
                            }
                        }
                        else
                        {
                            ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, ID does not correspond to an existing user";
                        }
                    }
                }
                else
                {
                    ErrorOccured = true;
                    Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, No operable Headers provided";
                }
            }
            else if (Context.URLSegments[1] == "signup")
            {
                if (Context.Headers.AllKeys.Contains("UserName") && Context.Headers.AllKeys.Contains("Email") && Context.Headers.AllKeys.Contains("Password"))
                {
                    Backend.Data.Objects.Login L = new Data.Objects.Login();
                    L.Email    = Context.Headers["Email"];
                    L.UserName = Context.Headers["UserName"];
                    if (L.UserName != null)
                    {
                        if (!Checks.IsAlphaNumericString(L.UserName))
                        {
                            Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Username is not AlphaNumeric"; return(Context.ResponseObject);
                        }
                    }
                    if (L.Email != null)
                    {
                        if (!Checks.IsValidEmail(L.Email))
                        {
                            Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Email is not valid"; return(Context.ResponseObject);
                        }
                    }
                    if (Data.Objects.Login.FromEmail(L.Email) == null && Data.Objects.Login.FromUserName(L.UserName) == null)
                    {
                        string RawPassword = Context.Headers["Password"];
                        if (RawPassword.Length < 8)
                        {
                            Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Password too short"; return(Context.ResponseObject);
                        }
                        if (!Checks.IsValidPassword(RawPassword))
                        {
                            Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Password requires at least 1 Capital, 1 Number, 1 Special"; return(Context.ResponseObject);
                        }
                        L.HashedPassword = Backend.Init.ScryptEncoder.Encode(RawPassword);
                        L.Save();
                    }
                    else
                    {
                        ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, User already exists";
                    }
                }
                else
                {
                    ErrorOccured = true;
                    Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, No operable Headers provided";
                }
            }
            else if (Context.URLSegments[1] == "bot")
            {
                if (Context.Headers.AllKeys.Contains("RefreshToken") && Context.Headers.AllKeys.Contains("BotID"))
                {
                    try { int.Parse(Context.Headers["BotID"]); } catch { Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Malformed ID"; return(Context.ResponseObject); }
                    Data.Objects.Bot B = Data.Objects.Bot.FromID(int.Parse(Context.Headers["BotID"]), true);
                    if (B != null)
                    {
                        if (Backend.Init.ScryptEncoder.Compare(Context.Headers["RefreshToken"], B.RefreshToken))
                        {
                            B.PerformRefresh();
                            Context.ResponseObject.Data = B.ToJson();
                        }
                        else
                        {
                            ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Refresh Token is not valid";
                        }
                    }
                    else
                    {
                        ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, BotID does not correspond to a bot";
                    }
                }
                else if (Context.Headers.AllKeys.Contains("AccessToken") && Context.Headers.AllKeys.Contains("CurrencyID") && Context.Headers.AllKeys.Contains("BotID") && Context.Headers.AllKeys.Contains("LoginID"))
                {
                    try { int.Parse(Context.Headers["CurrencyID"]); } catch { Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Malformed CurrencyID"; return(Context.ResponseObject); }
                    try { int.Parse(Context.Headers["BotID"]); } catch { Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Malformed BotID"; return(Context.ResponseObject); }
                    try { int.Parse(Context.Headers["LoginID"]); } catch { Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Malformed LoginID"; return(Context.ResponseObject); }
                    Data.Objects.Login L = Data.Objects.Login.FromID(int.Parse(Context.Headers["LoginID"]), true);
                    if (L != null)
                    {
                        if (Backend.Init.ScryptEncoder.Compare(Context.Headers["AccessToken"], L.AccessToken))
                        {
                            Data.Objects.Bot B = Data.Objects.Bot.FromID(int.Parse(Context.Headers["BotID"]));
                            if (B != null)
                            {
                                if (B.Currency == null)
                                {
                                    B.Currency = Data.Objects.Currency.FromLogin(L.ID).Find(x => x.ID == int.Parse(Context.Headers["CurrencyID"]));
                                    if (B.Currency == null)
                                    {
                                        ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, AccessToken is not allowed to edit that currency";
                                    }
                                    else
                                    {
                                        B.UpdateCurrency();
                                    }
                                }
                                else
                                {
                                    ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Bot is already bound to a currency";
                                }
                            }
                            else
                            {
                                ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, BotID doesnt match any bot";
                            }
                        }
                        else
                        {
                            ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, AccessToken is invalid";
                        }
                    }
                    else
                    {
                        ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, ID does not correspond to an existing user";
                    }
                }
                else if (Context.Headers.AllKeys.Contains("AccessToken") && Context.Headers.AllKeys.Contains("LoginID"))
                {
                    try { int.Parse(Context.Headers["LoginID"]); } catch { Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Malformed LoginID"; return(Context.ResponseObject); }
                    Data.Objects.Login L = Data.Objects.Login.FromID(int.Parse(Context.Headers["LoginID"]), true);
                    if (L != null)
                    {
                        if (Backend.Init.ScryptEncoder.Compare(Context.Headers["AccessToken"], L.AccessToken))
                        {
                            if (Data.Objects.Bot.FromLogin(L.ID).Count >= 5)
                            {
                                ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, You are already at the max Bot count";
                            }
                            else
                            {
                                Data.Objects.Bot B = new Data.Objects.Bot();
                                if (Context.Headers.AllKeys.Contains("BotName"))
                                {
                                    B.BotName = Context.Headers["BotName"];
                                    if (!Checks.IsAlphaNumericString(B.BotName))
                                    {
                                        Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, BotName is not AlphaNumeric"; return(Context.ResponseObject);
                                    }
                                }
                                else
                                {
                                    B.BotName = "No Name Given";
                                }
                                B.OwnerLogin = Data.Objects.Login.FromID(L.ID);
                                B.Save();
                                Data.Objects.Bot NewB = Data.Objects.Bot.FromLogin(L.ID, true).Last();
                                NewB.RefreshToken           = B.RefreshToken;
                                NewB.AccessToken            = B.AccessToken;
                                Context.ResponseObject.Data = NewB.ToJson();
                            }
                        }
                        else
                        {
                            ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, AccessToken is invalid";
                        }
                    }
                    else
                    {
                        ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, ID does not correspond to an existing user";
                    }
                }
                else
                {
                    ErrorOccured = true;
                    Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, No operable Headers provided";
                }
            }
            else if (Context.URLSegments[1] == "currency")
            {
                if (Context.URLSegments.Length == 3)
                {
                    if (Context.URLSegments[2] == "all")
                    {
                        if (CorrespondingBot != null && CorrespondingBot.IsSuperBot)
                        {
                            Context.ResponseObject.Data = Newtonsoft.Json.Linq.JToken.FromObject(Data.Objects.Currency.All(true));
                        }
                        else
                        {
                            Context.ResponseObject.Data = Newtonsoft.Json.Linq.JToken.FromObject(Data.Objects.Currency.All());
                        }
                    }
                    else if (Context.URLSegments[2] == "delete")
                    {
                        if (Context.Headers.AllKeys.Contains("AccessToken") && Context.Headers.AllKeys.Contains("LoginID") && Context.Headers.AllKeys.Contains("CurrencyID"))
                        {
                            try { int.Parse(Context.Headers["LoginID"]); } catch { Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Malformed LoginID"; return(Context.ResponseObject); }
                            try { int.Parse(Context.Headers["CurrencyID"]); } catch { Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Malformed CurrencyID"; return(Context.ResponseObject); }
                            Data.Objects.Login L = Data.Objects.Login.FromID(int.Parse(Context.Headers["LoginID"]), true);
                            if (L != null)
                            {
                                if (Backend.Init.ScryptEncoder.Compare(Context.Headers["AccessToken"], L.AccessToken))
                                {
                                    Data.Objects.Currency C = Data.Objects.Currency.FromID(int.Parse(Context.Headers["CurrencyID"]));
                                    if (C != null)
                                    {
                                        C.Delete();
                                    }
                                    else
                                    {
                                    }
                                }
                                else
                                {
                                    ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, AccessToken is invalid";
                                }
                            }
                            else
                            {
                                ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, ID does not correspond to an existing user";
                            }
                        }
                    }
                    else
                    {
                        ErrorOccured = true;
                        Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Bot is not SuperBot";
                    }
                }
                else if (((Context.Headers.AllKeys.Contains("AccessToken") && Context.Headers.AllKeys.Contains("LoginID")) || CorrespondingBot != null) && Context.RequestData != null && Context.Headers.AllKeys.Contains("CurrencyID"))
                {
                    try { int.Parse(Context.Headers["CurrencyID"]); } catch { Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Malformed CurrencyID"; return(Context.ResponseObject); }
                    Data.Objects.Login L = null;
                    if (Context.Headers.AllKeys.Contains("LoginID"))
                    {
                        try { int.Parse(Context.Headers["LoginID"]); } catch { Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Malformed LoginID"; return(Context.ResponseObject); }
                        L = Data.Objects.Login.FromID(int.Parse(Context.Headers["LoginID"]), true);
                        if (!Backend.Init.ScryptEncoder.Compare(Context.Headers["AccessToken"], L.AccessToken))
                        {
                            L = null;
                        }
                    }
                    if (L != null || CorrespondingBot != null)
                    {
                        Data.Objects.Currency B = Data.Objects.Currency.FromID(int.Parse(Context.Headers["CurrencyID"]));
                        B.LoadConfigs(true);
                        bool LoginGood = false, BotGood = false;
                        if (L != null)
                        {
                            LoginGood = B.OwnerLogin.ID == L.ID;
                        }
                        if (CorrespondingBot != null)
                        {
                            BotGood = /*CorrespondingBot.Currency.ID == B.ID ||*/ CorrespondingBot.IsSuperBot;
                        }
                        if (LoginGood || BotGood)
                        {
                            if (Context.RequestData["LoginConfig"] != null)
                            {
                                if (CorrespondingBot == null || CorrespondingBot.IsSuperBot)
                                {
                                    if (Checks.JSONLayoutCompare(
                                            Newtonsoft.Json.Linq.JToken.Parse(System.IO.File.ReadAllText("./Data/DefaultConfigs/Login.config.json")),
                                            Context.RequestData["LoginConfig"]))
                                    {
                                        B.LoginConfig = Context.RequestData["LoginConfig"];
                                    }
                                    else
                                    {
                                        ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, LoginConfig does not follow the required structure";
                                    }
                                }
                            }
                            if (Context.RequestData["CommandConfig"] != null)
                            {
                                if (Checks.JSONLayoutCompare(
                                        Newtonsoft.Json.Linq.JToken.Parse(System.IO.File.ReadAllText("./Data/DefaultConfigs/Command.config.json")),
                                        Context.RequestData["CommandConfig"]))
                                {
                                    B.CommandConfig = Context.RequestData["CommandConfig"];
                                }
                                else
                                {
                                    ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, ComamndConfig does not follow the required structure";
                                }
                            }
                            if (ErrorOccured == false)
                            {
                                B.UpdateConfigs();
                            }
                        }
                        else
                        {
                            ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, This login does not have permission to edit that Currency";
                        }
                    }
                    else
                    {
                        ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, AccessToken is invalid";
                    }
                }
                else if (Context.Headers.AllKeys.Contains("CurrencyID") && CorrespondingBot != null)
                {
                    try { int.Parse(Context.Headers["CurrencyID"]); } catch { Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Malformed CurrencyID"; return(Context.ResponseObject); }
                    Data.Objects.Currency C = Data.Objects.Currency.FromID(int.Parse(Context.Headers["CurrencyID"]));
                    if (/*CorrespondingBot.Currency.ID == C.ID ||*/ CorrespondingBot.IsSuperBot)
                    {
                        C.LoadConfigs(true);
                        Context.ResponseObject.Data = C.ToJson();
                    }
                    else
                    {
                        ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, This bot does not have permission to read that Currency";
                    }
                }
                else if (Context.Headers.AllKeys.Contains("AccessToken") && Context.Headers.AllKeys.Contains("LoginID"))
                {
                    try { int.Parse(Context.Headers["LoginID"]); } catch { Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Malformed LoginID"; return(Context.ResponseObject); }
                    Data.Objects.Login L = Data.Objects.Login.FromID(int.Parse(Context.Headers["LoginID"]), true);
                    if (L != null)
                    {
                        if (Backend.Init.ScryptEncoder.Compare(Context.Headers["AccessToken"], L.AccessToken))
                        {
                            if (Data.Objects.Currency.FromLogin(L.ID).Count >= 5)
                            {
                                ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, You are already at the max currency count";
                            }
                            else
                            {
                                Data.Objects.Currency B = new Data.Objects.Currency();
                                B.OwnerLogin = Data.Objects.Login.FromID(L.ID);
                                B.Save();
                                B = Data.Objects.Currency.FromLogin(L.ID).Last();
                                Context.ResponseObject.Data = B.ToJson();
                            }
                        }
                        else
                        {
                            ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, AccessToken is invalid";
                        }
                    }
                    else
                    {
                        ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, ID does not correspond to an existing user";
                    }
                }
                else
                {
                    ErrorOccured = true;
                    Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, No operable Headers provided";
                }
            }
            else
            {
                Context.ResponseObject.Code    = 404;
                Context.ResponseObject.Message = "Not Found";
                ErrorOccured = true;
            }
            if (ErrorOccured == false)
            {
                Context.ResponseObject.Code = 200; Context.ResponseObject.Message = "The requested task was performed successfully";
            }
            return(Context.ResponseObject);
        }
        public static ResponseObject Handle(StandardisedRequestObject Context)
        {
            bool ErrorOccured = false;

            // Check if TwitchID and DiscordID only compose of numbers
            if (Context.Headers.AllKeys.Contains("TwitchID"))
            {
                if (!Checks.IsValidID(Context.Headers["TwitchID"]))
                {
                    Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, TwitchID contains invalid characters"; return(Context.ResponseObject);
                }
            }
            if (Context.Headers.AllKeys.Contains("DiscordID"))
            {
                if (!Checks.IsValidID(Context.Headers["DiscordID"]))
                {
                    Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, DiscordID contains invalid characters"; return(Context.ResponseObject);
                }
            }

            if (Context.URLSegments[1] == "viewer")         //Check the url path for viewer
            {
                if (Context.Headers.AllKeys.Contains("ID")) // Get the viewer where header ID matches
                {
                    try { int.Parse(Context.Headers["ID"]); }//Check if the ID Header can be converted to an integer
                    catch {//If it cant be converted, set the contents of the Response Object to reflect this
                        Context.ResponseObject.Code    = 400;
                        Context.ResponseObject.Message = "Bad Request, Malformed ID";
                        return(Context.ResponseObject);
                    }
                    Data.Objects.Viewer B = Data.Objects.Viewer.FromID(int.Parse(Context.Headers["ID"]));//Fetch the Viewer Object with the given ID
                    if (B != null)
                    {
                        Context.ResponseObject.Data = B.ToJson();
                    }                                                           //If We get a Viewer back, set the Response Objects data to the JSON format of the Viewer
                    else  //If we didnt get a viewer back, set the contents of the Response Object to reflect that a viewer doesnt exist with the given ID
                    {
                        Context.ResponseObject.Code    = 400;
                        Context.ResponseObject.Message = "Bad Request, ID does not match an existing object";
                        ErrorOccured = true;
                    }
                }
                else if ((Context.Headers.AllKeys.Contains("TwitchID") || Context.Headers.AllKeys.Contains("DiscordID")) && Context.Headers.AllKeys.Contains("CurrencyID")) // Get the viewer where header (TwitchID and/or DiscordID) and CurrencyID matches
                {
                    try { int.Parse(Context.Headers["CurrencyID"]); } catch { Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Malformed CurrencyID"; return(Context.ResponseObject); }
                    Data.Objects.Viewer B = Data.Objects.Viewer.FromTwitchDiscord(Context.Headers["DiscordID"], Context.Headers["TwitchID"], int.Parse(Context.Headers["CurrencyID"]));
                    if (B != null)
                    {
                        Context.ResponseObject.Data = B.ToJson();
                    }
                    else
                    {
                        Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, TwitchID and/or DiscordID does not match an existing object"; ErrorOccured = true;
                    }
                }
                else if (Context.Headers.AllKeys.Contains("CurrencyID")) // Get all viewers for the CurrencyID
                {
                    string OrderBy = null;
                    if (Context.Headers["Order"] == "WatchTime" || Context.Headers["Order"] == "Balance")
                    {
                        OrderBy = Context.Headers["Order"];
                    }
                    try { int.Parse(Context.Headers["CurrencyID"]); } catch { Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Malformed CurrencyID"; return(Context.ResponseObject); }
                    List <Data.Objects.Viewer> B = Data.Objects.Viewer.FromCurrency(int.Parse(Context.Headers["CurrencyID"]), OrderBy);
                    if (B.Count != 0)
                    {
                        Context.ResponseObject.Data = Newtonsoft.Json.Linq.JToken.FromObject(B);
                    }
                    else
                    {
                        Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, CurrencyID does not match an existing object"; ErrorOccured = true;
                    }
                }
                else if (Context.Headers.AllKeys.Contains("TwitchID") || Context.Headers.AllKeys.Contains("DiscordID")) // Get all viewers for any currency where TwitchID and/or DiscordID matches
                {
                    List <Data.Objects.Viewer> B = Data.Objects.Viewer.FromTwitchDiscord(Context.Headers["DiscordID"], Context.Headers["TwitchID"]);
                    if (B.Count != 0)
                    {
                        Context.ResponseObject.Data = Newtonsoft.Json.Linq.JToken.FromObject(B);
                    }
                    else
                    {
                        Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, TwitchID and/or DiscordID does not match an existing object"; ErrorOccured = true;
                    }
                }
                else//Inform requestor that we dont have any infomation to work with
                {
                    ErrorOccured = true;
                    Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, No operable Headers provided";
                }
            }
            else if (Context.URLSegments[1] == "currency")
            {
                if (Context.Headers.AllKeys.Contains("ID"))//Get Currency where ID matches
                {
                    try { int.Parse(Context.Headers["ID"]); } catch { Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Malformed ID"; return(Context.ResponseObject); }
                    Data.Objects.Currency C = Data.Objects.Currency.FromID(int.Parse(Context.Headers["ID"]));
                    if (Context.Headers.AllKeys.Contains("AccessToken") && Context.Headers.AllKeys.Contains("LoginID"))
                    { // If a valid accesstoken is provided, get private information
                        try { int.Parse(Context.Headers["LoginID"]); } catch { Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Malformed ID"; return(Context.ResponseObject); }
                        Data.Objects.Login L = Data.Objects.Login.FromID(int.Parse(Context.Headers["LoginID"]), true);
                        if (L != null)
                        {
                            if (Backend.Init.ScryptEncoder.Compare(Context.Headers["AccessToken"], L.AccessToken))
                            {
                                if (Data.Objects.Currency.FromLogin(L.ID).Find(x => x.ID == C.ID) != null)
                                {
                                    C.LoadConfigs(true);
                                }
                            }
                            else
                            {
                                ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, AccessToken is invalid";
                            }
                        }
                        else
                        {
                            ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, LoginID does not correspond to an existing user";
                        }
                    }
                    if (C != null)
                    {
                        Context.ResponseObject.Data = C.ToJson();
                    }
                    else
                    {
                        Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, ID does not match an existing object"; ErrorOccured = true;
                    }
                }
                else if (Context.Headers.AllKeys.Contains("LoginID"))// Get all Currencies of the LoginID
                {
                    try { int.Parse(Context.Headers["LoginID"]); } catch { Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Malformed LoginID"; return(Context.ResponseObject); }
                    List <Data.Objects.Currency> C = Data.Objects.Currency.FromLogin(int.Parse(Context.Headers["LoginID"]));
                    Context.ResponseObject.Data = Newtonsoft.Json.Linq.JToken.FromObject(C);
                    Context.ResponseObject.Code = 200; Context.ResponseObject.Message = "Unknown Outcome, It is not known if the LoginID matches an object"; ErrorOccured = true;
                }
                else//Inform requestor that we dont have any infomation to work with
                {
                    ErrorOccured = true;
                    Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, No operable Headers provided";
                }
            }
            else if (Context.URLSegments[1] == "login")
            {
                if (Context.Headers.AllKeys.Contains("ID"))//Get Login where ID matches
                {
                    try { int.Parse(Context.Headers["ID"]); } catch { Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Malformed ID"; return(Context.ResponseObject); }
                    Data.Objects.Login L = Data.Objects.Login.FromID(int.Parse(Context.Headers["ID"]));
                    if (L != null)
                    {
                        Context.ResponseObject.Data = L.ToJson();
                        if (Context.Headers.AllKeys.Contains("AccessToken"))
                        {
                            if (Context.Headers["AccessToken"] != "")
                            {
                                L = Data.Objects.Login.FromID(int.Parse(Context.Headers["ID"]), true);
                                if (!Backend.Init.ScryptEncoder.Compare(Context.Headers["AccessToken"], L.AccessToken))
                                {
                                    Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, AccessToken doesnt match"; ErrorOccured = true;
                                }
                                else
                                {
                                    L.AccessToken = null; L.HashedPassword = null; Context.ResponseObject.Data = L.ToJson();
                                }
                            }
                        }
                    }
                    else
                    {
                        Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, ID does not match an existing object"; ErrorOccured = true;
                    }
                }
                else if (Context.Headers.AllKeys.Contains("UserName"))//Get Login where UserName matches
                {
                    Data.Objects.Login L = Data.Objects.Login.FromUserName(Context.Headers["UserName"]);
                    if (L != null)
                    {
                        Context.ResponseObject.Data = L.ToJson();
                    }
                    else
                    {
                        Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, UserName does not match an existing object"; ErrorOccured = true;
                    }
                }
                else if (Context.Headers.AllKeys.Contains("Email"))//Get Login where Email matches
                {
                    Data.Objects.Login L = Data.Objects.Login.FromEmail(Context.Headers["Email"]);
                    if (Context.URLSegments.Length == 3)
                    {
                        if (Context.URLSegments[2] == "recover")
                        {
                            if (!L.UpdateToken())
                            {
                                Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Was unable to recover"; ErrorOccured = true;
                            }
                            else
                            {
                                MailMessage Message = new MailMessage(Backend.Init.APIConfig["Email"]["Email"].ToString(), Context.Headers["Email"], "Password Recovery", Backend.Init.APIConfig["Email"]["Body"].ToString().Replace("<@Token>", L.AccessToken));
                                Message.BodyEncoding = UTF8Encoding.UTF8;
                                Message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
                                Backend.Init.Emailer.Send(Message);
                            }
                        }
                    }
                    else
                    {
                        if (L != null)
                        {
                            Context.ResponseObject.Data = L.ToJson();
                        }
                        else
                        {
                            Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Email does not match an existing object"; ErrorOccured = true;
                        }
                    }
                }
                else//Inform requestor that we dont have any infomation to work with
                {
                    ErrorOccured = true;
                    Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, No operable Headers provided";
                }
            }
            else if (Context.URLSegments[1] == "bot")
            {
                if (Context.Headers.AllKeys.Contains("ID") && Context.Headers.AllKeys.Contains("LoginID"))//Get Bot where ID matches
                {
                    bool WithSecretData = false;
                    try { int.Parse(Context.Headers["ID"]); } catch { Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Malformed ID"; return(Context.ResponseObject); }
                    if (Context.Headers.AllKeys.Contains("AccessToken"))// If a valid accesstoken is provided, get private information
                    {
                        try { int.Parse(Context.Headers["LoginID"]); } catch { Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Malformed ID"; return(Context.ResponseObject); }
                        Data.Objects.Login L = Data.Objects.Login.FromID(int.Parse(Context.Headers["LoginID"]), true);
                        if (L != null)
                        {
                            if (Backend.Init.ScryptEncoder.Compare(Context.Headers["AccessToken"], L.AccessToken))
                            {
                                if (Data.Objects.Bot.FromLogin(L.ID).Find(x => x.ID == int.Parse(Context.Headers["ID"])) != null)
                                {
                                    WithSecretData = true;
                                }
                            }
                            else
                            {
                                ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, AccessToken is invalid";
                            }
                        }
                        else
                        {
                            ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, LoginID does not correspond to an existing user";
                        }
                    }
                    Data.Objects.Bot B = Data.Objects.Bot.FromID(int.Parse(Context.Headers["ID"]), WithSecretData);
                    if (B != null)
                    {
                        Context.ResponseObject.Data = B.ToJson();
                    }
                    else
                    {
                        Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, ID does not match an existing object"; ErrorOccured = true;
                    }
                }
                else if (Context.Headers.AllKeys.Contains("LoginID"))//Get all Bots of LoginID
                {
                    try { int.Parse(Context.Headers["LoginID"]); } catch { Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Malformed LoginID"; return(Context.ResponseObject); }
                    List <Data.Objects.Bot> B = Data.Objects.Bot.FromLogin(int.Parse(Context.Headers["LoginID"]));
                    Context.ResponseObject.Data = Newtonsoft.Json.Linq.JToken.FromObject(B);
                    Context.ResponseObject.Code = 200; Context.ResponseObject.Message = "Unknown Outcome, It is not known if the LoginID matches an object"; ErrorOccured = true;
                }
                else if (Context.Headers.AllKeys.Contains("CurrencyID"))//Get all Bots of CurrencyID
                {
                    try { int.Parse(Context.Headers["CurrencyID"]); } catch { Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Malformed CurrencyID"; return(Context.ResponseObject); }
                    List <Data.Objects.Bot> B = Data.Objects.Bot.FromCurrency(int.Parse(Context.Headers["CurrencyID"]));
                    if (B.Count != 0)
                    {
                        Context.ResponseObject.Data = Newtonsoft.Json.Linq.JToken.FromObject(B);
                    }
                    else
                    {
                        Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, CurrencyID does not match an existing object"; ErrorOccured = true;
                    }
                }
                else//Inform requestor that we dont have any infomation to work with
                {
                    ErrorOccured = true;
                    Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, No operable Headers provided";
                }
            }
            else if (Context.URLSegments[1] == "nightbot")
            {
                Context.GetStateParams();
                if (Context.URLParamaters.ContainsKey("code") && Context.StateParamaters.ContainsKey("accesstoken") && Context.URLParamaters.ContainsKey("state") && Context.StateParamaters.ContainsKey("currencyid") && Context.StateParamaters.ContainsKey("accesstoken"))
                {
                    string Code = Context.URLParamaters["code"];
                    try { int.Parse(Context.StateParamaters["currencyid"]); } catch { Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Malformed CurrencyID"; return(Context.ResponseObject); }
                    Data.Objects.Currency C = Data.Objects.Currency.FromID(int.Parse(Context.StateParamaters["currencyid"]));
                    if (C == null)
                    {
                        Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, CurrencyID does not match an existing object"; ErrorOccured = true;
                    }
                    else
                    {
                        Data.Objects.Login L = Data.Objects.Login.FromID(C.OwnerLogin.ID, true);
                        if (Backend.Init.ScryptEncoder.Compare(Context.StateParamaters["accesstoken"], L.AccessToken))
                        {
                            C.LoadConfigs(true);
                            WebRequest Req = WebRequest.Create("https://api.nightbot.tv/oauth2/token");
                            Req.Method = "POST";
                            byte[] PostData = Encoding.UTF8.GetBytes("client_id=" + C.LoginConfig["NightBot"]["ClientId"] +
                                                                     "&client_secret=" + C.LoginConfig["NightBot"]["ClientSecret"] +
                                                                     "&grant_type=authorization_code&redirect_uri=" + Backend.Init.APIConfig["WebURL"] + "/nightbot/&code=" + Code);
                            Req.Method        = "POST";
                            Req.ContentType   = "application/x-www-form-urlencoded";
                            Req.ContentLength = PostData.Length;
                            Stream PostStream = Req.GetRequestStream();
                            PostStream.Write(PostData, 0, PostData.Length);
                            PostStream.Flush();
                            PostStream.Close();
                            try
                            {
                                WebResponse Res = Req.GetResponse();
                                string      D   = new StreamReader(Res.GetResponseStream()).ReadToEnd();
                                Newtonsoft.Json.Linq.JObject JD = Newtonsoft.Json.Linq.JObject.Parse(D);
                                C.LoginConfig["NightBot"]["RefreshToken"] = JD["refresh_token"];
                                C.UpdateConfigs();
                            }
                            catch (WebException E)
                            {
                                ErrorOccured = true;
                                Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Something went wrong";
                                Console.WriteLine(new StreamReader(E.Response.GetResponseStream()).ReadToEnd());
                            }
                        }
                        else
                        {
                            ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "AccessToken is not allowed to modify that currency";
                        }
                    }
                }
                else
                {
                    ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Code and/or currencyid and/or accesstoken is missing";
                }
            }
            else if (Context.URLSegments[1] == "streamlabs")
            {
                Context.GetStateParams();
                if (Context.URLParamaters.ContainsKey("code") && Context.URLParamaters.ContainsKey("state") && Context.StateParamaters.ContainsKey("currencyid") && Context.StateParamaters.ContainsKey("accesstoken"))
                {
                    string Code = Context.URLParamaters["code"];
                    try { int.Parse(Context.StateParamaters["currencyid"]); } catch { Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Malformed CurrencyID"; return(Context.ResponseObject); }
                    Data.Objects.Currency C = Data.Objects.Currency.FromID(int.Parse(Context.StateParamaters["currencyid"]));
                    if (C == null)
                    {
                        Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, CurrencyID does not match an existing object"; ErrorOccured = true;
                    }
                    else
                    {
                        Data.Objects.Login L = Data.Objects.Login.FromID(C.OwnerLogin.ID, true);
                        if (Backend.Init.ScryptEncoder.Compare(Context.StateParamaters["accesstoken"], L.AccessToken))
                        {
                            C.LoadConfigs(true);
                            WebRequest Req = WebRequest.Create("https://streamlabs.com/api/v1.0/token");
                            Req.Method      = "POST";
                            Req.ContentType = "application/x-www-form-urlencoded";
                            byte[] PostData = Encoding.UTF8.GetBytes("grant_type=authorization_code&client_id=" + C.LoginConfig["StreamLabs"]["ClientId"] +
                                                                     "&client_secret=" + C.LoginConfig["StreamLabs"]["ClientSecret"] +
                                                                     "&redirect_uri=" + Backend.Init.APIConfig["WebURL"] + "/streamlabs/&code=" + Code);
                            Req.ContentLength = PostData.Length;
                            Stream PostStream = Req.GetRequestStream();
                            PostStream.Write(PostData, 0, PostData.Length);
                            PostStream.Flush();
                            PostStream.Close();
                            WebResponse Res;
                            try
                            {
                                Res = Req.GetResponse();
                                Newtonsoft.Json.Linq.JObject D = Newtonsoft.Json.Linq.JObject.Parse(new StreamReader(Res.GetResponseStream()).ReadToEnd());
                                C.LoginConfig["StreamLabs"]["RefreshToken"] = D["refresh_token"];
                                C.UpdateConfigs();
                            }
                            catch (WebException E)
                            {
                                ErrorOccured = true;
                                Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Something went wrong";
                                Console.WriteLine(new StreamReader(E.Response.GetResponseStream()).ReadToEnd());
                            }
                        }
                        else
                        {
                            ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "AccessToken is not allowed to modify that currency";
                        }
                    }
                }
                else
                {
                    ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Code and/or currencyid and/or accesstoken is missing";
                }
            }
            else if (Context.URLSegments[1] == "twitch")
            {
                Context.GetStateParams();
                if (Context.URLParamaters.ContainsKey("code") && Context.StateParamaters.ContainsKey("accesstoken") && Context.URLParamaters.ContainsKey("state") && Context.StateParamaters.ContainsKey("currencyid"))
                {
                    string Code = Context.URLParamaters["code"];
                    try { int.Parse(Context.StateParamaters["currencyid"]); } catch { Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, Malformed CurrencyID"; return(Context.ResponseObject); }
                    Data.Objects.Currency C = Data.Objects.Currency.FromID(int.Parse(Context.StateParamaters["currencyid"]));
                    if (C == null)
                    {
                        Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Bad Request, CurrencyID does not match an existing object"; ErrorOccured = true;
                    }
                    else
                    {
                        Data.Objects.Login L = Data.Objects.Login.FromID(C.OwnerLogin.ID, true);
                        if (Backend.Init.ScryptEncoder.Compare(Context.StateParamaters["accesstoken"], L.AccessToken))
                        {
                            C.LoadConfigs(true);
                            WebRequest Req = WebRequest.Create("https://id.twitch.tv/oauth2/token");
                            Req.Method      = "POST";
                            Req.ContentType = "application/x-www-form-urlencoded";
                            byte[] PostData = Encoding.UTF8.GetBytes("grant_type=authorization_code&client_id=" + C.LoginConfig["Twitch"]["API"]["ClientId"] +
                                                                     "&client_secret=" + C.LoginConfig["Twitch"]["API"]["ClientSecret"] +
                                                                     "&redirect_uri=" + Backend.Init.APIConfig["WebURL"] + "/twitch/&code=" + Code);
                            Req.ContentLength = PostData.Length;
                            Stream PostStream = Req.GetRequestStream();
                            PostStream.Write(PostData, 0, PostData.Length);
                            PostStream.Flush();
                            PostStream.Close();
                            WebResponse Res;
                            try
                            {
                                Res = Req.GetResponse();
                                Newtonsoft.Json.Linq.JObject D = Newtonsoft.Json.Linq.JObject.Parse(new StreamReader(Res.GetResponseStream()).ReadToEnd());
                                C.LoginConfig["Twitch"]["API"]["RefreshToken"] = D["refresh_token"];
                                C.UpdateConfigs();
                            }
                            catch (WebException E)
                            {
                                ErrorOccured = true;
                                Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Something went wrong";
                                Console.WriteLine(new StreamReader(E.Response.GetResponseStream()).ReadToEnd());
                            }
                        }
                        else
                        {
                            ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "AccessToken is not allowed to modify that currency";
                        }
                    }
                }
                else
                {
                    ErrorOccured = true; Context.ResponseObject.Code = 400; Context.ResponseObject.Message = "Code and/or currencyid and/or accesstoken is missing";
                }
            }
            else//Inform requestor that the url does not got anywhere
            {
                Context.ResponseObject.Code    = 404;
                Context.ResponseObject.Message = "Not Found";
                ErrorOccured = true;
            }
            if (ErrorOccured == false)
            {
                Context.ResponseObject.Code = 200; Context.ResponseObject.Message = "The requested task was performed successfully";
            }
            return(Context.ResponseObject);
        }