Skip to content

Commit

Permalink
[Feature] Config Bypass for CanSendResponse (#2932)
Browse files Browse the repository at this point in the history
* Adds bool switch for DeferAsyncTimeCheck in DiscordConfig

* Adds DeferAsyncTimeCheck bool to DiscordSocketClient

* Adds DeferAsyncTimeCheck to DeferAsync in SocketCommandBase

* Adds DeferAsyncTimeCheck to DeferAsync in SocketModal

* Adds DeferAsyncTimeCheck to DeferAsync in SocketMessageComponent

* Changes name of DeferAsyncTimeCheck to be more general

* Moves ResponseInternalTimeCheck from socket client to base client

* Adds ResponseInternalTimeCheck to Rest interactions

* Adds ResponseInternalTimeCheck to socket interactions
  • Loading branch information
WhyNot180 committed May 16, 2024
1 parent aa424f6 commit 9030a5b
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 29 deletions.
8 changes: 8 additions & 0 deletions src/Discord.Net.Core/DiscordConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,14 @@ public class DiscordConfig
/// </remarks>
public bool UseInteractionSnowflakeDate { get; set; } = true;

/// <summary>
/// Gets or sets whether or not any responses to Discord will make an internal expiration check.
/// </summary>
/// <remarks>
/// This should generally be set to <see langword="false"/> in a development environment due to potential latency issues.
/// </remarks>
public bool ResponseInternalTimeCheck { get; set; } = true;

/// <summary>
/// Gets or sets if the Rest/Socket user <see cref="object.ToString"/> override formats the string in respect to bidirectional unicode.
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions src/Discord.Net.Rest/BaseDiscordClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public abstract class BaseDiscordClient : IDiscordClient
public TokenType TokenType => ApiClient.AuthTokenType;
internal bool UseInteractionSnowflakeDate { get; private set; }
internal bool FormatUsersInBidirectionalUnicode { get; private set; }
internal bool ResponseInternalTimeCheck { get; private set; }

/// <summary> Creates a new REST-only Discord client. </summary>
internal BaseDiscordClient(DiscordRestConfig config, API.DiscordRestApiClient client)
Expand All @@ -59,6 +60,7 @@ internal BaseDiscordClient(DiscordRestConfig config, API.DiscordRestApiClient cl

UseInteractionSnowflakeDate = config.UseInteractionSnowflakeDate;
FormatUsersInBidirectionalUnicode = config.FormatUsersInBidirectionalUnicode;
ResponseInternalTimeCheck = config.ResponseInternalTimeCheck;

ApiClient.RequestQueue.RateLimitTriggered += async (id, info, endpoint) =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public override string Respond(
if (!IsValidToken)
throw new InvalidOperationException("Interaction token is no longer valid");

if (!InteractionHelper.CanSendResponse(this))
if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck)
throw new TimeoutException($"Cannot respond to an interaction after {InteractionHelper.ResponseTimeLimit} seconds!");

embeds ??= Array.Empty<Embed>();
Expand Down Expand Up @@ -317,7 +317,7 @@ public override Task<RestFollowupMessage> FollowupWithFilesAsync(
/// </returns>
public override string Defer(bool ephemeral = false, RequestOptions options = null)
{
if (!InteractionHelper.CanSendResponse(this))
if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck)
throw new TimeoutException($"Cannot defer an interaction after {InteractionHelper.ResponseTimeLimit} seconds!");

var response = new API.InteractionResponse
Expand Down Expand Up @@ -352,7 +352,7 @@ public override string Defer(bool ephemeral = false, RequestOptions options = nu
/// <exception cref="InvalidOperationException"></exception>
public override string RespondWithModal(Modal modal, RequestOptions options = null)
{
if (!InteractionHelper.CanSendResponse(this))
if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck)
throw new TimeoutException($"Cannot defer an interaction after {InteractionHelper.ResponseTimeLimit} seconds of no response/acknowledgement");

var response = new API.InteractionResponse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public override string Respond(
if (!IsValidToken)
throw new InvalidOperationException("Interaction token is no longer valid");

if (!InteractionHelper.CanSendResponse(this))
if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck)
throw new TimeoutException($"Cannot respond to an interaction after {InteractionHelper.ResponseTimeLimit} seconds!");

embeds ??= Array.Empty<Embed>();
Expand Down Expand Up @@ -153,7 +153,7 @@ public async Task UpdateAsync(Action<MessageProperties> func, RequestOptions opt
if (!IsValidToken)
throw new InvalidOperationException("Interaction token is no longer valid");

if (!InteractionHelper.CanSendResponse(this))
if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck)
throw new TimeoutException($"Cannot respond to an interaction after {InteractionHelper.ResponseTimeLimit} seconds!");

if (args.AllowedMentions.IsSpecified)
Expand Down Expand Up @@ -424,7 +424,7 @@ public override Task<RestFollowupMessage> FollowupWithFilesAsync(
/// </returns>
public string DeferLoading(bool ephemeral = false, RequestOptions options = null)
{
if (!InteractionHelper.CanSendResponse(this))
if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck)
throw new TimeoutException($"Cannot defer an interaction after {InteractionHelper.ResponseTimeLimit} seconds of no response/acknowledgement");

var response = new API.InteractionResponse
Expand Down Expand Up @@ -458,7 +458,7 @@ public string DeferLoading(bool ephemeral = false, RequestOptions options = null
/// <exception cref="InvalidOperationException"></exception>
public override string Defer(bool ephemeral = false, RequestOptions options = null)
{
if (!InteractionHelper.CanSendResponse(this))
if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck)
throw new TimeoutException($"Cannot defer an interaction after {InteractionHelper.ResponseTimeLimit} seconds of no response/acknowledgement");

var response = new API.InteractionResponse
Expand Down Expand Up @@ -490,7 +490,7 @@ public override string Defer(bool ephemeral = false, RequestOptions options = nu
/// <exception cref="InvalidOperationException"></exception>
public override string RespondWithModal(Modal modal, RequestOptions options = null)
{
if (!InteractionHelper.CanSendResponse(this))
if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck)
throw new TimeoutException($"Cannot defer an interaction after {InteractionHelper.ResponseTimeLimit} seconds of no response/acknowledgement");

var response = new API.InteractionResponse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ internal RestModal(DiscordRestClient client, ModelBase model)
/// </returns>
public override string Defer(bool ephemeral = false, RequestOptions options = null)
{
if (!InteractionHelper.CanSendResponse(this))
if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck)
throw new TimeoutException($"Cannot defer an interaction after {InteractionHelper.ResponseTimeLimit} seconds!");

var response = new API.InteractionResponse
Expand Down Expand Up @@ -91,7 +91,7 @@ public override string Defer(bool ephemeral = false, RequestOptions options = nu
/// </returns>
public string DeferLoading(bool ephemeral = false, RequestOptions options = null)
{
if (!InteractionHelper.CanSendResponse(this))
if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck)
throw new TimeoutException($"Cannot defer an interaction after {InteractionHelper.ResponseTimeLimit} seconds of no response/acknowledgement");

var response = new API.InteractionResponse
Expand Down Expand Up @@ -320,7 +320,7 @@ public override string Respond(
if (!IsValidToken)
throw new InvalidOperationException("Interaction token is no longer valid");

if (!InteractionHelper.CanSendResponse(this))
if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck)
throw new TimeoutException($"Cannot respond to an interaction after {InteractionHelper.ResponseTimeLimit} seconds!");

embeds ??= Array.Empty<Embed>();
Expand Down Expand Up @@ -486,7 +486,7 @@ public async Task UpdateAsync(Action<MessageProperties> func, RequestOptions opt
if (!IsValidToken)
throw new InvalidOperationException("Interaction token is no longer valid");

if (!InteractionHelper.CanSendResponse(this))
if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck)
throw new TimeoutException($"Cannot respond to an interaction after {InteractionHelper.ResponseTimeLimit} seconds!");

if (args.AllowedMentions.IsSpecified)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ internal RestAutocompleteInteraction(DiscordRestClient client, Model model)
/// </returns>
public string Respond(IEnumerable<AutocompleteResult> result, RequestOptions options = null)
{
if (!InteractionHelper.CanSendResponse(this))
if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck)
throw new TimeoutException($"Cannot respond to an interaction after {InteractionHelper.ResponseTimeLimit} seconds!");

lock (_lock)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public override async Task RespondWithFilesAsync(
if (!IsValidToken)
throw new InvalidOperationException("Interaction token is no longer valid");

if (!InteractionHelper.CanSendResponse(this))
if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck)
throw new TimeoutException($"Cannot respond to an interaction after {InteractionHelper.ResponseTimeLimit} seconds!");

embeds ??= Array.Empty<Embed>();
Expand Down Expand Up @@ -154,7 +154,7 @@ public override async Task RespondAsync(
if (!IsValidToken)
throw new InvalidOperationException("Interaction token is no longer valid");

if (!InteractionHelper.CanSendResponse(this))
if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck)
throw new TimeoutException($"Cannot respond to an interaction after {InteractionHelper.ResponseTimeLimit} seconds!");

embeds ??= Array.Empty<Embed>();
Expand Down Expand Up @@ -218,7 +218,7 @@ public async Task UpdateAsync(Action<MessageProperties> func, RequestOptions opt
if (!IsValidToken)
throw new InvalidOperationException("Interaction token is no longer valid");

if (!InteractionHelper.CanSendResponse(this))
if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck)
throw new TimeoutException($"Cannot respond to an interaction after {InteractionHelper.ResponseTimeLimit} seconds!");

if (args.AllowedMentions.IsSpecified)
Expand Down Expand Up @@ -418,7 +418,7 @@ public override Task<RestFollowupMessage> FollowupWithFilesAsync(
/// <inheritdoc/>
public async Task DeferLoadingAsync(bool ephemeral = false, RequestOptions options = null)
{
if (!InteractionHelper.CanSendResponse(this))
if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck)
throw new TimeoutException($"Cannot defer an interaction after {InteractionHelper.ResponseTimeLimit} seconds of no response/acknowledgement");

var response = new API.InteractionResponse
Expand All @@ -442,7 +442,7 @@ public async Task DeferLoadingAsync(bool ephemeral = false, RequestOptions optio
/// <inheritdoc/>
public override async Task DeferAsync(bool ephemeral = false, RequestOptions options = null)
{
if (!InteractionHelper.CanSendResponse(this))
if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck)
throw new TimeoutException($"Cannot defer an interaction after {InteractionHelper.ResponseTimeLimit} seconds of no response/acknowledgement");

var response = new API.InteractionResponse
Expand All @@ -469,7 +469,7 @@ public override async Task RespondWithModalAsync(Modal modal, RequestOptions opt
if (!IsValidToken)
throw new InvalidOperationException("Interaction token is no longer valid");

if (!InteractionHelper.CanSendResponse(this))
if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck)
throw new TimeoutException($"Cannot respond to an interaction after {InteractionHelper.ResponseTimeLimit} seconds!");

var response = new API.InteractionResponse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public override async Task RespondWithFilesAsync(
if (!IsValidToken)
throw new InvalidOperationException("Interaction token is no longer valid");

if (!InteractionHelper.CanSendResponse(this))
if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck)
throw new TimeoutException($"Cannot respond to an interaction after {InteractionHelper.ResponseTimeLimit} seconds!");

embeds ??= Array.Empty<Embed>();
Expand Down Expand Up @@ -151,7 +151,7 @@ public override async Task RespondAsync(
if (!IsValidToken)
throw new InvalidOperationException("Interaction token is no longer valid");

if (!InteractionHelper.CanSendResponse(this))
if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck)
throw new TimeoutException($"Cannot respond to an interaction after {InteractionHelper.ResponseTimeLimit} seconds!");

embeds ??= Array.Empty<Embed>();
Expand Down Expand Up @@ -215,7 +215,7 @@ public async Task UpdateAsync(Action<MessageProperties> func, RequestOptions opt
if (!IsValidToken)
throw new InvalidOperationException("Interaction token is no longer valid");

if (!InteractionHelper.CanSendResponse(this))
if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck)
throw new TimeoutException($"Cannot respond to an interaction after {InteractionHelper.ResponseTimeLimit} seconds!");

if (args.AllowedMentions.IsSpecified)
Expand Down Expand Up @@ -420,7 +420,7 @@ public override Task<RestFollowupMessage> FollowupWithFilesAsync(
/// </remarks>
public override async Task DeferAsync(bool ephemeral = false, RequestOptions options = null)
{
if (!InteractionHelper.CanSendResponse(this))
if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck)
throw new TimeoutException($"Cannot defer an interaction after {InteractionHelper.ResponseTimeLimit} seconds of no response/acknowledgement");

var response = new API.InteractionResponse
Expand Down Expand Up @@ -450,7 +450,7 @@ public override async Task DeferAsync(bool ephemeral = false, RequestOptions opt
/// <inheritdoc/>
public async Task DeferLoadingAsync(bool ephemeral = false, RequestOptions options = null)
{
if (!InteractionHelper.CanSendResponse(this))
if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck)
throw new TimeoutException($"Cannot defer an interaction after {InteractionHelper.ResponseTimeLimit} seconds of no response/acknowledgement");

var response = new API.InteractionResponse
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ internal SocketAutocompleteInteraction(DiscordSocketClient client, Model model,
/// </returns>
public async Task RespondAsync(IEnumerable<AutocompleteResult> result, RequestOptions options = null)
{
if (!InteractionHelper.CanSendResponse(this))
if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck)
throw new TimeoutException($"Cannot respond to an interaction after {InteractionHelper.ResponseTimeLimit} seconds!");

lock (_lock)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public override async Task RespondAsync(
if (!IsValidToken)
throw new InvalidOperationException("Interaction token is no longer valid");

if (!InteractionHelper.CanSendResponse(this))
if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck)
throw new TimeoutException($"Cannot respond to an interaction after {InteractionHelper.ResponseTimeLimit} seconds!");

embeds ??= Array.Empty<Embed>();
Expand Down Expand Up @@ -143,7 +143,7 @@ public override async Task RespondWithModalAsync(Modal modal, RequestOptions opt
if (!IsValidToken)
throw new InvalidOperationException("Interaction token is no longer valid");

if (!InteractionHelper.CanSendResponse(this))
if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck)
throw new TimeoutException($"Cannot respond to an interaction after {InteractionHelper.ResponseTimeLimit} seconds!");

var response = new API.InteractionResponse
Expand Down Expand Up @@ -188,7 +188,7 @@ public override async Task RespondWithFilesAsync(
if (!IsValidToken)
throw new InvalidOperationException("Interaction token is no longer valid");

if (!InteractionHelper.CanSendResponse(this))
if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck)
throw new TimeoutException($"Cannot respond to an interaction after {InteractionHelper.ResponseTimeLimit} seconds!");

embeds ??= Array.Empty<Embed>();
Expand Down Expand Up @@ -352,7 +352,7 @@ public override Task<RestFollowupMessage> FollowupWithFilesAsync(
/// </returns>
public override async Task DeferAsync(bool ephemeral = false, RequestOptions options = null)
{
if (!InteractionHelper.CanSendResponse(this))
if (!InteractionHelper.CanSendResponse(this) && Discord.ResponseInternalTimeCheck)
throw new TimeoutException($"Cannot defer an interaction after {InteractionHelper.ResponseTimeLimit} seconds!");

var response = new API.InteractionResponse
Expand Down

0 comments on commit 9030a5b

Please sign in to comment.