Skip to content

Commit

Permalink
Implement enforced nonces
Browse files Browse the repository at this point in the history
  • Loading branch information
AnotherZane committed May 15, 2024
1 parent 906076a commit 690025c
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,37 @@ public static TMessage WithReply<TMessage>(this TMessage message, Snowflake mess
/// </remarks>
/// <param name="message"> The <see cref="LocalMessage"/> instance. </param>
/// <param name="nonce"> The nonce. </param>
/// <param name="enforceNonce"> Whether the nonce should be enforced. </param>
/// <typeparam name="TMessage"> The <see cref="LocalMessage"/> type. </typeparam>
/// <returns>
/// The input instance.
/// </returns>
/// <seealso cref="LocalMessage.Nonce"/>
public static TMessage WithNonce<TMessage>(this TMessage message, string nonce)
public static TMessage WithNonce<TMessage>(this TMessage message, string nonce, bool? enforceNonce = null)
where TMessage : LocalMessage
{
message.Nonce = nonce;
message.ShouldEnforceNonce = Optional.FromNullable(enforceNonce);
return message;
}

/// <summary>
/// Sets whether the nonce the uniqueness check of a nonce should be enforced on this message.
/// </summary>
/// <remarks>
/// <inheritdoc cref="LocalMessage.ShouldEnforceNonce"/>
/// </remarks>
/// <param name="message"> The <see cref="LocalMessage"/> instance. </param>
/// <param name="enforceNonce"> Whether the nonce should be enforced. </param>
/// <typeparam name="TMessage"> The <see cref="LocalMessage"/> type. </typeparam>
/// <returns>
/// The input instance.
/// </returns>
/// <seealso cref="LocalMessage.ShouldEnforceNonce"/>
public static TMessage EnforceNonce<TMessage>(this TMessage message, bool enforceNonce)
where TMessage : LocalMessage
{
message.ShouldEnforceNonce = enforceNonce;
return message;
}
}
10 changes: 10 additions & 0 deletions src/Disqord.Core/Entities/Local/Message/LocalMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@ public class LocalMessage : LocalMessageBase, ILocalConstruct<LocalMessage>
/// </remarks>
public Optional<string> Nonce { get; set; }

/// <summary>
/// Gets or sets whether the uniqueness check of a nonce should be enforced.
/// </summary>
/// <remarks>
/// If set to true and another message with the same nonce was created by the bot in the past few minutes,
/// that message will be returned.
/// </remarks>
public Optional<bool> ShouldEnforceNonce { get; set; }

/// <summary>
/// Instantiates a new <see cref="LocalMessage"/>.
/// </summary>
Expand All @@ -40,6 +49,7 @@ protected LocalMessage(LocalMessage other)
{
Reference = other.Reference.Clone();
Nonce = other.Nonce;
ShouldEnforceNonce = other.ShouldEnforceNonce;
}

/// <inheritdoc/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public class CreateMessageJsonRestRequestContent : JsonModelRestRequestContent,
[JsonProperty("flags")]
public Optional<MessageFlags> Flags;

[JsonProperty("enforce_nonce")]
public Optional<bool> EnforceNonce;

IList<PartialAttachmentJsonModel> IAttachmentRestRequestContent.Attachments
{
set => Attachments = new(value);
Expand Down
6 changes: 4 additions & 2 deletions src/Disqord.Rest/Extensions/RestClientExtensions.Channel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,8 @@ public static async Task<IUserMessage> SendMessageAsync(this IRestClient client,
MessageReference = Optional.Convert(message.Reference, reference => reference.ToModel()),
Components = Optional.Convert(message.Components, components => components.Select(component => component.ToModel()).ToArray()),
StickerIds = Optional.Convert(message.StickerIds, stickerIds => stickerIds.ToArray()),
Flags = message.Flags
Flags = message.Flags,
EnforceNonce = message.ShouldEnforceNonce
};

Task<MessageJsonModel> task;
Expand Down Expand Up @@ -649,7 +650,8 @@ public static async Task<IThreadChannel> CreateForumThreadAsync(this IRestClient
MessageReference = Optional.Convert(message.Reference, reference => reference.ToModel()),
Components = Optional.Convert(message.Components, components => components.Select(component => component.ToModel()).ToArray()),
StickerIds = Optional.Convert(message.StickerIds, stickerIds => stickerIds.ToArray()),
Flags = message.Flags
Flags = message.Flags,
EnforceNonce = message.ShouldEnforceNonce
};

var forumContent = new CreateForumThreadJsonRestRequestContent
Expand Down

0 comments on commit 690025c

Please sign in to comment.