Skip to content

Commit

Permalink
[Feature] Generic autocomplete (#2935)
Browse files Browse the repository at this point in the history
  • Loading branch information
Misha-133 authored Jun 14, 2024
1 parent 3be72a8 commit 35b102a
Showing 1 changed file with 29 additions and 27 deletions.
56 changes: 29 additions & 27 deletions src/Discord.Net.Interactions/Attributes/AutocompleteAttribute.cs
Original file line number Diff line number Diff line change
@@ -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
{
/// <summary>
/// Type of the <see cref="AutocompleteHandler"/>.
/// </summary>
public Type AutocompleteHandlerType { get; }
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)
{
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"/> 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");

/// <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;

0 comments on commit 35b102a

Please sign in to comment.