Add a new .editorconfig and reformat code (#76)

*I'll start working on features and bugfixes after this PR, I promise*
very short summary:
- no more braceless statements
- braces are on new lines now
- `sealed` on everything that can be `sealed`
- no more awkwardly looking alignment of fields/parameters
- no more `Service` suffix on service fields. yeah.
- no more `else`s. who needs them?
- code style is now enforced by CI

---------

Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>
This commit is contained in:
Octol1ttle 2023-08-03 01:51:16 +05:00 committed by GitHub
parent 4cb39a34b5
commit 84e730838b
Signed by: GitHub
GPG key ID: 4AEE18F83AFDEB23
39 changed files with 2917 additions and 623 deletions

View file

@ -7,19 +7,21 @@ namespace Boyfriend.Data;
/// Stores information about a guild. This information is not accessible via the Discord API.
/// </summary>
/// <remarks>This information is stored on disk as a JSON file.</remarks>
public class GuildData {
public sealed class GuildData
{
public readonly Dictionary<ulong, MemberData> MemberData;
public readonly string MemberDataPath;
public readonly string MemberDataPath;
public readonly Dictionary<ulong, ScheduledEventData> ScheduledEvents;
public readonly string ScheduledEventsPath;
public readonly JsonNode Settings;
public readonly string SettingsPath;
public readonly string ScheduledEventsPath;
public readonly JsonNode Settings;
public readonly string SettingsPath;
public GuildData(
JsonNode settings, string settingsPath,
JsonNode settings, string settingsPath,
Dictionary<ulong, ScheduledEventData> scheduledEvents, string scheduledEventsPath,
Dictionary<ulong, MemberData> memberData, string memberDataPath) {
Dictionary<ulong, MemberData> memberData, string memberDataPath)
{
Settings = settings;
SettingsPath = settingsPath;
ScheduledEvents = scheduledEvents;
@ -28,8 +30,12 @@ public class GuildData {
MemberDataPath = memberDataPath;
}
public MemberData GetMemberData(Snowflake userId) {
if (MemberData.TryGetValue(userId.Value, out var existing)) return existing;
public MemberData GetMemberData(Snowflake userId)
{
if (MemberData.TryGetValue(userId.Value, out var existing))
{
return existing;
}
var newData = new MemberData(userId.Value, null);
MemberData.Add(userId.Value, newData);

View file

@ -8,7 +8,8 @@ namespace Boyfriend.Data;
/// Contains all per-guild settings that can be set by a member
/// with <see cref="DiscordPermission.ManageGuild" /> using the /settings command
/// </summary>
public static class GuildSettings {
public static class GuildSettings
{
public static readonly LanguageOption Language = new("Language", "en");
/// <summary>
@ -56,9 +57,9 @@ public static class GuildSettings {
public static readonly SnowflakeOption PrivateFeedbackChannel = new("PrivateFeedbackChannel");
public static readonly SnowflakeOption EventNotificationChannel = new("EventNotificationChannel");
public static readonly SnowflakeOption DefaultRole = new("DefaultRole");
public static readonly SnowflakeOption MuteRole = new("MuteRole");
public static readonly SnowflakeOption EventNotificationRole = new("EventNotificationRole");
public static readonly SnowflakeOption DefaultRole = new("DefaultRole");
public static readonly SnowflakeOption MuteRole = new("MuteRole");
public static readonly SnowflakeOption EventNotificationRole = new("EventNotificationRole");
/// <summary>
/// Controls the amount of time before a scheduled event to send a reminder in <see cref="EventNotificationChannel" />.

View file

@ -3,14 +3,16 @@ namespace Boyfriend.Data;
/// <summary>
/// Stores information about a member
/// </summary>
public class MemberData {
public MemberData(ulong id, DateTimeOffset? bannedUntil) {
public sealed class MemberData
{
public MemberData(ulong id, DateTimeOffset? bannedUntil)
{
Id = id;
BannedUntil = bannedUntil;
}
public ulong Id { get; }
public ulong Id { get; }
public DateTimeOffset? BannedUntil { get; set; }
public List<ulong> Roles { get; set; } = new();
public List<Reminder> Reminders { get; } = new();
public List<ulong> Roles { get; set; } = new();
public List<Reminder> Reminders { get; } = new();
}

View file

@ -3,25 +3,31 @@ using Remora.Results;
namespace Boyfriend.Data.Options;
public class BoolOption : Option<bool> {
public sealed class BoolOption : Option<bool>
{
public BoolOption(string name, bool defaultValue) : base(name, defaultValue) { }
public override string Display(JsonNode settings) {
public override string Display(JsonNode settings)
{
return Get(settings) ? Messages.Yes : Messages.No;
}
public override Result Set(JsonNode settings, string from) {
public override Result Set(JsonNode settings, string from)
{
if (!TryParseBool(from, out var value))
{
return new ArgumentInvalidError(nameof(from), Messages.InvalidSettingValue);
}
settings[Name] = value;
return Result.FromSuccess();
}
private static bool TryParseBool(string from, out bool value) {
from = from.ToLowerInvariant();
private static bool TryParseBool(string from, out bool value)
{
value = false;
switch (from) {
switch (from.ToLowerInvariant())
{
case "true" or "1" or "y" or "yes" or "д" or "да":
value = true;
return true;

View file

@ -3,8 +3,9 @@ using Remora.Results;
namespace Boyfriend.Data.Options;
public interface IOption {
public interface IOption
{
string Name { get; }
string Display(JsonNode settings);
Result Set(JsonNode settings, string from);
Result Set(JsonNode settings, string from);
}

View file

@ -6,8 +6,10 @@ using Remora.Results;
namespace Boyfriend.Data.Options;
/// <inheritdoc />
public class LanguageOption : Option<CultureInfo> {
private static readonly Dictionary<string, CultureInfo> CultureInfoCache = new() {
public sealed class LanguageOption : Option<CultureInfo>
{
private static readonly Dictionary<string, CultureInfo> CultureInfoCache = new()
{
{ "en", new CultureInfo("en-US") },
{ "ru", new CultureInfo("ru-RU") },
{ "mctaylors-ru", new CultureInfo("tt-RU") }
@ -15,18 +17,21 @@ public class LanguageOption : Option<CultureInfo> {
public LanguageOption(string name, string defaultValue) : base(name, CultureInfoCache[defaultValue]) { }
public override string Display(JsonNode settings) {
public override string Display(JsonNode settings)
{
return Markdown.InlineCode(settings[Name]?.GetValue<string>() ?? "en");
}
/// <inheritdoc />
public override CultureInfo Get(JsonNode settings) {
public override CultureInfo Get(JsonNode settings)
{
var property = settings[Name];
return property != null ? CultureInfoCache[property.GetValue<string>()] : DefaultValue;
}
/// <inheritdoc />
public override Result Set(JsonNode settings, string from) {
public override Result Set(JsonNode settings, string from)
{
return CultureInfoCache.ContainsKey(from.ToLowerInvariant())
? base.Set(settings, from.ToLowerInvariant())
: new ArgumentInvalidError(nameof(from), Messages.LanguageNotSupported);

View file

@ -9,18 +9,21 @@ namespace Boyfriend.Data.Options;
/// </summary>
/// <typeparam name="T">The type of the option.</typeparam>
public class Option<T> : IOption
where T : notnull {
internal readonly T DefaultValue;
where T : notnull
{
protected readonly T DefaultValue;
public Option(string name, T defaultValue) {
public Option(string name, T defaultValue)
{
Name = name;
DefaultValue = defaultValue;
}
public string Name { get; }
public virtual string Display(JsonNode settings) {
return Markdown.InlineCode(Get(settings).ToString()!);
public virtual string Display(JsonNode settings)
{
return Markdown.InlineCode(Get(settings).ToString() ?? throw new InvalidOperationException());
}
/// <summary>
@ -29,7 +32,8 @@ where T : notnull {
/// <param name="settings">The <see cref="JsonNode" /> to set the value to.</param>
/// <param name="from">The string from which the new value of the option will be parsed.</param>
/// <returns>A value setting result which may or may not have succeeded.</returns>
public virtual Result Set(JsonNode settings, string from) {
public virtual Result Set(JsonNode settings, string from)
{
settings[Name] = from;
return Result.FromSuccess();
}
@ -39,7 +43,8 @@ where T : notnull {
/// </summary>
/// <param name="settings">The <see cref="JsonNode" /> to get the value from.</param>
/// <returns>The value of the option.</returns>
public virtual T Get(JsonNode settings) {
public virtual T Get(JsonNode settings)
{
var property = settings[Name];
return property != null ? property.GetValue<T>() : DefaultValue;
}

View file

@ -6,21 +6,29 @@ using Remora.Results;
namespace Boyfriend.Data.Options;
public partial class SnowflakeOption : Option<Snowflake> {
public sealed partial class SnowflakeOption : Option<Snowflake>
{
public SnowflakeOption(string name) : base(name, 0UL.ToSnowflake()) { }
public override string Display(JsonNode settings) {
return Name.EndsWith("Channel") ? Mention.Channel(Get(settings)) : Mention.Role(Get(settings));
public override string Display(JsonNode settings)
{
return Name.EndsWith("Channel", StringComparison.Ordinal)
? Mention.Channel(Get(settings))
: Mention.Role(Get(settings));
}
public override Snowflake Get(JsonNode settings) {
public override Snowflake Get(JsonNode settings)
{
var property = settings[Name];
return property != null ? property.GetValue<ulong>().ToSnowflake() : DefaultValue;
}
public override Result Set(JsonNode settings, string from) {
public override Result Set(JsonNode settings, string from)
{
if (!ulong.TryParse(NonNumbers().Replace(from, ""), out var parsed))
{
return new ArgumentInvalidError(nameof(from), Messages.InvalidSettingValue);
}
settings[Name] = parsed;
return Result.FromSuccess();

View file

@ -4,25 +4,31 @@ using Remora.Results;
namespace Boyfriend.Data.Options;
public class TimeSpanOption : Option<TimeSpan> {
public sealed class TimeSpanOption : Option<TimeSpan>
{
private static readonly TimeSpanParser Parser = new();
public TimeSpanOption(string name, TimeSpan defaultValue) : base(name, defaultValue) { }
public override TimeSpan Get(JsonNode settings) {
public override TimeSpan Get(JsonNode settings)
{
var property = settings[Name];
return property != null ? ParseTimeSpan(property.GetValue<string>()).Entity : DefaultValue;
}
public override Result Set(JsonNode settings, string from) {
public override Result Set(JsonNode settings, string from)
{
if (!ParseTimeSpan(from).IsDefined(out var span))
{
return new ArgumentInvalidError(nameof(from), Messages.InvalidSettingValue);
}
settings[Name] = span.ToString();
return Result.FromSuccess();
}
private static Result<TimeSpan> ParseTimeSpan(string from) {
private static Result<TimeSpan> ParseTimeSpan(string from)
{
return Parser.TryParseAsync(from).AsTask().GetAwaiter().GetResult();
}
}

View file

@ -1,7 +1,8 @@
namespace Boyfriend.Data;
public struct Reminder {
public struct Reminder
{
public DateTimeOffset At;
public string Text;
public ulong Channel;
public string Text;
public ulong Channel;
}

View file

@ -6,12 +6,14 @@ namespace Boyfriend.Data;
/// Stores information about scheduled events. This information is not provided by the Discord API.
/// </summary>
/// <remarks>This information is stored on disk as a JSON file.</remarks>
public class ScheduledEventData {
public ScheduledEventData(GuildScheduledEventStatus status) {
public sealed class ScheduledEventData
{
public ScheduledEventData(GuildScheduledEventStatus status)
{
Status = status;
}
public bool EarlyNotificationSent { get; set; }
public DateTimeOffset? ActualStartTime { get; set; }
public GuildScheduledEventStatus Status { get; set; }
public bool EarlyNotificationSent { get; set; }
public DateTimeOffset? ActualStartTime { get; set; }
public GuildScheduledEventStatus Status { get; set; }
}