protected override async Task Execute(IDMCommandContext context) { string webrequest = WebRequestService.EDSM_MultipleSystemsInfo_URL(new string[] { SystemA_name, SystemB_name }, showId: true, showCoords: true); RequestJSONResult requestResult = await WebRequestService.GetWebJSONAsync(webrequest); if (requestResult.IsSuccess) { if (requestResult.JSON.IsArray && requestResult.JSON.Array.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}```", requestResult.JSON.Build(true).MaxLength(2037))); } EmbedBuilder distanceEmbed = GetDistanceEmbed(requestResult.JSON); await context.Channel.SendEmbedAsync(distanceEmbed); } } else if (requestResult.IsException) { await context.Channel.SendEmbedAsync(string.Format("Could not connect to EDSMs services. Exception Message: `{0}`", requestResult.ThrownException.Message), true); } else { await context.Channel.SendEmbedAsync(string.Format("Could not connect to EDSMs services. HTTP Error: `{0} {1}`", (int)requestResult.Status, requestResult.Status.ToString()), true); } }
protected override async Task Execute(IDMCommandContext context) { string requestSystem = WebRequestService.EDSM_SystemInfo_URL(systemName, true, true, true, true, true); string requestStations = WebRequestService.EDSM_SystemStations_URL(systemName); string requestTraffic = WebRequestService.EDSM_SystemTraffic_URL(systemName); string requestDeaths = WebRequestService.EDSM_SystemDeaths_URL(systemName); if (webRequests) { EmbedBuilder embed = new EmbedBuilder() { Title = "Webrequests", Color = BotCore.EmbedColor, Description = $"[System]({requestSystem}) `{requestSystem}`\n[Stations]({requestStations}) `{requestStations}`\n[Traffic]({requestTraffic}) `{requestTraffic}`\n[Deaths]({requestDeaths}) `{requestDeaths}`" }; await context.Channel.SendEmbedAsync(embed); } RequestJSONResult requestResultSystem = await WebRequestService.GetWebJSONAsync(requestSystem); RequestJSONResult requestResultStations = await WebRequestService.GetWebJSONAsync(requestStations); RequestJSONResult requestResultTraffic = await WebRequestService.GetWebJSONAsync(requestTraffic); RequestJSONResult requestResultDeaths = await WebRequestService.GetWebJSONAsync(requestDeaths); if (requestResultSystem.IsSuccess) { if (requestResultSystem.JSON.IsArray && requestResultSystem.JSON.Array.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.JSON.Build(true).MaxLength(2037))); await context.Channel.SendEmbedAsync("Station Info", string.Format("```json\n{0}```", requestResultStations.JSON.Build(true).MaxLength(2037))); } EmbedBuilder systemEmbed = GetSystemInfoEmbed(requestResultSystem.JSON, requestResultStations.JSON, requestResultTraffic.JSON, requestResultDeaths.JSON, out List <EmbedFieldBuilder> allStations); await context.Channel.SendEmbedAsync(systemEmbed); } } 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: `{0} {1}`", (int)requestResultSystem.Status, requestResultSystem.Status.ToString()), true); } }
public async Task <bool> GetBestStation(string hideMinorFaction = null) { string request = WebRequestService.EDSM_SystemStations_URL(Name); RequestJSONResult result = await WebRequestService.GetWebJSONAsync(request); if (result.IsSuccess) { // Setup Stations if ((result.JSON != null) && result.JSON.TryGetArrayField("stations", out JSONContainer stationsJSON)) { foreach (JSONField stationField in stationsJSON.Array) { if (stationField.IsObject) { StationInfo info = new StationInfo(); if (info.FromJSON(stationField.Container)) { if (string.IsNullOrEmpty(hideMinorFaction) || info.ControllingFaction != hideMinorFaction) { if (BestStation == null) { BestStation = info; } else if (info.IsBetterThan(BestStation)) { BestStation = info; } } } } } return(true); } } return(false); }
protected override async Task Execute(IDMCommandContext context) { EmbedBuilder inaraEmbed; if (!WebRequestService.CanMakeInaraRequests) { inaraEmbed = new EmbedBuilder() { Color = BotCore.ErrorColor, Title = INARAREQUESTFAILURE, Description = "Can not make inara requests, as appname and/or apikey config variables are not set!" }; } else { JSONContainer inaraRequestcontent = WebRequestService.Inara_CMDR_Profile(cmdrName); if (printJson) { await context.Channel.SendEmbedAsync(new EmbedBuilder() { Title = "Request JSON sending to Inara", Color = BotCore.EmbedColor, Description = $"```json\n{inaraRequestcontent.Build(true).MaxLength(EmbedHelper.EMBEDDESCRIPTION_MAX - 11)}```" }); } RequestJSONResult requestResultInara = await WebRequestService.GetWebJSONAsync("https://inara.cz/inapi/v1/", inaraRequestcontent); if (requestResultInara.IsSuccess && string.IsNullOrEmpty(requestResultInara.jsonParseError)) { inaraEmbed = GetInaraCMDREmbed(requestResultInara.JSON, cmdrName); if (printJson) { await context.Channel.SendEmbedAsync(new EmbedBuilder() { Title = "Result JSON from Inara", Color = BotCore.EmbedColor, Description = $"```json\n{requestResultInara.JSON.Build(true).MaxLength(EmbedHelper.EMBEDDESCRIPTION_MAX - 11)}```" }); } } else if (!string.IsNullOrEmpty(requestResultInara.jsonParseError)) { inaraEmbed = new EmbedBuilder() { Color = BotCore.ErrorColor, Title = INARAREQUESTFAILURE, Description = $"JSON parse error: `{requestResultInara.jsonParseError}`!" }; } else if (requestResultInara.IsException) { inaraEmbed = new EmbedBuilder() { Color = BotCore.ErrorColor, Title = INARAREQUESTFAILURE, Description = $"Could not connect to Inaras services. Exception Message: `{requestResultInara.ThrownException.Message}`" }; } else { inaraEmbed = new EmbedBuilder() { Color = BotCore.ErrorColor, Title = INARAREQUESTFAILURE, Description = $"Could not connect to Inaras services. HTTP Error Message: `{(int)requestResultInara.Status} {requestResultInara.Status}`" }; } } EmbedBuilder edsmEmbed; RequestJSONResult requestResultEDSM = await WebRequestService.GetWebJSONAsync(WebRequestService.EDSM_Commander_Location(cmdrName, true, false)); if (requestResultEDSM.IsSuccess && string.IsNullOrEmpty(requestResultEDSM.jsonParseError)) { edsmEmbed = GetEDSMCMDREmbed(requestResultEDSM.JSON, cmdrName); if (printJson) { await context.Channel.SendEmbedAsync(new EmbedBuilder() { Title = "Result JSON from EDSM", Color = BotCore.EmbedColor, Description = $"```json\n{requestResultEDSM.JSON.Build(true).MaxLength(EmbedHelper.EMBEDDESCRIPTION_MAX - 11)}```" }); } } else if (!string.IsNullOrEmpty(requestResultEDSM.jsonParseError)) { edsmEmbed = new EmbedBuilder() { Color = BotCore.ErrorColor, Title = EDSMREQUESTFAILURE, Description = $"JSON parse error: `{requestResultEDSM.jsonParseError}`!" }; } else if (requestResultEDSM.IsException) { edsmEmbed = new EmbedBuilder() { Color = BotCore.ErrorColor, Title = EDSMREQUESTFAILURE, Description = $"Could not connect to Inaras services. Exception Message: `{requestResultEDSM.ThrownException.Message}`" }; } else { edsmEmbed = new EmbedBuilder() { Color = BotCore.ErrorColor, Title = EDSMREQUESTFAILURE, Description = $"Could not connect to Inaras services. HTTP Error Message: `{(int)requestResultEDSM.Status} {requestResultEDSM.Status}`" }; } inaraEmbed.Footer = new EmbedFooterBuilder() { Text = "Inara" }; edsmEmbed.Footer = new EmbedFooterBuilder() { Text = "EDSM" }; await context.Channel.SendEmbedAsync(inaraEmbed); await context.Channel.SendEmbedAsync(edsmEmbed); }
protected override async Task Execute(IDMCommandContext context) { string radiusRequest = WebRequestService.EDSM_MultipleSystemsRadius(SystemName, 50); RequestJSONResult requestResultRadius = await WebRequestService.GetWebJSONAsync(radiusRequest); if (!requestResultRadius.IsSuccess) { if (requestResultRadius.IsException) { await context.Channel.SendEmbedAsync(string.Format("Could not connect to EDSMs services. Exception Message: `{0}`", requestResultRadius.ThrownException.Message), true); } else { await context.Channel.SendEmbedAsync(string.Format("Could not connect to EDSMs services. HTTP Error: `{0} {1}`", (int)requestResultRadius.Status, requestResultRadius.Status.ToString()), true); } return; } if (requestResultRadius.JSON.IsObject) { await context.Channel.SendEmbedAsync("System not found in database!", true); return; } Dictionary <string, System> systemInfos = GetNames(requestResultRadius.JSON); List <System> allSystems = new List <System>(systemInfos.Values); foreach (System system in allSystems) { system.SetETTA(JumpRange); } allSystems.Sort(new SystemComparer() { RawDistance = true }); const int requestCnt = 20; System[] requestSystems = new System[requestCnt]; allSystems.CopyTo(0, requestSystems, 0, requestCnt); string infoRequest = WebRequestService.EDSM_MultipleSystemsInfo_URL(requestSystems.Select(system => { return(system.Name); }), true, true, true, true); RequestJSONResult requestResultInfo = await WebRequestService.GetWebJSONAsync(infoRequest); if (!requestResultInfo.IsSuccess) { if (requestResultInfo.IsException) { await context.Channel.SendEmbedAsync(string.Format("Could not connect to EDSMs services. Exception Message: `{0}`", requestResultInfo.ThrownException.Message), true); } else { await context.Channel.SendEmbedAsync(string.Format("Could not connect to EDSMs services. HTTP Error: `{0} {1}`", (int)requestResultInfo.Status, requestResultInfo.Status.ToString()), true); } return; } System targetSystem = default; if (requestResultInfo.JSON.IsArray) { foreach (JSONField systemField in requestResultInfo.JSON.Array) { if (systemField.IsObject) { if (systemField.Container.TryGetField("name", out string name)) { if (systemInfos.TryGetValue(name, out System system)) { system.FromJSON(systemField.Container); if (system.Distance == 0) { targetSystem = system; } } } } } } if (targetSystem.Name == null) { await context.Channel.SendEmbedAsync(string.Format("Internal Error! " + Macros.GetCodeLocation(), true)); return; } string hideFaction = Mode == CommandMode.Crime ? targetSystem.Name : null; List <System> validSystems = new List <System>(); foreach (System system in requestSystems) { if (await system.GetBestStation()) { system.SetETTA(JumpRange); validSystems.Add(system); } } validSystems.Sort(new SystemComparer()); EmbedBuilder embed = new EmbedBuilder() { Author = new EmbedAuthorBuilder() { Url = targetSystem.GetEDSM_URL(), Name = $"Suggestions for a temporary base near {targetSystem.Name}" }, Color = BotCore.EmbedColor }; for (int i = 0; i < 5 && i < validSystems.Count; i++) { System system = validSystems[i]; embed.AddField($"\"{system.Name}\" - \"{system.BestStation.Name}\"", $"{(system.RequirePermit ? $"{UnicodeEmoteService.Warning} Permit: `{system.PermitName}` " : string.Empty)} {system.BestStation.Services_Link}"); } await context.Channel.SendEmbedAsync(embed); }