diff --git a/TeamOctolings.Octobot/Commands/SettingsCommandGroup.cs b/TeamOctolings.Octobot/Commands/SettingsCommandGroup.cs index 0acaa88..15aa42b 100644 --- a/TeamOctolings.Octobot/Commands/SettingsCommandGroup.cs +++ b/TeamOctolings.Octobot/Commands/SettingsCommandGroup.cs @@ -202,6 +202,27 @@ public sealed class SettingsCommandGroup : CommandGroup IGuildOption option, string value, GuildData data, Snowflake channelId, IUser executor, IUser bot, CancellationToken ct = default) { + var equalsResult = option.ValueEquals(data.Settings, value); + if (!equalsResult.IsSuccess) + { + var failedEmbed = new EmbedBuilder().WithSmallTitle(Messages.SettingNotChanged, bot) + .WithDescription(equalsResult.Error.Message) + .WithColour(ColorsList.Red) + .Build(); + + return await _feedback.SendContextualEmbedResultAsync(failedEmbed, ct: ct); + } + + if (equalsResult.Entity) + { + var failedEmbed = new EmbedBuilder().WithSmallTitle(Messages.SettingNotChanged, bot) + .WithDescription(Messages.SettingValueEquals) + .WithColour(ColorsList.Red) + .Build(); + + return await _feedback.SendContextualEmbedResultAsync(failedEmbed, ct: ct); + } + var setResult = option.Set(data.Settings, value); if (!setResult.IsSuccess) { diff --git a/TeamOctolings.Octobot/Data/Options/BoolOption.cs b/TeamOctolings.Octobot/Data/Options/BoolOption.cs index 6a3c899..3b81abb 100644 --- a/TeamOctolings.Octobot/Data/Options/BoolOption.cs +++ b/TeamOctolings.Octobot/Data/Options/BoolOption.cs @@ -12,6 +12,16 @@ public sealed class BoolOption : GuildOption return Get(settings) ? Messages.Yes : Messages.No; } + public override Result ValueEquals(JsonNode settings, string value) + { + if (!TryParseBool(value, out var boolean)) + { + return new ArgumentInvalidError(nameof(value), Messages.InvalidSettingValue); + } + + return Value(settings).Equals(boolean.ToString()); + } + public override Result Set(JsonNode settings, string from) { if (!TryParseBool(from, out var value)) diff --git a/TeamOctolings.Octobot/Data/Options/GuildOption.cs b/TeamOctolings.Octobot/Data/Options/GuildOption.cs index 5d9f1a2..ea9c30e 100644 --- a/TeamOctolings.Octobot/Data/Options/GuildOption.cs +++ b/TeamOctolings.Octobot/Data/Options/GuildOption.cs @@ -21,9 +21,19 @@ public class GuildOption : IGuildOption public string Name { get; } + protected virtual string Value(JsonNode settings) + { + return Get(settings).ToString() ?? throw new InvalidOperationException(); + } + public virtual string Display(JsonNode settings) { - return Markdown.InlineCode(Get(settings).ToString() ?? throw new InvalidOperationException()); + return Markdown.InlineCode(Value(settings)); + } + + public virtual Result ValueEquals(JsonNode settings, string value) + { + return Value(settings).Equals(value); } /// diff --git a/TeamOctolings.Octobot/Data/Options/IGuildOption.cs b/TeamOctolings.Octobot/Data/Options/IGuildOption.cs index a8c3e6e..9920281 100644 --- a/TeamOctolings.Octobot/Data/Options/IGuildOption.cs +++ b/TeamOctolings.Octobot/Data/Options/IGuildOption.cs @@ -7,6 +7,7 @@ public interface IGuildOption { string Name { get; } string Display(JsonNode settings); + Result ValueEquals(JsonNode settings, string value); Result Set(JsonNode settings, string from); Result Reset(JsonNode settings); } diff --git a/TeamOctolings.Octobot/Data/Options/LanguageOption.cs b/TeamOctolings.Octobot/Data/Options/LanguageOption.cs index 15ab6ff..f58e011 100644 --- a/TeamOctolings.Octobot/Data/Options/LanguageOption.cs +++ b/TeamOctolings.Octobot/Data/Options/LanguageOption.cs @@ -1,6 +1,5 @@ using System.Globalization; using System.Text.Json.Nodes; -using Remora.Discord.Extensions.Formatting; using Remora.Results; namespace TeamOctolings.Octobot.Data.Options; @@ -16,9 +15,9 @@ public sealed class LanguageOption : GuildOption public LanguageOption(string name, string defaultValue) : base(name, CultureInfoCache[defaultValue]) { } - public override string Display(JsonNode settings) + protected override string Value(JsonNode settings) { - return Markdown.InlineCode(settings[Name]?.GetValue() ?? "en"); + return settings[Name]?.GetValue() ?? "en"; } /// diff --git a/TeamOctolings.Octobot/Data/Options/TimeSpanOption.cs b/TeamOctolings.Octobot/Data/Options/TimeSpanOption.cs index 3501f09..7e21343 100644 --- a/TeamOctolings.Octobot/Data/Options/TimeSpanOption.cs +++ b/TeamOctolings.Octobot/Data/Options/TimeSpanOption.cs @@ -8,6 +8,16 @@ public sealed class TimeSpanOption : GuildOption { public TimeSpanOption(string name, TimeSpan defaultValue) : base(name, defaultValue) { } + public override Result ValueEquals(JsonNode settings, string value) + { + if (!TimeSpanParser.TryParse(value).IsDefined(out var span)) + { + return new ArgumentInvalidError(nameof(value), Messages.InvalidSettingValue); + } + + return Value(settings).Equals(span.ToString()); + } + public override TimeSpan Get(JsonNode settings) { var property = settings[Name]; diff --git a/TeamOctolings.Octobot/Messages.Designer.cs b/TeamOctolings.Octobot/Messages.Designer.cs index bbc1366..ce59f1e 100644 --- a/TeamOctolings.Octobot/Messages.Designer.cs +++ b/TeamOctolings.Octobot/Messages.Designer.cs @@ -1196,5 +1196,11 @@ namespace TeamOctolings.Octobot { return ResourceManager.GetString("SettingsModeratorRole", resourceCulture); } } + + internal static string SettingValueEquals { + get { + return ResourceManager.GetString("SettingValueEquals", resourceCulture); + } + } } } diff --git a/TeamOctolings.Octobot/Messages.resx b/TeamOctolings.Octobot/Messages.resx index 47e7d4f..059584a 100644 --- a/TeamOctolings.Octobot/Messages.resx +++ b/TeamOctolings.Octobot/Messages.resx @@ -681,4 +681,7 @@ Moderator role + + The setting value is the same as the input value. + diff --git a/TeamOctolings.Octobot/Messages.ru.resx b/TeamOctolings.Octobot/Messages.ru.resx index 2eef257..fc8a594 100644 --- a/TeamOctolings.Octobot/Messages.ru.resx +++ b/TeamOctolings.Octobot/Messages.ru.resx @@ -681,4 +681,7 @@ Роль модератора + + Значение настройки такое же, как и вводное значение. +