Skip to content

Commit

Permalink
[Feature]: ComponentBuilder component removal methods (#2644)
Browse files Browse the repository at this point in the history
* New helper methods

* Auto-resolve empty ActionRows

* Resolve potential nulls

* Add RemoveButtonByURL

* split out url method
  • Loading branch information
cjbonn committed Aug 10, 2023
1 parent 166d40f commit d5d7378
Showing 1 changed file with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,39 @@ internal void AddComponent(IMessageComponent component, int row)
}
}

/// <summary>
/// Removes all components of the given type from the <see cref="ComponentBuilder"/>.
/// </summary>
/// <param name="t">The <see cref="ComponentType"/> to remove.</param>
/// <returns>The current builder.</returns>
public ComponentBuilder RemoveComponentsOfType(ComponentType t)
{
this.ActionRows.ForEach(ar => ar.Components.RemoveAll(c => c.Type == t));
return this;
}

/// <summary>
/// Removes a component from the <see cref="ComponentBuilder"/>.
/// </summary>
/// <param name="customId">The custom id of the component.</param>
/// <returns>The current builder.</returns>
public ComponentBuilder RemoveComponent(string customId)
{
this.ActionRows.ForEach(ar => ar.Components.RemoveAll(c => c.CustomId == customId));
return this;
}

/// <summary>
/// Removes a Link Button from the <see cref="ComponentBuilder"/> based on its URL.
/// </summary>
/// <param name="url">The URL of the Link Button.</param>
/// <returns>The current builder.</returns>
public ComponentBuilder RemoveButtonByURL(string url)
{
this.ActionRows.ForEach(ar => ar.Components.RemoveAll(c => c is ButtonComponent b && b.Url == url));
return this;
}

/// <summary>
/// Adds a <see cref="SelectMenuBuilder"/> to the <see cref="ComponentBuilder"/> at the specific row.
/// If the row cannot accept the component then it will add it to a row that can.
Expand Down Expand Up @@ -283,6 +316,11 @@ public MessageComponent Build()
if (_actionRows?.SelectMany(x => x.Components)?.Any(x => x.Type == ComponentType.TextInput) ?? false)
throw new ArgumentException("TextInputComponents are not allowed in messages.", nameof(ActionRows));

if (_actionRows?.Count > 0)
for (int i = 0; i < _actionRows?.Count; i++)
if (_actionRows[i]?.Components?.Count == 0)
_actionRows.RemoveAt(i);

return _actionRows != null
? new MessageComponent(_actionRows.Select(x => x.Build()).ToList())
: MessageComponent.Empty;
Expand Down

0 comments on commit d5d7378

Please sign in to comment.