1
0
Fork 1
mirror of https://github.com/TeamOctolings/Octobot.git synced 2025-01-31 00:19:00 +03:00

Show an error when entering the same value from the settings (#326)

Closes #324
This commit is contained in:
Macintxsh 2024-07-31 12:13:29 +03:00 committed by GitHub
parent 07e8784d2e
commit d6d2660fb0
Signed by: GitHub
GPG key ID: B5690EEEBB952194
9 changed files with 67 additions and 4 deletions

View file

@ -202,6 +202,27 @@ public sealed class SettingsCommandGroup : CommandGroup
IGuildOption option, string value, GuildData data, Snowflake channelId, IUser executor, IUser bot, IGuildOption option, string value, GuildData data, Snowflake channelId, IUser executor, IUser bot,
CancellationToken ct = default) 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); var setResult = option.Set(data.Settings, value);
if (!setResult.IsSuccess) if (!setResult.IsSuccess)
{ {

View file

@ -12,6 +12,16 @@ public sealed class BoolOption : GuildOption<bool>
return Get(settings) ? Messages.Yes : Messages.No; return Get(settings) ? Messages.Yes : Messages.No;
} }
public override Result<bool> 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) public override Result Set(JsonNode settings, string from)
{ {
if (!TryParseBool(from, out var value)) if (!TryParseBool(from, out var value))

View file

@ -21,9 +21,19 @@ public class GuildOption<T> : IGuildOption
public string Name { get; } public string Name { get; }
protected virtual string Value(JsonNode settings)
{
return Get(settings).ToString() ?? throw new InvalidOperationException();
}
public virtual string Display(JsonNode settings) public virtual string Display(JsonNode settings)
{ {
return Markdown.InlineCode(Get(settings).ToString() ?? throw new InvalidOperationException()); return Markdown.InlineCode(Value(settings));
}
public virtual Result<bool> ValueEquals(JsonNode settings, string value)
{
return Value(settings).Equals(value);
} }
/// <summary> /// <summary>

View file

@ -7,6 +7,7 @@ public interface IGuildOption
{ {
string Name { get; } string Name { get; }
string Display(JsonNode settings); string Display(JsonNode settings);
Result<bool> ValueEquals(JsonNode settings, string value);
Result Set(JsonNode settings, string from); Result Set(JsonNode settings, string from);
Result Reset(JsonNode settings); Result Reset(JsonNode settings);
} }

View file

@ -1,6 +1,5 @@
using System.Globalization; using System.Globalization;
using System.Text.Json.Nodes; using System.Text.Json.Nodes;
using Remora.Discord.Extensions.Formatting;
using Remora.Results; using Remora.Results;
namespace TeamOctolings.Octobot.Data.Options; namespace TeamOctolings.Octobot.Data.Options;
@ -16,9 +15,9 @@ public sealed class LanguageOption : GuildOption<CultureInfo>
public LanguageOption(string name, string defaultValue) : base(name, CultureInfoCache[defaultValue]) { } 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<string>() ?? "en"); return settings[Name]?.GetValue<string>() ?? "en";
} }
/// <inheritdoc /> /// <inheritdoc />

View file

@ -8,6 +8,16 @@ public sealed class TimeSpanOption : GuildOption<TimeSpan>
{ {
public TimeSpanOption(string name, TimeSpan defaultValue) : base(name, defaultValue) { } public TimeSpanOption(string name, TimeSpan defaultValue) : base(name, defaultValue) { }
public override Result<bool> 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) public override TimeSpan Get(JsonNode settings)
{ {
var property = settings[Name]; var property = settings[Name];

View file

@ -1196,5 +1196,11 @@ namespace TeamOctolings.Octobot {
return ResourceManager.GetString("SettingsModeratorRole", resourceCulture); return ResourceManager.GetString("SettingsModeratorRole", resourceCulture);
} }
} }
internal static string SettingValueEquals {
get {
return ResourceManager.GetString("SettingValueEquals", resourceCulture);
}
}
} }
} }

View file

@ -681,4 +681,7 @@
<data name="SettingsModeratorRole" xml:space="preserve"> <data name="SettingsModeratorRole" xml:space="preserve">
<value>Moderator role</value> <value>Moderator role</value>
</data> </data>
<data name="SettingValueEquals" xml:space="preserve">
<value>The setting value is the same as the input value.</value>
</data>
</root> </root>

View file

@ -681,4 +681,7 @@
<data name="SettingsModeratorRole" xml:space="preserve"> <data name="SettingsModeratorRole" xml:space="preserve">
<value>Роль модератора</value> <value>Роль модератора</value>
</data> </data>
<data name="SettingValueEquals" xml:space="preserve">
<value>Значение настройки такое же, как и вводное значение.</value>
</data>
</root> </root>