From 170d941925e9bd058c3cd731372280405429b9d0 Mon Sep 17 00:00:00 2001 From: quin lynch Date: Wed, 14 Jul 2021 22:12:30 -0300 Subject: [PATCH] Add support for multiple embeds on interaction response. Fix some preconditions missing --- src/Discord.Net.Core/Discord.Net.Core.csproj | 2 +- src/Discord.Net.Core/Discord.Net.Core.xml | 30 +++++----- .../SocketMessageComponent.cs | 56 +++++-------------- .../Slash Commands/SocketSlashCommand.cs | 20 ++++--- .../Entities/Interaction/SocketInteraction.cs | 46 +++++++++++++-- src/Discord.Net/Discord.Net.nuspec | 8 +-- 6 files changed, 90 insertions(+), 72 deletions(-) diff --git a/src/Discord.Net.Core/Discord.Net.Core.csproj b/src/Discord.Net.Core/Discord.Net.Core.csproj index 25d041d8e..783edc338 100644 --- a/src/Discord.Net.Core/Discord.Net.Core.csproj +++ b/src/Discord.Net.Core/Discord.Net.Core.csproj @@ -8,7 +8,7 @@ net461;netstandard2.0;netstandard2.1 netstandard2.0;netstandard2.1 Discord.Net.Labs.Core - 2.4.4 + 2.4.5 Discord.Net.Labs.Core https://github.com/Discord-Net-Labs/Discord.Net-Labs Temporary.png diff --git a/src/Discord.Net.Core/Discord.Net.Core.xml b/src/Discord.Net.Core/Discord.Net.Core.xml index 6afc006d4..d0298b1e7 100644 --- a/src/Discord.Net.Core/Discord.Net.Core.xml +++ b/src/Discord.Net.Core/Discord.Net.Core.xml @@ -7266,31 +7266,31 @@ The object is an emoji. - + Represents a class used to make timestamps in messages. see . - + Gets or sets the style of the timestamp tag. - + Gets or sets the time for this timestamp tag. - + Converts the current timestamp tag to the string representation supported by discord. - If the is null then the default 0 will be used. + If the is null then the default 0 will be used. A string thats compatable in a discord message, ex: <t:1625944201:f> - + Creates a new timestamp tag with the specified datetime object. @@ -7298,42 +7298,42 @@ The style for this timestamp tag. The newly create timestamp tag. - + - Represents a set of styles to use with a + Represents a set of styles to use with a - + A short time string: 16:20 - + A long time string: 16:20:30 - + A short date string: 20/04/2021 - + A long date string: 20 April 2021 - + A short datetime string: 20 April 2021 16:20 - + A long datetime string: Tuesday, 20 April 2021 16:20 - + The relative time to the user: 2 months ago 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 f42fb58ff..a445d19f2 100644 --- a/src/Discord.Net.WebSocket/Entities/Interaction/Message Components/SocketMessageComponent.cs +++ b/src/Discord.Net.WebSocket/Entities/Interaction/Message Components/SocketMessageComponent.cs @@ -70,27 +70,8 @@ namespace Discord.WebSocket } } - /// - /// Responds to an Interaction. - /// - /// If you have set to , You should use - /// instead. - /// - /// - /// The text of the message to be sent. - /// if the message should be read out by a text-to-speech reader, otherwise . - /// A to send with this response. - /// The type of response to this Interaction. - /// if the response should be hidden to everyone besides the invoker of the command, otherwise . - /// The allowed mentions for this response. - /// The request options for this response. - /// A to be sent with this response - /// - /// The sent as the response. If this is the first acknowledgement, it will return null. - /// - /// Message content is too long, length must be less or equal to . - /// The parameters provided were invalid or the token was invalid. - public override async Task RespondAsync(string text = null, bool isTTS = false, Embed embed = null, InteractionResponseType type = InteractionResponseType.ChannelMessageWithSource, + /// + public override async Task RespondAsync(string text = null, bool isTTS = false, Embed[] embeds = null, InteractionResponseType type = InteractionResponseType.ChannelMessageWithSource, bool ephemeral = false, AllowedMentions allowedMentions = null, RequestOptions options = null, MessageComponent component = null) { if (type == InteractionResponseType.Pong) @@ -101,12 +82,13 @@ namespace Discord.WebSocket if (Discord.AlwaysAcknowledgeInteractions) { - await FollowupAsync(text, isTTS, embed, ephemeral, type, allowedMentions, options); + await FollowupAsync(text, isTTS, embeds, ephemeral, type, allowedMentions, options); return; } 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."); // check that user flag and user Id list are exclusive, same with role flag and role Id list if (allowedMentions != null && allowedMentions.AllowedTypes.HasValue) @@ -131,8 +113,8 @@ namespace Discord.WebSocket Data = new API.InteractionApplicationCommandCallbackData(text) { AllowedMentions = allowedMentions?.ToModel(), - Embeds = embed != null - ? new API.Embed[] { embed.ToModel() } + Embeds = embeds != null + ? embeds.Select(x => x.ToModel()).ToArray() : Optional.Unspecified, TTS = isTTS, Components = component?.Components.Select(x => new API.ActionRowComponent(x)).ToArray() ?? Optional.Unspecified @@ -145,21 +127,8 @@ namespace Discord.WebSocket await InteractionHelper.SendInteractionResponse(this.Discord, this.Channel, response, this.Id, Token, options); } - /// - /// Sends a followup message for this interaction. - /// - /// The text of the message to be sent - /// if the message should be read out by a text-to-speech reader, otherwise . - /// A to send with this response. - /// The type of response to this Interaction. - /// /// if the response should be hidden to everyone besides the invoker of the command, otherwise . - /// The allowed mentions for this response. - /// The request options for this response. - /// A to be sent with this response - /// - /// The sent message. - /// - public override async Task FollowupAsync(string text = null, bool isTTS = false, Embed embed = null, bool ephemeral = false, + /// + public override async Task FollowupAsync(string text = null, bool isTTS = false, Embed[] embeds = null, bool ephemeral = false, InteractionResponseType type = InteractionResponseType.ChannelMessageWithSource, AllowedMentions allowedMentions = null, RequestOptions options = null, MessageComponent component = null) { @@ -169,11 +138,16 @@ namespace Discord.WebSocket if (!IsValidToken) throw new InvalidOperationException("Interaction token is no longer valid"); + 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."); + var args = new API.Rest.CreateWebhookMessageParams(text) { + AllowedMentions = allowedMentions?.ToModel(), IsTTS = isTTS, - Embeds = embed != null - ? new API.Embed[] { embed.ToModel() } + Embeds = embeds != null + ? embeds.Select(x => x.ToModel()).ToArray() : Optional.Unspecified, Components = component?.Components.Select(x => new API.ActionRowComponent(x)).ToArray() ?? Optional.Unspecified }; 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 e67029351..e3bb1168c 100644 --- a/src/Discord.Net.WebSocket/Entities/Interaction/Slash Commands/SocketSlashCommand.cs +++ b/src/Discord.Net.WebSocket/Entities/Interaction/Slash Commands/SocketSlashCommand.cs @@ -51,7 +51,7 @@ namespace Discord.WebSocket } /// - public override async Task RespondAsync(string text = null, bool isTTS = false, Embed embed = null, InteractionResponseType type = InteractionResponseType.ChannelMessageWithSource, + public override async Task RespondAsync(string text = null, bool isTTS = false, Embed[] embeds = null, InteractionResponseType type = InteractionResponseType.ChannelMessageWithSource, bool ephemeral = false, AllowedMentions allowedMentions = null, RequestOptions options = null, MessageComponent component = null) { if (type == InteractionResponseType.Pong) @@ -65,12 +65,13 @@ namespace Discord.WebSocket if (Discord.AlwaysAcknowledgeInteractions) { - await FollowupAsync(text, isTTS, embed, ephemeral, type, allowedMentions, options); + await FollowupAsync(text, isTTS, embeds, ephemeral, type, allowedMentions, options); return; } 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."); // check that user flag and user Id list are exclusive, same with role flag and role Id list if (allowedMentions != null && allowedMentions.AllowedTypes.HasValue) @@ -95,8 +96,8 @@ namespace Discord.WebSocket Data = new API.InteractionApplicationCommandCallbackData(text) { AllowedMentions = allowedMentions?.ToModel(), - Embeds = embed != null - ? new API.Embed[] { embed.ToModel() } + Embeds = embeds != null + ? embeds.Select(x => x.ToModel()).ToArray() : Optional.Unspecified, TTS = isTTS, Components = component?.Components.Select(x => new API.ActionRowComponent(x)).ToArray() ?? Optional.Unspecified @@ -110,7 +111,7 @@ namespace Discord.WebSocket } /// - public override async Task FollowupAsync(string text = null, bool isTTS = false, Embed embed = null, bool ephemeral = false, + public override async Task FollowupAsync(string text = null, bool isTTS = false, Embed[] embeds = null, bool ephemeral = false, InteractionResponseType type = InteractionResponseType.ChannelMessageWithSource, AllowedMentions allowedMentions = null, RequestOptions options = null, MessageComponent component = null) { @@ -120,11 +121,16 @@ namespace Discord.WebSocket if (!IsValidToken) throw new InvalidOperationException("Interaction token is no longer valid"); + 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."); + var args = new API.Rest.CreateWebhookMessageParams(text) { + AllowedMentions = allowedMentions?.ToModel(), IsTTS = isTTS, - Embeds = embed != null - ? new API.Embed[] { embed.ToModel() } + Embeds = embeds != null + ? embeds.Select(x => x.ToModel()).ToArray() : Optional.Unspecified, Components = component?.Components.Select(x => new API.ActionRowComponent(x)).ToArray() ?? Optional.Unspecified }; diff --git a/src/Discord.Net.WebSocket/Entities/Interaction/SocketInteraction.cs b/src/Discord.Net.WebSocket/Entities/Interaction/SocketInteraction.cs index c3a6a8221..d15308c66 100644 --- a/src/Discord.Net.WebSocket/Entities/Interaction/SocketInteraction.cs +++ b/src/Discord.Net.WebSocket/Entities/Interaction/SocketInteraction.cs @@ -99,7 +99,7 @@ namespace Discord.WebSocket /// Responds to an Interaction. /// /// If you have set to , You should use - /// instead. + /// instead. /// /// /// The text of the message to be sent. @@ -112,8 +112,46 @@ namespace Discord.WebSocket /// A to be sent with this response /// Message content is too long, length must be less or equal to . /// The parameters provided were invalid or the token was invalid. + public Task RespondAsync(string text = null, bool isTTS = false, Embed embed = null, InteractionResponseType type = InteractionResponseType.ChannelMessageWithSource, + bool ephemeral = false, AllowedMentions allowedMentions = null, RequestOptions options = null, MessageComponent component = null) + => RespondAsync(text, isTTS, new Embed[] { embed }, type, ephemeral, allowedMentions, options, component); - public abstract Task RespondAsync(string text = null, bool isTTS = false, Embed embed = null, InteractionResponseType type = InteractionResponseType.ChannelMessageWithSource, + /// + /// Sends a followup message for this interaction. + /// + /// The text of the message to be sent + /// if the message should be read out by a text-to-speech reader, otherwise . + /// A to send with this response + /// The type of response to this Interaction. + /// /// if the response should be hidden to everyone besides the invoker of the command, otherwise . + /// The allowed mentions for this response. + /// The request options for this response. + /// A to be sent with this response + /// + /// The sent message. + /// + public Task FollowupAsync(string text = null, bool isTTS = false, Embed embed = null, InteractionResponseType type = InteractionResponseType.ChannelMessageWithSource, + bool ephemeral = false, AllowedMentions allowedMentions = null, RequestOptions options = null, MessageComponent component = null) + => RespondAsync(text, isTTS, new Embed[] { embed }, type, ephemeral, allowedMentions, options, component); + /// + /// Responds to an Interaction. + /// + /// If you have set to , You should use + /// instead. + /// + /// + /// The text of the message to be sent. + /// if the message should be read out by a text-to-speech reader, otherwise . + /// A array of embeds to send with this response. Max 10 + /// The type of response to this Interaction. + /// if the response should be hidden to everyone besides the invoker of the command, otherwise . + /// The allowed mentions for this response. + /// The request options for this response. + /// A to be sent with this response + /// 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, bool isTTS = false, Embed[] embeds = null, InteractionResponseType type = InteractionResponseType.ChannelMessageWithSource, bool ephemeral = false, AllowedMentions allowedMentions = null, RequestOptions options = null, MessageComponent component = null); /// @@ -121,7 +159,7 @@ namespace Discord.WebSocket /// /// The text of the message to be sent /// if the message should be read out by a text-to-speech reader, otherwise . - /// A to send with this response. + /// A array of embeds to send with this response. Max 10 /// The type of response to this Interaction. /// /// if the response should be hidden to everyone besides the invoker of the command, otherwise . /// The allowed mentions for this response. @@ -130,7 +168,7 @@ namespace Discord.WebSocket /// /// The sent message. /// - public abstract Task FollowupAsync(string text = null, bool isTTS = false, Embed embed = null, bool ephemeral = false, + public abstract Task FollowupAsync(string text = null, bool isTTS = false, Embed[] embeds = null, bool ephemeral = false, InteractionResponseType type = InteractionResponseType.ChannelMessageWithSource, AllowedMentions allowedMentions = null, RequestOptions options = null, MessageComponent component = null); diff --git a/src/Discord.Net/Discord.Net.nuspec b/src/Discord.Net/Discord.Net.nuspec index 472e7651e..1c6b72ad7 100644 --- a/src/Discord.Net/Discord.Net.nuspec +++ b/src/Discord.Net/Discord.Net.nuspec @@ -2,7 +2,7 @@ Discord.Net.Labs - 2.4.6$suffix$ + 2.4.7$suffix$ Discord.Net Labs Discord.Net Contributors quinchs @@ -14,21 +14,21 @@ https://avatars.githubusercontent.com/u/84047264 - + - + - +