From 49215f628ac0b20be2fe1941468fcdda9350e39e Mon Sep 17 00:00:00 2001 From: mctaylors Date: Sat, 23 Dec 2023 20:42:48 +0300 Subject: [PATCH] what if i just Signed-off-by: mctaylors --- locale/Messages.resx | 4 ++-- locale/Messages.ru.resx | 4 ++-- locale/Messages.tt-ru.resx | 4 ++-- src/Commands/RemindCommandGroup.cs | 36 ++++++++++++------------------ src/Data/Options/TimeSpanOption.cs | 6 ++--- src/Messages.Designer.cs | 4 ++-- src/Parsers/TimeSpanParser.cs | 24 ++++++++++++++++++++ 7 files changed, 48 insertions(+), 34 deletions(-) create mode 100644 src/Parsers/TimeSpanParser.cs diff --git a/locale/Messages.resx b/locale/Messages.resx index 6e1a017..b546531 100644 --- a/locale/Messages.resx +++ b/locale/Messages.resx @@ -585,7 +585,7 @@ Report an issue - - Incorrect reminder send time specified. + + Incorrect time specified. diff --git a/locale/Messages.ru.resx b/locale/Messages.ru.resx index 772c2b3..0616f58 100644 --- a/locale/Messages.ru.resx +++ b/locale/Messages.ru.resx @@ -585,7 +585,7 @@ Сообщить о проблеме - - Указано некорректное время отправки напоминания. + + Указано некорректное время. diff --git a/locale/Messages.tt-ru.resx b/locale/Messages.tt-ru.resx index aa01b8e..7c01950 100644 --- a/locale/Messages.tt-ru.resx +++ b/locale/Messages.tt-ru.resx @@ -585,7 +585,7 @@ зарепортить баг - - ты там правильно напиши когда мне тебя пинать + + ты там правильно напиши таймспан diff --git a/src/Commands/RemindCommandGroup.cs b/src/Commands/RemindCommandGroup.cs index cc15945..5a8c3fb 100644 --- a/src/Commands/RemindCommandGroup.cs +++ b/src/Commands/RemindCommandGroup.cs @@ -7,7 +7,6 @@ using Octobot.Extensions; using Octobot.Services; using Remora.Commands.Attributes; using Remora.Commands.Groups; -using Remora.Commands.Parsers; using Remora.Discord.API.Abstractions.Objects; using Remora.Discord.API.Abstractions.Rest; using Remora.Discord.Commands.Attributes; @@ -18,6 +17,7 @@ using Remora.Discord.Extensions.Embeds; using Remora.Discord.Extensions.Formatting; using Remora.Rest.Core; using Remora.Results; +using TimeSpanParser = Octobot.Parsers.TimeSpanParser; namespace Octobot.Commands; @@ -111,7 +111,7 @@ public class RemindCommandGroup : CommandGroup /// /// A slash command that schedules a reminder with the specified text. /// - /// The period of time which must pass before the reminder will be sent. + /// The period of time which must pass before the reminder will be sent. /// The text of the reminder. /// A feedback sending result which may or may not have succeeded. [Command("remind")] @@ -122,7 +122,7 @@ public class RemindCommandGroup : CommandGroup public async Task ExecuteReminderAsync( [Description("After what period of time mention the reminder")] [Option("in")] - string timeSpanString, + string stringIn, [Description("Reminder text")] [MaxLength(512)] string text) { @@ -146,17 +146,22 @@ public class RemindCommandGroup : CommandGroup var data = await _guildData.GetData(guildId, CancellationToken); Messages.Culture = GuildSettings.Language.Get(data.Settings); - return await AddReminderAsync(timeSpanString, text, data, channelId, bot, executor, CancellationToken); + return await AddReminderAsync(stringIn, text, data, channelId, bot, executor, CancellationToken); } - private async Task AddReminderAsync(string timeSpanString, string text, GuildData data, + private async Task AddReminderAsync(string stringIn, string text, GuildData data, Snowflake channelId, IUser bot, IUser executor, CancellationToken ct = default) { - var timeSpan = await ParseReminderAsync(timeSpanString, ct); - if (timeSpan == TimeSpan.Zero) + var parseResult = TimeSpanParser.TryParse(stringIn, ct); + if (!parseResult.IsDefined(out var timeSpanIn)) + { + return Result.FromError(parseResult); + } + + if (timeSpanIn == TimeSpan.Zero) { var failedEmbed = new EmbedBuilder() - .WithSmallTitle(Messages.ReminderInvalidTimeSpan, bot) + .WithSmallTitle(Messages.InvalidTimeSpan, bot) .WithColour(ColorsList.Red) .Build(); @@ -164,7 +169,7 @@ public class RemindCommandGroup : CommandGroup } var memberData = data.GetOrCreateMemberData(executor.ID); - var remindAt = DateTimeOffset.UtcNow.Add(timeSpan); + var remindAt = DateTimeOffset.UtcNow.Add(timeSpanIn); var responseResult = await _interactionApi.GetOriginalInteractionResponseAsync(_context.Interaction.ApplicationID, _context.Interaction.Token, ct); if (!responseResult.IsDefined(out var response)) { @@ -193,19 +198,6 @@ public class RemindCommandGroup : CommandGroup return await _feedback.SendContextualEmbedResultAsync(embed, ct: ct); } - private static async Task ParseReminderAsync(string timeSpanString, CancellationToken ct) - { - if (timeSpanString.StartsWith('-')) - { - return TimeSpan.Zero; - } - - var parseResult = await new TimeSpanParser().TryParseAsync(timeSpanString, ct); - return !parseResult.IsDefined(out var @in) - ? TimeSpan.Zero - : @in; - } - /// /// A slash command that deletes a reminder using its list position. /// diff --git a/src/Data/Options/TimeSpanOption.cs b/src/Data/Options/TimeSpanOption.cs index 7f60ebb..b9b405f 100644 --- a/src/Data/Options/TimeSpanOption.cs +++ b/src/Data/Options/TimeSpanOption.cs @@ -1,13 +1,11 @@ using System.Text.Json.Nodes; -using Remora.Commands.Parsers; +using Octobot.Parsers; using Remora.Results; namespace Octobot.Data.Options; public sealed class TimeSpanOption : Option { - private static readonly TimeSpanParser Parser = new(); - public TimeSpanOption(string name, TimeSpan defaultValue) : base(name, defaultValue) { } public override TimeSpan Get(JsonNode settings) @@ -29,6 +27,6 @@ public sealed class TimeSpanOption : Option private static Result ParseTimeSpan(string from) { - return Parser.TryParseAsync(from).AsTask().GetAwaiter().GetResult(); + return TimeSpanParser.TryParse(from); } } diff --git a/src/Messages.Designer.cs b/src/Messages.Designer.cs index 5d883c9..682bc6b 100644 --- a/src/Messages.Designer.cs +++ b/src/Messages.Designer.cs @@ -1037,11 +1037,11 @@ namespace Octobot { } } - internal static string ReminderInvalidTimeSpan + internal static string InvalidTimeSpan { get { - return ResourceManager.GetString("ReminderInvalidTimeSpan", resourceCulture); + return ResourceManager.GetString("InvalidTimeSpan", resourceCulture); } } } diff --git a/src/Parsers/TimeSpanParser.cs b/src/Parsers/TimeSpanParser.cs new file mode 100644 index 0000000..ef5dd1c --- /dev/null +++ b/src/Parsers/TimeSpanParser.cs @@ -0,0 +1,24 @@ +using JetBrains.Annotations; +using Remora.Commands.Parsers; +using Remora.Results; + +namespace Octobot.Parsers; + +/// +/// Parses . +/// +[PublicAPI] +public class TimeSpanParser : AbstractTypeParser +{ + public static Result TryParse(string timeSpanString, CancellationToken ct = default) + { + if (timeSpanString.StartsWith('-')) + { + return TimeSpan.Zero; + } + + var parser = new Remora.Commands.Parsers.TimeSpanParser(); + var parseResult = parser.TryParseAsync(timeSpanString, ct).AsTask().GetAwaiter().GetResult(); + return !parseResult.IsDefined(out var @in) ? TimeSpan.Zero : @in; + } +}