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:
parent
07e8784d2e
commit
d6d2660fb0
9 changed files with 67 additions and 4 deletions
|
@ -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)
|
||||
{
|
||||
|
|
|
@ -12,6 +12,16 @@ public sealed class BoolOption : GuildOption<bool>
|
|||
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)
|
||||
{
|
||||
if (!TryParseBool(from, out var value))
|
||||
|
|
|
@ -21,9 +21,19 @@ public class GuildOption<T> : 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<bool> ValueEquals(JsonNode settings, string value)
|
||||
{
|
||||
return Value(settings).Equals(value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
|
@ -7,6 +7,7 @@ public interface IGuildOption
|
|||
{
|
||||
string Name { get; }
|
||||
string Display(JsonNode settings);
|
||||
Result<bool> ValueEquals(JsonNode settings, string value);
|
||||
Result Set(JsonNode settings, string from);
|
||||
Result Reset(JsonNode settings);
|
||||
}
|
||||
|
|
|
@ -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<CultureInfo>
|
|||
|
||||
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 />
|
||||
|
|
|
@ -8,6 +8,16 @@ public sealed class TimeSpanOption : GuildOption<TimeSpan>
|
|||
{
|
||||
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)
|
||||
{
|
||||
var property = settings[Name];
|
||||
|
|
6
TeamOctolings.Octobot/Messages.Designer.cs
generated
6
TeamOctolings.Octobot/Messages.Designer.cs
generated
|
@ -1196,5 +1196,11 @@ namespace TeamOctolings.Octobot {
|
|||
return ResourceManager.GetString("SettingsModeratorRole", resourceCulture);
|
||||
}
|
||||
}
|
||||
|
||||
internal static string SettingValueEquals {
|
||||
get {
|
||||
return ResourceManager.GetString("SettingValueEquals", resourceCulture);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -681,4 +681,7 @@
|
|||
<data name="SettingsModeratorRole" xml:space="preserve">
|
||||
<value>Moderator role</value>
|
||||
</data>
|
||||
<data name="SettingValueEquals" xml:space="preserve">
|
||||
<value>The setting value is the same as the input value.</value>
|
||||
</data>
|
||||
</root>
|
||||
|
|
|
@ -681,4 +681,7 @@
|
|||
<data name="SettingsModeratorRole" xml:space="preserve">
|
||||
<value>Роль модератора</value>
|
||||
</data>
|
||||
<data name="SettingValueEquals" xml:space="preserve">
|
||||
<value>Значение настройки такое же, как и вводное значение.</value>
|
||||
</data>
|
||||
</root>
|
||||
|
|
Loading…
Reference in a new issue