From a2f624e6de4d3b18e71ba10fac7666ceef297e27 Mon Sep 17 00:00:00 2001 From: Quin Lynch <49576606+quinchs@users.noreply.github.com> Date: Tue, 16 Apr 2024 00:57:10 -0300 Subject: [PATCH] Fix: Don't depend on WebSocket for Interaction service (#2912) * unfuck interaction service to not depend on WS * Add XML docs * fix summary refs --- .../SlashCommands/IAutocompleteInteraction.cs | 16 ++++++++++ .../AutocompleteHandler.cs | 24 +++++++------- .../Discord.Net.Interactions.csproj | 3 +- .../Parameters/SlashCommandParameterInfo.cs | 2 +- .../InteractionContext.cs | 2 +- .../InteractionService.cs | 32 +++++-------------- src/Discord.Net.Rest/DiscordRestClient.cs | 4 ++- .../RestAutocompleteInteraction.cs | 4 +++ src/Discord.Net.Rest/IRestClientProvider.cs | 14 ++++++++ src/Discord.Net.WebSocket/BaseSocketClient.cs | 4 ++- .../Interactions}/InteractionUtility.cs | 0 11 files changed, 63 insertions(+), 42 deletions(-) create mode 100644 src/Discord.Net.Rest/IRestClientProvider.cs rename src/{Discord.Net.Interactions/Utilities => Discord.Net.WebSocket/Interactions}/InteractionUtility.cs (100%) diff --git a/src/Discord.Net.Core/Entities/Interactions/SlashCommands/IAutocompleteInteraction.cs b/src/Discord.Net.Core/Entities/Interactions/SlashCommands/IAutocompleteInteraction.cs index bb5343d8..4d89e5ea 100644 --- a/src/Discord.Net.Core/Entities/Interactions/SlashCommands/IAutocompleteInteraction.cs +++ b/src/Discord.Net.Core/Entities/Interactions/SlashCommands/IAutocompleteInteraction.cs @@ -1,3 +1,6 @@ +using System.Collections.Generic; +using System.Threading.Tasks; + namespace Discord { /// @@ -9,5 +12,18 @@ namespace Discord /// Gets the autocomplete data of this interaction. /// new IAutocompleteInteractionData Data { get; } + + /// + /// Responds to this interaction with a set of choices. + /// + /// + /// The set of choices for the user to pick from. + /// + /// A max of 25 choices are allowed. Passing for this argument will show the executing user that + /// there is no choices for their autocompleted input. + /// + /// + /// The request options for this response. + Task RespondAsync(IEnumerable result, RequestOptions options = null); } } diff --git a/src/Discord.Net.Interactions/AutocompleteHandlers/AutocompleteHandler.cs b/src/Discord.Net.Interactions/AutocompleteHandlers/AutocompleteHandler.cs index 40ac54a0..636ac840 100644 --- a/src/Discord.Net.Interactions/AutocompleteHandlers/AutocompleteHandler.cs +++ b/src/Discord.Net.Interactions/AutocompleteHandlers/AutocompleteHandler.cs @@ -56,21 +56,21 @@ namespace Discord.Interactions var result = await GenerateSuggestionsAsync(context, autocompleteInteraction, parameter, services).ConfigureAwait(false); if (result.IsSuccess) - switch (autocompleteInteraction) + { + var task = autocompleteInteraction.RespondAsync(result.Suggestions); + + await task; + + if (task is Task strTask) { - case RestAutocompleteInteraction restAutocomplete: - var payload = restAutocomplete.Respond(result.Suggestions); + var payload = strTask.Result; - if (context is IRestInteractionContext restContext && restContext.InteractionResponseCallback != null) - await restContext.InteractionResponseCallback.Invoke(payload).ConfigureAwait(false); - else - await InteractionService._restResponseCallback(context, payload).ConfigureAwait(false); - break; - case SocketAutocompleteInteraction socketAutocomplete: - await socketAutocomplete.RespondAsync(result.Suggestions).ConfigureAwait(false); - break; + if (context is IRestInteractionContext {InteractionResponseCallback: not null} restContext) + await restContext.InteractionResponseCallback.Invoke(payload).ConfigureAwait(false); + else + await InteractionService._restResponseCallback(context, payload).ConfigureAwait(false); } - + } await InteractionService._autocompleteHandlerExecutedEvent.InvokeAsync(this, context, result).ConfigureAwait(false); return result; } diff --git a/src/Discord.Net.Interactions/Discord.Net.Interactions.csproj b/src/Discord.Net.Interactions/Discord.Net.Interactions.csproj index 7810c6a2..9508d3a0 100644 --- a/src/Discord.Net.Interactions/Discord.Net.Interactions.csproj +++ b/src/Discord.Net.Interactions/Discord.Net.Interactions.csproj @@ -11,11 +11,10 @@ false false - + - diff --git a/src/Discord.Net.Interactions/Info/Parameters/SlashCommandParameterInfo.cs b/src/Discord.Net.Interactions/Info/Parameters/SlashCommandParameterInfo.cs index 84cc61ba..e5faa92d 100644 --- a/src/Discord.Net.Interactions/Info/Parameters/SlashCommandParameterInfo.cs +++ b/src/Discord.Net.Interactions/Info/Parameters/SlashCommandParameterInfo.cs @@ -49,7 +49,7 @@ namespace Discord.Interactions public int? MaxLength { get; } /// - /// Gets the that will be used to convert the incoming into + /// Gets the that will be used to convert the incoming into /// . /// public TypeConverter TypeConverter { get; } diff --git a/src/Discord.Net.Interactions/InteractionContext.cs b/src/Discord.Net.Interactions/InteractionContext.cs index b81cc593..2c1dba44 100644 --- a/src/Discord.Net.Interactions/InteractionContext.cs +++ b/src/Discord.Net.Interactions/InteractionContext.cs @@ -20,7 +20,7 @@ namespace Discord.Interactions public IReadOnlyCollection SegmentMatches { get; private set; } /// - /// Initializes a new . + /// Initializes a new . /// /// The underlying client. /// The underlying interaction. diff --git a/src/Discord.Net.Interactions/InteractionService.cs b/src/Discord.Net.Interactions/InteractionService.cs index edf1b6a8..3089aa58 100644 --- a/src/Discord.Net.Interactions/InteractionService.cs +++ b/src/Discord.Net.Interactions/InteractionService.cs @@ -145,30 +145,6 @@ namespace Discord.Interactions /// public IReadOnlyCollection Modals => ModalUtils.Modals; - /// - /// Initialize a with provided configurations. - /// - /// The discord client. - /// The configuration class. - public InteractionService(DiscordSocketClient discord, InteractionServiceConfig config = null) - : this(() => discord.Rest, config ?? new InteractionServiceConfig()) { } - - /// - /// Initialize a with provided configurations. - /// - /// The discord client. - /// The configuration class. - public InteractionService(DiscordShardedClient discord, InteractionServiceConfig config = null) - : this(() => discord.Rest, config ?? new InteractionServiceConfig()) { } - - /// - /// Initialize a with provided configurations. - /// - /// The discord client. - /// The configuration class. - public InteractionService(BaseSocketClient discord, InteractionServiceConfig config = null) - : this(() => discord.Rest, config ?? new InteractionServiceConfig()) { } - /// /// Initialize a with provided configurations. /// @@ -177,6 +153,14 @@ namespace Discord.Interactions public InteractionService(DiscordRestClient discord, InteractionServiceConfig config = null) : this(() => discord, config ?? new InteractionServiceConfig()) { } + /// + /// Initialize a with provided configurations. + /// + /// The discord client provider. + /// The configuration class. + public InteractionService(IRestClientProvider discordProvider, InteractionServiceConfig config = null) + : this(() => discordProvider.RestClient, config ?? new InteractionServiceConfig()) { } + private InteractionService(Func getRestClient, InteractionServiceConfig config = null) { config ??= new InteractionServiceConfig(); diff --git a/src/Discord.Net.Rest/DiscordRestClient.cs b/src/Discord.Net.Rest/DiscordRestClient.cs index 69a9bb0f..4c8615db 100644 --- a/src/Discord.Net.Rest/DiscordRestClient.cs +++ b/src/Discord.Net.Rest/DiscordRestClient.cs @@ -15,7 +15,7 @@ namespace Discord.Rest /// /// Provides a client to send REST-based requests to Discord. /// - public class DiscordRestClient : BaseDiscordClient, IDiscordClient + public class DiscordRestClient : BaseDiscordClient, IDiscordClient, IRestClientProvider { #region DiscordRestClient private RestApplication _applicationInfo; @@ -399,5 +399,7 @@ namespace Discord.Rest async Task> IDiscordClient.BulkOverwriteGlobalApplicationCommand(ApplicationCommandProperties[] properties, RequestOptions options) => await BulkOverwriteGlobalCommands(properties, options).ConfigureAwait(false); #endregion + + DiscordRestClient IRestClientProvider.RestClient => this; } } diff --git a/src/Discord.Net.Rest/Entities/Interactions/SlashCommands/RestAutocompleteInteraction.cs b/src/Discord.Net.Rest/Entities/Interactions/SlashCommands/RestAutocompleteInteraction.cs index 4f57e3b6..ddcab2e6 100644 --- a/src/Discord.Net.Rest/Entities/Interactions/SlashCommands/RestAutocompleteInteraction.cs +++ b/src/Discord.Net.Rest/Entities/Interactions/SlashCommands/RestAutocompleteInteraction.cs @@ -118,5 +118,9 @@ namespace Discord.Rest //IAutocompleteInteraction /// IAutocompleteInteractionData IAutocompleteInteraction.Data => Data; + + /// + Task IAutocompleteInteraction.RespondAsync(IEnumerable result, RequestOptions options) + =>Task.FromResult(Respond(result, options)); } } diff --git a/src/Discord.Net.Rest/IRestClientProvider.cs b/src/Discord.Net.Rest/IRestClientProvider.cs new file mode 100644 index 00000000..c0a39973 --- /dev/null +++ b/src/Discord.Net.Rest/IRestClientProvider.cs @@ -0,0 +1,14 @@ +using Discord.Rest; + +namespace Discord.Rest; + +/// +/// An interface that represents a client provider for Rest-based clients. +/// +public interface IRestClientProvider +{ + /// + /// Gets the Rest client of this provider. + /// + DiscordRestClient RestClient { get; } +} diff --git a/src/Discord.Net.WebSocket/BaseSocketClient.cs b/src/Discord.Net.WebSocket/BaseSocketClient.cs index beaf67e5..03c0417b 100644 --- a/src/Discord.Net.WebSocket/BaseSocketClient.cs +++ b/src/Discord.Net.WebSocket/BaseSocketClient.cs @@ -10,7 +10,7 @@ namespace Discord.WebSocket /// /// Represents the base of a WebSocket-based Discord client. /// - public abstract partial class BaseSocketClient : BaseDiscordClient, IDiscordClient + public abstract partial class BaseSocketClient : BaseDiscordClient, IDiscordClient, IRestClientProvider { #region BaseSocketClient protected readonly DiscordSocketConfig BaseConfig; @@ -352,5 +352,7 @@ namespace Discord.WebSocket return await GetVoiceRegionsAsync().ConfigureAwait(false); } #endregion + + DiscordRestClient IRestClientProvider.RestClient => Rest; } } diff --git a/src/Discord.Net.Interactions/Utilities/InteractionUtility.cs b/src/Discord.Net.WebSocket/Interactions/InteractionUtility.cs similarity index 100% rename from src/Discord.Net.Interactions/Utilities/InteractionUtility.cs rename to src/Discord.Net.WebSocket/Interactions/InteractionUtility.cs