From 792a280c3e920afed0b46bd7ab7bd51e7b4730e9 Mon Sep 17 00:00:00 2001 From: Lala Sabathil Date: Wed, 20 Sep 2023 23:03:24 +0200 Subject: [PATCH] feat: default select menu values --- .../Select/DiscordBaseSelectComponent.cs | 17 +++- .../Select/DiscordChannelSelectComponent.cs | 12 +-- .../DiscordMentionableSelectComponent.cs | 13 ++- .../Select/DiscordRoleSelectComponent.cs | 12 ++- .../Select/DiscordSelectDefaultValue.cs | 86 +++++++++++++++++++ .../Select/DiscordUserSelectComponent.cs | 12 ++- 6 files changed, 133 insertions(+), 19 deletions(-) create mode 100644 DisCatSharp/Entities/Interaction/Components/Select/DiscordSelectDefaultValue.cs diff --git a/DisCatSharp/Entities/Interaction/Components/Select/DiscordBaseSelectComponent.cs b/DisCatSharp/Entities/Interaction/Components/Select/DiscordBaseSelectComponent.cs index b3f983aa8..7e7edec4d 100644 --- a/DisCatSharp/Entities/Interaction/Components/Select/DiscordBaseSelectComponent.cs +++ b/DisCatSharp/Entities/Interaction/Components/Select/DiscordBaseSelectComponent.cs @@ -21,6 +21,8 @@ // SOFTWARE. using System; +using System.Collections.Generic; +using System.Linq; using DisCatSharp.Enums; @@ -63,6 +65,11 @@ public class DiscordBaseSelectComponent : DiscordComponent [JsonProperty("label", NullValueHandling = NullValueHandling.Ignore)] public string Label { get; internal set; } = null; + /// + /// The default values of this select menu. + /// + [JsonProperty("default_values", NullValueHandling = NullValueHandling.Ignore)] + public List? DefaultValues { get; internal set; } = null; /// /// Constructs a new . @@ -73,7 +80,8 @@ public class DiscordBaseSelectComponent : DiscordComponent /// Minimum count of selectable options. /// Maximum count of selectable options. /// Whether this select component should be initialized as being disabled. User sees a greyed out select component that cannot be interacted with. - internal DiscordBaseSelectComponent(ComponentType type, string placeholder, string customId = null, int minOptions = 1, int maxOptions = 1, bool disabled = false) + /// The default values of this select menu. Not valid for . + internal DiscordBaseSelectComponent(ComponentType type, string placeholder, string customId = null, int minOptions = 1, int maxOptions = 1, bool disabled = false, IEnumerable? defaultValues = null) { this.Type = type; this.CustomId = customId ?? Guid.NewGuid().ToString(); ; @@ -81,6 +89,8 @@ internal DiscordBaseSelectComponent(ComponentType type, string placeholder, stri this.Placeholder = placeholder; this.MinimumSelectedValues = minOptions; this.MaximumSelectedValues = maxOptions; + if (this.Type != ComponentType.ActionRow && this.Type != ComponentType.InputText && this.Type != ComponentType.Button && this.Type != ComponentType.StringSelect) + this.DefaultValues = defaultValues?.ToList(); } /// @@ -93,7 +103,8 @@ internal DiscordBaseSelectComponent(ComponentType type, string placeholder, stri /// Minimum count of selectable options. /// Maximum count of selectable options. /// Whether this select component should be initialized as being disabled. User sees a greyed out select component that cannot be interacted with. - internal DiscordBaseSelectComponent(ComponentType type, string label, string placeholder, string customId = null, int minOptions = 1, int maxOptions = 1, bool disabled = false) + /// The default values of this select menu. Not valid for . + internal DiscordBaseSelectComponent(ComponentType type, string label, string placeholder, string customId = null, int minOptions = 1, int maxOptions = 1, bool disabled = false, IEnumerable? defaultValues = null) { this.Type = type; this.Label = label; @@ -102,6 +113,8 @@ internal DiscordBaseSelectComponent(ComponentType type, string label, string pla this.Placeholder = placeholder; this.MinimumSelectedValues = minOptions; this.MaximumSelectedValues = maxOptions; + if (this.Type != ComponentType.ActionRow && this.Type != ComponentType.InputText && this.Type != ComponentType.Button && this.Type != ComponentType.StringSelect) + this.DefaultValues = defaultValues?.ToList(); } internal DiscordBaseSelectComponent() diff --git a/DisCatSharp/Entities/Interaction/Components/Select/DiscordChannelSelectComponent.cs b/DisCatSharp/Entities/Interaction/Components/Select/DiscordChannelSelectComponent.cs index 41868dc1a..06049d77d 100644 --- a/DisCatSharp/Entities/Interaction/Components/Select/DiscordChannelSelectComponent.cs +++ b/DisCatSharp/Entities/Interaction/Components/Select/DiscordChannelSelectComponent.cs @@ -39,7 +39,7 @@ public sealed class DiscordChannelSelectComponent : DiscordBaseSelectComponent /// The channel types to filter by. /// [JsonProperty("channel_types", NullValueHandling = NullValueHandling.Ignore)] - public IReadOnlyList ChannelTypes { get; internal set; } = null; + public IReadOnlyList? ChannelTypes { get; internal set; } = null; /// /// Enables this component if it was disabled before. @@ -72,8 +72,9 @@ public DiscordChannelSelectComponent Disable() /// Minimum count of selectable options. /// Maximum count of selectable options. /// Whether this select component should be initialized as being disabled. User sees a greyed out select component that cannot be interacted with. - public DiscordChannelSelectComponent(string placeholder, IEnumerable channelTypes = null, string customId = null, int minOptions = 1, int maxOptions = 1, bool disabled = false) - : base(ComponentType.ChannelSelect, placeholder, customId, minOptions, maxOptions, disabled) + /// The default values of this select menu. + public DiscordChannelSelectComponent(string placeholder, IEnumerable channelTypes = null, string customId = null, int minOptions = 1, int maxOptions = 1, bool disabled = false, IEnumerable? defaultValues = null) + : base(ComponentType.ChannelSelect, placeholder, customId, minOptions, maxOptions, disabled, defaultValues) { this.ChannelTypes = channelTypes?.ToArray() ?? Array.Empty(); } @@ -88,8 +89,9 @@ public DiscordChannelSelectComponent(string placeholder, IEnumerableMinimum count of selectable options. /// Maximum count of selectable options. /// Whether this select component should be initialized as being disabled. User sees a greyed out select component that cannot be interacted with. - public DiscordChannelSelectComponent(string label, string placeholder, IEnumerable channelTypes = null, string customId = null, int minOptions = 1, int maxOptions = 1, bool disabled = false) - : base(ComponentType.ChannelSelect, label, placeholder, customId, minOptions, maxOptions, disabled) + /// The default values of this select menu. + public DiscordChannelSelectComponent(string label, string placeholder, IEnumerable channelTypes = null, string customId = null, int minOptions = 1, int maxOptions = 1, bool disabled = false, IEnumerable? defaultValues = null) + : base(ComponentType.ChannelSelect, label, placeholder, customId, minOptions, maxOptions, disabled, defaultValues) { this.ChannelTypes = channelTypes?.ToArray() ?? Array.Empty(); } diff --git a/DisCatSharp/Entities/Interaction/Components/Select/DiscordMentionableSelectComponent.cs b/DisCatSharp/Entities/Interaction/Components/Select/DiscordMentionableSelectComponent.cs index de199a427..518ddded4 100644 --- a/DisCatSharp/Entities/Interaction/Components/Select/DiscordMentionableSelectComponent.cs +++ b/DisCatSharp/Entities/Interaction/Components/Select/DiscordMentionableSelectComponent.cs @@ -20,6 +20,9 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. +using System.Collections.Generic; +using System.Linq; + using DisCatSharp.Enums; namespace DisCatSharp.Entities; @@ -59,8 +62,9 @@ public DiscordMentionableSelectComponent Disable() /// Minimum count of selectable options. /// Maximum count of selectable options. /// Whether this select component should be initialized as being disabled. User sees a greyed out select component that cannot be interacted with. - public DiscordMentionableSelectComponent(string placeholder, string customId = null, int minOptions = 1, int maxOptions = 1, bool disabled = false) - : base(ComponentType.MentionableSelect, placeholder, customId, minOptions, maxOptions, disabled) + /// The default values of this select menu. + public DiscordMentionableSelectComponent(string placeholder, string customId = null, int minOptions = 1, int maxOptions = 1, bool disabled = false, IEnumerable? defaultValues = null) + : base(ComponentType.MentionableSelect, placeholder, customId, minOptions, maxOptions, disabled, defaultValues) { } /// @@ -72,8 +76,9 @@ public DiscordMentionableSelectComponent(string placeholder, string customId = n /// Minimum count of selectable options. /// Maximum count of selectable options. /// Whether this select component should be initialized as being disabled. User sees a greyed out select component that cannot be interacted with. - public DiscordMentionableSelectComponent(string label, string placeholder, string customId = null, int minOptions = 1, int maxOptions = 1, bool disabled = false) - : base(ComponentType.MentionableSelect, label, placeholder, customId, minOptions, maxOptions, disabled) + /// The default values of this select menu. + public DiscordMentionableSelectComponent(string label, string placeholder, string customId = null, int minOptions = 1, int maxOptions = 1, bool disabled = false, IEnumerable? defaultValues = null) + : base(ComponentType.MentionableSelect, label, placeholder, customId, minOptions, maxOptions, disabled, defaultValues) { } /// diff --git a/DisCatSharp/Entities/Interaction/Components/Select/DiscordRoleSelectComponent.cs b/DisCatSharp/Entities/Interaction/Components/Select/DiscordRoleSelectComponent.cs index 08142ec48..44f62ecca 100644 --- a/DisCatSharp/Entities/Interaction/Components/Select/DiscordRoleSelectComponent.cs +++ b/DisCatSharp/Entities/Interaction/Components/Select/DiscordRoleSelectComponent.cs @@ -20,6 +20,8 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. +using System.Collections.Generic; + using DisCatSharp.Enums; namespace DisCatSharp.Entities; @@ -59,8 +61,9 @@ public DiscordRoleSelectComponent Disable() /// Minimum count of selectable options. /// Maximum count of selectable options. /// Whether this select component should be initialized as being disabled. User sees a greyed out select component that cannot be interacted with. - public DiscordRoleSelectComponent(string placeholder, string customId = null, int minOptions = 1, int maxOptions = 1, bool disabled = false) - : base(ComponentType.RoleSelect, placeholder, customId, minOptions, maxOptions, disabled) + /// The default values of this select menu. + public DiscordRoleSelectComponent(string placeholder, string customId = null, int minOptions = 1, int maxOptions = 1, bool disabled = false, IEnumerable? defaultValues = null) + : base(ComponentType.RoleSelect, placeholder, customId, minOptions, maxOptions, disabled, defaultValues) { } /// @@ -72,8 +75,9 @@ public DiscordRoleSelectComponent(string placeholder, string customId = null, in /// Minimum count of selectable options. /// Maximum count of selectable options. /// Whether this select component should be initialized as being disabled. User sees a greyed out select component that cannot be interacted with. - public DiscordRoleSelectComponent(string label, string placeholder, string customId = null, int minOptions = 1, int maxOptions = 1, bool disabled = false) - : base(ComponentType.RoleSelect, label, placeholder, customId, minOptions, maxOptions, disabled) + /// The default values of this select menu. + public DiscordRoleSelectComponent(string label, string placeholder, string customId = null, int minOptions = 1, int maxOptions = 1, bool disabled = false, IEnumerable? defaultValues = null) + : base(ComponentType.RoleSelect, label, placeholder, customId, minOptions, maxOptions, disabled, defaultValues) { } /// diff --git a/DisCatSharp/Entities/Interaction/Components/Select/DiscordSelectDefaultValue.cs b/DisCatSharp/Entities/Interaction/Components/Select/DiscordSelectDefaultValue.cs new file mode 100644 index 000000000..9f2369c07 --- /dev/null +++ b/DisCatSharp/Entities/Interaction/Components/Select/DiscordSelectDefaultValue.cs @@ -0,0 +1,86 @@ +// This file is part of the DisCatSharp project, based off DSharpPlus. +// +// Copyright (c) 2021-2023 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 NON-INFRINGEMENT. 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; + +/// +/// A select default value for use with , , or . +/// +public sealed class DiscordSelectDefaultValue +{ + /// + /// The id of a user, role, or channel. + /// + [JsonProperty("id", NullValueHandling = NullValueHandling.Ignore)] + public ulong Id { get; internal set; } + + /// + /// The type of value that represents. + /// + [JsonProperty("type", NullValueHandling = NullValueHandling.Ignore)] + public string Type { get; internal set; } + + /// + /// Constructs a new for an with corrosponding . + /// + /// The id to set. + /// The type of the . Can be user, role or channel + public DiscordSelectDefaultValue(ulong id, string type) + { + this.Id = id; + this.Type = type; + } + + /// + /// Constructs a new for a role. + /// + /// The role to set. + public DiscordSelectDefaultValue(DiscordRole role) + { + this.Id = role.Id; + this.Type = "role"; + } + + /// + /// Constructs a new for a user. + /// + /// The user to set. + public DiscordSelectDefaultValue(DiscordUser user) + { + this.Id = user.Id; + this.Type = "user"; + } + + /// + /// Constructs a new for a channel. + /// + /// The channel to set. + public DiscordSelectDefaultValue(DiscordChannel channel) + { + this.Id = channel.Id; + this.Type = "channel"; + } +} diff --git a/DisCatSharp/Entities/Interaction/Components/Select/DiscordUserSelectComponent.cs b/DisCatSharp/Entities/Interaction/Components/Select/DiscordUserSelectComponent.cs index 772dbf89b..eb4a66086 100644 --- a/DisCatSharp/Entities/Interaction/Components/Select/DiscordUserSelectComponent.cs +++ b/DisCatSharp/Entities/Interaction/Components/Select/DiscordUserSelectComponent.cs @@ -20,6 +20,8 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. +using System.Collections.Generic; + using DisCatSharp.Enums; namespace DisCatSharp.Entities; @@ -57,8 +59,9 @@ public DiscordUserSelectComponent Disable() /// Minimum count of selectable options. /// Maximum count of selectable options. /// Whether this select component should be initialized as being disabled. User sees a greyed out select component that cannot be interacted with. - public DiscordUserSelectComponent(string placeholder, string customId = null, int minOptions = 1, int maxOptions = 1, bool disabled = false) - : base(ComponentType.UserSelect, placeholder, customId, minOptions, maxOptions, disabled) + /// The default values of this select menu. + public DiscordUserSelectComponent(string placeholder, string customId = null, int minOptions = 1, int maxOptions = 1, bool disabled = false, IEnumerable? defaultValues = null) + : base(ComponentType.UserSelect, placeholder, customId, minOptions, maxOptions, disabled, defaultValues) { } /// @@ -70,8 +73,9 @@ public DiscordUserSelectComponent(string placeholder, string customId = null, in /// Minimum count of selectable options. /// Maximum count of selectable options. /// Whether this select component should be initialized as being disabled. User sees a greyed out select component that cannot be interacted with. - public DiscordUserSelectComponent(string label, string placeholder, string customId = null, int minOptions = 1, int maxOptions = 1, bool disabled = false) - : base(ComponentType.UserSelect, label, placeholder, customId, minOptions, maxOptions, disabled) + /// The default values of this select menu. + public DiscordUserSelectComponent(string label, string placeholder, string customId = null, int minOptions = 1, int maxOptions = 1, bool disabled = false, IEnumerable? defaultValues = null) + : base(ComponentType.UserSelect, label, placeholder, customId, minOptions, maxOptions, disabled, defaultValues) { } ///