forked from TeamInklings/Octobot
The Milestone Commit (#48)
mctaylors: - updated readme 7 times (and only adding new logo from /about) - [removed](aeeb3d4399
) bot footer from created event embed on the second try - [changed](4b9b91d9e4
) cdn from discord to upload.systems Octol1ttle: - Guild settings code has been overhauled. Instead of instances of a `GuildConfiguration` class being (de-)serialized when used with listing and setting options provided by reflection, there are now multiple `Option` classes responsible for the type of option they are storing. The classes support getting a value, validating and setting values with Results, and getting a user-friendly representation of these values. This makes use of polymorphism, providing clean and easier to use and refactor code. - Gateway event responders have been split into their own separate files, which should make it easier to find and modify responders when needed. - Warning suppressions regarding unused and never instantiated classes have been replaced by `[ImplicitUse]` annotations provided by `JetBrains.Annotations`. This avoids hiding real issues and provides a better way to suppress false warnings while being explicit. - It is no longer possible to execute some slash commands if they are run without the correct permissions - Dependencies are now more explicitly defined neroduckale: - Made easter eggs case-insensitive --------- Signed-off-by: Macintosh II <95250141+mctaylors@users.noreply.github.com> Signed-off-by: Octol1ttle <l1ttleofficial@outlook.com> Co-authored-by: Octol1ttle <l1ttleofficial@outlook.com> Co-authored-by: nrdk <neroduck@vk.com>
This commit is contained in:
parent
3eb17b96c5
commit
c6dd3727c3
39 changed files with 912 additions and 658 deletions
34
src/Data/Options/BoolOption.cs
Normal file
34
src/Data/Options/BoolOption.cs
Normal file
|
@ -0,0 +1,34 @@
|
|||
using System.Text.Json.Nodes;
|
||||
using Remora.Results;
|
||||
|
||||
namespace Boyfriend.Data.Options;
|
||||
|
||||
public class BoolOption : Option<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 Result.FromError(new ArgumentInvalidError(nameof(from), Messages.InvalidSettingValue));
|
||||
|
||||
settings[Name] = value;
|
||||
return Result.FromSuccess();
|
||||
}
|
||||
|
||||
private static bool TryParseBool(string from, out bool value) {
|
||||
value = false;
|
||||
switch (from) {
|
||||
case "1" or "y" or "yes" or "д" or "да":
|
||||
value = true;
|
||||
return true;
|
||||
case "0" or "n" or "no" or "н" or "не" or "нет":
|
||||
value = false;
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
10
src/Data/Options/IOption.cs
Normal file
10
src/Data/Options/IOption.cs
Normal file
|
@ -0,0 +1,10 @@
|
|||
using System.Text.Json.Nodes;
|
||||
using Remora.Results;
|
||||
|
||||
namespace Boyfriend.Data.Options;
|
||||
|
||||
public interface IOption {
|
||||
string Name { get; }
|
||||
string Display(JsonNode settings);
|
||||
Result Set(JsonNode settings, string from);
|
||||
}
|
35
src/Data/Options/LanguageOption.cs
Normal file
35
src/Data/Options/LanguageOption.cs
Normal file
|
@ -0,0 +1,35 @@
|
|||
using System.Globalization;
|
||||
using System.Text.Json.Nodes;
|
||||
using Remora.Discord.Extensions.Formatting;
|
||||
using Remora.Results;
|
||||
|
||||
namespace Boyfriend.Data.Options;
|
||||
|
||||
/// <inheritdoc />
|
||||
public 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") }
|
||||
};
|
||||
|
||||
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) {
|
||||
if (!CultureInfoCache.ContainsKey(from.ToLowerInvariant()))
|
||||
return Result.FromError(new ArgumentInvalidError(nameof(from), Messages.LanguageNotSupported));
|
||||
|
||||
return base.Set(settings, from.ToLowerInvariant());
|
||||
}
|
||||
}
|
46
src/Data/Options/Option.cs
Normal file
46
src/Data/Options/Option.cs
Normal file
|
@ -0,0 +1,46 @@
|
|||
using System.Text.Json.Nodes;
|
||||
using Remora.Discord.Extensions.Formatting;
|
||||
using Remora.Results;
|
||||
|
||||
namespace Boyfriend.Data.Options;
|
||||
|
||||
/// <summary>
|
||||
/// Represents an per-guild option.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of the option.</typeparam>
|
||||
public class Option<T> : IOption
|
||||
where T : notnull {
|
||||
internal readonly 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()!);
|
||||
}
|
||||
|
||||
/// <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.FromSuccess();
|
||||
}
|
||||
|
||||
/// <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;
|
||||
}
|
||||
}
|
27
src/Data/Options/SnowflakeOption.cs
Normal file
27
src/Data/Options/SnowflakeOption.cs
Normal file
|
@ -0,0 +1,27 @@
|
|||
using System.Text.Json.Nodes;
|
||||
using Remora.Discord.Extensions.Formatting;
|
||||
using Remora.Rest.Core;
|
||||
using Remora.Results;
|
||||
|
||||
namespace Boyfriend.Data.Options;
|
||||
|
||||
public 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 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(from, out var parsed))
|
||||
return Result.FromError(new ArgumentInvalidError(nameof(from), Messages.InvalidSettingValue));
|
||||
|
||||
settings[Name] = parsed;
|
||||
return Result.FromSuccess();
|
||||
}
|
||||
}
|
28
src/Data/Options/TimeSpanOption.cs
Normal file
28
src/Data/Options/TimeSpanOption.cs
Normal file
|
@ -0,0 +1,28 @@
|
|||
using System.Text.Json.Nodes;
|
||||
using Remora.Commands.Parsers;
|
||||
using Remora.Results;
|
||||
|
||||
namespace Boyfriend.Data.Options;
|
||||
|
||||
public 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) {
|
||||
var property = settings[Name];
|
||||
return property != null ? ParseTimeSpan(property.GetValue<string>()).Entity : DefaultValue;
|
||||
}
|
||||
|
||||
public override Result Set(JsonNode settings, string from) {
|
||||
if (!ParseTimeSpan(from).IsDefined(out var span))
|
||||
return Result.FromError(new ArgumentInvalidError(nameof(from), Messages.InvalidSettingValue));
|
||||
|
||||
settings[Name] = span.ToString();
|
||||
return Result.FromSuccess();
|
||||
}
|
||||
|
||||
private static Result<TimeSpan> ParseTimeSpan(string from) {
|
||||
return Parser.TryParseAsync(from).AsTask().GetAwaiter().GetResult();
|
||||
}
|
||||
}
|
Reference in a new issue