Skip to content

Commit

Permalink
feat: support for message create nonce and enforce_nonce
Browse files Browse the repository at this point in the history
  • Loading branch information
Lulalaby committed Feb 13, 2024
1 parent b7546d5 commit a64d3ec
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
6 changes: 6 additions & 0 deletions DisCatSharp/Entities/Message/DiscordMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,12 @@ public IReadOnlyList<DiscordReaction> Reactions
[JsonProperty("nonce", NullValueHandling = NullValueHandling.Ignore)]
public string Nonce { get; internal set; }

/// <summary>
/// Gets whether the <see cref="Nonce"/> is enforced to be validated.
/// </summary>
[JsonProperty("enforce_nonce", NullValueHandling = NullValueHandling.Ignore)]
public bool EnforceNonce { get; internal set; }

/// <summary>
/// Gets whether the message is pinned.
/// </summary>
Expand Down
37 changes: 37 additions & 0 deletions DisCatSharp/Entities/Message/DiscordMessageBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,38 @@ public DiscordEmbed Embed
/// </summary>
public bool FailOnInvalidReply { get; set; }

/// <summary>
/// Gets the nonce for the message.
/// </summary>
public string? Nonce { get; internal set; }

/// <summary>
/// Gets whether to enforce the nonce.
/// </summary>
public bool EnforceNonce { get; internal set; }

/// <summary>
/// Sets the nonce for the message.
/// </summary>
/// <param name="nonce">The nonce for the message. Max 25 chars.</param>
/// <returns>The current builder to be chained.</returns>
public DiscordMessageBuilder WithNonce(string nonce)
{
this.Nonce = nonce;
return this;
}

/// <summary>
/// Whether to enforce the nonce.
/// </summary>
/// <param name="enforceNonce">Controls the nonce enforcement.</param>
/// <returns>The current builder to be chained.</returns>
public DiscordMessageBuilder WithEnforceNonce(bool enforceNonce)
{
this.EnforceNonce = enforceNonce;
return this;
}

/// <summary>
/// Sets the Content of the Message.
/// </summary>
Expand Down Expand Up @@ -430,6 +462,8 @@ public void Clear()
this.Sticker = null;
this.AttachmentsInternal.Clear();
this.KeepAttachmentsInternal = false;
this.Nonce = null;
this.EnforceNonce = false;
}

/// <summary>
Expand All @@ -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.");
}
}
}
12 changes: 12 additions & 0 deletions DisCatSharp/Net/Abstractions/Rest/RestChannelPayloads.cs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,18 @@ internal sealed class RestChannelMessageCreatePayload : RestChannelMessageEditPa
/// </summary>
[JsonProperty("message_reference", NullValueHandling = NullValueHandling.Ignore)]
public InternalDiscordMessageReference? MessageReference { get; set; }

/// <summary>
/// Gets or sets the nonce sent with the message.
/// </summary>
[JsonProperty("nonce", NullValueHandling = NullValueHandling.Ignore)]
public string Nonce { get; internal set; }

/// <summary>
/// Gets or sets whether to enforce the <see cref="Nonce"/> to be validated.
/// </summary>
[JsonProperty("enforce_nonce", NullValueHandling = NullValueHandling.Ignore)]
public bool EnforceNonce { get; internal set; }
}

/// <summary>
Expand Down
4 changes: 3 additions & 1 deletion DisCatSharp/Net/Rest/DiscordApiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2739,7 +2739,9 @@ internal async Task<DiscordMessage> 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)
Expand Down

0 comments on commit a64d3ec

Please sign in to comment.