-
Notifications
You must be signed in to change notification settings - Fork 1
/
EmbedJsonConverter.cs
82 lines (72 loc) · 2.94 KB
/
EmbedJsonConverter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using Discord;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
namespace RecklessBoon.MacroDeck.Discord
{
class EmbedJsonConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(Embed));
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
// Load the JSON for the Result into a JObject
JObject jo = JObject.Load(reader);
// Read the properties which will be used as constructor parameters
EmbedAuthorBuilder authorBuilder = null;
if (jo.ContainsKey("Author") && !String.IsNullOrEmpty(jo["Author"].ToString()))
{
authorBuilder = new EmbedAuthorBuilder()
{
Name = jo["Author"]["Name"].ToString(),
Url = jo["Author"]["Url"].ToString(),
IconUrl = jo["Author"]["IconUrl"].ToString()
};
}
string title = (string)jo["Title"];
string url = (string)jo["Url"];
string description = (string)jo["Description"];
string thumbnailUrl = jo.ContainsKey("Thumbnail") ? (string)jo["Thumbnail"]["Url"] : null;
string imageUrl = jo.ContainsKey("Image") ? (string)jo["Image"]["Url"] : null;
Color? color = null;
if (jo.ContainsKey("Color") && !String.IsNullOrEmpty(jo["Color"].ToString()))
{
color = new Color(int.Parse(jo["Color"]["R"].ToString()), int.Parse(jo["Color"]["G"].ToString()), int.Parse(jo["Color"]["B"].ToString()));
}
EmbedFooterBuilder footerBuilder = null;
if (jo.ContainsKey("Footer") && !String.IsNullOrEmpty(jo["Footer"].ToString()))
{
footerBuilder = new EmbedFooterBuilder()
{
Text = jo["Footer"]["Text"].ToString(),
IconUrl = jo["Footer"]["IconUrl"].ToString()
};
}
var builder = new EmbedBuilder()
{
Author = authorBuilder,
Title = title,
Url = url,
Description = description,
ThumbnailUrl = thumbnailUrl,
ImageUrl = imageUrl,
Color = color,
Footer = footerBuilder,
};
// Construct the Result object using the non-default constructor
// (If anything else needs to be populated on the result object, do that here)
// Return the result
return builder.Build();
}
public override bool CanWrite
{
get { return false; }
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
}