public static async Task <RequestJSONResult> GetWebJSONAsync(string url, JSONContainer requestinfo) { RequestJSONResult loadresult = new RequestJSONResult(); try { using (HttpRequestMessage requestmessage = new HttpRequestMessage(HttpMethod.Post, url)) { requestmessage.Version = new Version(1, 1); string requestcontent = requestinfo.Build(); requestmessage.Content = new StringContent(requestcontent, Encoding.UTF8, "application/json"); using (HttpResponseMessage responsemessage = await httpClient.SendAsync(requestmessage)) { loadresult.Status = responsemessage.StatusCode; loadresult.IsSuccess = responsemessage.IsSuccessStatusCode; if (responsemessage.IsSuccessStatusCode) { loadresult.rawData = await responsemessage.Content.ReadAsStringAsync(); JSONContainer.TryParse(loadresult.rawData, out loadresult.JSON, out loadresult.jsonParseError); } } } } catch (Exception e) { loadresult.IsException = true; loadresult.ThrownException = e; } return(loadresult); }
public override string ToString() { switch (Type) { case BotVarType.Undefined: return($"Type: `{Type}`"); case BotVarType.UInt64: return($"Type: `{Type}`\nValue: `{UInt64}`"); case BotVarType.Int64: return($"Type: `{Type}`\nValue: `{Int64}`"); case BotVarType.Float64: return($"Type: `{Type}`\nValue: `{Float64}`"); case BotVarType.String: return($"Type: `{Type}`\nValue: ```{String}```"); case BotVarType.Bool: return($"Type: `{Type}`\nValue: `{Bool}`"); case BotVarType.Generic: return($"Type: `{Type}`\nValue: ```{Generic.Build(true)}```"); default: return(base.ToString()); } }
public static async Task WriteJSONObjectToFile(string path, JSONContainer json) { try { await File.WriteAllTextAsync(path, json.Build(), Encoding.UTF8); } catch (Exception e) { await YNBBotCore.Logger(new Discord.LogMessage(Discord.LogSeverity.Critical, "Save/Load", "Failed to save " + path, e)); } }
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, object argObject) { ArgumentContainer args = argObject as ArgumentContainer; JSONContainer json = EmbedHelper.GetJSONFromUserMessage(args.message); IReadOnlyCollection <IAttachment> attachments = args.message.Attachments; bool pretty = args.options.Contains(ExecutionOptions.pretty); bool remove = args.options.Contains(ExecutionOptions.remove); EmbedBuilder embed; if (pretty) { embed = new EmbedBuilder() { Color = BotCore.EmbedColor, Title = $"Message JSON for original message in {args.guild.Name} - {args.channel.Name} by {args.message.Author}", Description = ("```json\n" + json.Build(true).Replace("```", "[3`]")).MaxLength(EmbedHelper.EMBEDDESCRIPTION_MAX - 8) + "```", }; } else { embed = new EmbedBuilder() { Color = BotCore.EmbedColor, Title = $"Message JSON for original message in {args.guild.Name} - {args.channel.Name} by {args.message.Author}", Footer = new EmbedFooterBuilder() { Text = json.Build(false).MaxLength(EmbedHelper.EMBEDFOOTERTEXT_MAX) } }; } if (attachments.Count > 0) { StringBuilder attachments_str = new StringBuilder(); foreach (IAttachment attachment in attachments) { if (attachment.Url.IsValidImageURL() && string.IsNullOrEmpty(embed.ImageUrl)) { embed.ImageUrl = attachment.Url; } attachments_str.AppendLine($"[{attachment.Filename}]({attachment.Url})"); } embed.AddField("Attachments", attachments_str.ToString()); } await context.Channel.SendEmbedAsync(embed); if (remove) { try { await args.channel.DeleteMessageAsync(args.message); } catch (Exception e) { await context.Channel.SendEmbedAsync($"Failed to remove the message. Probably missing permissions! Exception: {e.GetType()} - {e.Message}", true); } } }