1
0
Fork 1
mirror of https://github.com/TeamOctolings/Octobot.git synced 2025-01-31 09:09:00 +03:00

Add StringBuilder & Markdown extensions (#206)

In this PR, I have added StringBuilder extensions to avoid `.Append`
reuse such as `.Append("- ").AppendLine()`

Closes #205

---------

Signed-off-by: mctaylors <cantsendmails@mctaylors.ru>
This commit is contained in:
Macintxsh 2023-12-04 17:09:47 +03:00 committed by GitHub
parent 5fce01c15c
commit 18cdc63883
Signed by: GitHub
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 119 additions and 44 deletions

View file

@ -88,7 +88,7 @@ public class AboutCommandGroup : CommandGroup
guildId, dev.Id, ct); guildId, dev.Id, ct);
var tag = guildMemberResult.IsSuccess ? $"<@{dev.Id}>" : $"@{dev.Username}"; 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)"); builder.Append($"### [{Messages.AboutTitleRepository}](https://github.com/LabsDevelopment/Octobot)");

View file

@ -135,11 +135,10 @@ public class BanCommandGroup : CommandGroup
return await _feedback.SendContextualEmbedResultAsync(errorEmbed, ct); return await _feedback.SendContextualEmbedResultAsync(errorEmbed, ct);
} }
var builder = new StringBuilder().Append("- ") var builder = new StringBuilder().AppendBulletPointLine(string.Format(Messages.DescriptionActionReason, reason));
.AppendLine(string.Format(Messages.DescriptionActionReason, reason));
if (duration is not null) if (duration is not null)
{ {
builder.Append("- ").Append( builder.AppendBulletPoint(
string.Format( string.Format(
Messages.DescriptionActionExpiresAt, Messages.DescriptionActionExpiresAt,
Markdown.Timestamp(DateTimeOffset.UtcNow.Add(duration.Value)))); Markdown.Timestamp(DateTimeOffset.UtcNow.Add(duration.Value))));
@ -274,8 +273,7 @@ public class BanCommandGroup : CommandGroup
.WithColour(ColorsList.Green).Build(); .WithColour(ColorsList.Green).Build();
var title = string.Format(Messages.UserUnbanned, target.GetTag()); var title = string.Format(Messages.UserUnbanned, target.GetTag());
var description = new StringBuilder().Append("- ") var description = new StringBuilder().AppendBulletPoint(string.Format(Messages.DescriptionActionReason, reason));
.Append(string.Format(Messages.DescriptionActionReason, reason));
var logResult = _utility.LogActionAsync( var logResult = _utility.LogActionAsync(
data.Settings, channelId, executor, title, description.ToString(), target, ColorsList.Green, ct: ct); data.Settings, channelId, executor, title, description.ToString(), target, ColorsList.Green, ct: ct);
if (!logResult.IsSuccess) if (!logResult.IsSuccess)

View file

@ -134,7 +134,7 @@ public class KickCommandGroup : CommandGroup
{ {
var dmEmbed = new EmbedBuilder().WithGuildTitle(guild) var dmEmbed = new EmbedBuilder().WithGuildTitle(guild)
.WithTitle(Messages.YouWereKicked) .WithTitle(Messages.YouWereKicked)
.WithDescription($"- {string.Format(Messages.DescriptionActionReason, reason)}") .WithDescription(MarkdownExtensions.BulletPoint(string.Format(Messages.DescriptionActionReason, reason)))
.WithActionFooter(executor) .WithActionFooter(executor)
.WithCurrentTimestamp() .WithCurrentTimestamp()
.WithColour(ColorsList.Red) .WithColour(ColorsList.Red)
@ -159,7 +159,7 @@ public class KickCommandGroup : CommandGroup
data.GetOrCreateMemberData(target.ID).Roles.Clear(); data.GetOrCreateMemberData(target.ID).Roles.Clear();
var title = string.Format(Messages.UserKicked, target.GetTag()); 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( var logResult = _utility.LogActionAsync(
data.Settings, channelId, executor, title, description, target, ColorsList.Red, ct: ct); data.Settings, channelId, executor, title, description, target, ColorsList.Red, ct: ct);
if (!logResult.IsSuccess) if (!logResult.IsSuccess)

View file

@ -136,8 +136,8 @@ public class MuteCommandGroup : CommandGroup
} }
var title = string.Format(Messages.UserMuted, target.GetTag()); var title = string.Format(Messages.UserMuted, target.GetTag());
var description = new StringBuilder().Append("- ").AppendLine(string.Format(Messages.DescriptionActionReason, reason)) var description = new StringBuilder().AppendBulletPointLine(string.Format(Messages.DescriptionActionReason, reason))
.Append("- ").Append(string.Format( .AppendBulletPoint(string.Format(
Messages.DescriptionActionExpiresAt, Markdown.Timestamp(until))).ToString(); Messages.DescriptionActionExpiresAt, Markdown.Timestamp(until))).ToString();
var logResult = _utility.LogActionAsync( var logResult = _utility.LogActionAsync(
@ -325,7 +325,7 @@ public class MuteCommandGroup : CommandGroup
} }
var title = string.Format(Messages.UserUnmuted, target.GetTag()); 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( var logResult = _utility.LogActionAsync(
data.Settings, channelId, executor, title, description, target, ColorsList.Green, ct: ct); data.Settings, channelId, executor, title, description, target, ColorsList.Green, ct: ct);
if (!logResult.IsSuccess) if (!logResult.IsSuccess)

View file

@ -90,10 +90,9 @@ public class RemindCommandGroup : CommandGroup
for (var i = 0; i < data.Reminders.Count; i++) for (var i = 0; i < data.Reminders.Count; i++)
{ {
var reminder = data.Reminders[i]; var reminder = data.Reminders[i];
builder.Append("- ").AppendLine(string.Format(Messages.ReminderPosition, Markdown.InlineCode((i + 1).ToString()))) builder.AppendBulletPointLine(string.Format(Messages.ReminderPosition, Markdown.InlineCode((i + 1).ToString())))
.Append(" - ").AppendLine(string.Format(Messages.ReminderText, Markdown.InlineCode(reminder.Text))) .AppendSubBulletPointLine(string.Format(Messages.ReminderText, Markdown.InlineCode(reminder.Text)))
.Append(" - ") .AppendSubBulletPointLine(string.Format(Messages.ReminderTime, Markdown.Timestamp(reminder.At)));
.AppendLine(string.Format(Messages.ReminderTime, Markdown.Timestamp(reminder.At)));
} }
var embed = new EmbedBuilder().WithSmallTitle( var embed = new EmbedBuilder().WithSmallTitle(
@ -155,9 +154,9 @@ public class RemindCommandGroup : CommandGroup
Text = text Text = text
}); });
var builder = new StringBuilder().Append("- ").AppendLine(string.Format( var builder = new StringBuilder().AppendBulletPointLine(string.Format(
Messages.ReminderText, Markdown.InlineCode(text))) 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( var embed = new EmbedBuilder().WithSmallTitle(
string.Format(Messages.ReminderCreated, executor.GetTag()), executor) string.Format(Messages.ReminderCreated, executor.GetTag()), executor)
@ -215,8 +214,8 @@ public class RemindCommandGroup : CommandGroup
var reminder = data.Reminders[index]; var reminder = data.Reminders[index];
var description = new StringBuilder() var description = new StringBuilder()
.Append("- ").AppendLine(string.Format(Messages.ReminderText, Markdown.InlineCode(reminder.Text))) .AppendBulletPointLine(string.Format(Messages.ReminderText, Markdown.InlineCode(reminder.Text)))
.Append("- ").AppendLine(string.Format(Messages.ReminderTime, Markdown.Timestamp(reminder.At))); .AppendBulletPointLine(string.Format(Messages.ReminderTime, Markdown.Timestamp(reminder.At)));
data.Reminders.RemoveAt(index); data.Reminders.RemoveAt(index);

View file

@ -138,9 +138,9 @@ public class SettingsCommandGroup : CommandGroup
var optionName = AllOptions[i].Name; var optionName = AllOptions[i].Name;
var optionValue = AllOptions[i].Display(cfg); var optionValue = AllOptions[i].Display(cfg);
description.AppendLine($"- {$"Settings{optionName}".Localized()}") description.AppendBulletPointLine($"Settings{optionName}".Localized())
.Append($" - {Markdown.InlineCode(optionName)}: ") .AppendSubBulletPoint(Markdown.InlineCode(optionName))
.AppendLine(optionValue); .Append(": ").AppendLine(optionValue);
} }
var embed = new EmbedBuilder().WithSmallTitle(Messages.SettingsListTitle, bot) var embed = new EmbedBuilder().WithSmallTitle(Messages.SettingsListTitle, bot)

View file

@ -102,11 +102,11 @@ public class ToolsCommandGroup : CommandGroup
if (target.GlobalName is not null) if (target.GlobalName is not null)
{ {
builder.Append("- ").AppendLine(Messages.UserInfoDisplayName) builder.AppendBulletPointLine(Messages.UserInfoDisplayName)
.AppendLine(Markdown.InlineCode(target.GlobalName)); .AppendLine(Markdown.InlineCode(target.GlobalName));
} }
builder.Append("- ").AppendLine(Messages.UserInfoDiscordUserSince) builder.AppendBulletPointLine(Messages.UserInfoDiscordUserSince)
.AppendLine(Markdown.Timestamp(target.ID.Timestamp)); .AppendLine(Markdown.Timestamp(target.ID.Timestamp));
var memberData = data.GetOrCreateMemberData(target.ID); var memberData = data.GetOrCreateMemberData(target.ID);
@ -170,23 +170,23 @@ public class ToolsCommandGroup : CommandGroup
{ {
if (guildMember.Nickname.IsDefined(out var nickname)) if (guildMember.Nickname.IsDefined(out var nickname))
{ {
builder.Append("- ").AppendLine(Messages.UserInfoGuildNickname) builder.AppendBulletPointLine(Messages.UserInfoGuildNickname)
.AppendLine(Markdown.InlineCode(nickname)); .AppendLine(Markdown.InlineCode(nickname));
} }
builder.Append("- ").AppendLine(Messages.UserInfoGuildMemberSince) builder.AppendBulletPointLine(Messages.UserInfoGuildMemberSince)
.AppendLine(Markdown.Timestamp(guildMember.JoinedAt)); .AppendLine(Markdown.Timestamp(guildMember.JoinedAt));
if (guildMember.PremiumSince.IsDefined(out var premiumSince)) if (guildMember.PremiumSince.IsDefined(out var premiumSince))
{ {
builder.Append("- ").AppendLine(Messages.UserInfoGuildMemberPremiumSince) builder.AppendBulletPointLine(Messages.UserInfoGuildMemberPremiumSince)
.AppendLine(Markdown.Timestamp(premiumSince.Value)); .AppendLine(Markdown.Timestamp(premiumSince.Value));
color = ColorsList.Magenta; color = ColorsList.Magenta;
} }
if (guildMember.Roles.Count > 0) if (guildMember.Roles.Count > 0)
{ {
builder.Append("- ").AppendLine(Messages.UserInfoGuildRoles); builder.AppendBulletPointLine(Messages.UserInfoGuildRoles);
for (var i = 0; i < guildMember.Roles.Count - 1; i++) for (var i = 0; i < guildMember.Roles.Count - 1; i++)
{ {
builder.Append($"<@&{guildMember.Roles[i]}>, "); builder.Append($"<@&{guildMember.Roles[i]}>, ");
@ -202,30 +202,30 @@ public class ToolsCommandGroup : CommandGroup
{ {
if (memberData.BannedUntil < DateTimeOffset.MaxValue) if (memberData.BannedUntil < DateTimeOffset.MaxValue)
{ {
builder.Append("- ").AppendLine(Messages.UserInfoBanned) builder.AppendBulletPointLine(Messages.UserInfoBanned)
.Append(" - ").AppendLine(string.Format( .AppendSubBulletPointLine(string.Format(
Messages.DescriptionActionExpiresAt, Markdown.Timestamp(memberData.BannedUntil.Value))); Messages.DescriptionActionExpiresAt, Markdown.Timestamp(memberData.BannedUntil.Value)));
return; return;
} }
builder.Append("- ").AppendLine(Messages.UserInfoBannedPermanently); builder.AppendBulletPointLine(Messages.UserInfoBannedPermanently);
} }
private static void AppendMuteInformation( private static void AppendMuteInformation(
MemberData memberData, DateTimeOffset? communicationDisabledUntil, StringBuilder builder) 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) if (memberData.MutedUntil is not null && DateTimeOffset.UtcNow <= memberData.MutedUntil)
{ {
builder.Append(" - ").AppendLine(Messages.UserInfoMutedByMuteRole) builder.AppendSubBulletPointLine(Messages.UserInfoMutedByMuteRole)
.Append(" - ").AppendLine(string.Format( .AppendSubBulletPointLine(string.Format(
Messages.DescriptionActionExpiresAt, Markdown.Timestamp(memberData.MutedUntil.Value))); Messages.DescriptionActionExpiresAt, Markdown.Timestamp(memberData.MutedUntil.Value)));
} }
if (communicationDisabledUntil is not null) if (communicationDisabledUntil is not null)
{ {
builder.Append(" - ").AppendLine(Messages.UserInfoMutedByTimeout) builder.AppendSubBulletPointLine(Messages.UserInfoMutedByTimeout)
.Append(" - ").AppendLine(string.Format( .AppendSubBulletPointLine(string.Format(
Messages.DescriptionActionExpiresAt, Markdown.Timestamp(communicationDisabledUntil.Value))); Messages.DescriptionActionExpiresAt, Markdown.Timestamp(communicationDisabledUntil.Value)));
} }
} }
@ -282,13 +282,13 @@ public class ToolsCommandGroup : CommandGroup
if (guild.Description is not null) if (guild.Description is not null)
{ {
description.Append("- ").AppendLine(Messages.GuildInfoDescription) description.AppendBulletPointLine(Messages.GuildInfoDescription)
.AppendLine(Markdown.InlineCode(guild.Description)); .AppendLine(Markdown.InlineCode(guild.Description));
} }
description.Append("- ").AppendLine(Messages.GuildInfoCreatedAt) description.AppendBulletPointLine(Messages.GuildInfoCreatedAt)
.AppendLine(Markdown.Timestamp(guild.ID.Timestamp)) .AppendLine(Markdown.Timestamp(guild.ID.Timestamp))
.Append("- ").AppendLine(Messages.GuildInfoOwner) .AppendBulletPointLine(Messages.GuildInfoOwner)
.AppendLine(Mention.User(guild.OwnerID)); .AppendLine(Mention.User(guild.OwnerID));
var embedColor = ColorsList.Cyan; var embedColor = ColorsList.Cyan;
@ -296,9 +296,9 @@ public class ToolsCommandGroup : CommandGroup
if (guild.PremiumTier > PremiumTier.None) if (guild.PremiumTier > PremiumTier.None)
{ {
description.Append("### ").AppendLine(Messages.GuildInfoServerBoost) description.Append("### ").AppendLine(Messages.GuildInfoServerBoost)
.Append("- ").Append(Messages.GuildInfoBoostTier) .AppendBulletPoint(Messages.GuildInfoBoostTier)
.Append(": ").AppendLine(Markdown.InlineCode(guild.PremiumTier.ToString())) .Append(": ").AppendLine(Markdown.InlineCode(guild.PremiumTier.ToString()))
.Append("- ").Append(Messages.GuildInfoBoostCount) .AppendBulletPoint(Messages.GuildInfoBoostCount)
.Append(": ").AppendLine(Markdown.InlineCode(guild.PremiumSubscriptionCount.ToString())); .Append(": ").AppendLine(Markdown.InlineCode(guild.PremiumSubscriptionCount.ToString()));
embedColor = ColorsList.Magenta; embedColor = ColorsList.Magenta;
} }
@ -362,14 +362,14 @@ public class ToolsCommandGroup : CommandGroup
var description = new StringBuilder().Append("# ").Append(i); var description = new StringBuilder().Append("# ").Append(i);
description.AppendLine().Append("- ").Append(string.Format( description.AppendLine().AppendBulletPoint(string.Format(
Messages.RandomMin, Markdown.InlineCode(min.ToString()))); Messages.RandomMin, Markdown.InlineCode(min.ToString())));
if (secondNullable is null && first >= secondDefault) if (secondNullable is null && first >= secondDefault)
{ {
description.Append(' ').Append(Messages.Default); description.Append(' ').Append(Messages.Default);
} }
description.AppendLine().Append("- ").Append(string.Format( description.AppendLine().AppendBulletPoint(string.Format(
Messages.RandomMax, Markdown.InlineCode(max.ToString()))); Messages.RandomMax, Markdown.InlineCode(max.ToString())));
if (secondNullable is null && first < secondDefault) if (secondNullable is null && first < secondDefault)
{ {
@ -449,7 +449,7 @@ public class ToolsCommandGroup : CommandGroup
foreach (var markdownTimestamp in AllStyles.Select(style => Markdown.Timestamp(timestamp, style))) 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); .Append(" → ").AppendLine(markdownTimestamp);
} }

View file

@ -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}";
}
}

View file

@ -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);
}
}