Browse Source

Added documentation for all the new classes

pull/1923/head
quin lynch 4 years ago
parent
commit
6203c48c11
6 changed files with 213 additions and 2 deletions
  1. +7
    -0
      src/Discord.Net.Core/Entities/Interactions/Message Components/ActionRowComponent.cs
  2. +25
    -0
      src/Discord.Net.Core/Entities/Interactions/Message Components/ButtonComponent.cs
  3. +160
    -1
      src/Discord.Net.Core/Entities/Interactions/Message Components/ComponentBuilder.cs
  4. +9
    -0
      src/Discord.Net.Core/Entities/Interactions/Message Components/MessageComponent.cs
  5. +9
    -1
      src/Discord.Net.WebSocket/Entities/Interaction/Message Components/SocketMessageComponent.cs
  6. +3
    -0
      src/Discord.Net.WebSocket/Entities/Interaction/Message Components/SocketMessageComponentData.cs

+ 7
- 0
src/Discord.Net.Core/Entities/Interactions/Message Components/ActionRowComponent.cs View File

@@ -7,10 +7,17 @@ using System.Threading.Tasks;

namespace Discord
{
/// <summary>
/// Represents a <see cref="IMessageComponent"/> Row for child components to live in.
/// </summary>
public class ActionRowComponent : IMessageComponent
{
/// <inheritdoc/>
public ComponentType Type { get; } = ComponentType.ActionRow;

/// <summary>
/// The child components in this row.
/// </summary>
public IReadOnlyCollection<ButtonComponent> Components { get; internal set; }

internal ActionRowComponent() { }


+ 25
- 0
src/Discord.Net.Core/Entities/Interactions/Message Components/ButtonComponent.cs View File

@@ -7,20 +7,45 @@ using System.Threading.Tasks;

namespace Discord
{
/// <summary>
/// Represents a <see cref="IMessageComponent"/> Button.
/// </summary>
public class ButtonComponent : IMessageComponent
{
/// <inheritdoc/>
public ComponentType Type { get; } = ComponentType.Button;

/// <summary>
/// The <see cref="ButtonStyle"/> of this button, example buttons with each style can be found <see href="https://discord.com/assets/7bb017ce52cfd6575e21c058feb3883b.png">Here</see>.
/// </summary>
public ButtonStyle Style { get; }

/// <summary>
/// The label of the button, this is the text that is shown.
/// </summary>
public string Label { get; }

/// <summary>
/// A <see cref="IEmote"/> that will be displayed with this button.
/// </summary>
public IEmote Emote { get; }

/// <summary>
/// A unique id that will be sent with a <see cref="IDiscordInteraction"/>. This is how you know what button was pressed.
/// </summary>
public string CustomId { get; }

/// <summary>
/// A URL for a <see cref="ButtonStyle.Link"/> button.
/// </summary>
/// <remarks>
/// You cannot have a button with a <b>URL</b> and a <b>CustomId</b>.
/// </remarks>
public string Url { get; }

/// <summary>
/// Whether this button is disabled or not.
/// </summary>
public bool Disabled { get; }

internal ButtonComponent(ButtonStyle style, string label, IEmote emote, string customId, string url, bool disabled)


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

@@ -6,10 +6,19 @@ using System.Threading.Tasks;

namespace Discord
{
/// <summary>
/// Represents a builder for creating a <see cref="MessageComponent"/>.
/// </summary>
public class ComponentBuilder
{
/// <summary>
/// The max amount of rows a message can have.
/// </summary>
public const int MaxActionRowCount = 5;

/// <summary>
/// Gets or sets the Action Rows for this Component Builder.
/// </summary>
public List<ActionRowBuilder> ActionRows
{
get => _actionRows;
@@ -25,11 +34,22 @@ namespace Discord

private List<ActionRowBuilder> _actionRows { get; set; }

/// <summary>
/// Adds a button to the specified row.
/// </summary>
/// <param name="label">The label text for the newly added button.</param>
/// <param name="style">The style of this newly added button.</param>
/// <param name="emote">A <see cref="IEmote"/> to be used with this button.</param>
/// <param name="customId">The custom id of the newly added button.</param>
/// <param name="url">A URL to be used only if the <see cref="ButtonStyle"/> is a Link.</param>
/// <param name="disabled">Whether or not the newly created button is disabled.</param>
/// <param name="row">The row the button should be placed on.</param>
/// <returns>The current builder.</returns>
public ComponentBuilder WithButton(
string label,
string customId,
ButtonStyle style = ButtonStyle.Primary,
IEmote emote = null,
string customId = null,
string url = null,
bool disabled = false,
int row = 0)
@@ -45,9 +65,20 @@ namespace Discord
return this.WithButton(button, row);
}

/// <summary>
/// Adds a button to the first row.
/// </summary>
/// <param name="button">The button to add to the first row.</param>
/// <returns>The current builder.</returns>
public ComponentBuilder WithButton(ButtonBuilder button)
=> this.WithButton(button, 0);

/// <summary>
/// Adds a button to the specified row.
/// </summary>
/// <param name="button">The button to add.</param>
/// <param name="row">The row to add the button.</param>
/// <returns>The current builder.</returns>
public ComponentBuilder WithButton(ButtonBuilder button, int row)
{
var builtButton = button.Build();
@@ -75,6 +106,10 @@ namespace Discord
return this;
}

/// <summary>
/// Builds this builder into a <see cref="MessageComponent"/> used to send your components.
/// </summary>
/// <returns>A <see cref="MessageComponent"/> that can be sent with <see cref="IMessageChannel.SendMessageAsync(string, bool, Embed, RequestOptions, AllowedMentions, MessageReference, MessageComponent)"/></returns>
public MessageComponent Build()
{
if (this._actionRows != null)
@@ -84,9 +119,19 @@ namespace Discord
}
}

/// <summary>
/// Represents a class used to build Action rows.
/// </summary>
public class ActionRowBuilder
{
/// <summary>
/// The max amount of child components this row can hold.
/// </summary>
public const int MaxChildCount = 5;

/// <summary>
/// Gets or sets the components inside this row.
/// </summary>
public List<ButtonComponent> Components
{
get => _components;
@@ -101,12 +146,22 @@ namespace Discord

private List<ButtonComponent> _components { get; set; }

/// <summary>
/// Adds a list of components to the current row.
/// </summary>
/// <param name="components">The list of components to add.</param>
/// <returns>The current builder.</returns>
public ActionRowBuilder WithComponents(List<ButtonComponent> components)
{
this.Components = components;
return this;
}

/// <summary>
/// Adds a component at the end of the current row.
/// </summary>
/// <param name="component">The component to add.</param>
/// <returns>The current builder.</returns>
public ActionRowBuilder WithComponent(ButtonComponent component)
{
if (this.Components == null)
@@ -117,6 +172,12 @@ namespace Discord
return this;
}

/// <summary>
/// Builds the current builder to a <see cref="ActionRowComponent"/> that can be used within a <see cref="ComponentBuilder"/>
/// </summary>
/// <returns>A <see cref="ActionRowComponent"/> that can be used within a <see cref="ComponentBuilder"/></returns>
/// <exception cref="ArgumentNullException"><see cref="Components"/> cannot be null.</exception>
/// <exception cref="ArgumentException">There must be at least 1 component in a row.</exception>
public ActionRowComponent Build()
{
if (this.Components == null)
@@ -129,11 +190,24 @@ namespace Discord
}
}

/// <summary>
/// Represents a class used to build <see cref="ButtonComponent"/>'s.
/// </summary>
public class ButtonBuilder
{
/// <summary>
/// The max length of a <see cref="ButtonComponent.Label"/>.
/// </summary>
public const int MaxLabelLength = 80;

/// <summary>
/// The max length of a <see cref="ButtonComponent.CustomId"/>.
/// </summary>
public const int MaxCustomIdLength = 100;

/// <summary>
/// Gets or sets the label of the current button.
/// </summary>
public string Label
{
get => _label;
@@ -147,6 +221,9 @@ namespace Discord
}
}

/// <summary>
/// Gets or sets the custom id of the current button.
/// </summary>
public string CustomId
{
get => _customId;
@@ -159,15 +236,36 @@ namespace Discord
}
}

/// <summary>
/// Gets or sets the <see cref="ButtonStyle"/> of the current button.
/// </summary>
public ButtonStyle Style { get; set; }

/// <summary>
/// Gets or sets the <see cref="IEmote"/> of the current button.
/// </summary>
public IEmote Emote { get; set; }

/// <summary>
/// Gets or sets the url of the current button.
/// </summary>
public string Url { get; set; }

/// <summary>
/// Gets or sets whether the current button is disabled.
/// </summary>
public bool Disabled { get; set; }


private string _label;
private string _customId;

/// <summary>
/// Creates a button with the <see cref="ButtonStyle.Link"/> style.
/// </summary>
/// <param name="label">The label to use on the newly created link button.</param>
/// <param name="url">The url for this link button to go to.</param>
/// <returns>A builder with the newly created button.</returns>
public static ButtonBuilder CreateLinkButton(string label, string url)
{
var builder = new ButtonBuilder()
@@ -178,6 +276,12 @@ namespace Discord
return builder;
}

/// <summary>
/// Creates a button with the <see cref="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>
/// <returns>A builder with the newly created button.</returns>
public static ButtonBuilder CreateDangerButton(string label, string customId)
{
var builder = new ButtonBuilder()
@@ -188,6 +292,12 @@ namespace Discord
return builder;
}

/// <summary>
/// Creates a button with the <see cref="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>
/// <returns>A builder with the newly created button.</returns>
public static ButtonBuilder CreatePrimaryButton(string label, string customId)
{
var builder = new ButtonBuilder()
@@ -198,6 +308,12 @@ namespace Discord
return builder;
}

/// <summary>
/// Creates a button with the <see cref="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>
/// <returns>A builder with the newly created button.</returns>
public static ButtonBuilder CreateSecondaryButton(string label, string customId)
{
var builder = new ButtonBuilder()
@@ -208,6 +324,12 @@ namespace Discord
return builder;
}

/// <summary>
/// Creates a button with the <see cref="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>
/// <returns>A builder with the newly created button.</returns>
public static ButtonBuilder CreateSuccessButton(string label, string customId)
{
var builder = new ButtonBuilder()
@@ -218,41 +340,78 @@ namespace Discord
return builder;
}

/// <summary>
/// Sets the current buttons label to the specified text.
/// </summary>
/// <param name="label">The text for the label</param>
/// <returns>The current builder.</returns>
public ButtonBuilder WithLabel(string label)
{
this.Label = label;
return this;
}

/// <summary>
/// Sets the current buttons style.
/// </summary>
/// <param name="style">The style for this builders button.</param>
/// <returns>The current builder.</returns>
public ButtonBuilder WithStyle(ButtonStyle style)
{
this.Style = style;
return this;
}

/// <summary>
/// Sets the current buttons emote.
/// </summary>
/// <param name="emote">The emote to use for the current button.</param>
/// <returns>The current builder.</returns>
public ButtonBuilder WithEmote(IEmote emote)
{
this.Emote = emote;
return this;
}

/// <summary>
/// Sets the current buttons url.
/// </summary>
/// <param name="url">The url to use for the current button.</param>
/// <returns>The current builder.</returns>
public ButtonBuilder WithUrl(string url)
{
this.Url = url;
return this;
}

/// <summary>
/// Sets the custom id of the current button.
/// </summary>
/// <param name="id">The id to use for the current button.</param>
/// <returns>The current builder.</returns>
public ButtonBuilder WithCustomId(string id)
{
this.CustomId = id;
return this;
}

/// <summary>
/// Sets whether the current button is disabled.
/// </summary>
/// <param name="disabled">Whether the current button is disabled or not.</param>
/// <returns>The current builder.</returns>
public ButtonBuilder WithDisabled(bool disabled)
{
this.Disabled = disabled;
return this;
}

/// <summary>
/// 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 URL and a CustomId.</exception>
/// <exception cref="ArgumentException">A button must have an Emote or a label.</exception>
public ButtonComponent Build()
{
if (string.IsNullOrEmpty(this.Label) && this.Emote == null)


+ 9
- 0
src/Discord.Net.Core/Entities/Interactions/Message Components/MessageComponent.cs View File

@@ -6,8 +6,14 @@ using System.Threading.Tasks;

namespace Discord
{
/// <summary>
/// Represents a component object used to send components with messages.
/// </summary>
public class MessageComponent
{
/// <summary>
/// The components to be used in a message.
/// </summary>
public IReadOnlyCollection<ActionRowComponent> Components { get; }

internal MessageComponent(List<ActionRowComponent> components)
@@ -15,6 +21,9 @@ namespace Discord
this.Components = components;
}

/// <summary>
/// Returns a empty <see cref="MessageComponent"/>.
/// </summary>
internal static MessageComponent Empty
=> new MessageComponent(new List<ActionRowComponent>());
}


+ 9
- 1
src/Discord.Net.WebSocket/Entities/Interaction/Message Components/SocketMessageComponent.cs View File

@@ -10,10 +10,19 @@ using Discord.Rest;

namespace Discord.WebSocket
{
/// <summary>
/// Represents a Websocket-based interaction type for Message Components.
/// </summary>
public class SocketMessageComponent : SocketInteraction
{
/// <summary>
/// The data received with this interaction, contains the button that was clicked.
/// </summary>
new public SocketMessageComponentData Data { get; }

/// <summary>
/// The message that contained the trigger for this interaction.
/// </summary>
public SocketMessage Message { get; private set; }

internal SocketMessageComponent(DiscordSocketClient client, Model model)
@@ -72,7 +81,6 @@ namespace Discord.WebSocket
/// </returns>
/// <exception cref="ArgumentOutOfRangeException">Message content is too long, length must be less or equal to <see cref="DiscordConfig.MaxMessageSize"/>.</exception>
/// <exception cref="InvalidOperationException">The parameters provided were invalid or the token was invalid.</exception>

public override async Task<RestUserMessage> 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)
{


+ 3
- 0
src/Discord.Net.WebSocket/Entities/Interaction/Message Components/SocketMessageComponentData.cs View File

@@ -7,6 +7,9 @@ using Model = Discord.API.MessageComponentInteractionData;

namespace Discord.WebSocket
{
/// <summary>
/// Represents the data sent with a <see cref="InteractionType.MessageComponent"/>.
/// </summary>
public class SocketMessageComponentData
{
/// <summary>


Loading…
Cancel
Save