Browse Source

URL validation and error handling (#64)

* Error handling on URL additions in embeds and components.

* Wording on exception comment

* Wording on exceptions
pull/1923/head
Hussein Alzein GitHub 3 years ago
parent
commit
014dd9fc2d
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 63 additions and 17 deletions
  1. +23
    -8
      src/Discord.Net.Core/Discord.Net.Core.xml
  2. +16
    -8
      src/Discord.Net.Core/Entities/Interactions/Message Components/ComponentBuilder.cs
  3. +14
    -0
      src/Discord.Net.Core/Entities/Messages/EmbedBuilder.cs
  4. +10
    -1
      src/Discord.Net.WebSocket/Discord.Net.WebSocket.xml

+ 23
- 8
src/Discord.Net.Core/Discord.Net.Core.xml View File

@@ -4107,6 +4107,11 @@
A <see cref="T:Discord.IUser"/> or <see cref="T:Discord.IRole"/>.
</summary>
</member>
<member name="F:Discord.ApplicationCommandOptionType.Number">
<summary>
A <see cref="T:System.Double"/>.
</summary>
</member>
<member name="T:Discord.ApplicationCommandProperties">
<summary>
Provides properties that are used to modify a <see cref="T:Discord.IApplicationCommand" /> with the specified changes.
@@ -4502,7 +4507,8 @@
</member>
<member name="M:Discord.ComponentBuilder.WithButton(System.String,System.String,Discord.ButtonStyle,Discord.IEmote,System.String,System.Boolean,System.Int32)">
<summary>
Adds a button to the specified row.
Adds a <see cref="T:Discord.ButtonBuilder"/> with specified parameters to the <see cref="T:Discord.ComponentBuilder"/> at the specific row.
If the row cannot accept the component then it will add it to a row that can.
</summary>
<param name="label">The label text for the newly added button.</param>
<param name="style">The style of this newly added button.</param>
@@ -4627,44 +4633,49 @@
Creates a new instance of a <see cref="T:Discord.ButtonBuilder"/> from instance of a <see cref="T:Discord.ButtonComponent"/>.
</summary>
</member>
<member name="M:Discord.ButtonBuilder.CreateLinkButton(System.String,System.String)">
<member name="M:Discord.ButtonBuilder.CreateLinkButton(System.String,System.String,Discord.IEmote)">
<summary>
Creates a button with the <see cref="F:Discord.ButtonStyle.Link"/> style.
</summary>
<param name="label">The label to use on the newly created link button.</param>
<param name="label">The label for this link button.</param>
<param name="url">The url for this link button to go to.</param>
<param name="emote">The emote for this link button</param>
<returns>A builder with the newly created button.</returns>
</member>
<member name="M:Discord.ButtonBuilder.CreateDangerButton(System.String,System.String)">
<member name="M:Discord.ButtonBuilder.CreateDangerButton(System.String,System.String,Discord.IEmote)">
<summary>
Creates a button with the <see cref="F:Discord.ButtonStyle.Danger"/> style.
</summary>
<param name="label">The label for this danger button.</param>
<param name="customId">The custom id for this danger button.</param>
<param name="emote">The emote for this danger button</param>
<returns>A builder with the newly created button.</returns>
</member>
<member name="M:Discord.ButtonBuilder.CreatePrimaryButton(System.String,System.String)">
<member name="M:Discord.ButtonBuilder.CreatePrimaryButton(System.String,System.String,Discord.IEmote)">
<summary>
Creates a button with the <see cref="F:Discord.ButtonStyle.Primary"/> style.
</summary>
<param name="label">The label for this primary button.</param>
<param name="customId">The custom id for this primary button.</param>
<param name="emote">The emote for this primary button</param>
<returns>A builder with the newly created button.</returns>
</member>
<member name="M:Discord.ButtonBuilder.CreateSecondaryButton(System.String,System.String)">
<member name="M:Discord.ButtonBuilder.CreateSecondaryButton(System.String,System.String,Discord.IEmote)">
<summary>
Creates a button with the <see cref="F:Discord.ButtonStyle.Secondary"/> style.
</summary>
<param name="label">The label for this secondary button.</param>
<param name="customId">The custom id for this secondary button.</param>
<param name="emote">The emote for this secondary button</param>
<returns>A builder with the newly created button.</returns>
</member>
<member name="M:Discord.ButtonBuilder.CreateSuccessButton(System.String,System.String)">
<member name="M:Discord.ButtonBuilder.CreateSuccessButton(System.String,System.String,Discord.IEmote)">
<summary>
Creates a button with the <see cref="F:Discord.ButtonStyle.Success"/> style.
</summary>
<param name="label">The label for this success button.</param>
<param name="customId">The custom id for this success button.</param>
<param name="emote">The emote for this success button</param>
<returns>A builder with the newly created button.</returns>
</member>
<member name="M:Discord.ButtonBuilder.WithLabel(System.String)">
@@ -4716,8 +4727,11 @@
Builds this builder into a <see cref="T:Discord.ButtonComponent"/> to be used in a <see cref="T:Discord.ComponentBuilder"/>.
</summary>
<returns>A <see cref="T:Discord.ButtonComponent"/> to be used in a <see cref="T:Discord.ComponentBuilder"/>.</returns>
<exception cref="T:System.InvalidOperationException">A button cannot contain a <see cref="P:Discord.ButtonBuilder.Url"/> and a <see cref="P:Discord.ButtonBuilder.CustomId"/>.</exception>
<exception cref="T:System.InvalidOperationException">A button must contain either a <see cref="P:Discord.ButtonBuilder.Url"/> or a <see cref="P:Discord.ButtonBuilder.CustomId"/>, but not both.</exception>
<exception cref="T:System.InvalidOperationException">A button must have an <see cref="P:Discord.ButtonBuilder.Emote"/> or a <see cref="P:Discord.ButtonBuilder.Label"/>.</exception>
<exception cref="T:System.InvalidOperationException">A link button must contain a URL.</exception>
<exception cref="T:System.InvalidOperationException">A link must include a protocol (http or https).</exception>
<exception cref="T:System.InvalidOperationException">A non-link button must contain a custom id</exception>
</member>
<member name="T:Discord.SelectMenuBuilder">
<summary>
@@ -6010,6 +6024,7 @@
The built embed object.
</returns>
<exception cref="T:System.InvalidOperationException">Total embed length exceeds <see cref="F:Discord.EmbedBuilder.MaxEmbedLength"/>.</exception>
<exception cref="T:System.InvalidOperationException">Any Url must include protocols (i.e http:// or https://).</exception>
</member>
<member name="T:Discord.EmbedFieldBuilder">
<summary>


+ 16
- 8
src/Discord.Net.Core/Entities/Interactions/Message Components/ComponentBuilder.cs View File

@@ -520,21 +520,29 @@ namespace Discord
/// Builds this builder into a <see cref="ButtonComponent"/> to be used in a <see cref="ComponentBuilder"/>.
/// </summary>
/// <returns>A <see cref="ButtonComponent"/> to be used in a <see cref="ComponentBuilder"/>.</returns>
/// <exception cref="InvalidOperationException">A button cannot contain a <see cref="Url"/> and a <see cref="CustomId"/>.</exception>
/// <exception cref="InvalidOperationException">A button must contain either a <see cref="Url"/> or a <see cref="CustomId"/>, but not both.</exception>
/// <exception cref="InvalidOperationException">A button must have an <see cref="Emote"/> or a <see cref="Label"/>.</exception>
/// <exception cref="InvalidOperationException">A link button must contain a URL.</exception>
/// <exception cref="InvalidOperationException">A URL must include a protocol (http or https).</exception>
/// <exception cref="InvalidOperationException">A non-link button must contain a custom id</exception>
public ButtonComponent Build()
{
if (string.IsNullOrEmpty(this.Label) && this.Emote == null)
throw new InvalidOperationException("A button must have an Emote or a label!");

if (!string.IsNullOrEmpty(this.Url) && !string.IsNullOrEmpty(this.CustomId))
throw new InvalidOperationException("A button cannot contain a URL and a CustomId!");
if (!(string.IsNullOrEmpty(this.Url) ^ string.IsNullOrEmpty(this.CustomId)))
throw new InvalidOperationException("A button must contain either a URL or a CustomId, but not both!");

if (this.Style == ButtonStyle.Link && !string.IsNullOrEmpty(this.CustomId))
this.CustomId = null;

else if (this.Style != ButtonStyle.Link && !string.IsNullOrEmpty(this.Url)) // Thanks 𝑴𝒓𝑪𝒂𝒌𝒆𝑺𝒍𝒂𝒚𝒆𝒓 :D
this.Url = null;
if (this.Style == ButtonStyle.Link)
{
if (string.IsNullOrEmpty(this.Url))
throw new InvalidOperationException("Link buttons must have a link associated with them");
else if (!Uri.IsWellFormedUriString(this.Url, UriKind.Absolute))
throw new InvalidOperationException("Urls must be well formatted and include their protocol (either HTTP or HTTPS)");
}
else if (string.IsNullOrEmpty(this.CustomId))
throw new InvalidOperationException("Non-link buttons must have a custom id associated with them");

return new ButtonComponent(this.Style, this.Label, this.Emote, this.CustomId, this.Url, this.Disabled);
}


+ 14
- 0
src/Discord.Net.Core/Entities/Messages/EmbedBuilder.cs View File

@@ -401,10 +401,24 @@ namespace Discord
/// The built embed object.
/// </returns>
/// <exception cref="InvalidOperationException">Total embed length exceeds <see cref="MaxEmbedLength"/>.</exception>
/// <exception cref="InvalidOperationException">Any Url must be well formatted include its protocols (i.e http:// or https://).</exception>
public Embed Build()
{
if (Length > MaxEmbedLength)
throw new InvalidOperationException($"Total embed length must be less than or equal to {MaxEmbedLength}.");
if (!string.IsNullOrEmpty(Url) && !Uri.IsWellFormedUriString(Url, UriKind.Absolute))
throw new InvalidOperationException("Url must be well formatted and include its protocol (either HTTP or HTTPS)");
if (!string.IsNullOrEmpty(ThumbnailUrl) && !Uri.IsWellFormedUriString(ThumbnailUrl, UriKind.Absolute))
throw new InvalidOperationException("Thumbnail Url must be well formatted and include its protocol (either HTTP or HTTPS)");
if (!string.IsNullOrEmpty(ImageUrl) && !Uri.IsWellFormedUriString(ImageUrl, UriKind.Absolute))
throw new InvalidOperationException("Image Url must be well formatted and include its protocol (either HTTP or HTTPS)");
if (Author != null)
{
if(!string.IsNullOrEmpty(Author.Url) && !Uri.IsWellFormedUriString(Author.Url, UriKind.Absolute))
throw new InvalidOperationException("Author Url must be well formatted and include its protocol (either HTTP or HTTPS)");
if (!string.IsNullOrEmpty(Author.IconUrl) && !Uri.IsWellFormedUriString(Author.IconUrl, UriKind.Absolute))
throw new InvalidOperationException("Author Icon Url must be well formatted and include its protocol (either HTTP or HTTPS)");
}

var fields = ImmutableArray.CreateBuilder<EmbedField>(Fields.Count);
for (int i = 0; i < Fields.Count; i++)


+ 10
- 1
src/Discord.Net.WebSocket/Discord.Net.WebSocket.xml View File

@@ -3431,10 +3431,11 @@
</member>
<member name="M:Discord.WebSocket.SocketMessageComponent.UpdateAsync(System.Action{Discord.MessageProperties},Discord.RequestOptions)">
<summary>
Updates the original message of the component on which the interaction was received on.
Updates the message which this component resides in with the type <see cref="F:Discord.InteractionResponseType.UpdateMessage"/>
</summary>
<param name="func">A delegate containing the properties to modify the message with.</param>
<param name="options">The request options for this async request.</param>
<returns>A task that represents the asynchronous operation of updating the message.</returns>
</member>
<member name="M:Discord.WebSocket.SocketMessageComponent.FollowupAsync(System.String,Discord.Embed[],System.Boolean,System.Boolean,Discord.AllowedMentions,Discord.RequestOptions,Discord.MessageComponent)">
<inheritdoc/>
@@ -3689,6 +3690,14 @@
<param name="options">The request options for this async request.</param>
<returns>A <see cref="T:Discord.Rest.RestInteractionMessage"/> that represents the initial response.</returns>
</member>
<member name="M:Discord.WebSocket.SocketInteraction.AcknowledgeAsync(Discord.RequestOptions)">
<summary>
Acknowledges this interaction.
</summary>
<returns>
A task that represents the asynchronous operation of acknowledging the interaction.
</returns>
</member>
<member name="M:Discord.WebSocket.SocketInteraction.DeferAsync(Discord.RequestOptions)">
<summary>
Acknowledges this interaction.


Loading…
Cancel
Save