From a1a498d4345c8b44927b27b026334d4dd518eb29 Mon Sep 17 00:00:00 2001 From: Hussein Alzein Date: Sat, 31 Jul 2021 15:40:43 -0400 Subject: [PATCH] Restoring the option to send a single embed in interaction responses. (#72) * Error handling on URL additions in embeds and components. * Wording on exception comment * Wording on exceptions * changed IsWellFormatted to validating urls start with protocol. May not keep all url validation, may just keep image-based url validation as those definitely only require http/s. * Helper utility made for url validation * Add embed for single embed to Respond/FollowupAsync * xml * Removed trailing spaces. Clarified params in SocketInteractions. Co-authored-by: Quin Lynch <49576606+quinchs@users.noreply.github.com> --- .../Message Components/SocketMessageComponent.cs | 11 ++++++++--- .../Slash Commands/SocketSlashCommand.cs | 14 ++++++++++---- .../Entities/Interaction/SocketInteraction.cs | 10 ++++++---- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/Discord.Net.WebSocket/Entities/Interaction/Message Components/SocketMessageComponent.cs b/src/Discord.Net.WebSocket/Entities/Interaction/Message Components/SocketMessageComponent.cs index 338ef52a0..921f81de4 100644 --- a/src/Discord.Net.WebSocket/Entities/Interaction/Message Components/SocketMessageComponent.cs +++ b/src/Discord.Net.WebSocket/Entities/Interaction/Message Components/SocketMessageComponent.cs @@ -75,10 +75,13 @@ namespace Discord.WebSocket bool ephemeral = false, AllowedMentions allowedMentions = null, RequestOptions options = null, - MessageComponent component = null) + MessageComponent component = null, + Embed embed = null) { if (!IsValidToken) throw new InvalidOperationException("Interaction token is no longer valid"); + if (embeds == null && embed != null) + embeds = new[] { embed }; if (Discord.AlwaysAcknowledgeInteractions) { @@ -106,7 +109,6 @@ namespace Discord.WebSocket } } - var response = new API.InteractionResponse { Type = InteractionResponseType.ChannelMessageWithSource, @@ -193,11 +195,14 @@ namespace Discord.WebSocket bool ephemeral = false, AllowedMentions allowedMentions = null, RequestOptions options = null, - MessageComponent component = null) + MessageComponent component = null, + Embed embed = null) { if (!IsValidToken) throw new InvalidOperationException("Interaction token is no longer valid"); + if (embeds == null && embed != null) + embeds = new[] { embed }; Preconditions.AtMost(allowedMentions?.RoleIds?.Count ?? 0, 100, nameof(allowedMentions.RoleIds), "A max of 100 role Ids are allowed."); Preconditions.AtMost(allowedMentions?.UserIds?.Count ?? 0, 100, nameof(allowedMentions.UserIds), "A max of 100 user Ids are allowed."); Preconditions.AtMost(embeds?.Length ?? 0, 10, nameof(embeds), "A max of 10 embeds are allowed."); diff --git a/src/Discord.Net.WebSocket/Entities/Interaction/Slash Commands/SocketSlashCommand.cs b/src/Discord.Net.WebSocket/Entities/Interaction/Slash Commands/SocketSlashCommand.cs index 81518cd95..245274613 100644 --- a/src/Discord.Net.WebSocket/Entities/Interaction/Slash Commands/SocketSlashCommand.cs +++ b/src/Discord.Net.WebSocket/Entities/Interaction/Slash Commands/SocketSlashCommand.cs @@ -57,11 +57,15 @@ namespace Discord.WebSocket bool ephemeral = false, AllowedMentions allowedMentions = null, RequestOptions options = null, - MessageComponent component = null) + MessageComponent component = null, + Embed embed = null) { if (!IsValidToken) throw new InvalidOperationException("Interaction token is no longer valid"); + if (embeds == null && embed != null) + embeds = new[] { embed }; + if (Discord.AlwaysAcknowledgeInteractions) { await FollowupAsync(text, embeds, isTTS, ephemeral, allowedMentions, options, component); @@ -87,8 +91,7 @@ namespace Discord.WebSocket throw new ArgumentException("The Roles flag is mutually exclusive with the list of Role Ids.", nameof(allowedMentions)); } } - - + var response = new API.InteractionResponse { Type = InteractionResponseType.ChannelMessageWithSource, @@ -116,11 +119,14 @@ namespace Discord.WebSocket bool ephemeral = false, AllowedMentions allowedMentions = null, RequestOptions options = null, - MessageComponent component = null) + MessageComponent component = null, + Embed embed = null) { if (!IsValidToken) throw new InvalidOperationException("Interaction token is no longer valid"); + if (embeds == null && embed != null) + embeds = new[] { embed }; Preconditions.AtMost(allowedMentions?.RoleIds?.Count ?? 0, 100, nameof(allowedMentions.RoleIds), "A max of 100 role Ids are allowed."); Preconditions.AtMost(allowedMentions?.UserIds?.Count ?? 0, 100, nameof(allowedMentions.UserIds), "A max of 100 user Ids are allowed."); Preconditions.AtMost(embeds?.Length ?? 0, 10, nameof(embeds), "A max of 10 embeds are allowed."); diff --git a/src/Discord.Net.WebSocket/Entities/Interaction/SocketInteraction.cs b/src/Discord.Net.WebSocket/Entities/Interaction/SocketInteraction.cs index e49205d7c..4b2e3baec 100644 --- a/src/Discord.Net.WebSocket/Entities/Interaction/SocketInteraction.cs +++ b/src/Discord.Net.WebSocket/Entities/Interaction/SocketInteraction.cs @@ -96,7 +96,7 @@ namespace Discord.WebSocket /// Responds to an Interaction with type . /// /// If you have set to , You should use - /// instead. + /// instead. /// /// /// The text of the message to be sent. @@ -106,10 +106,11 @@ namespace Discord.WebSocket /// The allowed mentions for this response. /// The request options for this response. /// A to be sent with this response + /// A single embed to send with this response. If this is passed alongside an array of embeds, the single embed will be ignored. /// Message content is too long, length must be less or equal to . /// The parameters provided were invalid or the token was invalid. public abstract Task RespondAsync(string text = null, Embed[] embeds = null, bool isTTS = false, - bool ephemeral = false, AllowedMentions allowedMentions = null, RequestOptions options = null, MessageComponent component = null); + bool ephemeral = false, AllowedMentions allowedMentions = null, RequestOptions options = null, MessageComponent component = null, Embed embed = null); /// /// Sends a followup message for this interaction. @@ -121,11 +122,12 @@ namespace Discord.WebSocket /// The allowed mentions for this response. /// The request options for this response. /// A to be sent with this response + /// A single embed to send with this response. If this is passed alongside an array of embeds, the single embed will be ignored. /// /// The sent message. /// - public abstract Task FollowupAsync(string text = null, Embed[] embeds = null, bool isTTS = false, bool ephemeral = false, - AllowedMentions allowedMentions = null, RequestOptions options = null, MessageComponent component = null); + public abstract Task FollowupAsync(string text = null, Embed[] embeds = null, bool isTTS = false, bool ephemeral = false, + AllowedMentions allowedMentions = null, RequestOptions options = null, MessageComponent component = null, Embed embed = null); /// /// Gets the original response for this interaction.