Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
namespace Discord;

/// <summary>
/// Represents an unknown message component type that Discord has sent but is not yet supported by the library.
/// </summary>
public class UnknownComponent : IMessageComponent
{
/// <summary>
/// Gets the raw component type value from Discord.
/// </summary>
public int RawType { get; }

/// <inheritdoc/>
public ComponentType Type => (ComponentType)RawType;

/// <inheritdoc/>
public int? Id { get; }

/// <summary>
/// Gets the raw JSON data of this component.
/// </summary>
public string RawJson { get; }

internal UnknownComponent(int rawType, string rawJson, int? id = null)
{
RawType = rawType;
RawJson = rawJson;
Id = id;
}

/// <inheritdoc />
IMessageComponentBuilder IMessageComponent.ToBuilder()
=> throw new System.NotSupportedException("Unknown components cannot be converted to builders.");
}
25 changes: 25 additions & 0 deletions src/Discord.Net.Rest/API/Common/UnknownComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Newtonsoft.Json;

namespace Discord.API
{
internal class UnknownComponent : IMessageComponent
{
[JsonProperty("type")]
public int RawType { get; set; }

public ComponentType Type => (ComponentType)RawType;

[JsonProperty("id")]
public Optional<int> Id { get; set; }

int? IMessageComponent.Id => Id.ToNullable();

public string RawJson { get; set; }

public UnknownComponent() { }

/// <inheritdoc />
IMessageComponentBuilder IMessageComponent.ToBuilder()
=> throw new System.NotSupportedException("Unknown components cannot be converted to builders.");
}
}
7 changes: 7 additions & 0 deletions src/Discord.Net.Rest/Extensions/MessageComponentExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ internal static IMessageComponent ToModel(this IMessageComponent component)

case FileUploadComponent fileUpload:
return new API.FileUploadComponent(fileUpload);

case UnknownComponent unknown:
return new API.UnknownComponent { RawType = unknown.RawType, RawJson = unknown.RawJson, Id = unknown.Id ?? Optional<int>.Unspecified };
}

return null;
Expand Down Expand Up @@ -197,7 +200,11 @@ internal static IMessageComponent ToEntity(this IMessageComponent component)
}

default:
{
if (component is API.UnknownComponent unknown)
return new UnknownComponent(unknown.RawType, unknown.RawJson, unknown.Id.ToNullable());
return null;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
messageComponent = new API.FileUploadComponent();
break;
default:
throw new JsonSerializationException($"Unknown component type value '{typeProperty}' while deserializing message component");
messageComponent = new API.UnknownComponent { RawType = typeProperty, RawJson = jsonObject.ToString() };
break;
}
serializer.Populate(jsonObject.CreateReader(), messageComponent);
return messageComponent;
Expand Down