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
{
/// <summary>
@@ -9,5 +12,18 @@ namespace Discord
/// Gets the autocomplete data of this interaction.
/// </summary>
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);
if (result.IsSuccess)
switch (autocompleteInteraction)
{
var task = autocompleteInteraction.RespondAsync(result.Suggestions);
await task;
if (task is Task<string> 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;
}

View File

@@ -11,11 +11,10 @@
<IsTrimmable>false</IsTrimmable>
<IsAotCompatible>false</IsAotCompatible>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Discord.Net.Core\Discord.Net.Core.csproj" />
<ProjectReference Include="..\Discord.Net.Rest\Discord.Net.Rest.csproj" />
<ProjectReference Include="..\Discord.Net.WebSocket\Discord.Net.WebSocket.csproj" />
</ItemGroup>
<ItemGroup>

View File

@@ -49,7 +49,7 @@ namespace Discord.Interactions
public int? MaxLength { get; }
/// <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"/>.
/// </summary>
public TypeConverter TypeConverter { get; }

View File

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

View File

@@ -145,30 +145,6 @@ namespace Discord.Interactions
/// </summary>
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>
/// Initialize a <see cref="InteractionService"/> with provided configurations.
/// </summary>
@@ -177,6 +153,14 @@ namespace Discord.Interactions
public InteractionService(DiscordRestClient discord, InteractionServiceConfig config = null)
: 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)
{
config ??= new InteractionServiceConfig();

View File

@@ -15,7 +15,7 @@ namespace Discord.Rest
/// <summary>
/// Provides a client to send REST-based requests to Discord.
/// </summary>
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<IReadOnlyCollection<IApplicationCommand>> IDiscordClient.BulkOverwriteGlobalApplicationCommand(ApplicationCommandProperties[] properties, RequestOptions options)
=> await BulkOverwriteGlobalCommands(properties, options).ConfigureAwait(false);
#endregion
DiscordRestClient IRestClientProvider.RestClient => this;
}
}

View File

@@ -118,5 +118,9 @@ namespace Discord.Rest
//IAutocompleteInteraction
/// <inheritdoc/>
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>
/// Represents the base of a WebSocket-based Discord client.
/// </summary>
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;
}
}