Skip to content

Commit ad8182f

Browse files
authored
Added UnknownComponent classes, and support to MessageComponentConverter and MessageComponentExtensions (#3207)
1 parent 161a91e commit ad8182f

File tree

4 files changed

+68
-1
lines changed

4 files changed

+68
-1
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespace Discord;
2+
3+
/// <summary>
4+
/// Represents an unknown message component type that Discord has sent but is not yet supported by the library.
5+
/// </summary>
6+
public class UnknownComponent : IMessageComponent
7+
{
8+
/// <summary>
9+
/// Gets the raw component type value from Discord.
10+
/// </summary>
11+
public int RawType { get; }
12+
13+
/// <inheritdoc/>
14+
public ComponentType Type => (ComponentType)RawType;
15+
16+
/// <inheritdoc/>
17+
public int? Id { get; }
18+
19+
/// <summary>
20+
/// Gets the raw JSON data of this component.
21+
/// </summary>
22+
public string RawJson { get; }
23+
24+
internal UnknownComponent(int rawType, string rawJson, int? id = null)
25+
{
26+
RawType = rawType;
27+
RawJson = rawJson;
28+
Id = id;
29+
}
30+
31+
/// <inheritdoc />
32+
IMessageComponentBuilder IMessageComponent.ToBuilder()
33+
=> throw new System.NotSupportedException("Unknown components cannot be converted to builders.");
34+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Newtonsoft.Json;
2+
3+
namespace Discord.API
4+
{
5+
internal class UnknownComponent : IMessageComponent
6+
{
7+
[JsonProperty("type")]
8+
public int RawType { get; set; }
9+
10+
public ComponentType Type => (ComponentType)RawType;
11+
12+
[JsonProperty("id")]
13+
public Optional<int> Id { get; set; }
14+
15+
int? IMessageComponent.Id => Id.ToNullable();
16+
17+
public string RawJson { get; set; }
18+
19+
public UnknownComponent() { }
20+
21+
/// <inheritdoc />
22+
IMessageComponentBuilder IMessageComponent.ToBuilder()
23+
=> throw new System.NotSupportedException("Unknown components cannot be converted to builders.");
24+
}
25+
}

src/Discord.Net.Rest/Extensions/MessageComponentExtension.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ internal static IMessageComponent ToModel(this IMessageComponent component)
4747

4848
case FileUploadComponent fileUpload:
4949
return new API.FileUploadComponent(fileUpload);
50+
51+
case UnknownComponent unknown:
52+
return new API.UnknownComponent { RawType = unknown.RawType, RawJson = unknown.RawJson, Id = unknown.Id ?? Optional<int>.Unspecified };
5053
}
5154

5255
return null;
@@ -197,7 +200,11 @@ internal static IMessageComponent ToEntity(this IMessageComponent component)
197200
}
198201

199202
default:
203+
{
204+
if (component is API.UnknownComponent unknown)
205+
return new UnknownComponent(unknown.RawType, unknown.RawJson, unknown.Id.ToNullable());
200206
return null;
207+
}
201208
}
202209
}
203210

src/Discord.Net.Rest/Net/Converters/MessageComponentConverter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
6969
messageComponent = new API.FileUploadComponent();
7070
break;
7171
default:
72-
throw new JsonSerializationException($"Unknown component type value '{typeProperty}' while deserializing message component");
72+
messageComponent = new API.UnknownComponent { RawType = typeProperty, RawJson = jsonObject.ToString() };
73+
break;
7374
}
7475
serializer.Populate(jsonObject.CreateReader(), messageComponent);
7576
return messageComponent;

0 commit comments

Comments
 (0)