Fix: Don't depend on WebSocket for Interaction service (#2912)

* unfuck interaction service to not depend on WS

* Add XML docs

* fix summary refs
This commit is contained in:
Quin Lynch
2024-04-16 00:57:10 -03:00
committed by GitHub
parent 13ea5664e1
commit a2f624e6de
11 changed files with 63 additions and 42 deletions

View File

@@ -1,3 +1,6 @@
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Discord namespace Discord
{ {
/// <summary> /// <summary>
@@ -9,5 +12,18 @@ namespace Discord
/// Gets the autocomplete data of this interaction. /// Gets the autocomplete data of this interaction.
/// </summary> /// </summary>
new IAutocompleteInteractionData Data { get; } new IAutocompleteInteractionData Data { get; }
/// <summary>
/// Responds to this interaction with a set of choices.
/// </summary>
/// <param name="result">
/// The set of choices for the user to pick from.
/// <remarks>
/// A max of 25 choices are allowed. Passing <see langword="null"/> for this argument will show the executing user that
/// there is no choices for their autocompleted input.
/// </remarks>
/// </param>
/// <param name="options">The request options for this response.</param>
Task RespondAsync(IEnumerable<AutocompleteResult> result, RequestOptions options = null);
} }
} }

View File

@@ -56,21 +56,21 @@ namespace Discord.Interactions
var result = await GenerateSuggestionsAsync(context, autocompleteInteraction, parameter, services).ConfigureAwait(false); var result = await GenerateSuggestionsAsync(context, autocompleteInteraction, parameter, services).ConfigureAwait(false);
if (result.IsSuccess) if (result.IsSuccess)
switch (autocompleteInteraction)
{ {
case RestAutocompleteInteraction restAutocomplete: var task = autocompleteInteraction.RespondAsync(result.Suggestions);
var payload = restAutocomplete.Respond(result.Suggestions);
if (context is IRestInteractionContext restContext && restContext.InteractionResponseCallback != null) await task;
if (task is Task<string> strTask)
{
var payload = strTask.Result;
if (context is IRestInteractionContext {InteractionResponseCallback: not null} restContext)
await restContext.InteractionResponseCallback.Invoke(payload).ConfigureAwait(false); await restContext.InteractionResponseCallback.Invoke(payload).ConfigureAwait(false);
else else
await InteractionService._restResponseCallback(context, payload).ConfigureAwait(false); await InteractionService._restResponseCallback(context, payload).ConfigureAwait(false);
break;
case SocketAutocompleteInteraction socketAutocomplete:
await socketAutocomplete.RespondAsync(result.Suggestions).ConfigureAwait(false);
break;
} }
}
await InteractionService._autocompleteHandlerExecutedEvent.InvokeAsync(this, context, result).ConfigureAwait(false); await InteractionService._autocompleteHandlerExecutedEvent.InvokeAsync(this, context, result).ConfigureAwait(false);
return result; return result;
} }

View File

@@ -15,7 +15,6 @@
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\Discord.Net.Core\Discord.Net.Core.csproj" /> <ProjectReference Include="..\Discord.Net.Core\Discord.Net.Core.csproj" />
<ProjectReference Include="..\Discord.Net.Rest\Discord.Net.Rest.csproj" /> <ProjectReference Include="..\Discord.Net.Rest\Discord.Net.Rest.csproj" />
<ProjectReference Include="..\Discord.Net.WebSocket\Discord.Net.WebSocket.csproj" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@@ -49,7 +49,7 @@ namespace Discord.Interactions
public int? MaxLength { get; } public int? MaxLength { get; }
/// <summary> /// <summary>
/// Gets the <see cref="TypeConverter{T}"/> that will be used to convert the incoming <see cref="Discord.WebSocket.SocketSlashCommandDataOption"/> into /// Gets the <see cref="TypeConverter{T}"/> that will be used to convert the incoming <see cref="Discord.IDiscordInteractionData"/> into
/// <see cref="CommandParameterInfo.ParameterType"/>. /// <see cref="CommandParameterInfo.ParameterType"/>.
/// </summary> /// </summary>
public TypeConverter TypeConverter { get; } public TypeConverter TypeConverter { get; }

View File

@@ -20,7 +20,7 @@ namespace Discord.Interactions
public IReadOnlyCollection<IRouteSegmentMatch> SegmentMatches { get; private set; } public IReadOnlyCollection<IRouteSegmentMatch> SegmentMatches { get; private set; }
/// <summary> /// <summary>
/// Initializes a new <see cref="SocketInteractionContext{TInteraction}"/>. /// Initializes a new <see cref="InteractionContext"/>.
/// </summary> /// </summary>
/// <param name="client">The underlying client.</param> /// <param name="client">The underlying client.</param>
/// <param name="interaction">The underlying interaction.</param> /// <param name="interaction">The underlying interaction.</param>

View File

@@ -145,30 +145,6 @@ namespace Discord.Interactions
/// </summary> /// </summary>
public IReadOnlyCollection<ModalInfo> Modals => ModalUtils.Modals; public IReadOnlyCollection<ModalInfo> Modals => ModalUtils.Modals;
/// <summary>
/// Initialize a <see cref="InteractionService"/> with provided configurations.
/// </summary>
/// <param name="discord">The discord client.</param>
/// <param name="config">The configuration class.</param>
public InteractionService(DiscordSocketClient discord, InteractionServiceConfig config = null)
: this(() => discord.Rest, config ?? new InteractionServiceConfig()) { }
/// <summary>
/// Initialize a <see cref="InteractionService"/> with provided configurations.
/// </summary>
/// <param name="discord">The discord client.</param>
/// <param name="config">The configuration class.</param>
public InteractionService(DiscordShardedClient discord, InteractionServiceConfig config = null)
: this(() => discord.Rest, config ?? new InteractionServiceConfig()) { }
/// <summary>
/// Initialize a <see cref="InteractionService"/> with provided configurations.
/// </summary>
/// <param name="discord">The discord client.</param>
/// <param name="config">The configuration class.</param>
public InteractionService(BaseSocketClient discord, InteractionServiceConfig config = null)
: this(() => discord.Rest, config ?? new InteractionServiceConfig()) { }
/// <summary> /// <summary>
/// Initialize a <see cref="InteractionService"/> with provided configurations. /// Initialize a <see cref="InteractionService"/> with provided configurations.
/// </summary> /// </summary>
@@ -177,6 +153,14 @@ namespace Discord.Interactions
public InteractionService(DiscordRestClient discord, InteractionServiceConfig config = null) public InteractionService(DiscordRestClient discord, InteractionServiceConfig config = null)
: this(() => discord, config ?? new InteractionServiceConfig()) { } : this(() => discord, config ?? new InteractionServiceConfig()) { }
/// <summary>
/// Initialize a <see cref="InteractionService"/> with provided configurations.
/// </summary>
/// <param name="discordProvider">The discord client provider.</param>
/// <param name="config">The configuration class.</param>
public InteractionService(IRestClientProvider discordProvider, InteractionServiceConfig config = null)
: this(() => discordProvider.RestClient, config ?? new InteractionServiceConfig()) { }
private InteractionService(Func<DiscordRestClient> getRestClient, InteractionServiceConfig config = null) private InteractionService(Func<DiscordRestClient> getRestClient, InteractionServiceConfig config = null)
{ {
config ??= new InteractionServiceConfig(); config ??= new InteractionServiceConfig();

View File

@@ -15,7 +15,7 @@ namespace Discord.Rest
/// <summary> /// <summary>
/// Provides a client to send REST-based requests to Discord. /// Provides a client to send REST-based requests to Discord.
/// </summary> /// </summary>
public class DiscordRestClient : BaseDiscordClient, IDiscordClient public class DiscordRestClient : BaseDiscordClient, IDiscordClient, IRestClientProvider
{ {
#region DiscordRestClient #region DiscordRestClient
private RestApplication _applicationInfo; private RestApplication _applicationInfo;
@@ -399,5 +399,7 @@ namespace Discord.Rest
async Task<IReadOnlyCollection<IApplicationCommand>> IDiscordClient.BulkOverwriteGlobalApplicationCommand(ApplicationCommandProperties[] properties, RequestOptions options) async Task<IReadOnlyCollection<IApplicationCommand>> IDiscordClient.BulkOverwriteGlobalApplicationCommand(ApplicationCommandProperties[] properties, RequestOptions options)
=> await BulkOverwriteGlobalCommands(properties, options).ConfigureAwait(false); => await BulkOverwriteGlobalCommands(properties, options).ConfigureAwait(false);
#endregion #endregion
DiscordRestClient IRestClientProvider.RestClient => this;
} }
} }

View File

@@ -118,5 +118,9 @@ namespace Discord.Rest
//IAutocompleteInteraction //IAutocompleteInteraction
/// <inheritdoc/> /// <inheritdoc/>
IAutocompleteInteractionData IAutocompleteInteraction.Data => Data; IAutocompleteInteractionData IAutocompleteInteraction.Data => Data;
/// <inheritdoc/>
Task IAutocompleteInteraction.RespondAsync(IEnumerable<AutocompleteResult> result, RequestOptions options)
=>Task.FromResult(Respond(result, options));
} }
} }

View File

@@ -0,0 +1,14 @@
using Discord.Rest;
namespace Discord.Rest;
/// <summary>
/// An interface that represents a client provider for Rest-based clients.
/// </summary>
public interface IRestClientProvider
{
/// <summary>
/// Gets the Rest client of this provider.
/// </summary>
DiscordRestClient RestClient { get; }
}

View File

@@ -10,7 +10,7 @@ namespace Discord.WebSocket
/// <summary> /// <summary>
/// Represents the base of a WebSocket-based Discord client. /// Represents the base of a WebSocket-based Discord client.
/// </summary> /// </summary>
public abstract partial class BaseSocketClient : BaseDiscordClient, IDiscordClient public abstract partial class BaseSocketClient : BaseDiscordClient, IDiscordClient, IRestClientProvider
{ {
#region BaseSocketClient #region BaseSocketClient
protected readonly DiscordSocketConfig BaseConfig; protected readonly DiscordSocketConfig BaseConfig;
@@ -352,5 +352,7 @@ namespace Discord.WebSocket
return await GetVoiceRegionsAsync().ConfigureAwait(false); return await GetVoiceRegionsAsync().ConfigureAwait(false);
} }
#endregion #endregion
DiscordRestClient IRestClientProvider.RestClient => Rest;
} }
} }