Skip to content

Commit

Permalink
moved common builder base to single file
Browse files Browse the repository at this point in the history
  • Loading branch information
Lulalaby committed Dec 15, 2024
1 parent 8efa76e commit 3d982d8
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 302 deletions.
2 changes: 1 addition & 1 deletion DisCatSharp.CommandsNext/CommandsNextExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ public async Task DefaultHelpAsync(CommandContext ctx, [Description("Command to

var helpMessage = helpBuilder.Build();

var builder = new DiscordMessageBuilder().WithContent(helpMessage.Content).WithEmbed(helpMessage.Embed);
var builder = new DiscordMessageBuilder().WithContent(helpMessage.Content).AddEmbed(helpMessage.Embed);

if (!ctx.Config.DmHelp || ctx.Channel is DiscordDmChannel || ctx.Guild == null)
await ctx.RespondAsync(builder).ConfigureAwait(false);
Expand Down
2 changes: 1 addition & 1 deletion DisCatSharp.Interactivity/EventHandling/Paginator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ private async Task PaginateAsync(IPaginationRequest p, DiscordEmoji emoji)
var page = await p.GetPageAsync().ConfigureAwait(false);
var builder = new DiscordMessageBuilder()
.WithContent(page.Content)
.WithEmbed(page.Embed);
.AddEmbed(page.Embed);

await builder.ModifyAsync(msg).ConfigureAwait(false);
}
Expand Down
4 changes: 2 additions & 2 deletions DisCatSharp.Interactivity/InteractivityExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ public async Task SendPaginatedMessageAsync(

var builder = new DiscordMessageBuilder()
.WithContent(pages.First().Content)
.WithEmbed(pages.First().Embed)
.AddEmbed(pages.First().Embed)
.AddComponents(bts.ButtonArray);

var message = await builder.SendAsync(channel).ConfigureAwait(false);
Expand Down Expand Up @@ -830,7 +830,7 @@ public async Task SendPaginatedMessageAsync(
{
var builder = new DiscordMessageBuilder()
.WithContent(pages.First().Content)
.WithEmbed(pages.First().Embed);
.AddEmbed(pages.First().Embed);
var m = await builder.SendAsync(channel).ConfigureAwait(false);

var timeout = timeoutOverride ?? this.Config.Timeout;
Expand Down
81 changes: 78 additions & 3 deletions DisCatSharp/Entities/Core/DisCatSharpBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;

namespace DisCatSharp.Entities.Core;
Expand All @@ -8,12 +9,86 @@ namespace DisCatSharp.Entities.Core;
public class DisCatSharpBuilder
{
/// <summary>
/// The components.
/// The attachments of this builder.
/// </summary>
internal readonly List<DiscordComponent> ComponentsInternal = [];
internal List<DiscordAttachment> AttachmentsInternal { get; } = [];

/// <summary>
/// Components to send.
/// The components of this builder.
/// </summary>
internal List<DiscordComponent> ComponentsInternal { get; } = [];

/// <summary>
/// The embeds of this builder.
/// </summary>
internal List<DiscordEmbed> EmbedsInternal { get; } = [];

/// <summary>
/// The files of this builder.
/// </summary>
internal List<DiscordMessageFile> FilesInternal { get; } = [];

/// <summary>
/// The content of this builder.
/// </summary>
internal string? ContentInternal { get; set; }

/// <summary>
/// The components of this builder.
/// </summary>
public IReadOnlyList<DiscordComponent> Components => this.ComponentsInternal;

/// <summary>
/// The content of this builder.
/// </summary>
public string? Content
{
get => this.ContentInternal;
set
{
if (value is { Length: > 2000 })
throw new ArgumentException("Content length cannot exceed 2000 characters.", nameof(value));

this.ContentInternal = value;
}
}

/// <summary>
/// The embeds for this builder.
/// </summary>
public IReadOnlyList<DiscordEmbed> Embeds => this.EmbedsInternal;

/// <summary>
/// The attachments of this builder.
/// </summary>
public IReadOnlyList<DiscordAttachment> Attachments => this.AttachmentsInternal;

/// <summary>
/// The files of this builder.
/// </summary>
public IReadOnlyList<DiscordMessageFile> Files => this.FilesInternal;

/// <summary>
/// Clears the components on this builder.
/// </summary>
public void ClearComponents()
=> this.ComponentsInternal.Clear();

/// <summary>
/// Allows for clearing the builder so that it can be used again.
/// </summary>
public virtual void Clear()
{
this.Content = null;
this.FilesInternal.Clear();
this.EmbedsInternal.Clear();
this.AttachmentsInternal.Clear();
this.ComponentsInternal.Clear();
}

/// <summary>
/// Validates the builder.
/// </summary>
internal virtual void Validate()
{ }
}
86 changes: 18 additions & 68 deletions DisCatSharp/Entities/Interaction/DiscordFollowupMessageBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,6 @@ namespace DisCatSharp.Entities;
/// </summary>
public sealed class DiscordFollowupMessageBuilder : DisCatSharpBuilder
{
private readonly List<DiscordEmbed> _embeds = [];

private readonly List<DiscordMessageFile> _files = [];

internal readonly List<DiscordAttachment> AttachmentsInternal = [];

private string _content;

/// <summary>
/// Whether flags were changed.
/// </summary>
Expand Down Expand Up @@ -91,36 +83,6 @@ public bool NotificationsSuppressed

private bool NOTI_SUP { get; set; }

/// <summary>
/// Message to send on followup message.
/// </summary>
public string Content
{
get => this._content;
set
{
if (value is { Length: > 2000 })
throw new ArgumentException("Content length cannot exceed 2000 characters.", nameof(value));

this._content = value;
}
}

/// <summary>
/// Embeds to send on followup message.
/// </summary>
public IReadOnlyList<DiscordEmbed> Embeds => this._embeds;

/// <summary>
/// Files to send on this followup message.
/// </summary>
public IReadOnlyList<DiscordMessageFile> Files => this._files;

/// <summary>
/// Attachments to be send with this followup request.
/// </summary>
public IReadOnlyList<DiscordAttachment> Attachments => this.AttachmentsInternal;

/// <summary>
/// Mentions to send on this followup message.
/// </summary>
Expand Down Expand Up @@ -217,7 +179,7 @@ public DiscordFollowupMessageBuilder WithContent(string content)
/// <returns>The builder to chain calls with.</returns>
public DiscordFollowupMessageBuilder AddEmbed(DiscordEmbed embed)
{
this._embeds.Add(embed);
this.EmbedsInternal.Add(embed);
return this;
}

Expand All @@ -228,7 +190,7 @@ public DiscordFollowupMessageBuilder AddEmbed(DiscordEmbed embed)
/// <returns>The builder to chain calls with.</returns>
public DiscordFollowupMessageBuilder AddEmbeds(IEnumerable<DiscordEmbed> embeds)
{
this._embeds.AddRange(embeds);
this.EmbedsInternal.AddRange(embeds);
return this;
}

Expand All @@ -248,13 +210,13 @@ public DiscordFollowupMessageBuilder AddFile(string filename, Stream data, bool
if (this.Files.Count >= 10)
throw new ArgumentException("Cannot send more than 10 files with a single message.");

if (this._files.Any(x => x.Filename == filename))
if (this.FilesInternal.Any(x => x.Filename == filename))
throw new ArgumentException("A File with that filename already exists");

if (resetStreamPosition)
this._files.Add(new(filename, data, data.Position, description: description));
this.FilesInternal.Add(new(filename, data, data.Position, description: description));
else
this._files.Add(new(filename, data, null, description: description));
this.FilesInternal.Add(new(filename, data, null, description: description));

return this;
}
Expand All @@ -274,13 +236,13 @@ public DiscordFollowupMessageBuilder AddFile(FileStream stream, bool resetStream
if (this.Files.Count >= 10)
throw new ArgumentException("Cannot send more than 10 files with a single message.");

if (this._files.Any(x => x.Filename == stream.Name))
if (this.FilesInternal.Any(x => x.Filename == stream.Name))
throw new ArgumentException("A File with that filename already exists");

if (resetStreamPosition)
this._files.Add(new(stream.Name, stream, stream.Position, description: description));
this.FilesInternal.Add(new(stream.Name, stream, stream.Position, description: description));
else
this._files.Add(new(stream.Name, stream, null, description: description));
this.FilesInternal.Add(new(stream.Name, stream, null, description: description));

return this;
}
Expand All @@ -301,13 +263,13 @@ public DiscordFollowupMessageBuilder AddFiles(Dictionary<string, Stream> files,

foreach (var file in files)
{
if (this._files.Any(x => x.Filename == file.Key))
if (this.FilesInternal.Any(x => x.Filename == file.Key))
throw new ArgumentException("A File with that filename already exists");

if (resetStreamPosition)
this._files.Add(new(file.Key, file.Value, file.Value.Position));
this.FilesInternal.Add(new(file.Key, file.Value, file.Value.Position));
else
this._files.Add(new(file.Key, file.Value, null));
this.FilesInternal.Add(new(file.Key, file.Value, null));
}

return this;
Expand Down Expand Up @@ -381,40 +343,28 @@ public DiscordFollowupMessageBuilder AsSilentMessage()
return this;
}

/// <summary>
/// Clears all message components on this builder.
/// </summary>
public void ClearComponents()
=> this.ComponentsInternal.Clear();

/// <summary>
/// Clears the poll from this builder.
/// </summary>
public void ClearPoll()
=> this.Poll = null;

/// <summary>
/// Allows for clearing the Followup Message builder so that it can be used again to send a new message.
/// </summary>
public void Clear()
/// <inheritdoc />
public override void Clear()
{
this.Content = null;
this._embeds.Clear();
this.IsTts = false;
this.Mentions = null;
this._files.Clear();
this.IsEphemeral = false;
this.ComponentsInternal.Clear();
base.Clear();
}

/// <summary>
/// Validates the builder.
/// </summary>
internal void Validate()
/// <inheritdoc />
internal override void Validate()
{
if (this.Files?.Count == 0 && string.IsNullOrEmpty(this.Content) && !this.Embeds.Any() && !this.Components.Any() && this.Poll is null && this?.Attachments.Count == 0)
if (this.Files?.Count == 0 && string.IsNullOrEmpty(this.Content) && this.EmbedsInternal.Count == 0 && this.ComponentsInternal.Count == 0 && this.Poll is null && this.AttachmentsInternal.Count == 0)
throw new ArgumentException("You must specify content, an embed, a component, a poll, or at least one file.");

this.Poll?.Validate();
base.Validate();
}
}
20 changes: 13 additions & 7 deletions DisCatSharp/Entities/Interaction/DiscordInteractionModalBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ namespace DisCatSharp.Entities;
/// <summary>
/// Constructs an interaction modal response.
/// </summary>
public sealed class DiscordInteractionModalBuilder : DisCatSharpBuilder
public sealed class DiscordInteractionModalBuilder
{
private readonly List<DiscordInteractionCallbackHint> _callbackHints = [];

/// <summary>
/// The components.
/// </summary>
internal readonly List<DiscordComponent> ComponentsInternal = [];

private string _title;

/// <summary>
Expand All @@ -24,6 +29,11 @@ public DiscordInteractionModalBuilder(string title = null, string customId = nul
this.CustomId = customId ?? Guid.NewGuid().ToString();
}

/// <summary>
/// Components to send. Please use <see cref="ModalComponents"/> instead.
/// </summary>
public IReadOnlyList<DiscordComponent> Components => this.ComponentsInternal;

/// <summary>
/// Title of modal.
/// </summary>
Expand Down Expand Up @@ -147,9 +157,7 @@ public DiscordInteractionModalBuilder AddModalComponents(params DiscordComponent

foreach (var ar in ara)
this.ComponentsInternal.Add(new DiscordActionRowComponent(
[
ar
]));
[ar]));

return this;
}
Expand Down Expand Up @@ -181,9 +189,7 @@ public DiscordInteractionModalBuilder AddModalComponents(IEnumerable<DiscordActi
internal DiscordInteractionModalBuilder AddModalComponents(DiscordComponent component)
{
this.ComponentsInternal.Add(new DiscordActionRowComponent(
[
component
]));
[component]));

return this;
}
Expand Down
Loading

0 comments on commit 3d982d8

Please sign in to comment.