Skip to content

Actually good CV2 ToBuilder methods #3121

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,19 @@ public class ActionRowComponent : IMessageComponent
/// </summary>
public IReadOnlyCollection<IMessageComponent> Components { get; internal set; }

/// <summary>
/// Converts a <see cref="ActionRowComponent"/> to a <see cref="ActionRowBuilder"/>.
/// </summary>
public ActionRowBuilder ToBuilder()
=> new(this);

internal ActionRowComponent() { }

internal ActionRowComponent(IReadOnlyCollection<IMessageComponent> components)
{
Components = components;
}

/// <inheritdoc />
IMessageComponentBuilder IMessageComponent.ToBuilder() => ToBuilder();
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,99 +18,7 @@ public static BuilderT WithId<BuilderT>(this BuilderT builder, int? id)
builder.Id = id;
return builder;
}

/// <summary>
/// Converts a <see cref="IMessageComponent"/> to a builder.
/// </summary>
/// <exception cref="ArgumentException">Unknown component type</exception>
public static IMessageComponentBuilder ToBuilder(this IMessageComponent component)
=> component switch
{
ActionRowComponent actionRow => actionRow.ToBuilder(),
ButtonComponent button => button.ToBuilder(),
SelectMenuComponent select => select.ToBuilder(),
SectionComponent section => section.ToBuilder(),
TextDisplayComponent textDisplay => textDisplay.ToBuilder(),
ThumbnailComponent thumbnail => thumbnail.ToBuilder(),
MediaGalleryComponent mediaGallery => mediaGallery.ToBuilder(),
FileComponent file => file.ToBuilder(),
SeparatorComponent separator => separator.ToBuilder(),
ContainerComponent container => container.ToBuilder(),
_ => throw new ArgumentException("Unknown component type")
};

/// <summary>
/// Converts a <see cref="FileComponent"/> to a <see cref="FileComponentBuilder"/>.
/// </summary>
public static FileComponentBuilder ToBuilder(this FileComponent file)
=> new(file);

/// <summary>
/// Converts a <see cref="SeparatorComponent"/> to a <see cref="SeparatorBuilder"/>.
/// </summary>
public static SeparatorBuilder ToBuilder(this SeparatorComponent separator)
=> new(separator);

/// <summary>
/// Converts a <see cref="MediaGalleryComponent"/> to a <see cref="MediaGalleryBuilder"/>.
/// </summary>
public static MediaGalleryBuilder ToBuilder(this MediaGalleryComponent mediaGallery)
=> new(mediaGallery);

/// <summary>
/// Converts a <see cref="ButtonComponent"/> to a <see cref="ButtonBuilder"/>.
/// </summary>
public static ButtonBuilder ToBuilder(this ButtonComponent button)
=> new(button);

/// <summary>
/// Converts a <see cref="SelectMenuComponent"/> to a <see cref="SelectMenuBuilder"/>.
/// </summary>
public static SelectMenuBuilder ToBuilder(this SelectMenuComponent select)
=> new(select);

/// <summary>
/// Converts a <see cref="ActionRowComponent"/> to a <see cref="ActionRowBuilder"/>.
/// </summary>
public static ActionRowBuilder ToBuilder(this ActionRowComponent actionRow)
=> new(actionRow);

/// <summary>
/// Converts a <see cref="ContainerComponent"/> to a <see cref="ContainerBuilder"/>.
/// </summary>
public static ContainerBuilder ToBuilder(this ContainerComponent container)
=> new(container);

/// <summary>
/// Converts a <see cref="SectionComponent"/> to a <see cref="SectionBuilder"/>.
/// </summary>
public static SectionBuilder ToBuilder(this SectionComponent section)
=> new(section);

/// <summary>
/// Converts a <see cref="ThumbnailComponent"/> to a <see cref="ThumbnailBuilder"/>.
/// </summary>
public static ThumbnailBuilder ToBuilder(this ThumbnailComponent thumbnail)
=> new(thumbnail);

/// <summary>
/// Converts a <see cref="TextDisplayComponent"/> to a <see cref="TextDisplayBuilder"/>.
/// </summary>
public static TextDisplayBuilder ToBuilder(this TextDisplayComponent textDisplay)
=> new (textDisplay);

/// <summary>
/// Converts a <see cref="MediaGalleryItem"/> to a <see cref="MediaGalleryItemProperties"/>.
/// </summary>
public static MediaGalleryItemProperties ToProperties(this MediaGalleryItem item)
=> new(item.Media.ToProperties(), item.Description, item.IsSpoiler);

/// <summary>
/// Converts a <see cref="UnfurledMediaItem"/> to a <see cref="UnfurledMediaItemProperties"/>.
/// </summary>
public static UnfurledMediaItemProperties ToProperties(this UnfurledMediaItem item)
=> new(item.Url);


/// <summary>
/// Converts a collection of <see cref="IMessageComponent"/> to a <see cref="ComponentBuilderV2"/>.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,22 @@ public TextInputBuilder()

}

/// <summary>
/// Creates a new instance of a <see cref="TextInputBuilder"/> from existing component.
/// </summary>
public TextInputBuilder(TextInputComponent textInput)
{
Label = textInput.Label;
Style = textInput.Style;
CustomId = textInput.CustomId;
Placeholder = textInput.Placeholder;
MinLength = textInput.MinLength;
MaxLength = textInput.MaxLength;
Required = textInput.Required;
Value = textInput.Value;
Id = textInput.Id;
}

/// <summary>
/// Sets the label of the current builder.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,10 @@ public class ButtonComponent : IInteractableComponent
public ulong? SkuId { get; }

/// <summary>
/// Turns this button into a button builder.
/// Converts a <see cref="ButtonComponent"/> to a <see cref="ButtonBuilder"/>.
/// </summary>
/// <returns>
/// A newly created button builder with the same properties as this button.
/// </returns>
public ButtonBuilder ToBuilder()
=> new (Label, CustomId, Style, Url, Emote, IsDisabled);
=> new(this);

internal ButtonComponent(ButtonStyle style, string label, IEmote emote, string customId, string url, bool isDisabled, ulong? skuId, int? id)
{
Expand All @@ -70,4 +67,7 @@ internal ButtonComponent(ButtonStyle style, string label, IEmote emote, string c
IsDisabled = isDisabled;
SkuId = skuId;
}

/// <inheritdoc />
IMessageComponentBuilder IMessageComponent.ToBuilder() => ToBuilder();
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,20 @@ public class ContainerComponent : IMessageComponent
/// </summary>
public bool? IsSpoiler { get; }

/// <summary>
/// Converts a <see cref="ContainerComponent"/> to a <see cref="ContainerBuilder"/>.
/// </summary>
public ContainerBuilder ToBuilder()
=> new(this);

internal ContainerComponent(IReadOnlyCollection<IMessageComponent> components, Color? accentColor, bool? isSpoiler, int? id = null)
{
Components = components;
AccentColor = accentColor;
IsSpoiler = isSpoiler;
Id = id;
}

/// <inheritdoc />
IMessageComponentBuilder IMessageComponent.ToBuilder() => ToBuilder();
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,19 @@ public class FileComponent : IMessageComponent
/// </summary>
public bool? IsSpoiler { get; }

/// <summary>
/// Converts a <see cref="FileComponent"/> to a <see cref="FileComponentBuilder"/>.
/// </summary>
public FileComponentBuilder ToBuilder()
=> new(this);

internal FileComponent(UnfurledMediaItem file, bool? isSpoiler, int? id = null)
{
File = file;
IsSpoiler = isSpoiler;
Id = id;
}

/// <inheritdoc />
IMessageComponentBuilder IMessageComponent.ToBuilder() => ToBuilder();
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,13 @@ public interface IMessageComponent
/// </summary>
ComponentType Type { get; }

/// <summary>
///
/// </summary>
int? Id { get; }

/// <summary>
/// Converts a <see cref="IMessageComponent"/> to a <see cref="IMessageComponentBuilder"/>.
/// </summary>
public IMessageComponentBuilder ToBuilder();
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,18 @@ public class MediaGalleryComponent : IMessageComponent
/// </summary>
public IReadOnlyCollection<MediaGalleryItem> Items { get; }

/// <summary>
/// Converts a <see cref="MediaGalleryComponent"/> to a <see cref="MediaGalleryBuilder"/>.
/// </summary>
public MediaGalleryBuilder ToBuilder()
=> new(this);

internal MediaGalleryComponent(IReadOnlyCollection<MediaGalleryItem> items, int? id)
{
Items = items;
Id = id;
}

/// <inheritdoc />
IMessageComponentBuilder IMessageComponent.ToBuilder() => ToBuilder();
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ public readonly struct MediaGalleryItem
/// </summary>
public bool IsSpoiler { get; }

/// <summary>
/// Converts a <see cref="MediaGalleryItem"/> to a <see cref="MediaGalleryItemProperties"/>.
/// </summary>
public MediaGalleryItemProperties ToProperties()
=> new(Media.ToProperties(), Description, IsSpoiler);

internal MediaGalleryItem(UnfurledMediaItem media, string description, bool? isSpoiler)
{
Media = media;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,19 @@ public class SectionComponent : IMessageComponent
/// </summary>
public IMessageComponent Accessory { get; }

/// <summary>
/// Converts a <see cref="SectionComponent"/> to a <see cref="SectionBuilder"/>.
/// </summary>
public SectionBuilder ToBuilder()
=> new(this);

internal SectionComponent(int? id, IReadOnlyCollection<IMessageComponent> components, IMessageComponent accessory)
{
Id = id;
Components = components;
Accessory = accessory;
}

/// <inheritdoc />
IMessageComponentBuilder IMessageComponent.ToBuilder() => ToBuilder();
}
Loading