Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 7 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions TeamOctolings.Octobot/Commands/SettingsCommandGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,27 @@ private async Task<Result> EditSettingAsync(
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)
{
Expand Down
10 changes: 10 additions & 0 deletions TeamOctolings.Octobot/Data/Options/BoolOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ public override string Display(JsonNode settings)
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))
Expand Down
12 changes: 11 additions & 1 deletion TeamOctolings.Octobot/Data/Options/GuildOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,19 @@ public GuildOption(string name, T defaultValue)

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>
Expand Down
1 change: 1 addition & 0 deletions TeamOctolings.Octobot/Data/Options/IGuildOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
5 changes: 2 additions & 3 deletions TeamOctolings.Octobot/Data/Options/LanguageOption.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 />
Expand Down
10 changes: 10 additions & 0 deletions TeamOctolings.Octobot/Data/Options/TimeSpanOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
6 changes: 6 additions & 0 deletions TeamOctolings.Octobot/Messages.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions TeamOctolings.Octobot/Messages.resx
Original file line number Diff line number Diff line change
Expand Up @@ -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>
3 changes: 3 additions & 0 deletions TeamOctolings.Octobot/Messages.ru.resx
Original file line number Diff line number Diff line change
Expand Up @@ -681,4 +681,7 @@
<data name="SettingsModeratorRole" xml:space="preserve">
<value>Роль модератора</value>
</data>
<data name="SettingValueEquals" xml:space="preserve">
<value>Значение настройки такое же, как и вводное значение.</value>
</data>
</root>