From 12179a93d7aef4a07f2fb194f42e9d537941cefd Mon Sep 17 00:00:00 2001 From: Cenk Ergen <57065323+Cenngo@users.noreply.github.com> Date: Wed, 17 Jan 2024 00:11:29 +0300 Subject: [PATCH] Add generic [AutocompleteAttribute] (#2611) * add generic AutocompleteAttribute * fancy syntax * it didn't work -_- --------- Co-authored-by: Misha133 --- .../Attributes/AutocompleteAttribute.cs | 64 +++++++++++-------- .../Discord.Net.Interactions.csproj | 2 + 2 files changed, 39 insertions(+), 27 deletions(-) diff --git a/src/Discord.Net.Interactions/Attributes/AutocompleteAttribute.cs b/src/Discord.Net.Interactions/Attributes/AutocompleteAttribute.cs index c8a3428d..fc84d58f 100644 --- a/src/Discord.Net.Interactions/Attributes/AutocompleteAttribute.cs +++ b/src/Discord.Net.Interactions/Attributes/AutocompleteAttribute.cs @@ -1,36 +1,46 @@ using System; -namespace Discord.Interactions +namespace Discord.Interactions; + +/// +/// Set the to . +/// +[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = true)] +public class AutocompleteAttribute : Attribute { /// - /// Set the to . + /// Type of the . /// - [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = true)] - public class AutocompleteAttribute : Attribute + public Type AutocompleteHandlerType { get; } + + /// + /// Set the to and define a to handle + /// Autocomplete interactions targeting the parameter this is applied to. + /// + /// + /// must be set to to use this constructor. + /// + public AutocompleteAttribute(Type autocompleteHandlerType) { - /// - /// Type of the . - /// - public Type AutocompleteHandlerType { get; } + if (!typeof(IAutocompleteHandler).IsAssignableFrom(autocompleteHandlerType)) + throw new InvalidOperationException($"{autocompleteHandlerType.FullName} isn't a valid {nameof(IAutocompleteHandler)} type"); - /// - /// Set the to and define a to handle - /// Autocomplete interactions targeting the parameter this is applied to. - /// - /// - /// must be set to to use this constructor. - /// - public AutocompleteAttribute(Type autocompleteHandlerType) - { - if (!typeof(IAutocompleteHandler).IsAssignableFrom(autocompleteHandlerType)) - throw new InvalidOperationException($"{autocompleteHandlerType.FullName} isn't a valid {nameof(IAutocompleteHandler)} type"); - - AutocompleteHandlerType = autocompleteHandlerType; - } - - /// - /// Set the to without specifying a . - /// - public AutocompleteAttribute() { } + AutocompleteHandlerType = autocompleteHandlerType; } + + /// + /// Set the to without specifying a . + /// + public AutocompleteAttribute() { } +} + +/// +/// Set the to . +/// +/// Type of the that will be used to handle Autocomplete interactions targeting the parameter. +[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = true)] +public class AutocompleteAttribute : AutocompleteAttribute + where T : class, IAutocompleteHandler +{ + public AutocompleteAttribute() : base(typeof(T)) { } } diff --git a/src/Discord.Net.Interactions/Discord.Net.Interactions.csproj b/src/Discord.Net.Interactions/Discord.Net.Interactions.csproj index a3ac3d50..653f746b 100644 --- a/src/Discord.Net.Interactions/Discord.Net.Interactions.csproj +++ b/src/Discord.Net.Interactions/Discord.Net.Interactions.csproj @@ -3,12 +3,14 @@ net6.0;net5.0;net461;netstandard2.0;netstandard2.1 + preview net6.0;net5.0;netstandard2.0;netstandard2.1 Discord.Interactions Discord.Net.Interactions A Discord.Net extension adding support for Application Commands. 5 True + true