[Fix] Update modal's DeferAsync impl (#2722)
* initial commit * updates
This commit is contained in:
@@ -31,5 +31,13 @@ namespace Discord
|
||||
/// This method can be used only if the modal was created from a message component.
|
||||
/// </remarks>
|
||||
Task UpdateAsync(Action<MessageProperties> func, RequestOptions options = null);
|
||||
|
||||
/// <summary>
|
||||
/// Defers an interaction with the response type 5 (<see cref="InteractionResponseType.DeferredChannelMessageWithSource"/>).
|
||||
/// </summary>
|
||||
/// <param name="ephemeral"><see langword="true"/> to defer ephemerally, otherwise <see langword="false"/>.</param>
|
||||
/// <param name="options">The options to be used when sending the request.</param>
|
||||
/// <returns>A task that represents the asynchronous operation of acknowledging the interaction.</returns>
|
||||
Task DeferLoadingAsync(bool ephemeral = false, RequestOptions options = null);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,7 +43,8 @@ namespace Discord.Rest
|
||||
private object _lock = new object();
|
||||
|
||||
/// <summary>
|
||||
/// Acknowledges this interaction with the <see cref="InteractionResponseType.DeferredChannelMessageWithSource"/>.
|
||||
/// Acknowledges this interaction with the <see cref="InteractionResponseType.DeferredUpdateMessage"/> if the modal was created
|
||||
/// in a response to a message component interaction, <see cref="InteractionResponseType.DeferredChannelMessageWithSource"/> otherwise.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// A string that contains json to write back to the incoming http request.
|
||||
@@ -55,7 +56,9 @@ namespace Discord.Rest
|
||||
|
||||
var response = new API.InteractionResponse
|
||||
{
|
||||
Type = InteractionResponseType.DeferredChannelMessageWithSource,
|
||||
Type = Message is not null
|
||||
? InteractionResponseType.DeferredUpdateMessage
|
||||
: InteractionResponseType.DeferredChannelMessageWithSource,
|
||||
Data = new API.InteractionCallbackData
|
||||
{
|
||||
Flags = ephemeral ? MessageFlags.Ephemeral : Optional<MessageFlags>.Unspecified
|
||||
@@ -78,6 +81,39 @@ namespace Discord.Rest
|
||||
return SerializePayload(response);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Defers an interaction and responds with type 5 (<see cref="InteractionResponseType.DeferredChannelMessageWithSource"/>)
|
||||
/// </summary>
|
||||
/// <param name="ephemeral"><see langword="true"/> to send this message ephemerally, otherwise <see langword="false"/>.</param>
|
||||
/// <param name="options">The request options for this <see langword="async"/> request.</param>
|
||||
/// <returns>
|
||||
/// A string that contains json to write back to the incoming http request.
|
||||
/// </returns>
|
||||
public string DeferLoading(bool ephemeral = false, RequestOptions options = null)
|
||||
{
|
||||
if (!InteractionHelper.CanSendResponse(this))
|
||||
throw new TimeoutException($"Cannot defer an interaction after {InteractionHelper.ResponseTimeLimit} seconds of no response/acknowledgement");
|
||||
|
||||
var response = new API.InteractionResponse
|
||||
{
|
||||
Type = InteractionResponseType.DeferredChannelMessageWithSource,
|
||||
Data = ephemeral ? new API.InteractionCallbackData { Flags = MessageFlags.Ephemeral } : Optional<API.InteractionCallbackData>.Unspecified
|
||||
};
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
if (HasResponded)
|
||||
{
|
||||
throw new InvalidOperationException("Cannot respond or defer twice to the same interaction");
|
||||
}
|
||||
|
||||
HasResponded = true;
|
||||
}
|
||||
|
||||
return SerializePayload(response);
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Sends a followup message for this interaction.
|
||||
/// </summary>
|
||||
@@ -413,6 +449,10 @@ namespace Discord.Rest
|
||||
|
||||
IModalInteractionData IModalInteraction.Data => Data;
|
||||
|
||||
/// <inheritdoc />
|
||||
Task IModalInteraction.DeferLoadingAsync(bool ephemeral, RequestOptions options)
|
||||
=> Task.FromResult(DeferLoading(ephemeral, options));
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task UpdateAsync(Action<MessageProperties> func, RequestOptions options = null)
|
||||
{
|
||||
|
||||
@@ -376,6 +376,10 @@ namespace Discord.WebSocket
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
/// <remarks>
|
||||
/// Acknowledges this interaction with the <see cref="InteractionResponseType.DeferredUpdateMessage"/> if the modal was created
|
||||
/// in a response to a message component interaction, <see cref="InteractionResponseType.DeferredChannelMessageWithSource"/> otherwise.
|
||||
/// </remarks>
|
||||
public override async Task DeferAsync(bool ephemeral = false, RequestOptions options = null)
|
||||
{
|
||||
if (!InteractionHelper.CanSendResponse(this))
|
||||
@@ -383,7 +387,9 @@ namespace Discord.WebSocket
|
||||
|
||||
var response = new API.InteractionResponse
|
||||
{
|
||||
Type = InteractionResponseType.DeferredUpdateMessage,
|
||||
Type = Message is not null
|
||||
? InteractionResponseType.DeferredUpdateMessage
|
||||
: InteractionResponseType.DeferredChannelMessageWithSource,
|
||||
Data = ephemeral ? new API.InteractionCallbackData { Flags = MessageFlags.Ephemeral } : Optional<API.InteractionCallbackData>.Unspecified
|
||||
};
|
||||
|
||||
@@ -403,6 +409,30 @@ namespace Discord.WebSocket
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public async Task DeferLoadingAsync(bool ephemeral = false, RequestOptions options = null)
|
||||
{
|
||||
if (!InteractionHelper.CanSendResponse(this))
|
||||
throw new TimeoutException($"Cannot defer an interaction after {InteractionHelper.ResponseTimeLimit} seconds of no response/acknowledgement");
|
||||
|
||||
var response = new API.InteractionResponse
|
||||
{
|
||||
Type = InteractionResponseType.DeferredChannelMessageWithSource,
|
||||
Data = ephemeral ? new API.InteractionCallbackData { Flags = MessageFlags.Ephemeral } : Optional<API.InteractionCallbackData>.Unspecified
|
||||
};
|
||||
|
||||
lock (_lock)
|
||||
{
|
||||
if (HasResponded)
|
||||
{
|
||||
throw new InvalidOperationException("Cannot respond or defer twice to the same interaction");
|
||||
}
|
||||
}
|
||||
|
||||
await Discord.Rest.ApiClient.CreateInteractionResponseAsync(response, Id, Token, options).ConfigureAwait(false);
|
||||
HasResponded = true;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override Task RespondWithModalAsync(Modal modal, RequestOptions options = null)
|
||||
=> throw new NotSupportedException("You cannot respond to a modal with a modal!");
|
||||
|
||||
Reference in New Issue
Block a user