[Feature] Generic autocomplete (#2935)

This commit is contained in:
Mihail Gribkov
2024-06-14 11:22:40 +03:00
committed by GitHub
parent 3be72a8ae6
commit 35b102a7c0

View File

@@ -1,36 +1,38 @@
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>
/// Set the <see cref="ApplicationCommandOptionProperties.IsAutocomplete"/> to <see langword="true"/>.
/// Type of the <see cref="AutocompleteHandler"/>.
/// </summary>
[AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = true)]
public class AutocompleteAttribute : Attribute
public Type AutocompleteHandlerType { get; }
/// <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>
/// Type of the <see cref="AutocompleteHandler"/>.
/// </summary>
public Type AutocompleteHandlerType { get; }
if (!typeof(IAutocompleteHandler).IsAssignableFrom(autocompleteHandlerType))
throw new InvalidOperationException($"{autocompleteHandlerType.FullName} isn't a valid {nameof(IAutocompleteHandler)} type");
/// <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)
{
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() { }
AutocompleteHandlerType = autocompleteHandlerType;
}
}
/// <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(typeof(T))
where T : class, IAutocompleteHandler;