mirror of
https://github.com/TeamOctolings/Octobot.git
synced 2025-05-03 12:39:54 +03:00
Apply official naming guidelines to Octobot (#306)
1. The root namespace was changed from `Octobot` to `TeamOctolings.Octobot`: > DO prefix namespace names with a company name to prevent namespaces from different companies from having the same name. 2. `Octobot.cs` was renamed to `Program.cs`: > DO NOT use the same name for a namespace and a type in that namespace. 3. `IOption`, `Option` were renamed to `IGuildOption` and `GuildOption` respectively: > DO NOT introduce generic type names such as Element, Node, Log, and Message. 4. `Utility` was moved out of the `Services` namespace. It didn't belong there anyway 5. `Program` static fields were moved to `Utility` 6. Localisation files were moved back to the project source files. Looks like this fixed `Message.Designer.cs` code generation --------- Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com>
This commit is contained in:
parent
19fadead91
commit
793afd0e06
61 changed files with 447 additions and 462 deletions
32
TeamOctolings.Octobot/Data/Options/AllOptionsEnum.cs
Normal file
32
TeamOctolings.Octobot/Data/Options/AllOptionsEnum.cs
Normal file
|
@ -0,0 +1,32 @@
|
|||
using JetBrains.Annotations;
|
||||
using TeamOctolings.Octobot.Commands;
|
||||
|
||||
namespace TeamOctolings.Octobot.Data.Options;
|
||||
|
||||
/// <summary>
|
||||
/// Represents all options as enums.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// WARNING: This enum is order-dependent! It's values are used as indexes for
|
||||
/// <see cref="SettingsCommandGroup.AllOptions" />.
|
||||
/// </remarks>
|
||||
public enum AllOptionsEnum
|
||||
{
|
||||
[UsedImplicitly] Language,
|
||||
[UsedImplicitly] WelcomeMessage,
|
||||
[UsedImplicitly] LeaveMessage,
|
||||
[UsedImplicitly] ReceiveStartupMessages,
|
||||
[UsedImplicitly] RemoveRolesOnMute,
|
||||
[UsedImplicitly] ReturnRolesOnRejoin,
|
||||
[UsedImplicitly] AutoStartEvents,
|
||||
[UsedImplicitly] RenameHoistedUsers,
|
||||
[UsedImplicitly] PublicFeedbackChannel,
|
||||
[UsedImplicitly] PrivateFeedbackChannel,
|
||||
[UsedImplicitly] WelcomeMessagesChannel,
|
||||
[UsedImplicitly] EventNotificationChannel,
|
||||
[UsedImplicitly] DefaultRole,
|
||||
[UsedImplicitly] MuteRole,
|
||||
[UsedImplicitly] ModeratorRole,
|
||||
[UsedImplicitly] EventNotificationRole,
|
||||
[UsedImplicitly] EventEarlyNotificationOffset
|
||||
}
|
41
TeamOctolings.Octobot/Data/Options/BoolOption.cs
Normal file
41
TeamOctolings.Octobot/Data/Options/BoolOption.cs
Normal file
|
@ -0,0 +1,41 @@
|
|||
using System.Text.Json.Nodes;
|
||||
using Remora.Results;
|
||||
|
||||
namespace TeamOctolings.Octobot.Data.Options;
|
||||
|
||||
public sealed class BoolOption : GuildOption<bool>
|
||||
{
|
||||
public BoolOption(string name, bool defaultValue) : base(name, defaultValue) { }
|
||||
|
||||
public override string Display(JsonNode settings)
|
||||
{
|
||||
return Get(settings) ? Messages.Yes : Messages.No;
|
||||
}
|
||||
|
||||
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.Success;
|
||||
}
|
||||
|
||||
private static bool TryParseBool(string from, out bool value)
|
||||
{
|
||||
value = false;
|
||||
switch (from.ToLowerInvariant())
|
||||
{
|
||||
case "true" or "1" or "y" or "yes" or "д" or "да":
|
||||
value = true;
|
||||
return true;
|
||||
case "false" or "0" or "n" or "no" or "н" or "не" or "нет" or "нъет":
|
||||
value = false;
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
57
TeamOctolings.Octobot/Data/Options/GuildOption.cs
Normal file
57
TeamOctolings.Octobot/Data/Options/GuildOption.cs
Normal file
|
@ -0,0 +1,57 @@
|
|||
using System.Text.Json.Nodes;
|
||||
using Remora.Discord.Extensions.Formatting;
|
||||
using Remora.Results;
|
||||
|
||||
namespace TeamOctolings.Octobot.Data.Options;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a per-guild option.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the option.</typeparam>
|
||||
public class GuildOption<T> : IGuildOption
|
||||
where T : notnull
|
||||
{
|
||||
protected readonly T DefaultValue;
|
||||
|
||||
public GuildOption(string name, T defaultValue)
|
||||
{
|
||||
Name = name;
|
||||
DefaultValue = defaultValue;
|
||||
}
|
||||
|
||||
public string Name { get; }
|
||||
|
||||
public virtual string Display(JsonNode settings)
|
||||
{
|
||||
return Markdown.InlineCode(Get(settings).ToString() ?? throw new InvalidOperationException());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets the value of the option from a <see cref="string" /> to the provided JsonNode.
|
||||
/// </summary>
|
||||
/// <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)
|
||||
{
|
||||
settings[Name] = from;
|
||||
return Result.Success;
|
||||
}
|
||||
|
||||
public Result Reset(JsonNode settings)
|
||||
{
|
||||
settings[Name] = null;
|
||||
return Result.Success;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the value of the option from the provided <paramref name="settings" />.
|
||||
/// </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)
|
||||
{
|
||||
var property = settings[Name];
|
||||
return property != null ? property.GetValue<T>() : DefaultValue;
|
||||
}
|
||||
}
|
12
TeamOctolings.Octobot/Data/Options/IGuildOption.cs
Normal file
12
TeamOctolings.Octobot/Data/Options/IGuildOption.cs
Normal file
|
@ -0,0 +1,12 @@
|
|||
using System.Text.Json.Nodes;
|
||||
using Remora.Results;
|
||||
|
||||
namespace TeamOctolings.Octobot.Data.Options;
|
||||
|
||||
public interface IGuildOption
|
||||
{
|
||||
string Name { get; }
|
||||
string Display(JsonNode settings);
|
||||
Result Set(JsonNode settings, string from);
|
||||
Result Reset(JsonNode settings);
|
||||
}
|
38
TeamOctolings.Octobot/Data/Options/LanguageOption.cs
Normal file
38
TeamOctolings.Octobot/Data/Options/LanguageOption.cs
Normal file
|
@ -0,0 +1,38 @@
|
|||
using System.Globalization;
|
||||
using System.Text.Json.Nodes;
|
||||
using Remora.Discord.Extensions.Formatting;
|
||||
using Remora.Results;
|
||||
|
||||
namespace TeamOctolings.Octobot.Data.Options;
|
||||
|
||||
/// <inheritdoc />
|
||||
public sealed class LanguageOption : GuildOption<CultureInfo>
|
||||
{
|
||||
private static readonly Dictionary<string, CultureInfo> CultureInfoCache = new()
|
||||
{
|
||||
{ "en", new CultureInfo("en-US") },
|
||||
{ "ru", new CultureInfo("ru-RU") }
|
||||
};
|
||||
|
||||
public LanguageOption(string name, string defaultValue) : base(name, CultureInfoCache[defaultValue]) { }
|
||||
|
||||
public override string Display(JsonNode settings)
|
||||
{
|
||||
return Markdown.InlineCode(settings[Name]?.GetValue<string>() ?? "en");
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
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)
|
||||
{
|
||||
return CultureInfoCache.ContainsKey(from.ToLowerInvariant())
|
||||
? base.Set(settings, from.ToLowerInvariant())
|
||||
: new ArgumentInvalidError(nameof(from), Messages.LanguageNotSupported);
|
||||
}
|
||||
}
|
40
TeamOctolings.Octobot/Data/Options/SnowflakeOption.cs
Normal file
40
TeamOctolings.Octobot/Data/Options/SnowflakeOption.cs
Normal file
|
@ -0,0 +1,40 @@
|
|||
using System.Text.Json.Nodes;
|
||||
using System.Text.RegularExpressions;
|
||||
using Remora.Discord.Extensions.Formatting;
|
||||
using Remora.Rest.Core;
|
||||
using Remora.Results;
|
||||
using TeamOctolings.Octobot.Extensions;
|
||||
|
||||
namespace TeamOctolings.Octobot.Data.Options;
|
||||
|
||||
public sealed partial class SnowflakeOption : GuildOption<Snowflake>
|
||||
{
|
||||
public SnowflakeOption(string name) : base(name, 0UL.ToSnowflake()) { }
|
||||
|
||||
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)
|
||||
{
|
||||
var property = settings[Name];
|
||||
return property != null ? property.GetValue<ulong>().ToSnowflake() : DefaultValue;
|
||||
}
|
||||
|
||||
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.Success;
|
||||
}
|
||||
|
||||
[GeneratedRegex("[^0-9]")]
|
||||
private static partial Regex NonNumbers();
|
||||
}
|
27
TeamOctolings.Octobot/Data/Options/TimeSpanOption.cs
Normal file
27
TeamOctolings.Octobot/Data/Options/TimeSpanOption.cs
Normal file
|
@ -0,0 +1,27 @@
|
|||
using System.Text.Json.Nodes;
|
||||
using Remora.Results;
|
||||
using TeamOctolings.Octobot.Parsers;
|
||||
|
||||
namespace TeamOctolings.Octobot.Data.Options;
|
||||
|
||||
public sealed class TimeSpanOption : GuildOption<TimeSpan>
|
||||
{
|
||||
public TimeSpanOption(string name, TimeSpan defaultValue) : base(name, defaultValue) { }
|
||||
|
||||
public override TimeSpan Get(JsonNode settings)
|
||||
{
|
||||
var property = settings[Name];
|
||||
return property != null ? TimeSpanParser.TryParse(property.GetValue<string>()).Entity : DefaultValue;
|
||||
}
|
||||
|
||||
public override Result Set(JsonNode settings, string from)
|
||||
{
|
||||
if (!TimeSpanParser.TryParse(from).IsDefined(out var span))
|
||||
{
|
||||
return new ArgumentInvalidError(nameof(from), Messages.InvalidSettingValue);
|
||||
}
|
||||
|
||||
settings[Name] = span.ToString();
|
||||
return Result.Success;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue