Skip to content

Commit

Permalink
Add StringBuilder & Markdown extensions (#206)
Browse files Browse the repository at this point in the history
In this PR, I have added StringBuilder extensions to avoid `.Append`
reuse such as `.Append("- ").AppendLine()`

Closes #205

---------

Signed-off-by: mctaylors <[email protected]>
  • Loading branch information
mctaylors authored Dec 4, 2023
1 parent 5fce01c commit 18cdc63
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/Commands/AboutCommandGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ private async Task<Result> SendAboutBotAsync(IUser bot, Snowflake guildId, Cance
guildId, dev.Id, ct);
var tag = guildMemberResult.IsSuccess ? $"<@{dev.Id}>" : $"@{dev.Username}";

builder.AppendLine($"- {tag}{$"AboutDeveloper@{dev.Username}".Localized()}");
builder.AppendBulletPointLine($"{tag}{$"AboutDeveloper@{dev.Username}".Localized()}");
}

builder.Append($"### [{Messages.AboutTitleRepository}](https://github.com/LabsDevelopment/Octobot)");
Expand Down
8 changes: 3 additions & 5 deletions src/Commands/BanCommandGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,10 @@ var interactionResult
return await _feedback.SendContextualEmbedResultAsync(errorEmbed, ct);
}

var builder = new StringBuilder().Append("- ")
.AppendLine(string.Format(Messages.DescriptionActionReason, reason));
var builder = new StringBuilder().AppendBulletPointLine(string.Format(Messages.DescriptionActionReason, reason));
if (duration is not null)
{
builder.Append("- ").Append(
builder.AppendBulletPoint(
string.Format(
Messages.DescriptionActionExpiresAt,
Markdown.Timestamp(DateTimeOffset.UtcNow.Add(duration.Value))));
Expand Down Expand Up @@ -274,8 +273,7 @@ private async Task<Result> UnbanUserAsync(
.WithColour(ColorsList.Green).Build();

var title = string.Format(Messages.UserUnbanned, target.GetTag());
var description = new StringBuilder().Append("- ")
.Append(string.Format(Messages.DescriptionActionReason, reason));
var description = new StringBuilder().AppendBulletPoint(string.Format(Messages.DescriptionActionReason, reason));
var logResult = _utility.LogActionAsync(
data.Settings, channelId, executor, title, description.ToString(), target, ColorsList.Green, ct: ct);
if (!logResult.IsSuccess)
Expand Down
4 changes: 2 additions & 2 deletions src/Commands/KickCommandGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ var interactionResult
{
var dmEmbed = new EmbedBuilder().WithGuildTitle(guild)
.WithTitle(Messages.YouWereKicked)
.WithDescription($"- {string.Format(Messages.DescriptionActionReason, reason)}")
.WithDescription(MarkdownExtensions.BulletPoint(string.Format(Messages.DescriptionActionReason, reason)))
.WithActionFooter(executor)
.WithCurrentTimestamp()
.WithColour(ColorsList.Red)
Expand All @@ -159,7 +159,7 @@ var interactionResult
data.GetOrCreateMemberData(target.ID).Roles.Clear();

var title = string.Format(Messages.UserKicked, target.GetTag());
var description = $"- {string.Format(Messages.DescriptionActionReason, reason)}";
var description = MarkdownExtensions.BulletPoint(string.Format(Messages.DescriptionActionReason, reason));
var logResult = _utility.LogActionAsync(
data.Settings, channelId, executor, title, description, target, ColorsList.Red, ct: ct);
if (!logResult.IsSuccess)
Expand Down
6 changes: 3 additions & 3 deletions src/Commands/MuteCommandGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ var interactionResult
}

var title = string.Format(Messages.UserMuted, target.GetTag());
var description = new StringBuilder().Append("- ").AppendLine(string.Format(Messages.DescriptionActionReason, reason))
.Append("- ").Append(string.Format(
var description = new StringBuilder().AppendBulletPointLine(string.Format(Messages.DescriptionActionReason, reason))
.AppendBulletPoint(string.Format(
Messages.DescriptionActionExpiresAt, Markdown.Timestamp(until))).ToString();

var logResult = _utility.LogActionAsync(
Expand Down Expand Up @@ -325,7 +325,7 @@ var interactionResult
}

var title = string.Format(Messages.UserUnmuted, target.GetTag());
var description = $"- {string.Format(Messages.DescriptionActionReason, reason)}";
var description = MarkdownExtensions.BulletPoint(string.Format(Messages.DescriptionActionReason, reason));
var logResult = _utility.LogActionAsync(
data.Settings, channelId, executor, title, description, target, ColorsList.Green, ct: ct);
if (!logResult.IsSuccess)
Expand Down
15 changes: 7 additions & 8 deletions src/Commands/RemindCommandGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,9 @@ private async Task<Result> ListRemindersAsync(MemberData data, IUser executor, I
for (var i = 0; i < data.Reminders.Count; i++)
{
var reminder = data.Reminders[i];
builder.Append("- ").AppendLine(string.Format(Messages.ReminderPosition, Markdown.InlineCode((i + 1).ToString())))
.Append(" - ").AppendLine(string.Format(Messages.ReminderText, Markdown.InlineCode(reminder.Text)))
.Append(" - ")
.AppendLine(string.Format(Messages.ReminderTime, Markdown.Timestamp(reminder.At)));
builder.AppendBulletPointLine(string.Format(Messages.ReminderPosition, Markdown.InlineCode((i + 1).ToString())))
.AppendSubBulletPointLine(string.Format(Messages.ReminderText, Markdown.InlineCode(reminder.Text)))
.AppendSubBulletPointLine(string.Format(Messages.ReminderTime, Markdown.Timestamp(reminder.At)));
}

var embed = new EmbedBuilder().WithSmallTitle(
Expand Down Expand Up @@ -155,9 +154,9 @@ private async Task<Result> AddReminderAsync(
Text = text
});

var builder = new StringBuilder().Append("- ").AppendLine(string.Format(
var builder = new StringBuilder().AppendBulletPointLine(string.Format(
Messages.ReminderText, Markdown.InlineCode(text)))
.Append("- ").Append(string.Format(Messages.ReminderTime, Markdown.Timestamp(remindAt)));
.AppendBulletPoint(string.Format(Messages.ReminderTime, Markdown.Timestamp(remindAt)));

var embed = new EmbedBuilder().WithSmallTitle(
string.Format(Messages.ReminderCreated, executor.GetTag()), executor)
Expand Down Expand Up @@ -215,8 +214,8 @@ private async Task<Result> DeleteReminderAsync(MemberData data, int index, IUser
var reminder = data.Reminders[index];

var description = new StringBuilder()
.Append("- ").AppendLine(string.Format(Messages.ReminderText, Markdown.InlineCode(reminder.Text)))
.Append("- ").AppendLine(string.Format(Messages.ReminderTime, Markdown.Timestamp(reminder.At)));
.AppendBulletPointLine(string.Format(Messages.ReminderText, Markdown.InlineCode(reminder.Text)))
.AppendBulletPointLine(string.Format(Messages.ReminderTime, Markdown.Timestamp(reminder.At)));

data.Reminders.RemoveAt(index);

Expand Down
6 changes: 3 additions & 3 deletions src/Commands/SettingsCommandGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ private async Task<Result> SendSettingsListAsync(JsonNode cfg, IUser bot, int pa
var optionName = AllOptions[i].Name;
var optionValue = AllOptions[i].Display(cfg);

description.AppendLine($"- {$"Settings{optionName}".Localized()}")
.Append($" - {Markdown.InlineCode(optionName)}: ")
.AppendLine(optionValue);
description.AppendBulletPointLine($"Settings{optionName}".Localized())
.AppendSubBulletPoint(Markdown.InlineCode(optionName))
.Append(": ").AppendLine(optionValue);
}

var embed = new EmbedBuilder().WithSmallTitle(Messages.SettingsListTitle, bot)
Expand Down
44 changes: 22 additions & 22 deletions src/Commands/ToolsCommandGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,11 @@ private async Task<Result> ShowUserInfoAsync(

if (target.GlobalName is not null)
{
builder.Append("- ").AppendLine(Messages.UserInfoDisplayName)
builder.AppendBulletPointLine(Messages.UserInfoDisplayName)
.AppendLine(Markdown.InlineCode(target.GlobalName));
}

builder.Append("- ").AppendLine(Messages.UserInfoDiscordUserSince)
builder.AppendBulletPointLine(Messages.UserInfoDiscordUserSince)
.AppendLine(Markdown.Timestamp(target.ID.Timestamp));

var memberData = data.GetOrCreateMemberData(target.ID);
Expand Down Expand Up @@ -170,23 +170,23 @@ private static Color AppendGuildInformation(Color color, IGuildMember guildMembe
{
if (guildMember.Nickname.IsDefined(out var nickname))
{
builder.Append("- ").AppendLine(Messages.UserInfoGuildNickname)
builder.AppendBulletPointLine(Messages.UserInfoGuildNickname)
.AppendLine(Markdown.InlineCode(nickname));
}

builder.Append("- ").AppendLine(Messages.UserInfoGuildMemberSince)
builder.AppendBulletPointLine(Messages.UserInfoGuildMemberSince)
.AppendLine(Markdown.Timestamp(guildMember.JoinedAt));

if (guildMember.PremiumSince.IsDefined(out var premiumSince))
{
builder.Append("- ").AppendLine(Messages.UserInfoGuildMemberPremiumSince)
builder.AppendBulletPointLine(Messages.UserInfoGuildMemberPremiumSince)
.AppendLine(Markdown.Timestamp(premiumSince.Value));
color = ColorsList.Magenta;
}

if (guildMember.Roles.Count > 0)
{
builder.Append("- ").AppendLine(Messages.UserInfoGuildRoles);
builder.AppendBulletPointLine(Messages.UserInfoGuildRoles);
for (var i = 0; i < guildMember.Roles.Count - 1; i++)
{
builder.Append($"<@&{guildMember.Roles[i]}>, ");
Expand All @@ -202,30 +202,30 @@ private static void AppendBanInformation(MemberData memberData, StringBuilder bu
{
if (memberData.BannedUntil < DateTimeOffset.MaxValue)
{
builder.Append("- ").AppendLine(Messages.UserInfoBanned)
.Append(" - ").AppendLine(string.Format(
builder.AppendBulletPointLine(Messages.UserInfoBanned)
.AppendSubBulletPointLine(string.Format(
Messages.DescriptionActionExpiresAt, Markdown.Timestamp(memberData.BannedUntil.Value)));
return;
}

builder.Append("- ").AppendLine(Messages.UserInfoBannedPermanently);
builder.AppendBulletPointLine(Messages.UserInfoBannedPermanently);
}

private static void AppendMuteInformation(
MemberData memberData, DateTimeOffset? communicationDisabledUntil, StringBuilder builder)
{
builder.Append("- ").AppendLine(Messages.UserInfoMuted);
builder.AppendBulletPointLine(Messages.UserInfoMuted);
if (memberData.MutedUntil is not null && DateTimeOffset.UtcNow <= memberData.MutedUntil)
{
builder.Append(" - ").AppendLine(Messages.UserInfoMutedByMuteRole)
.Append(" - ").AppendLine(string.Format(
builder.AppendSubBulletPointLine(Messages.UserInfoMutedByMuteRole)
.AppendSubBulletPointLine(string.Format(
Messages.DescriptionActionExpiresAt, Markdown.Timestamp(memberData.MutedUntil.Value)));
}

if (communicationDisabledUntil is not null)
{
builder.Append(" - ").AppendLine(Messages.UserInfoMutedByTimeout)
.Append(" - ").AppendLine(string.Format(
builder.AppendSubBulletPointLine(Messages.UserInfoMutedByTimeout)
.AppendSubBulletPointLine(string.Format(
Messages.DescriptionActionExpiresAt, Markdown.Timestamp(communicationDisabledUntil.Value)));
}
}
Expand Down Expand Up @@ -282,23 +282,23 @@ private async Task<Result> ShowGuildInfoAsync(IUser bot, IGuild guild, Cancellat

if (guild.Description is not null)
{
description.Append("- ").AppendLine(Messages.GuildInfoDescription)
description.AppendBulletPointLine(Messages.GuildInfoDescription)
.AppendLine(Markdown.InlineCode(guild.Description));
}

description.Append("- ").AppendLine(Messages.GuildInfoCreatedAt)
description.AppendBulletPointLine(Messages.GuildInfoCreatedAt)
.AppendLine(Markdown.Timestamp(guild.ID.Timestamp))
.Append("- ").AppendLine(Messages.GuildInfoOwner)
.AppendBulletPointLine(Messages.GuildInfoOwner)
.AppendLine(Mention.User(guild.OwnerID));

var embedColor = ColorsList.Cyan;

if (guild.PremiumTier > PremiumTier.None)
{
description.Append("### ").AppendLine(Messages.GuildInfoServerBoost)
.Append("- ").Append(Messages.GuildInfoBoostTier)
.AppendBulletPoint(Messages.GuildInfoBoostTier)
.Append(": ").AppendLine(Markdown.InlineCode(guild.PremiumTier.ToString()))
.Append("- ").Append(Messages.GuildInfoBoostCount)
.AppendBulletPoint(Messages.GuildInfoBoostCount)
.Append(": ").AppendLine(Markdown.InlineCode(guild.PremiumSubscriptionCount.ToString()));
embedColor = ColorsList.Magenta;
}
Expand Down Expand Up @@ -362,14 +362,14 @@ private async Task<Result> SendRandomNumberAsync(long first, long? secondNullabl

var description = new StringBuilder().Append("# ").Append(i);

description.AppendLine().Append("- ").Append(string.Format(
description.AppendLine().AppendBulletPoint(string.Format(
Messages.RandomMin, Markdown.InlineCode(min.ToString())));
if (secondNullable is null && first >= secondDefault)
{
description.Append(' ').Append(Messages.Default);
}

description.AppendLine().Append("- ").Append(string.Format(
description.AppendLine().AppendBulletPoint(string.Format(
Messages.RandomMax, Markdown.InlineCode(max.ToString())));
if (secondNullable is null && first < secondDefault)
{
Expand Down Expand Up @@ -449,7 +449,7 @@ private async Task<Result> SendTimestampAsync(TimeSpan? offset, IUser executor,

foreach (var markdownTimestamp in AllStyles.Select(style => Markdown.Timestamp(timestamp, style)))
{
description.Append("- ").Append(Markdown.InlineCode(markdownTimestamp))
description.AppendBulletPoint(Markdown.InlineCode(markdownTimestamp))
.Append(" → ").AppendLine(markdownTimestamp);
}

Expand Down
16 changes: 16 additions & 0 deletions src/Extensions/MarkdownExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace Octobot.Extensions;

public static class MarkdownExtensions
{
/// <summary>
/// Formats a string to use Markdown Bullet formatting.
/// </summary>
/// <param name="text">The input text to format.</param>
/// <returns>
/// A markdown-formatted bullet string.
/// </returns>
public static string BulletPoint(string text)
{
return $"- {text}";
}
}
62 changes: 62 additions & 0 deletions src/Extensions/StringBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System.Text;

namespace Octobot.Extensions;

public static class StringBuilderExtensions
{
/// <summary>
/// Appends the input string with Markdown Bullet formatting to the specified <see cref="StringBuilder" /> object.
/// </summary>
/// <param name="builder">The <see cref="StringBuilder" /> object.</param>
/// <param name="value">The string to append with bullet point.</param>
/// <returns>
/// The builder with the appended string with Markdown Bullet formatting.
/// </returns>
public static StringBuilder AppendBulletPoint(this StringBuilder builder, string? value)
{
return builder.Append("- ").Append(value);
}

/// <summary>
/// Appends the input string with Markdown Sub-Bullet formatting to the specified <see cref="StringBuilder" /> object.
/// </summary>
/// <param name="builder">The <see cref="StringBuilder" /> object.</param>
/// <param name="value">The string to append with sub-bullet point.</param>
/// <returns>
/// The builder with the appended string with Markdown Sub-Bullet formatting.
/// </returns>
public static StringBuilder AppendSubBulletPoint(this StringBuilder builder, string? value)
{
return builder.Append(" - ").Append(value);
}

/// <summary>
/// Appends the input string with Markdown Bullet formatting followed by
/// the default line terminator to the end of specified <see cref="StringBuilder" /> object.
/// </summary>
/// <param name="builder">The <see cref="StringBuilder" /> object.</param>
/// <param name="value">The string to append with bullet point.</param>
/// <returns>
/// The builder with the appended string with Markdown Bullet formatting
/// and default line terminator at the end.
/// </returns>
public static StringBuilder AppendBulletPointLine(this StringBuilder builder, string? value)
{
return builder.Append("- ").AppendLine(value);
}

/// <summary>
/// Appends the input string with Markdown Sub-Bullet formatting followed by
/// the default line terminator to the end of specified <see cref="StringBuilder" /> object.
/// </summary>
/// <param name="builder">The <see cref="StringBuilder" /> object.</param>
/// <param name="value">The string to append with sub-bullet point.</param>
/// <returns>
/// The builder with the appended string with Markdown Sub-Bullet formatting
/// and default line terminator at the end.
/// </returns>
public static StringBuilder AppendSubBulletPointLine(this StringBuilder builder, string? value)
{
return builder.Append(" - ").AppendLine(value);
}
}

0 comments on commit 18cdc63

Please sign in to comment.