Add generic [AutocompleteAttribute] (#2611)
* add generic AutocompleteAttribute * fancy syntax * it didn't work -_- --------- Co-authored-by: Misha133 <mihagribkov133@gmail.com>
This commit is contained in:
@@ -1,36 +1,46 @@
|
|||||||
using System;
|
using System;
|
||||||
|
|
||||||
namespace Discord.Interactions
|
namespace Discord.Interactions;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Set the <see cref="ApplicationCommandOptionProperties.IsAutocomplete"/> to <see langword="true"/>.
|
||||||
|
/// </summary>
|
||||||
|
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = true)]
|
||||||
|
public class AutocompleteAttribute : Attribute
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Set the <see cref="ApplicationCommandOptionProperties.IsAutocomplete"/> to <see langword="true"/>.
|
/// Type of the <see cref="AutocompleteHandler"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = true)]
|
public Type AutocompleteHandlerType { get; }
|
||||||
public class AutocompleteAttribute : Attribute
|
|
||||||
|
/// <summary>
|
||||||
|
/// Set the <see cref="ApplicationCommandOptionProperties.IsAutocomplete"/> to <see langword="true"/> and define a <see cref="AutocompleteHandler"/> to handle
|
||||||
|
/// Autocomplete interactions targeting the parameter this <see cref="Attribute"/> is applied to.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// <see cref="InteractionServiceConfig.EnableAutocompleteHandlers"/> must be set to <see langword="true"/> to use this constructor.
|
||||||
|
/// </remarks>
|
||||||
|
public AutocompleteAttribute(Type autocompleteHandlerType)
|
||||||
{
|
{
|
||||||
/// <summary>
|
if (!typeof(IAutocompleteHandler).IsAssignableFrom(autocompleteHandlerType))
|
||||||
/// Type of the <see cref="AutocompleteHandler"/>.
|
throw new InvalidOperationException($"{autocompleteHandlerType.FullName} isn't a valid {nameof(IAutocompleteHandler)} type");
|
||||||
/// </summary>
|
|
||||||
public Type AutocompleteHandlerType { get; }
|
|
||||||
|
|
||||||
/// <summary>
|
AutocompleteHandlerType = autocompleteHandlerType;
|
||||||
/// Set the <see cref="ApplicationCommandOptionProperties.IsAutocomplete"/> to <see langword="true"/> and define a <see cref="AutocompleteHandler"/> to handle
|
|
||||||
/// Autocomplete interactions targeting the parameter this <see cref="Attribute"/> is applied to.
|
|
||||||
/// </summary>
|
|
||||||
/// <remarks>
|
|
||||||
/// <see cref="InteractionServiceConfig.EnableAutocompleteHandlers"/> must be set to <see langword="true"/> to use this constructor.
|
|
||||||
/// </remarks>
|
|
||||||
public AutocompleteAttribute(Type autocompleteHandlerType)
|
|
||||||
{
|
|
||||||
if (!typeof(IAutocompleteHandler).IsAssignableFrom(autocompleteHandlerType))
|
|
||||||
throw new InvalidOperationException($"{autocompleteHandlerType.FullName} isn't a valid {nameof(IAutocompleteHandler)} type");
|
|
||||||
|
|
||||||
AutocompleteHandlerType = autocompleteHandlerType;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Set the <see cref="ApplicationCommandOptionProperties.IsAutocomplete"/> to <see langword="true"/> without specifying a <see cref="AutocompleteHandler"/>.
|
|
||||||
/// </summary>
|
|
||||||
public AutocompleteAttribute() { }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Set the <see cref="ApplicationCommandOptionProperties.IsAutocomplete"/> to <see langword="true"/> without specifying a <see cref="AutocompleteHandler"/>.
|
||||||
|
/// </summary>
|
||||||
|
public AutocompleteAttribute() { }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Set the <see cref="ApplicationCommandOptionProperties.IsAutocomplete"/> to <see langword="true"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">Type of the <see cref="AutocompleteHandler"/> that will be used to handle Autocomplete interactions targeting the parameter.</typeparam>
|
||||||
|
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = true)]
|
||||||
|
public class AutocompleteAttribute<T> : AutocompleteAttribute
|
||||||
|
where T : class, IAutocompleteHandler
|
||||||
|
{
|
||||||
|
public AutocompleteAttribute() : base(typeof(T)) { }
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,12 +3,14 @@
|
|||||||
<Import Project="../../StyleAnalyzer.targets" />
|
<Import Project="../../StyleAnalyzer.targets" />
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">net6.0;net5.0;net461;netstandard2.0;netstandard2.1</TargetFrameworks>
|
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">net6.0;net5.0;net461;netstandard2.0;netstandard2.1</TargetFrameworks>
|
||||||
|
<LangVersion>preview</LangVersion>
|
||||||
<TargetFrameworks Condition=" '$(OS)' != 'Windows_NT' ">net6.0;net5.0;netstandard2.0;netstandard2.1</TargetFrameworks>
|
<TargetFrameworks Condition=" '$(OS)' != 'Windows_NT' ">net6.0;net5.0;netstandard2.0;netstandard2.1</TargetFrameworks>
|
||||||
<RootNamespace>Discord.Interactions</RootNamespace>
|
<RootNamespace>Discord.Interactions</RootNamespace>
|
||||||
<AssemblyName>Discord.Net.Interactions</AssemblyName>
|
<AssemblyName>Discord.Net.Interactions</AssemblyName>
|
||||||
<Description>A Discord.Net extension adding support for Application Commands.</Description>
|
<Description>A Discord.Net extension adding support for Application Commands.</Description>
|
||||||
<WarningLevel>5</WarningLevel>
|
<WarningLevel>5</WarningLevel>
|
||||||
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
|
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
|
||||||
|
<EnablePreviewFeatures>true</EnablePreviewFeatures>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
Reference in New Issue
Block a user