diff --git a/DisCatSharp/Entities/Interaction/Components/Button/DiscordButtonComponent.cs b/DisCatSharp/Entities/Interaction/Components/Button/DiscordButtonComponent.cs index 3be8fb42d..fcee64ae5 100644 --- a/DisCatSharp/Entities/Interaction/Components/Button/DiscordButtonComponent.cs +++ b/DisCatSharp/Entities/Interaction/Components/Button/DiscordButtonComponent.cs @@ -1,127 +1,154 @@ // 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. /// 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.CustomId = customId ?? Guid.NewGuid().ToString(); this.Disabled = disabled; 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; } + + /// + /// 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. + /// Is thrown when neither the nor the is set. + public DiscordButtonComponent(ButtonStyle style, string customId = null, string label = null, bool disabled = false, DiscordEmoji emoji = null) + { + this.Style = style; + this.CustomId = customId ?? Guid.NewGuid().ToString(); + this.Disabled = disabled; + if (emoji != null) + { + this.Label = label; + this.Emoji = new DiscordComponentEmoji(emoji); + } + else + { + this.Label = label ?? throw new ArgumentException("Label can only be null if emoji is set."); + this.Emoji = null; + } + this.Type = ComponentType.Button; + } }