diff --git a/DisCatSharp/Entities/Interaction/Components/Button/DiscordButtonComponent.cs b/DisCatSharp/Entities/Interaction/Components/Button/DiscordButtonComponent.cs index 6eed6dc4d..53e54557d 100644 --- a/DisCatSharp/Entities/Interaction/Components/Button/DiscordButtonComponent.cs +++ b/DisCatSharp/Entities/Interaction/Components/Button/DiscordButtonComponent.cs @@ -1,118 +1,129 @@ // 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.Enums; using Newtonsoft.Json; namespace DisCatSharp.Entities { /// /// Represents a button that can be pressed. Fires event when pressed. /// public sealed class DiscordButtonComponent : DiscordComponent { /// /// The style of the button. /// [JsonProperty("style", NullValueHandling = NullValueHandling.Ignore)] public ButtonStyle Style { get; internal set; } /// /// The text to apply to the button. If this is not specified becomes required. /// [JsonProperty("label", NullValueHandling = NullValueHandling.Ignore)] public string Label { get; internal set; } /// /// Whether this button can be pressed. /// [JsonProperty("disabled", NullValueHandling = NullValueHandling.Ignore)] public bool Disabled { get; internal 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; internal set; } /// /// Enables this component if it was disabled before. /// /// The current component. public DiscordButtonComponent Enable() { this.Disabled = false; return this; } /// /// Disables this component. /// /// The current component. public DiscordButtonComponent Disable() { this.Disabled = true; return this; } /// /// Constructs a new . /// public DiscordButtonComponent() { this.Type = ComponentType.Button; } /// /// Constructs a new button based on another button. /// /// The button to copy. public DiscordButtonComponent(DiscordButtonComponent other) : this() { this.CustomId = other.CustomId; this.Style = other.Style; this.Label = other.Label; this.Disabled = other.Disabled; this.Emoji = other.Emoji; } /// /// Constructs a new button with the specified options. /// /// The style/color of the button. /// The Id to assign to the button. This is sent back when a user presses it. /// The text to display on the button, up to 80 characters. Can be left blank if is set. /// Whether this button should be initialized as being disabled. User sees a greyed out button that cannot be interacted with. /// The emoji to add to the button. This is required if is empty or null. - public DiscordButtonComponent(ButtonStyle style, string customId, string label, bool disabled = false, DiscordComponentEmoji emoji = null) + /// Is thrown when neither the nor the is set. + public DiscordButtonComponent(ButtonStyle style, string customId = null, string label = null, bool disabled = false, DiscordComponentEmoji emoji = null) { this.Style = style; - this.Label = label; - this.CustomId = customId; + this.CustomId = customId ?? Guid.NewGuid().ToString(); this.Disabled = disabled; - this.Emoji = emoji; + if (emoji != null) + { + this.Label = label; + this.Emoji = emoji; + } + else + { + this.Label = label ?? throw new ArgumentException("Label can only be null if emoji is set."); + this.Emoji = null; + } this.Type = ComponentType.Button; } } } diff --git a/DisCatSharp/Entities/Interaction/Components/Text/DiscordTextComponent.cs b/DisCatSharp/Entities/Interaction/Components/Text/DiscordTextComponent.cs index ed2c7350b..d89993e86 100644 --- a/DisCatSharp/Entities/Interaction/Components/Text/DiscordTextComponent.cs +++ b/DisCatSharp/Entities/Interaction/Components/Text/DiscordTextComponent.cs @@ -1,156 +1,159 @@ // 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.Enums; using Newtonsoft.Json; namespace DisCatSharp.Entities { /// /// Represents a text component that can be submitted. Fires event when submitted. /// public sealed class DiscordTextComponent : DiscordComponent { /// /// The style of the text component. /// [JsonProperty("style", NullValueHandling = NullValueHandling.Ignore)] public TextComponentStyle Style { get; internal set; } /// /// The text description to apply to the text component. /// [JsonProperty("label", NullValueHandling = NullValueHandling.Ignore)] public string Label { get; internal set; } /// /// The placeholder for the text component. /// [JsonProperty("placeholder", NullValueHandling = NullValueHandling.Ignore)] public string Placeholder { get; internal set; } /// /// The pre-filled value for the text component. /// [JsonProperty("value", NullValueHandling = NullValueHandling.Ignore)] public string Value { get; internal set; } /// /// The minimal length of text input. /// Defaults to 0. /// [JsonProperty("min_length", NullValueHandling = NullValueHandling.Ignore)] public int? MinLength { get; internal set; } = 0; /// /// The maximal length of text input. /// [JsonProperty("max_length", NullValueHandling = NullValueHandling.Ignore)] public int? MaxLength { get; internal set; } // NOTE: Probably will be introduced in future /*/// /// Whether this text component can be used. /// [JsonProperty("disabled", NullValueHandling = NullValueHandling.Ignore)] public bool Disabled { get; internal set; }*/ /// /// Whether this text component is required. /// Defaults to true. /// [JsonProperty("required", NullValueHandling = NullValueHandling.Ignore)] public bool Required { get; internal set; } /*/// /// Enables this component if it was disabled before. /// /// The current component. public DiscordTextComponent Enable() { this.Disabled = false; return this; } /// /// Disables this component. /// /// The current component. public DiscordTextComponent Disable() { this.Disabled = true; return this; }*/ /// /// Constructs a new . /// internal DiscordTextComponent() { this.Type = ComponentType.InputText; } /// /// Constucts a new text component based on another text component. /// /// The button to copy. public DiscordTextComponent(DiscordTextComponent other) : this() { this.CustomId = other.CustomId; this.Style = other.Style; this.Label = other.Label; //this.Disabled = other.Disabled; this.MinLength = other.MinLength; this.MaxLength = other.MaxLength; this.Placeholder = other.Placeholder; this.Required = other.Required; this.Value = other.Value; } /// /// Constructs a new text component field with the specified options. /// /// The style of the text component. /// The Id to assign to the text component. This is sent back when a user presses it. - /// The text to display before the text component, up to 80 characters. + /// The text to display before the text component, up to 80 characters. Required, but set to null to avoid breaking change. /// The placeholder for the text input. /// The minimal length of text input. /// The maximal length of text input. /// Whether this text component should be required. /// Pre-filled value for text field. - public DiscordTextComponent(TextComponentStyle style, string customId, string label, string placeholder = null, int? minLength = null, int? maxLength = null, bool required = true, string defaultValue = null) + /// Is thrown when no label is set. + public DiscordTextComponent(TextComponentStyle style, string customId = null, string label = null, string placeholder = null, int? minLength = null, int? maxLength = null, bool required = true, string defaultValue = null) { this.Style = style; - this.Label = label; - this.CustomId = customId; + this.Label = label ?? throw new ArgumentException("A label is required."); + this.CustomId = customId ?? Guid.NewGuid().ToString(); this.MinLength = minLength; this.MaxLength = maxLength; this.Placeholder = placeholder; //this.Disabled = disabled; this.Required = required; this.Value = defaultValue; this.Type = ComponentType.InputText; } } }