From 9030a5b2bb15ba356739d6f57dbf71f97d0edc80 Mon Sep 17 00:00:00 2001
From: Bence Szucs <99207620+WhyNot180@users.noreply.github.com>
Date: Thu, 16 May 2024 17:25:21 -0400
Subject: [PATCH] [Feature] Config Bypass for CanSendResponse (#2932)
* 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
---
src/Discord.Net.Core/DiscordConfig.cs | 8 ++++++++
src/Discord.Net.Rest/BaseDiscordClient.cs | 2 ++
.../Interactions/CommandBase/RestCommandBase.cs | 6 +++---
.../MessageComponents/RestMessageComponent.cs | 10 +++++-----
.../Entities/Interactions/Modals/RestModal.cs | 8 ++++----
.../SlashCommands/RestAutocompleteInteraction.cs | 2 +-
.../MessageComponents/SocketMessageComponent.cs | 12 ++++++------
.../Entities/Interaction/Modals/SocketModal.cs | 10 +++++-----
.../SlashCommands/SocketAutocompleteInteraction.cs | 2 +-
.../SocketBaseCommand/SocketCommandBase.cs | 8 ++++----
10 files changed, 39 insertions(+), 29 deletions(-)
diff --git a/src/Discord.Net.Core/DiscordConfig.cs b/src/Discord.Net.Core/DiscordConfig.cs
index 4056484e39..c98b7c44d8 100644
--- a/src/Discord.Net.Core/DiscordConfig.cs
+++ b/src/Discord.Net.Core/DiscordConfig.cs
@@ -205,6 +205,14 @@ public class DiscordConfig
///
public bool UseInteractionSnowflakeDate { get; set; } = true;
+ ///
+ /// Gets or sets whether or not any responses to Discord will make an internal expiration check.
+ ///
+ ///
+ /// This should generally be set to in a development environment due to potential latency issues.
+ ///
+ public bool ResponseInternalTimeCheck { get; set; } = true;
+
///
/// Gets or sets if the Rest/Socket user override formats the string in respect to bidirectional unicode.
///
diff --git a/src/Discord.Net.Rest/BaseDiscordClient.cs b/src/Discord.Net.Rest/BaseDiscordClient.cs
index d369050e72..372dea35a2 100644
--- a/src/Discord.Net.Rest/BaseDiscordClient.cs
+++ b/src/Discord.Net.Rest/BaseDiscordClient.cs
@@ -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; }
/// Creates a new REST-only Discord client.
internal BaseDiscordClient(DiscordRestConfig config, API.DiscordRestApiClient client)
@@ -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) =>
{
diff --git a/src/Discord.Net.Rest/Entities/Interactions/CommandBase/RestCommandBase.cs b/src/Discord.Net.Rest/Entities/Interactions/CommandBase/RestCommandBase.cs
index ab51adda02..dae9b890d7 100644
--- a/src/Discord.Net.Rest/Entities/Interactions/CommandBase/RestCommandBase.cs
+++ b/src/Discord.Net.Rest/Entities/Interactions/CommandBase/RestCommandBase.cs
@@ -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