-
-
Notifications
You must be signed in to change notification settings - Fork 737
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature] Generic autocomplete (#2935)
- Loading branch information
Showing
1 changed file
with
29 additions
and
27 deletions.
There are no files selected for viewing
56 changes: 29 additions & 27 deletions
56
src/Discord.Net.Interactions/Attributes/AutocompleteAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |