Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Parse and ToJsonString from EmbedBuilder #2669

Closed
3 tasks done
ForceFK opened this issue Apr 18, 2023 · 3 comments · Fixed by #2787
Closed
3 tasks done

[Bug]: Parse and ToJsonString from EmbedBuilder #2669

ForceFK opened this issue Apr 18, 2023 · 3 comments · Fixed by #2787
Assignees
Labels

Comments

@ForceFK
Copy link
Contributor

ForceFK commented Apr 18, 2023

Check The Docs

  • I double checked the docs and couldn't find any useful information.

Verify Issue Source

  • I verified the issue was caused by Discord.Net.

Check your intents

  • I double checked that I have the required intents.

Description

When using the ToJsonString extension, a Cast error is returned Unable to cast object of type 'Discord.API.Embed' to type 'Discord.EmbedType' and when using the Parse, a constructor error is returned Unable to find a constructor to use for type Discord.Embed. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute.

Version

3.10.0

Working Version

No response

Logs

ToJsonString:
System.InvalidCastException: Unable to cast object of type 'Discord.API.Embed' to type 'Discord.EmbedType'. at Discord.Net.Converters.EmbedTypeConverter.WriteJson(JsonWriter writer, Object value, JsonSerializer serializer) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeConvertable(JsonWriter writer, JsonConverter converter, Object value, JsonContract contract, JsonContainerContract collectionContract, JsonProperty containerProperty) at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType) at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType) at Newtonsoft.Json.JsonConvert.SerializeObjectInternal(Object value, Type type, JsonSerializer jsonSerializer) at Newtonsoft.Json.JsonConvert.SerializeObject(Object value, Type type, Formatting formatting, JsonSerializerSettings settings) at Newtonsoft.Json.JsonConvert.SerializeObject(Object value, Formatting formatting, JsonSerializerSettings settings) at Discord.Rest.StringExtensions.ToJsonString(Embed embed, Formatting formatting) at BOT.Commands.Interaction.Modules.Slash.Global.EmbedCreate.EmbedCreateCommand.RunExportButton() in C:\repos\Alfredo\BOT\Commands\Interaction\Modules\Slash\Global\EmbedCreate\ButtonExportCommand.cs:line 27 at Discord.Interactions.Builders.ModuleClassBuilder.<>c__DisplayClass11_0.<<CreateCallback>g__ExecuteCallback|1>d.MoveNext()

Parse:
Newtonsoft.Json.JsonSerializationException: Unable to find a constructor to use for type Discord.Embed. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute. Path 'description', line 1, position 15. at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateNewObject(JsonReader reader, JsonObjectContract objectContract, JsonProperty containerMember, JsonProperty containerProperty, String id, Boolean& createdFromNonDefaultCreator) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent) at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType) at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings) at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings) at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value) at Discord.EmbedBuilder.Parse(String json) at BOT.Commands.Interaction.Modules.Slash.Global.EmbedCreate.EmbedCreateCommand.RunImportModal(ModalEditEmbed modal) in C:\repos\Alfredo\BOT\Commands\Interaction\Modules\Slash\Global\EmbedCreate\ButtonImportCommand.cs:line 39 at Discord.Interactions.Builders.ModuleClassBuilder.<>c__DisplayClass11_0.<<CreateCallback>g__ExecuteCallback|1>d.MoveNext()

Sample

Embed eb = ((SocketMessageComponent)Context.Interaction).Message.Embeds.First();
string json = eb.ToJsonString();
await RespondAsync(embed: new EmbedBuilder() { Description = Format.Code(json,"json")}.Build());
//String json received by modal in variable `Paragraph0`.
EmbedBuilder eb = EmbedBuilder.Parse(modal.Paragraph0);
await DeferAsync();
await Context.Interaction.ModifyOriginalResponseAsync(x => x.Embed = eb.Build());

Packages

Newtonsoft.Json v13.0.2

@ForceFK ForceFK added the bug label Apr 18, 2023
@markski1
Copy link

Hello, could you explain what it is you're trying to achieve with this code, and why you've chosen to do it this way?
It seems like the issue here is that an Embed can't really be constructed from a json, if that's what you're trying to do, which isn't really something documented as far as I can tell

@ForceFK
Copy link
Contributor Author

ForceFK commented Apr 23, 2023

Hello, could you explain what it is you're trying to achieve with this code, and why you've chosen to do it this way? It seems like the issue here is that an Embed can't really be constructed from a json, if that's what you're trying to do, which isn't really something documented as far as I can tell

The extensions I mentioned (Embed.ToJsonString() and EmbedBuilder.Parse()) were included in pull #2284.
As they are native Discord.Net functions and have errors, I reported them.

@quinchs
Copy link
Member

quinchs commented Jun 11, 2023

cc @csmir this is your code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants