C
C#10mo ago
kyeede

Parse embed from Json into EmbedBuilderUtils

Hello! :D For my own convenience and to not clog up my entire class file with endless amounts of lines. I created a json file that holds numerous of preset embeds that I wanna use. However, I'm stuck on an issue and I certainly cannot seem to figure out the issue whatsoever.
{
"embeds": {
"first_embed": {
"title": "First Embed",
"description": "This is the description for the first embed.",
"color": 16711680,
"author": {
"name": "Author Name",
},
"footer": {
"text": "Footer Text"
}
},
"second_embed": {
"title": "Second Embed",
"description": "This is the description for the second embed.",
"color": 16711680
}
}
}
{
"embeds": {
"first_embed": {
"title": "First Embed",
"description": "This is the description for the first embed.",
"color": 16711680,
"author": {
"name": "Author Name",
},
"footer": {
"text": "Footer Text"
}
},
"second_embed": {
"title": "Second Embed",
"description": "This is the description for the second embed.",
"color": 16711680
}
}
}
i simply want to parse the embed into a utility that is named EmbedBuilderUtils which is provided by Discord.NET. It basically goes like this
_ = EmbedBuilderUtils.TryParse(Configuration.GetSection("embeds").GetValue<string>("first_embed"), out var builder);

await ReplyAsync(null, false, embed: builder.Build());
_ = EmbedBuilderUtils.TryParse(Configuration.GetSection("embeds").GetValue<string>("first_embed"), out var builder);

await ReplyAsync(null, false, embed: builder.Build());
This should technically speaking work? unless there is another way to parse first_embed's object entirely. I'm quite lost on this. Any help is appreciated!
19 Replies
kyeede
kyeedeOP10mo ago
To give an insight on the error,
Message: The server responded with error 50035: Invalid Form Body
Inner Errors:
BASE_TYPE_REQUIRED: This field is required
Message: The server responded with error 50035: Invalid Form Body
Inner Errors:
BASE_TYPE_REQUIRED: This field is required
there could be two possibilities, something inside the embed is missing a field OR the parsing is not done correctly and it isn't retrieving the entire object which I assume is GetValue.
Angius
Angius10mo ago
first_embed value is not a string It's an object
kyeede
kyeedeOP10mo ago
Correct, in my previous code, one that actually worked, I did something similar by reading the json and then covnerting it to an object like ToObject<string>()
Angius
Angius10mo ago
Why not use a Dictionary<string, Embed> for the embeds type? Then you can re-serialize the selected embed Or just send it straight with PostAsJsoNAsync()
kyeede
kyeedeOP10mo ago
I had actually tried that but presumably not correctly wouldn't it be
var embeds = JsonSerializer.Deserialize<Dictionary<string, Embed>>("json_path");

var getEmbed = JsonSerializer.Serialize(embeds["first_embed"]);
var embeds = JsonSerializer.Deserialize<Dictionary<string, Embed>>("json_path");

var getEmbed = JsonSerializer.Serialize(embeds["first_embed"]);
Angius
Angius10mo ago
You could bind the embeds section of the configuration to Dictionary<string, Embed>
kyeede
kyeedeOP10mo ago
ah, so var embeds = JsonSerializer.Deserialize<Dictionary<string, Embed>>(Configuration.GetSection("embeds");
kyeede
kyeedeOP10mo ago
I see,
var embeds = new Dictionary<string, Embed>();
Configuration.GetSection("embeds").Bind(embeds);
var embeds = new Dictionary<string, Embed>();
Configuration.GetSection("embeds").Bind(embeds);
Angius
Angius10mo ago
Something like that
kyeede
kyeedeOP10mo ago
so ultimately, this is what I shoud have
var embeds = Configuration.GetSection("embeds").Get<Dictionary<string, Embed>>();

_ = EmbedBuilderUtils.TryParse(JsonConvert.SerializeObject(embeds["first_embed"]), out var builder);
var embeds = Configuration.GetSection("embeds").Get<Dictionary<string, Embed>>();

_ = EmbedBuilderUtils.TryParse(JsonConvert.SerializeObject(embeds["first_embed"]), out var builder);
im not sure if Get can be directly used as a dictionary instead of using bind?
Angius
Angius10mo ago
If it works, then it works I'd get rid of Newtonsoft And I'd use the return of your TryParse method But besides that LGTM
kyeede
kyeedeOP10mo ago
the TryParse expects a string (json)
Angius
Angius10mo ago
So? Use System.Text.Json is what I mean
kyeede
kyeedeOP10mo ago
assuming that I deserialize everything and pass it through as is
Angius
Angius10mo ago
Get rid of Newtonsoft.Json
kyeede
kyeedeOP10mo ago
ah ah, i have never worked with System.Text.Json as much but I am sure it isn't much of a difference so switching over shouldn't be too bothersome
Angius
Angius10mo ago
You seem to have used STJ here just fine
kyeede
kyeedeOP10mo ago
yeah.. i have been constantly mixing them up since I had both depedencies in the class but i ultimately got rid of newtonsoft because text.json had asynchronous functions I feel like would be better at not blocking a handler it seems that the first_embed entry is not found at all despite existing
public static string GetEmbed(IConfiguration configuration, string embedKey)
{
var GetEmbeds = configuration.GetSection("embeds").Get<Dictionary<string, Embed>>();
var EmbedJson = JsonSerializer.Serialize(GetEmbeds[embedKey]);

if (GetEmbeds != null && GetEmbeds.ContainsKey(embedKey))
return EmbedJson;

else
return $"Embed {embedKey} was not found in the Configuration.";
}
public static string GetEmbed(IConfiguration configuration, string embedKey)
{
var GetEmbeds = configuration.GetSection("embeds").Get<Dictionary<string, Embed>>();
var EmbedJson = JsonSerializer.Serialize(GetEmbeds[embedKey]);

if (GetEmbeds != null && GetEmbeds.ContainsKey(embedKey))
return EmbedJson;

else
return $"Embed {embedKey} was not found in the Configuration.";
}
{
"embeds": {
"first_embed": {
"title": "First Embed",
"description": "This is the description for the first embed.",
"color": 16711680,
"author": {
"name": "Author Name"
},
"footer": {
"text": "Footer Text"
}
},
"second_embed": {
"title": "Second Embed",
"description": "This is the description for the second embed.",
"color": 16711680
}
}
}
{
"embeds": {
"first_embed": {
"title": "First Embed",
"description": "This is the description for the first embed.",
"color": 16711680,
"author": {
"name": "Author Name"
},
"footer": {
"text": "Footer Text"
}
},
"second_embed": {
"title": "Second Embed",
"description": "This is the description for the second embed.",
"color": 16711680
}
}
}
resolved the issue, however, the issue that I absolutely hate is
[Exception] [02-19-2024 07-40-55 AM] An error has occurred while performing an operation.
Line: 45
Column: 17
Filename: Test.cs
ILOffset: 203
Method: Void MoveNext()
Message: The server responded with error 50035: Invalid Form Body
Inner Errors:
BASE_TYPE_REQUIRED: This field is required
[Exception] [02-19-2024 07-40-55 AM] An error has occurred while performing an operation.
Line: 45
Column: 17
Filename: Test.cs
ILOffset: 203
Method: Void MoveNext()
Message: The server responded with error 50035: Invalid Form Body
Inner Errors:
BASE_TYPE_REQUIRED: This field is required
Want results from more Discord servers?
Add your server