Ejemplo n.º 1
0
        public static async Task <RequestJSONResult> GetWebJSONAsync(string url)
        {
            RequestJSONResult loadresult = new RequestJSONResult();

            try
            {
                using (HttpRequestMessage requestmessage = new HttpRequestMessage(HttpMethod.Get, url))
                {
                    requestmessage.Version = new Version(1, 1);
                    using (HttpResponseMessage responsemessage = await httpClient.SendAsync(requestmessage))
                    {
                        loadresult.Status    = responsemessage.StatusCode;
                        loadresult.IsSuccess = responsemessage.IsSuccessStatusCode;
                        if (responsemessage.IsSuccessStatusCode)
                        {
                            string content = await responsemessage.Content.ReadAsStringAsync();

                            loadresult.Object = new JSONObject(content);
                        }
                    }
                }
            } catch (Exception e)
            {
                loadresult.IsException     = true;
                loadresult.ThrownException = e;
            }
            return(loadresult);
        }
Ejemplo n.º 2
0
        public async Task HandleSystemInfoCommand(CommandContext context)
        {
            bool printJSON    = context.Args[1].Equals("json");
            bool listStations = context.Args[1].Equals("list");

            string requestedSystem;

            if (printJSON || listStations)
            {
                requestedSystem = context.Message.Content.Substring(CMDKEYS_SYSTEMINFO.Length + 7);
            }
            else
            {
                requestedSystem = context.Message.Content.Substring(CMDKEYS_SYSTEMINFO.Length + 2);
            }
            RequestJSONResult requestResultSystem = await WebRequestService.GetWebJSONAsync(WebRequestService.EDSM_SystemInfo_URL(requestedSystem, true, true, true, true, true));

            RequestJSONResult requestResultStations = await WebRequestService.GetWebJSONAsync(WebRequestService.EDSM_SystemStations_URL(requestedSystem));

            RequestJSONResult requestResultTraffic = await WebRequestService.GetWebJSONAsync(WebRequestService.EDSM_SystemTraffic_URL(requestedSystem));

            RequestJSONResult requestResultDeaths = await WebRequestService.GetWebJSONAsync(WebRequestService.EDSM_SystemDeaths_URL(requestedSystem));

            if (requestResultSystem.IsSuccess)
            {
                if (requestResultSystem.Object.IsArray && requestResultSystem.Object.Count < 1)
                {
                    await context.Channel.SendEmbedAsync("System not found in database!", true);
                }
                else
                {
                    if (printJSON)
                    {
                        await context.Channel.SendEmbedAsync("General System Info", string.Format("```json\n{0}```", requestResultSystem.Object.Print(true).MaxLength(2037)));

                        await context.Channel.SendEmbedAsync("Station Info", string.Format("```json\n{0}```", requestResultStations.Object.Print(true).MaxLength(2037)));
                    }
                    await SendMessage_SystemInfo(context, requestResultSystem.Object, requestResultStations.Object, requestResultTraffic.Object, requestResultDeaths.Object, listStations);
                }
            }
            else if (requestResultSystem.IsException)
            {
                await context.Channel.SendEmbedAsync(string.Format("Could not connect to EDSMs services. Exception Message: `{0}`", requestResultSystem.ThrownException.Message), true);
            }
            else
            {
                await context.Channel.SendEmbedAsync(string.Format("Could not connect to EDSMs services. HTTP Error Message: `{0} {1}`", (int)requestResultSystem.Status, requestResultSystem.Status.ToString()), true);
            }
        }
Ejemplo n.º 3
0
        public async Task HandleCMDRCommand(CommandContext context)
        {
            string cmdrName;
            bool   printJSON = context.Args[1].Equals("json");

            if (printJSON)
            {
                cmdrName = context.Message.Content.Substring(CMDKEYS_CMDR.Length + 7);
            }
            else
            {
                cmdrName = context.Message.Content.Substring(CMDKEYS_CMDR.Length + 2);
            }
            JSONObject RequestContent = WebRequestService.Inara_CMDR_Profile(cmdrName);
            //await context.Channel.SendEmbedAsync("Request JSON", string.Format("```json\n{0}```", RequestContent.Print(true).MaxLength(2037)));
            RequestJSONResult requestResultInara = await WebRequestService.GetWebJSONAsync("https://inara.cz/inapi/v1/", RequestContent);

            RequestJSONResult requestResultEDSM = await WebRequestService.GetWebJSONAsync(WebRequestService.EDSM_Commander_Location(cmdrName, true, false));

            bool       inaraResultOK = false;
            JSONObject inaraEvents   = null;

            if (requestResultInara.IsSuccess)
            {
                JSONObject result = requestResultInara.Object;
                if (printJSON)
                {
                    await context.Channel.SendEmbedAsync("Inara result JSON", string.Format("```json\n{0}```", result.Print(true).MaxLength(2037)));
                }
                JSONObject header = result["header"];
                inaraEvents = result["events"];
                if (header != null && inaraEvents != null)
                {
                    int eventStatus = 0;
                    header.GetField(ref eventStatus, "eventStatus");
                    if (eventStatus == 200 && inaraEvents.IsArray && inaraEvents.Count > 0)
                    {
                        inaraResultOK = true;
                    }
                }
                if (!inaraResultOK)
                {
                    await context.Channel.SendEmbedAsync("Result not OK! Here the result JSON:", string.Format("```json\n{0}```", result.Print(true).MaxLength(2037)));
                }
            }
            else if (requestResultInara.IsException)
            {
                await context.Channel.SendEmbedAsync(string.Format("Could not connect to Inaras services. Exception Message: `{0}`", requestResultInara.ThrownException.Message), true);
            }
            else
            {
                await context.Channel.SendEmbedAsync(string.Format("Could not connect to Inaras services. HTTP Error Message: `{0} {1}`", (int)requestResultInara.Status, requestResultInara.Status.ToString()), true);
            }
            bool edsmResultOK = false;

            if (requestResultEDSM.IsSuccess)
            {
                JSONObject result = requestResultEDSM.Object;
                if (printJSON)
                {
                    await context.Channel.SendEmbedAsync("EDSM result JSON", string.Format("```json\n{0}```", result.Print(true).MaxLength(2037)));
                }
                if (result.Count > 0)
                {
                    edsmResultOK = true;
                }
            }
            if (inaraResultOK)
            {
                await context.Channel.SendEmbedAsync(FormatMessage_InaraCMDR(inaraEvents[0]));
            }
            if (edsmResultOK)
            {
                await context.Channel.SendEmbedAsync(FormatMessage_EDSMCMDR(cmdrName, requestResultEDSM.Object));
            }
        }
Ejemplo n.º 4
0
        public async Task HandleDistanceCommand(CommandContext context)
        {
            // Parse input systems
            string requestedSystem1 = string.Empty;
            string requestedSystem2 = string.Empty;
            bool   commaEncountered = false;

            for (int i = 1; i < context.ArgCnt; i++)
            {
                string partial = context.Args[i];
                if (partial.EndsWith(','))
                {
                    if (!string.IsNullOrEmpty(requestedSystem1))
                    {
                        requestedSystem1 += '+';
                    }
                    requestedSystem1 += partial.Substring(0, partial.Length - 1);
                    commaEncountered  = true;
                }
                else if (commaEncountered)
                {
                    if (!string.IsNullOrEmpty(requestedSystem2))
                    {
                        requestedSystem2 += '+';
                    }

                    requestedSystem2 += partial;
                }
                else
                {
                    if (!string.IsNullOrEmpty(requestedSystem1))
                    {
                        requestedSystem1 += '+';
                    }
                    requestedSystem1 += partial;
                }
            }

            // two systems found, make the request
            if (commaEncountered)
            {
                string            requestURL    = WebRequestService.EDSM_MultipleSystemsInfo_URL(new string[] { requestedSystem1, requestedSystem2 }, false, true, false, false, false);
                RequestJSONResult requestResult = await WebRequestService.GetWebJSONAsync(requestURL);

                if (requestResult.IsSuccess)
                {
                    if (requestResult.Object.Count > 1)
                    {
                        await context.Channel.SendEmbedAsync(FormatMessage_SystemDistance(requestResult.Object[0], requestResult.Object[1]));
                    }
                    else
                    {
                        if (requestResult.Object.Count > 0)
                        {
                            string systemnameFound = requestResult.Object[0]["name"].str;
                            if (requestedSystem1.Equals(systemnameFound, StringComparison.CurrentCultureIgnoreCase))
                            {
                                await context.Channel.SendEmbedAsync(string.Format("Only found system `{0}`. Could not find system `{1}`!", systemnameFound, requestedSystem2), true);
                            }
                            else
                            {
                                await context.Channel.SendEmbedAsync(string.Format("Only found system `{0}`. Could not find system `{1}`!", systemnameFound, requestedSystem1), true);
                            }
                        }
                        else
                        {
                            await context.Channel.SendEmbedAsync("Could not find both of your mentioned systems!", true);
                        }
                    }
                }
            }
            else
            {
                await context.Channel.SendEmbedAsync("You need to supply two systems, separated by a comma!", true);
            }
        }