From a64d3ec52ee3c65d396f90307e4e26907e7ba2a6 Mon Sep 17 00:00:00 2001 From: Lala Sabathil Date: Tue, 13 Feb 2024 11:22:00 +0100 Subject: [PATCH] feat: support for message create `nonce` and `enforce_nonce` --- .../Entities/Message/DiscordMessage.cs | 6 +++ .../Entities/Message/DiscordMessageBuilder.cs | 37 +++++++++++++++++++ .../Abstractions/Rest/RestChannelPayloads.cs | 12 ++++++ DisCatSharp/Net/Rest/DiscordApiClient.cs | 4 +- 4 files changed, 58 insertions(+), 1 deletion(-) diff --git a/DisCatSharp/Entities/Message/DiscordMessage.cs b/DisCatSharp/Entities/Message/DiscordMessage.cs index c743a54181..9a25ffce4e 100644 --- a/DisCatSharp/Entities/Message/DiscordMessage.cs +++ b/DisCatSharp/Entities/Message/DiscordMessage.cs @@ -286,6 +286,12 @@ public IReadOnlyList Reactions [JsonProperty("nonce", NullValueHandling = NullValueHandling.Ignore)] public string Nonce { get; internal set; } + /// + /// Gets whether the is enforced to be validated. + /// + [JsonProperty("enforce_nonce", NullValueHandling = NullValueHandling.Ignore)] + public bool EnforceNonce { get; internal set; } + /// /// Gets whether the message is pinned. /// diff --git a/DisCatSharp/Entities/Message/DiscordMessageBuilder.cs b/DisCatSharp/Entities/Message/DiscordMessageBuilder.cs index 7584f82994..8f7025b257 100644 --- a/DisCatSharp/Entities/Message/DiscordMessageBuilder.cs +++ b/DisCatSharp/Entities/Message/DiscordMessageBuilder.cs @@ -111,6 +111,38 @@ public DiscordEmbed Embed /// public bool FailOnInvalidReply { get; set; } + /// + /// Gets the nonce for the message. + /// + public string? Nonce { get; internal set; } + + /// + /// Gets whether to enforce the nonce. + /// + public bool EnforceNonce { get; internal set; } + + /// + /// Sets the nonce for the message. + /// + /// The nonce for the message. Max 25 chars. + /// The current builder to be chained. + public DiscordMessageBuilder WithNonce(string nonce) + { + this.Nonce = nonce; + return this; + } + + /// + /// Whether to enforce the nonce. + /// + /// Controls the nonce enforcement. + /// The current builder to be chained. + public DiscordMessageBuilder WithEnforceNonce(bool enforceNonce) + { + this.EnforceNonce = enforceNonce; + return this; + } + /// /// Sets the Content of the Message. /// @@ -430,6 +462,8 @@ public void Clear() this.Sticker = null; this.AttachmentsInternal.Clear(); this.KeepAttachmentsInternal = false; + this.Nonce = null; + this.EnforceNonce = false; } /// @@ -451,6 +485,9 @@ internal void Validate(bool isModify = false) if (this.Components.Any(c => c.Components.Count > 5)) throw new InvalidOperationException("Action rows can only have 5 components"); + + if (this.EnforceNonce && string.IsNullOrEmpty(this.Nonce)) + throw new InvalidOperationException("Nonce enforcement is enabled, but no nonce is set."); } } } diff --git a/DisCatSharp/Net/Abstractions/Rest/RestChannelPayloads.cs b/DisCatSharp/Net/Abstractions/Rest/RestChannelPayloads.cs index 3cab60bbd0..9f6f9c664c 100644 --- a/DisCatSharp/Net/Abstractions/Rest/RestChannelPayloads.cs +++ b/DisCatSharp/Net/Abstractions/Rest/RestChannelPayloads.cs @@ -311,6 +311,18 @@ internal sealed class RestChannelMessageCreatePayload : RestChannelMessageEditPa /// [JsonProperty("message_reference", NullValueHandling = NullValueHandling.Ignore)] public InternalDiscordMessageReference? MessageReference { get; set; } + + /// + /// Gets or sets the nonce sent with the message. + /// + [JsonProperty("nonce", NullValueHandling = NullValueHandling.Ignore)] + public string Nonce { get; internal set; } + + /// + /// Gets or sets whether to enforce the to be validated. + /// + [JsonProperty("enforce_nonce", NullValueHandling = NullValueHandling.Ignore)] + public bool EnforceNonce { get; internal set; } } /// diff --git a/DisCatSharp/Net/Rest/DiscordApiClient.cs b/DisCatSharp/Net/Rest/DiscordApiClient.cs index 202d636bca..dc4120b276 100644 --- a/DisCatSharp/Net/Rest/DiscordApiClient.cs +++ b/DisCatSharp/Net/Rest/DiscordApiClient.cs @@ -2739,7 +2739,9 @@ internal async Task CreateMessageAsync(ulong channelId, DiscordM IsTts = builder.IsTts, HasEmbed = builder.Embeds != null, Embeds = builder.Embeds, - Components = builder.Components + Components = builder.Components, + Nonce = builder.Nonce, + EnforceNonce = builder.EnforceNonce }; if (builder.ReplyId != null)