Skip to content

Commit

Permalink
Start moderation plugin. Finalize BetterEmbeds
Browse files Browse the repository at this point in the history
  • Loading branch information
Foxtrek64 committed Dec 1, 2021
1 parent 2f45803 commit ad53a9a
Show file tree
Hide file tree
Showing 57 changed files with 1,389 additions and 430 deletions.
46 changes: 42 additions & 4 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
<Project>

<PropertyGroup>
<Nullable>enable</Nullable>
</PropertyGroup>

<PropertyGroup>
<Authors>Mara Contributors</Authors>
<Company>LuzFaltex</Company>
Expand All @@ -20,5 +16,47 @@
<OutputPath>..\output\$(Configuration)\</OutputPath>
</PropertyGroup>

<PropertyGroup>
<Nullable>enable</Nullable>
<GenerateDocumentationFile>true</GenerateDocumentationFile>

<WarningsAsErrors>
CS8600;
CS8601;
CS8602;
CS8603;
CS8604;
CS8608;
CS8609;
CS8610;
CS8611;
CS8612;
CS8613;
CS8614;
CS8615;
CS8616;
CS8617;
CS8618;
CS8619;
CS8620;
CS8621;
CS8622;
CS8625;
CS8626;
CS8629;
CS8631;
CS8633;
CS8634;
CS8638;
CS8639;
CS8643;
CS8644;
CS8645;
</WarningsAsErrors>
<NoWarn>

</NoWarn>
</PropertyGroup>


</Project>
15 changes: 6 additions & 9 deletions Mara.Common/Constants.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Mara.Common
namespace Mara.Common
{
public class Constants
/// <summary>
/// Provides a set of constant values
/// </summary>
public static class Constants
{

}
}
64 changes: 40 additions & 24 deletions Mara.Common/Discord/EmbedBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.ComponentModel.DataAnnotations;
using System.Drawing;
using System.Linq;
using System.Security.Cryptography;
using Mara.Common.Extensions;
using Mara.Common.Results;
using Remora.Discord.API;
Expand All @@ -21,18 +20,29 @@ public class EmbedBuilder
public string Title { get; set; }

public EmbedType Type { get; set; }

[MaxLength(EmbedConstants.MaxDescriptionLength)]
public string Description { get; set; }

[Url]

public string Url { get; set; }

public DateTimeOffset Timestamp { get; set; }

public Color Color { get; set; }

public IEmbedFooter Footer { get; set; }
public IEmbedImage Image { get; set; }
public IEmbedThumbnail Thumbnail { get; set; }
public IEmbedAuthor Author { get; set; }

public IEmbedImage? Image { get; set; }

public IEmbedThumbnail? Thumbnail { get; set; }

public IEmbedAuthor? Author { get; set; }

public IReadOnlyList<IEmbedField> Fields => new ReadOnlyCollection<IEmbedField>(_fields);


private IList<IEmbedField> _fields;

public EmbedBuilder() : this(new List<IEmbedField>(EmbedConstants.MaxFieldCount))
Expand All @@ -55,6 +65,7 @@ private EmbedBuilder(List<IEmbedField> fields)
Footer = EmbedConstants.DefaultFooter;
Image = null;
Thumbnail = null;
Author = null;
_fields = fields;
}

Expand All @@ -68,18 +79,19 @@ public static EmbedBuilder FromEmbed(Embed embed)
Timestamp = embed.Timestamp.GetValueOrDefault(DateTimeOffset.UtcNow),
Color = embed.Colour.GetValueOrDefault(EmbedConstants.DefaultColor),
Footer = embed.Footer.GetValueOrDefault(EmbedConstants.DefaultFooter),
Image = embed.Image.GetValueOrDefault(default),
Thumbnail = embed.Thumbnail.GetValueOrDefault(default)
Image = embed.Image.HasValue ? embed.Image.Value : null,
Thumbnail = embed.Thumbnail.HasValue ? embed.Thumbnail.Value : null
};

/// <summary>
/// Returns the overall length of the embed.
/// </summary>
public int Length
{
get
{
int titleLength = Title.Length;
int authorLength = Author.Name.HasValue
? Author.Name.Value!.Length
: 0;
int authorLength = Author?.Name.Length ?? 0;
int descriptionLength = Description.Length;
int footerLength = Footer?.Text.Length ?? 0;
int fieldSum = _fields.Sum(field => field.Name.Length + field.Value.Length);
Expand Down Expand Up @@ -182,9 +194,9 @@ public EmbedBuilder WithTimestamp(DateTimeOffset dateTimeOffset)
/// <returns>
/// The current builder.
/// </returns>
public EmbedBuilder WithColor(Color colour)
public EmbedBuilder WithColor(Color color)
{
Color = colour;
Color = color;
return this;
}

Expand All @@ -194,7 +206,7 @@ public EmbedBuilder WithColor(Color colour)
/// <returns>
/// The current builder.
/// </returns>
public EmbedBuilder WithAuthor([MaxLength(EmbedConstants.MaxAuthorNameLength)] string name, [Url] string url = default, [Url] string iconUrl = default)
public EmbedBuilder WithAuthor([MaxLength(EmbedConstants.MaxAuthorNameLength)] string name, [Url] string url = "", [Url] string iconUrl = "")
{
Author = new EmbedAuthor(name, url, iconUrl);
return this;
Expand All @@ -220,7 +232,11 @@ public EmbedBuilder WithUserAsAuthor(IUser user)
/// <returns>
/// The current builder.
/// </returns>
public EmbedBuilder WithFooter(string text, [Url] string iconUrl = default)
public EmbedBuilder WithFooter
(
[MaxLength(EmbedConstants.MaxFooterTextLength)] string text,
[Url] string iconUrl = ""
)
{
if (text.Length > EmbedConstants.MaxFooterTextLength)
throw new ArgumentException(
Expand Down Expand Up @@ -254,7 +270,7 @@ public EmbedBuilder AddField(string name, string value, bool inline = false)
/// <see cref="Embed"/>.
/// </summary>
/// <param name="field">The field builder class containing the field properties.</param>
/// <exception cref="ArgumentException">Field count exceeds <see cref="MaxFieldCount"/>.</exception>
/// <exception cref="ArgumentException">Field count exceeds <see cref="EmbedConstants.MaxFieldCount"/>.</exception>
/// <returns>
/// The current builder.
/// </returns>
Expand Down Expand Up @@ -286,23 +302,23 @@ public EmbedBuilder SetFields(IList<IEmbedField> fields)
/// <returns>
/// The built embed object.
/// </returns>
/// <exception cref="InvalidOperationException">Total embed length exceeds <see cref="MaxEmbedLength"/>.</exception>
/// <exception cref="InvalidOperationException">Total embed length exceeds <see cref="EmbedConstants.MaxEmbedLength"/>.</exception>
public Embed Build()
=> Length > EmbedConstants.MaxEmbedLength
? throw new InvalidOperationException(
$"Total embed length must be less than or equal to {EmbedConstants.MaxEmbedLength}.")
: new Embed()
{
Title = new Optional<string>(Title),
Type = new Optional<EmbedType>(Type),
Description = new Optional<string>(Description),
Url = new Optional<string>(Url),
Timestamp = new Optional<DateTimeOffset>(Timestamp),
Colour = new Optional<Color>(Color),
Title = Title,
Type = Type,
Description = Description,
Url = Url,
Timestamp = Timestamp,
Colour = Color,
Footer = new Optional<IEmbedFooter>(Footer),
Image = new Optional<IEmbedImage>(Image),
Thumbnail = new Optional<IEmbedThumbnail>(Thumbnail),
Author = new Optional<IEmbedAuthor>(Author),
Image = Image is null ? default : new Optional<IEmbedImage>(Image),
Thumbnail = Thumbnail is null ? default : new Optional<IEmbedThumbnail>(Thumbnail),
Author = Author is null ? default : new Optional<IEmbedAuthor>(Author),
Fields = new Optional<IReadOnlyList<IEmbedField>>(Fields)
};
}
Expand Down
13 changes: 12 additions & 1 deletion Mara.Common/Discord/EmbedConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

namespace Mara.Common.Discord
{
/// <summary>
/// A set of constants which represent restraits on Embeds.
/// </summary>
public static class EmbedConstants
{
/// <summary>
Expand Down Expand Up @@ -38,6 +41,14 @@ public static class EmbedConstants
/// <summary>
/// Default embed footer. "React with ❌ to remove this embed."
/// </summary>
public static readonly EmbedFooter DefaultFooter = new("React with ❌ to remove this embed.");
public static readonly EmbedFooter DefaultFooter = new("React with ❌ to remove this embed");

/// <summary>
/// A set of image assets in url form for use in embeds.
/// </summary>
public static class ImageUrls
{

}
}
}
16 changes: 8 additions & 8 deletions Mara.Common/Discord/Feedback/IdentityInformationConfiguration.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
using Remora.Discord.Core;
using System;
using Remora.Discord.API.Abstractions.Objects;
using Remora.Discord.Core;

namespace Mara.Common.Discord.Feedback
{
/// <summary>
/// Provides information about the identity of the current application.
/// </summary>
public sealed class IdentityInformationConfiguration
{
/// <summary>
Expand All @@ -10,13 +15,8 @@ public sealed class IdentityInformationConfiguration
public Snowflake Id { get; set; }

/// <summary>
/// Gets the application id if the bot.
/// Gets the application behind this bot.
/// </summary>
public Snowflake ApplicationId { get; set; }

/// <summary>
/// Gets the id of the bot's owner.
/// </summary>
public Snowflake OwnerId { get; set; }
public IApplication Application { get; set; }
}
}
81 changes: 78 additions & 3 deletions Mara.Common/Discord/FormatUtilities.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
using System.Linq;
using System;
using System.Linq;
using System.Text.RegularExpressions;
using Remora.Discord.API.Abstractions.Objects;
using Remora.Discord.API.Objects;

namespace Mara.Common.Discord
{
public static class FormatUtilities
public static class StringUtilities
{
/// <summary>
/// Represents a zero width space character.
/// </summary>
public const char ZeroWidthSpace = '\x200b';

/// <summary>
/// Represents an empty string. This field is constant.
/// </summary>
public const string EmptyString = "";
}
public static class FormatUtilities
{
// Characters which need escaping
private static readonly string[] SensitiveCharacters = { "\\", "*", "_", "~", "`", "|", ">" };

Expand Down Expand Up @@ -66,6 +77,8 @@ public static string Mention(IChannel channel)
/// <summary> Returns a string with spoiler formatting. </summary>
public static string Spoiler(string text) => $"||{text}||";
/// <summary> Returns a markdown-formatted URL. Only works in <see cref="Embed"/> descriptions and fields. </summary>
public static string Url(string url) => $"[{url}]({url})";
/// <summary> Returns a markdown-formatted URL. Only works in <see cref="Embed"/> descriptions and fields. </summary>
public static string Url(string text, string url) => $"[{text}]({url})";
/// <summary> Escapes a URL so that a preview is not generated. </summary>
public static string EscapeUrl(string url) => $"<{url}>";
Expand All @@ -76,9 +89,71 @@ public static string Mention(IChannel channel)
/// <param name="text">The text to wrap in a code block.</param>
/// <param name="language">The code language. Ignored for single-line code blocks.</param>
/// <returns></returns>
public static string Code(string text, string language = null)
public static string Code(string text, string? language = null)
=> language is not null || text.Contains("\n")
? $"```{language ?? ""}\n{text}\n```"
: $"`{text}`";

/// <summary>
/// Returns a markdown timestamp which renders the specified date and time in the user's timezone and locale.
/// </summary>
/// <param name="timeStamp">The date and time to display.</param>
/// <param name="style">What format to display it in.</param>
/// <seealso href="https://discord.com/developers/docs/reference#message-formatting-timestamp-styles">Discord Message Formatting Timestamp Styles</seealso>
public static string DynamicTimeStamp(DateTime timeStamp, TimeStampStyle style)
=> DynamicTimeStamp(new DateTimeOffset(timeStamp), style);

/// <inheritdoc cref="DynamicTimeStamp(System.DateTime,Mara.Common.Discord.FormatUtilities.TimeStampStyle)"/>
public static string DynamicTimeStamp(DateTimeOffset timeStamp, TimeStampStyle style = TimeStampStyle.ShortDateTime)
{
char format = style switch
{
TimeStampStyle.ShortTime => 't',
TimeStampStyle.LongTime => 'T',
TimeStampStyle.ShortDate => 'd',
TimeStampStyle.LongDate => 'D',
TimeStampStyle.ShortDateTime => 'f',
TimeStampStyle.LongDateTime => 'F',
TimeStampStyle.RelativeTime => 'R',
_ => 'f'
};

return $"<t:{timeStamp.ToUnixTimeSeconds()}:{format}>";
}

/// <summary>
/// Determines the output style for the Dynamic Time Stamp
/// </summary>
public enum TimeStampStyle
{
/// <summary>
/// 16:20
/// </summary>
ShortTime,
/// <summary>
/// 16:20:30
/// </summary>
LongTime,
/// <summary>
/// 20/04/2021
/// </summary>
ShortDate,
/// <summary>
/// 20 April 2021
/// </summary>
LongDate,
/// <summary>
/// 20 April 2021 16:20
/// </summary>
ShortDateTime,
/// <summary>
/// Tuesday, 20 April 2021 16:20
/// </summary>
LongDateTime,
/// <summary>
/// 2 months ago
/// </summary>
RelativeTime
}
}
}
Loading

0 comments on commit ad53a9a

Please sign in to comment.