Skip to content

Commit

Permalink
little bit of cleaning up
Browse files Browse the repository at this point in the history
  • Loading branch information
Connor committed Aug 26, 2018
1 parent 72164c0 commit be42a51
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 14 deletions.
14 changes: 4 additions & 10 deletions extension/DiscordEmbedBuilder/Discord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ internal static async Task HandleRequest(string[] args)
Types.EmbedsArray embeds = Converter.DeserializeObject<Types.EmbedsArray>(args[5]);

// Discord 2000 character limit
if (content.Length > 1999)
content = content.Substring(0, 1999);
if (content.Length > 1999) content = content.Substring(0, 1999);

// Bare bones
JObject package = new JObject(
Expand All @@ -45,8 +44,7 @@ internal static async Task HandleRequest(string[] args)
Types.EmbedArray embed = embedList.ElementAt(i);
if (embed == null) break;
JObject embedObject = BuildEmbedObject(embed);
if (embedObject.Count > 0)
embedProperty.Add(embedObject);
if (embedObject.Count > 0) embedProperty.Add(embedObject);
}
if (embedProperty.Count() > 0) package.Add(new JProperty("embeds", embedProperty));

Expand All @@ -56,9 +54,7 @@ internal static async Task HandleRequest(string[] args)
APIClient.BaseAddress = new Uri("https://discordapp.com/api/webhooks/");
APIClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await APIClient.PostAsync(url, new StringContent(JsonConvert.SerializeObject(package), Encoding.UTF8, "application/json"));
string responseStr = await Tools.BuildAsyncReply(response.Content);
if (responseStr.Length > 0)
Tools.Logger(null, $"APIRET: {responseStr}");
await Tools.LogAsyncReply(response.Content);
}
}
catch (Exception e)
Expand All @@ -80,14 +76,13 @@ private static JObject BuildEmbedObject(Types.EmbedArray embed)
Types.EmbedFields embedFields = embed.fields;
Types.EmbedFooter embedFooter = embed.footer;

// Adding the basics
embed.title = RemoveReservedString(embed.title);
embed.description = RemoveReservedString(embed.description);
embed.url = RemoveReservedString(embed.url);
embed.color = RemoveReservedString(embed.color);
embed.thumbnail = RemoveReservedString(embed.thumbnail);
embed.image = RemoveReservedString(embed.image);

// Adding the basics
if (embed.title.Length > 0) embedObject.Add(new JProperty("title", embed.title));
if (embed.description.Length > 0) embedObject.Add(new JProperty("description", embed.description));
if (embed.url.StartsWith("https://")) embedObject.Add(new JProperty("url", embed.url));
Expand All @@ -109,7 +104,6 @@ private static JObject BuildEmbedObject(Types.EmbedArray embed)
embedObject.Add(new JProperty("author", embedObjectAuthor));
}


embedFooter.text = RemoveReservedString(embedFooter.text);
embedFooter.icon_url = RemoveReservedString(embedFooter.icon_url);
if (embedFooter.text.Length > 0)
Expand Down
7 changes: 3 additions & 4 deletions extension/DiscordEmbedBuilder/Tools.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Newtonsoft.Json.Linq;
using System;
using System;
using System.IO;
using System.Net.Http;
using System.Reflection;
Expand Down Expand Up @@ -46,14 +45,14 @@ internal static string GenTimeEncode()
return id;
}

internal static async Task<string> BuildAsyncReply(HttpContent responseContent)
internal static async Task LogAsyncReply(HttpContent responseContent)
{
string readResponse = "";
using (var reader = new StreamReader(await responseContent.ReadAsStreamAsync()))
{
readResponse += await reader.ReadToEndAsync();
}
return readResponse;
if (readResponse.Length > 0) Logger(null, $"AsyncRet: {readResponse}");
}
}
}

0 comments on commit be42a51

Please sign in to comment.