Files
Discord.Net/src/Discord.Net.Rest/Net/Converters/MessageComponentConverter.cs
Mihail Gribkov a4760144a5 [Feature] Modal refactoring & select menu support (#3172)
* add missing xmldoc + new component type

* add API models

* label builder

* most POG commit ever

* another pog commit (add file upload component)

* add a constructor & missing extension methods

* refactor modal builder

* Fix build errors

* Add xml docs and fluent methods to new components

* Fix naming

* Set default value of `FileUploadComponentBuilder.IsRequired` to `true`

* xmldoc fixes

* Support receiving modal interactions with new components & implement resolved data

* Add missing text display methods to modal builder

* Update src/Discord.Net.Rest/API/Common/FileUploadComponent.cs

Co-authored-by: Quin Lynch <49576606+quinchs@users.noreply.github.com>

---------

Co-authored-by: Quin Lynch <49576606+quinchs@users.noreply.github.com>
Co-authored-by: d4n <dan3436@hotmail.com>
2025-11-09 13:57:57 +03:00

79 lines
3.2 KiB
C#

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
namespace Discord.Net.Converters
{
internal class MessageComponentConverter : JsonConverter
{
public static MessageComponentConverter Instance => new ();
public override bool CanRead => true;
public override bool CanWrite => false;
public override bool CanConvert(Type objectType) => true;
public override void WriteJson(JsonWriter writer,
object value, JsonSerializer serializer)
{
serializer.Serialize(writer, value);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var jsonObject = JObject.Load(reader);
var typeProperty = jsonObject["type"].Value<int>();
IMessageComponent messageComponent;
switch ((ComponentType)typeProperty)
{
case ComponentType.ActionRow:
messageComponent = new API.ActionRowComponent();
break;
case ComponentType.Button:
messageComponent = new API.ButtonComponent();
break;
case ComponentType.SelectMenu:
case ComponentType.ChannelSelect:
case ComponentType.MentionableSelect:
case ComponentType.RoleSelect:
case ComponentType.UserSelect:
messageComponent = new API.SelectMenuComponent(){Type = (ComponentType)typeProperty};
break;
case ComponentType.TextInput:
messageComponent = new API.TextInputComponent();
break;
case ComponentType.TextDisplay:
messageComponent = new API.TextDisplayComponent();
break;
case ComponentType.Thumbnail:
messageComponent = new API.ThumbnailComponent();
break;
case ComponentType.Section:
messageComponent = new API.SectionComponent();
break;
case ComponentType.MediaGallery:
messageComponent = new API.MediaGalleryComponent();
break;
case ComponentType.Separator:
messageComponent = new API.SeparatorComponent();
break;
case ComponentType.File:
messageComponent = new API.FileComponent();
break;
case ComponentType.Container:
messageComponent = new API.ContainerComponent();
break;
case ComponentType.Label:
messageComponent = new API.LabelComponent();
break;
case ComponentType.FileUpload:
messageComponent = new API.FileUploadComponent();
break;
default:
throw new JsonSerializationException($"Unknown component type value '{typeProperty}' while deserializing message component");
}
serializer.Populate(jsonObject.CreateReader(), messageComponent);
return messageComponent;
}
}
}