diff --git a/DisCatSharp/Entities/Interaction/Components/Button/DiscordLinkButtonComponent.cs b/DisCatSharp/Entities/Interaction/Components/Button/DiscordLinkButtonComponent.cs index 768e6a571..f82d24df5 100644 --- a/DisCatSharp/Entities/Interaction/Components/Button/DiscordLinkButtonComponent.cs +++ b/DisCatSharp/Entities/Interaction/Components/Button/DiscordLinkButtonComponent.cs @@ -1,107 +1,107 @@ // This file is part of the DisCatSharp project, based off DSharpPlus. // // Copyright (c) 2021-2022 AITSYS // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. using DisCatSharp.Enums; using Newtonsoft.Json; namespace DisCatSharp.Entities { /// /// Represents a link button. Clicking a link button does not send an interaction. /// public class DiscordLinkButtonComponent : DiscordComponent { /// /// The url to open when pressing this button. /// [JsonProperty("url", NullValueHandling = NullValueHandling.Ignore)] public string Url { get; set; } /// /// The text to add to this button. If this is not specified, must be. /// [JsonProperty("label", NullValueHandling = NullValueHandling.Ignore)] public string Label { get; set; } /// /// Whether this button can be pressed. /// [JsonProperty("disabled", NullValueHandling = NullValueHandling.Ignore)] public bool Disabled { get; set; } /// /// The emoji to add to the button. Can be used in conjunction with a label, or as standalone. Must be added if label is not specified. /// [JsonProperty("emoji", NullValueHandling = NullValueHandling.Ignore)] public DiscordComponentEmoji Emoji { get; set; } /// /// Gets the style. /// [JsonProperty("style", NullValueHandling = NullValueHandling.Ignore)] - internal int Style { get; } = 5; // Link = 5; Discord throws 400 otherwise // + internal int Style { get; set; } = 5; // Link = 5; Discord throws 400 otherwise // /// /// Enables this component if it was disabled before. /// /// The current component. public DiscordLinkButtonComponent Enable() { this.Disabled = false; return this; } /// /// Disables this component. /// /// The current component. public DiscordLinkButtonComponent Disable() { this.Disabled = true; return this; } /// /// Constructs a new . This type of button does not send back and interaction when pressed. /// /// The url to set the button to. /// The text to display on the button. Can be left blank if is set. /// Whether or not this button can be pressed. /// The emoji to set with this button. This is required if is null or empty. public DiscordLinkButtonComponent(string url, string label, bool disabled = false, DiscordComponentEmoji emoji = null) : this() { this.Url = url; this.Label = label; this.Disabled = disabled; this.Emoji = emoji; } /// /// Initializes a new instance of the class. /// internal DiscordLinkButtonComponent() { this.Type = ComponentType.Button; } } } diff --git a/DisCatSharp/Net/Serialization/DiscordComponentJsonConverter.cs b/DisCatSharp/Net/Serialization/DiscordComponentJsonConverter.cs index 1035b5ce9..0596576bf 100644 --- a/DisCatSharp/Net/Serialization/DiscordComponentJsonConverter.cs +++ b/DisCatSharp/Net/Serialization/DiscordComponentJsonConverter.cs @@ -1,102 +1,93 @@ // This file is part of the DisCatSharp project, based off DSharpPlus. // // Copyright (c) 2021-2022 AITSYS // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. using System; using DisCatSharp.Entities; using DisCatSharp.Enums; using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace DisCatSharp.Net.Serialization { /// /// Represents a discord component json converter. /// internal sealed class DiscordComponentJsonConverter : JsonConverter { /// /// Whether the converter can write. /// public override bool CanWrite => false; /// /// Writes the json. /// /// The writer. /// The value. /// The serializer. public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) => throw new NotImplementedException(); /// /// Reads the json. /// /// The reader. /// The object type. /// The existing value. /// The serializer. public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { if (reader.TokenType == JsonToken.Null) return null; var job = JObject.Load(reader); var type = job["type"]?.ToObject(); if (type == null) throw new ArgumentException($"Value {reader} does not have a component type specifier"); DiscordComponent cmp; - if (type is ComponentType.Button) - { - var style = job["style"]?.ToObject(); - cmp = style switch - { - ButtonStyle.Link => new DiscordLinkButtonComponent(), - _ => new DiscordButtonComponent() - }; - } - else - { - cmp = type switch - { - ComponentType.ActionRow => new DiscordActionRowComponent(), - ComponentType.Select => new DiscordSelectComponent(), - ComponentType.InputText => new DiscordTextComponent(), - _ => new DiscordComponent() { Type = type.Value } - }; - } - // Populate the existing component with the values in the JObject. This avoids a recursive JsonConverter loop - using var jreader = job.CreateReader(); + cmp = type switch + { + ComponentType.ActionRow => new DiscordActionRowComponent(), + ComponentType.Button when (string)job["url"] is not null => new DiscordLinkButtonComponent(), + ComponentType.Button => new DiscordButtonComponent(), + ComponentType.Select => new DiscordSelectComponent(), + ComponentType.InputText => new DiscordTextComponent(), + _ => new DiscordComponent() { Type = type.Value } + }; + + // Populate the existing component with the values in the JObject. This avoids a recursive JsonConverter loop + using var jreader = job.CreateReader(); serializer.Populate(jreader, cmp); return cmp; } /// /// Whether the json can convert. /// /// The object type. public override bool CanConvert(Type objectType) => typeof(DiscordComponent).IsAssignableFrom(objectType); } }